trees 7.4 KB

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