trees 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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-2024 Leah Rowe <leah@libreboot.org>
  6. set -u -e
  7. . "include/err.sh"
  8. . "include/option.sh"
  9. . "include/git.sh"
  10. eval "$(setvars "" xarch cfgsdir codedir config config_name xlang mode \
  11. elfdir listfile project target target_dir targets tree _f target1 \
  12. bootstrapargs autoconfargs cmakedir makeargs autogenargs xtree)"
  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. elfdir="elf/${project}"
  34. cfgsdir="config/${project}"
  35. remkdir "${tmpgit%/*}"
  36. check_project
  37. _cmd="build_projects"
  38. [ -f "config/${project}/build.list" ] && _cmd="build_targets"
  39. $_cmd $@
  40. }
  41. build_projects()
  42. {
  43. [ $# -gt 0 ] && x_ ./update trees $_f $@
  44. [ "$mode" = "fetch" ] && [ ! -f "CHANGELOG" ] && \
  45. eval "fetch_project_repo; return 0;"
  46. load_project_config "$cfgsdir"
  47. codedir="src/${project}"
  48. [ -d "$codedir" ] || x_ ./update trees -f "$project"
  49. [ "$mode" = "distclean" ] && mode="clean"
  50. run_make_command || return 0
  51. }
  52. build_targets()
  53. {
  54. [ "$elfdir" = "elf/coreboot" ] && \
  55. elfdir="elf/coreboot_nopayload_DO_NOT_FLASH"
  56. [ -d "$cfgsdir" ] || $err "directory, $cfgsdir, does not exist"
  57. listfile="${cfgsdir}/build.list"
  58. [ -f "$listfile" ] || $err "list file, $listfile, does not exist"
  59. # Build for all targets if no argument is given
  60. [ $# -gt 0 ] && target1="$1"
  61. [ "$target1" = "utils" ] && [ "$project" = "coreboot" ] && \
  62. shift 1
  63. targets=$(items "$cfgsdir") || \
  64. $err "Cannot get options for $cfgsdir"
  65. [ $# -gt 0 ] && targets=$@
  66. [ -z "$mode" ] && x_ mkdir -p "$elfdir"
  67. handle_targets
  68. }
  69. handle_targets()
  70. {
  71. for x in $targets; do
  72. target="${x}"
  73. printf "Running 'make %s' for project '%s, target '%s''\n" \
  74. "$mode" "$project" "$target"
  75. [ "$project" != "coreboot" ] || [ -n "$mode" ] || \
  76. [ "$target1" = "utils" ] || \
  77. x_ ./vendor download $target
  78. x_ handle_defconfig
  79. done
  80. [ "$target1" = "utils" ] && [ "$project" = "coreboot" ] && return 0
  81. [ -z "${mode}" ] && printf "Done! Check %s/\n\n" "$elfdir"; return 0
  82. }
  83. handle_defconfig()
  84. {
  85. handle_src_tree "$target" || return 0
  86. [ "$target1" = "utils" ] && [ "$project" = "coreboot" ] && \
  87. eval "handle_coreboot_utils \"$tree\"; return 0"
  88. for y in "${target_dir}/config"/*; do
  89. [ -f "$y" ] || continue
  90. config="$y"
  91. config_name="${config#"${target_dir}/config/"}"
  92. printf "handle/make/config %s %s: handling config %s\n" \
  93. "$project" "$target" "$config_name"
  94. [ -n "$mode" ] || check_config || continue
  95. handle_makefile
  96. [ -n "$mode" ] || copy_elf
  97. done
  98. }
  99. handle_src_tree()
  100. {
  101. target_dir="${cfgsdir}/${target}"
  102. [ "$mode" = "fetch" ] && [ ! -f "CHANGELOG" ] && \
  103. eval "fetch_project_trees; return 1;"
  104. load_project_config "$target_dir"
  105. x_ mkdir -p "${elfdir}/${target}"
  106. [ -z "$tree" ] && $err "handle_src_tree $project/$tree: tree unset"
  107. codedir="src/${project}/${tree}"
  108. if [ ! -d "$codedir" ]; then
  109. if [ "$mode" = "distclean" ] || \
  110. [ "$mode" = "crossgcc-clean" ]; then
  111. printf "Directory %s missing; skipping clean\n" \
  112. "$codedir" 1>&2
  113. return 1
  114. fi
  115. x_ ./update trees -f "$project" "$target"
  116. fi
  117. [ "$target1" = "utils" ] && [ "$project" = "coreboot" ] && return 0
  118. [ -z "$mode" ] && check_cross_compiler; return 0
  119. }
  120. load_project_config()
  121. {
  122. eval "$(setvars "" xarch xlang tree bootstrapargs autoconfargs \
  123. xtree tree_depend)"
  124. [ -f "${1}/target.cfg" ] || return 0
  125. . "${1}/target.cfg" || \
  126. $err "load_project_config ${1}: cannot load target.cfg"; return 0
  127. }
  128. check_cross_compiler()
  129. {
  130. for _xarch in $xarch; do
  131. cbdir="src/coreboot/${tree}"
  132. [ "$project" != "coreboot" ] && cbdir="src/coreboot/default"
  133. [ -n "$xtree" ] && cbdir="src/coreboot/$xtree"
  134. x_ ./update trees -f coreboot ${cbdir#src/coreboot/}
  135. export PATH="${PWD}/${cbdir}/util/crossgcc/xgcc/bin:$PATH"
  136. export CROSS_COMPILE="${xarch% *}-"
  137. [ -n "${xlang}" ] && export BUILD_LANGUAGES="$xlang"
  138. [ -d "${cbdir}/util/crossgcc/xgcc/${_xarch}/" ] && continue
  139. x_ make -C "$cbdir" crossgcc-${_xarch%-*} CPUS=$threads
  140. done
  141. }
  142. check_config()
  143. {
  144. [ -f "$config" ] || $err "check_config: ${project}/${target}: no config"
  145. dest_dir="${elfdir}/${target}/${config_name}"
  146. # TODO: very hacky check. do it properly (based on build.list)
  147. for elftest in "${dest_dir}"/*; do
  148. [ -f "$elftest" ] || continue
  149. printf "Build already exists, so skipping build\n" 1>&2
  150. return 1
  151. done
  152. x_ mkdir -p "$dest_dir"
  153. }
  154. handle_makefile()
  155. {
  156. x_ make clean -C "$codedir"
  157. x_ cp "$config" "${codedir}/.config"
  158. [ -n "$mode" ] || make -C "$codedir" silentoldconfig || \
  159. make -C "$codedir" oldconfig || :
  160. run_make_command || $err "handle_makefile $codedir: no makefile!"
  161. if [ -e "${codedir}/.git" ] && [ "$project" = "u-boot" ] && \
  162. [ "$mode" = "distclean" ]; then
  163. x_ git -C "$codedir" clean -fdx
  164. elif [ "$mode" = "oldconfig" ] || [ "$mode" = "olddefconfig" ] || \
  165. [ "$mode" = "menuconfig" ] || [ "$mode" = "nconfig" ]; then
  166. x_ cp "${codedir}/.config" "$config"
  167. elif [ "$mode" = "savedefconfig" ]; then
  168. x_ cp "${codedir}/defconfig" "$config"
  169. fi
  170. }
  171. run_make_command()
  172. {
  173. check_cmake "$codedir"
  174. [ -z "$mode" ] && check_autoconf "$codedir"
  175. check_makefile "$codedir" || return 1
  176. [ "$project" = "coreboot" ] && [ -z "$mode" ] && x_ \
  177. printf "%s\n" "${version%%-*}" > "$codedir/.coreboot-version"
  178. make $mode -j$threads $makeargs -C "$codedir" || \
  179. $err "run_make $codedir: !make $mode"
  180. [ "$mode" != "clean" ] && return 0
  181. make -C "$codedir" distclean 2>/dev/null || :
  182. }
  183. check_cmake()
  184. {
  185. [ -z "${cmakedir}" ] || \
  186. check_makefile "${1}" || \
  187. cmake -B "${1}" "${1}/${cmakedir}" || \
  188. check_makefile "${1}" || \
  189. $err "check_cmake ${1}: can't cmake ${cmakedir}"
  190. [ -z "${cmakedir}" ] || check_makefile "${1}" || \
  191. $err "check_cmake ${1}: could not generate Makefile"
  192. return 0
  193. }
  194. check_autoconf()
  195. {
  196. (
  197. _cfgopt=""
  198. cd "${1}" || $err "!cd $1"
  199. [ -f "bootstrap" ] && x_ ./bootstrap $bootstrapargs
  200. [ -f "autogen.sh" ] && x_ ./autogen.sh ${autogenargs}
  201. [ -f "configure" ] && x_ ./configure $autoconfargs; return 0
  202. ) || $err "can't bootstrap project: $1"
  203. }
  204. check_makefile()
  205. {
  206. [ -f "${1}/Makefile" ] || [ -f "${1}/makefile" ] || \
  207. [ -f "${1}/GNUmakefile" ] || return 1; return 0
  208. }
  209. copy_elf()
  210. {
  211. while read -r f; do
  212. [ ! -f "${codedir}/$f" ] || \
  213. x_ cp "${codedir}/${f}" "$dest_dir"
  214. done < "$listfile"
  215. x_ make clean -C "$codedir"
  216. }
  217. main $@