trees 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # SPDX-FileCopyrightText: 2022-2023 Alper Nebi Yasak <alpernebiyasak@gmail.com>
  4. # SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  5. # SPDX-FileCopyrightText: 2023 Leah Rowe <leah@libreboot.org>
  6. [ "x${DEBUG+set}" = 'xset' ] && set -v
  7. set -u -e
  8. . "include/err.sh"
  9. . "include/option.sh"
  10. . "include/git.sh"
  11. export LOCALVERSION="-${projectname}-${version%%-*}"
  12. eval "$(setvars "" arch cfgsdir codedir config config_name crossgcc_ada mode \
  13. elfdir listfile project romtype target target_dir targets tree _f target1)"
  14. main()
  15. {
  16. while getopts f:b:m:u:c:x:s:l:n: option; do
  17. _f="${1}"
  18. case "${1}" in
  19. -b) : ;;
  20. -u) mode="oldconfig" ;;
  21. -m) mode="menuconfig" ;;
  22. -c) mode="distclean" ;;
  23. -x) mode="crossgcc-clean" ;;
  24. -f) mode="fetch" ;;
  25. -s) mode="savedefconfig" ;;
  26. -l) mode="olddefconfig" ;;
  27. -n) mode="nconfig" ;;
  28. *) err "Invalid option" ;;
  29. esac
  30. shift; project="${OPTARG#src/}"; shift
  31. done
  32. [ -z "${_f}" ] && err "missing flag (-m/-u/-b/-c/-x/-f/-s/-l/-n)"
  33. [ -z "${project}" ] && err "project name not specified"
  34. [ -f "config/${project}/build.list" ] && build_targets $@ && return 0
  35. build_projects $@
  36. }
  37. build_projects()
  38. {
  39. [ $# -gt 0 ] && x_ ./update trees ${_f} ${@}
  40. if [ "${mode}" = "fetch" ]; then
  41. fetch_project_repo
  42. for x in config/git/*; do
  43. [ ! -f "${x}" ] && continue
  44. nukeblobs "${x##*/}" "src/${x##*/}"
  45. done
  46. return 0
  47. fi
  48. codedir="src/${project}"
  49. [ -d "${codedir}" ] || x_ ./update trees -f "${project}"
  50. [ "${mode}" = "distclean" ] && mode="clean"
  51. run_make_command || return 0
  52. }
  53. build_targets()
  54. {
  55. elfdir="elf/${project}"
  56. [ "${elfdir}" = "elf/coreboot" ] && \
  57. elfdir="elf/coreboot_nopayload_DO_NOT_FLASH"
  58. cfgsdir="config/${project}"
  59. [ -d "${cfgsdir}" ] || err "directory, ${cfgsdir}, does not exist"
  60. listfile="${cfgsdir}/build.list"
  61. [ -f "${listfile}" ] || err "list file, ${listfile}, does not exist"
  62. # Build for all targets if no argument is given
  63. [ $# -gt 0 ] && target1="${1}"
  64. [ "${target1}" = "utils" ] && [ "${project}" = "coreboot" ] && \
  65. shift 1
  66. targets=$(items "${cfgsdir}") || \
  67. err "Cannot get options for ${cfgsdir}"
  68. [ $# -gt 0 ] && targets=$@
  69. [ -z "${mode}" ] && x_ mkdir -p "${elfdir}/"
  70. handle_targets
  71. }
  72. handle_targets()
  73. {
  74. for x in ${targets}; do
  75. target="${x}"
  76. printf "Running 'make %s' for project '%s, target '%s''\n" \
  77. "${mode}" "${project}" "${target}"
  78. x_ handle_defconfig
  79. done
  80. [ "${target1}" = "utils" ] && [ "${project}" = "coreboot" ] && return 0
  81. [ -z "${mode}" ] || return 0
  82. printf "Done! The files are stored under %s/\n\n" "${elfdir}"
  83. }
  84. handle_defconfig()
  85. {
  86. handle_src_tree "${target}" || return 0
  87. if [ "${target1}" = "utils" ] && [ "${project}" = "coreboot" ]; then
  88. handle_coreboot_utils "${tree}"
  89. return 0
  90. fi
  91. for y in "${target_dir}/config"/*; do
  92. [ -f "${y}" ] || continue
  93. config="${y}"
  94. config_name="${config#${target_dir}/config/}"
  95. printf "handle/make/config %s %s: handling config %s\n" \
  96. "${project}" "${target}" "${config_name}"
  97. [ ! -z ${mode} ] || check_config || continue
  98. handle_makefile
  99. [ ! -z ${mode} ] || copy_elf
  100. done
  101. }
  102. handle_src_tree()
  103. {
  104. romtype="normal"
  105. target_dir="${cfgsdir}/${target}"
  106. if [ "${mode}" = "fetch" ]; then
  107. fetch_project_trees
  108. nukeblobs "${project}/${tree}" "${project}/${tree}"
  109. return 1
  110. fi
  111. x_ mkdir -p "${elfdir}/${target}"
  112. eval "$(setvars "" arch tree)"
  113. . "${target_dir}/target.cfg" || \
  114. err "handle_src_tree ${target_dir}: cannot load target.cfg"
  115. [ -z "${arch}" ] && err "handle_src_tree $project/$tree: arch unset"
  116. [ -z "${tree}" ] && err "handle_src_tree $project/$tree: tree unset"
  117. codedir="src/${project}/${tree}"
  118. if [ ! -d "${codedir}" ]; then
  119. if [ "${mode}" = "distclean" ] || \
  120. [ "${mode}" = "crossgcc-clean" ]; then
  121. printf "Directory %s doesn't exist; skipping clean\n" \
  122. "${codedir}" 1>&2
  123. return 1
  124. fi
  125. x_ ./update trees -f "${project}" "${target}"
  126. fi
  127. [ "${target1}" = "utils" ] && [ "${project}" = "coreboot" ] && return 0
  128. [ "$project" != "coreboot" ] && [ "$project" != "u-boot" ] && return 0
  129. # u-boot and coreboot are both compiled with coreboot's crossgcc
  130. [ -z "${mode}" ] || return 0
  131. check_cross_compiler
  132. }
  133. # set up cross-compiler (coreboot crossgcc) for u-boot and coreboot
  134. # (seabios and grub currently use hostcc, not crossgcc)
  135. check_cross_compiler()
  136. {
  137. [ "${crossgcc_ada}" = "y" ] || [ "${crossgcc_ada}" = "n" ] || \
  138. crossgcc_ada="y"
  139. [ "${crossgcc_ada}" = "y" ] || export BUILD_LANGUAGES=c
  140. cbdir="src/coreboot/${tree}"
  141. [ "${project}" != "coreboot" ] && cbdir="src/coreboot/default"
  142. # only true if not building coreboot:
  143. ctarget="${cbdir#src/coreboot/}"
  144. [ -d "${cbdir}" ] || x_ ./update trees -f coreboot ${ctarget}
  145. if [ "${arch}" = "x86_32" ] || [ "${arch}" = "x86_64" ]; then
  146. [ -d "${cbdir}/util/crossgcc/xgcc/i386-elf/" ] || \
  147. x_ make -C "${cbdir}" crossgcc-i386 CPUS=$(nproc)
  148. case "$(uname -m)" in
  149. x86*|i*86|amd64) : ;;
  150. *) export CROSS_COMPILE=i386-elf- ;;
  151. esac
  152. elif [ "${arch}" = "ARMv7" ]; then
  153. [ -d "${cbdir}/util/crossgcc/xgcc/arm-eabi/" ] || \
  154. x_ make -C "${cbdir}" crossgcc-arm CPUS=$(nproc)
  155. case "$(uname -m)" in
  156. arm|arm32|armv6*|armv7*) : ;;
  157. *) export CROSS_COMPILE=arm-eabi- ;;
  158. esac
  159. elif [ "${arch}" = "AArch64" ]; then
  160. [ -d "${cbdir}/util/crossgcc/xgcc/aarch64-elf/" ] || \
  161. x_ make -C "${cbdir}" crossgcc-aarch64 CPUS=$(nproc)
  162. # aarch64 also needs armv7 toolchain for arm-trusted-firmware
  163. [ -d "${cbdir}/util/crossgcc/xgcc/arm-eabi/" ] || \
  164. x_ make -C "${cbdir}" crossgcc-arm CPUS=$(nproc)
  165. case "$(uname -m)" in
  166. arm64|aarch64) : ;;
  167. *) export CROSS_COMPILE=aarch64-elf- ;;
  168. esac
  169. fi
  170. # we *must* ensure that u-boot's build system uses crossgcc first
  171. export PATH="${PWD}/${cbdir}/util/crossgcc/xgcc/bin:$PATH"
  172. }
  173. check_config()
  174. {
  175. [ ! -f "${config}" ] && \
  176. err "check_config: ${project}/${target}: configs missing"
  177. dest_dir="${elfdir}/${target}/${config_name}"
  178. # TODO: very hacky check. do it properly (based on build.list)
  179. for elftest in "${dest_dir}"/*; do
  180. [ -f "${elftest}" ] || continue
  181. printf "Build already exists, so skipping build\n" 1>&2
  182. return 1
  183. done
  184. x_ mkdir -p "${dest_dir}"
  185. }
  186. handle_makefile()
  187. {
  188. x_ make clean -C "${codedir}"
  189. x_ cp "${config}" "${codedir}/.config"
  190. [ ! -z ${mode} ] || make -C "${codedir}" silentoldconfig || \
  191. make -C "${codedir}" oldconfig || :
  192. run_make_command || err "handle_makefile ${codedir}: no makefile!"
  193. if [ -e "${codedir}/.git" ] && [ "${project}" = "u-boot" ] && \
  194. [ "${mode}" = "distclean" ]; then
  195. x_ git -C "${codedir}" clean -fdx
  196. elif [ "${mode}" = "oldconfig" ] || [ "${mode}" = "olddefconfig" ] || \
  197. [ "${mode}" = "menuconfig" ] || [ "${mode}" = "nconfig" ]; then
  198. x_ cp "${codedir}/.config" "${config}"
  199. elif [ "${mode}" = "savedefconfig" ]; then
  200. x_ cp "${codedir}/defconfig" "${config}"
  201. fi
  202. }
  203. run_make_command()
  204. {
  205. [ -f "${codedir}/Makefile" ] || [ -f "${codedir}/makefile" ] || \
  206. [ -f "${codedir}/GNUmakefile" ] || return 1
  207. [ "${project}" = "coreboot" ] && [ -z "${mode}" ] && \
  208. x_ printf "%s\n" "${version%%-*}" >"${codedir}/.coreboot-version"
  209. x_ make ${mode} -j$(nproc) -C "${codedir}"
  210. [ "${mode}" != "clean" ] && return 0
  211. make -C "${codedir}" distclean 2>/dev/null || :
  212. }
  213. copy_elf()
  214. {
  215. while read f; do
  216. [ ! -f "${codedir}/$f" ] || \
  217. x_ cp "${codedir}/${f}" "${dest_dir}/"
  218. done < ${listfile}
  219. x_ make clean -C "${codedir}"
  220. }
  221. main $@