trees 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # Copyright (c) 2022-2023 Alper Nebi Yasak <alpernebiyasak@gmail.com>
  4. # Copyright (c) 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  5. # Copyright (c) 2023-2024 Leah Rowe <leah@libreboot.org>
  6. set -u -e
  7. . "include/lib.sh"
  8. . "include/git.sh"
  9. eval `setvars "" xarch srcdir premake cmakedir xlang mode makeargs elfdir cmd \
  10. project target target_dir targets xtree _f release bootstrapargs mkhelper \
  11. autoconfargs listfile autogenargs btype tree rev tree_depend build_depend \
  12. defconfig postmake mkhelpercfg dry dest_dir mdir`; badhash="n"
  13. main()
  14. {
  15. while getopts f:b:m:u:c:x:s:l:n:d: option; do
  16. [ -n "$_f" ] && $err "only one flag is permitted"
  17. _f="$1" && [ "$_f" = "-d" ] && dry=":"
  18. case "$1" in
  19. -d) mode="" ;;
  20. -b) mode="" ;;
  21. -u) mode="oldconfig" ;;
  22. -m) mode="menuconfig" ;;
  23. -c) mode="distclean" ;;
  24. -x) mode="crossgcc-clean" ;;
  25. -f) mode="fetch" ;;
  26. -s) mode="savedefconfig" ;;
  27. -l) mode="olddefconfig" ;;
  28. -n) mode="nconfig" ;;
  29. *) $err "invalid option '-$option'" ;;
  30. esac
  31. [ -z "${OPTARG+x}" ] && shift 1 && break
  32. project="${OPTARG#src/}"; shift 2
  33. done
  34. [ -z "$_f" ] && $err "missing flag (-m/-u/-b/-c/-x/-f/-s/-l/-n)"
  35. [ -z "$project" ] && mk $_f $(ls -1 config/git) && return 1
  36. [ -f "config/git/$project/pkg.cfg" ] || $err "'$project' not defined"
  37. for d in "elf" "config/data" "config" "src"; do
  38. eval "${d#*/}dir=\"$d/$project\""
  39. done; dest_dir="$elfdir"
  40. listfile="$datadir/build.list"
  41. [ -f "$listfile" ] || listfile="" # optional on all projects
  42. mkhelpercfg="$datadir/mkhelper.cfg"
  43. e "$mkhelpercfg" f missing && mkhelpercfg="$TMPDIR/mkhelper.cfg" && \
  44. x_ touch "$mkhelpercfg"
  45. targets="$@"; cmd="build_targets $targets"
  46. singletree "$project" && cmd="build_project"
  47. remkdir "${tmpgit%/*}"
  48. }
  49. build_project()
  50. {
  51. configure_project "$configdir" || return 0
  52. [ ! -f "$listfile" ] || $dry elfcheck || return 0
  53. [ "$mode" = "distclean" ] && mode="clean"
  54. run_make_command || return 0
  55. [ -n "$mode" ] || $dry copy_elf; return 0
  56. }
  57. build_targets()
  58. {
  59. [ -d "$configdir" ] || $err "directory, $configdir, does not exist"
  60. [ $# -gt 0 ] || targets="$(ls -1 "$configdir")" || $err "!o $configdir"
  61. for x in $targets; do
  62. [ "$x" = "list" ] && x_ ls -1 "config/$project" && \
  63. listfile="" && break
  64. target="$x"
  65. printf "'make %s', '%s', '%s'\n" "$mode" "$project" "$target"
  66. x_ handle_defconfig
  67. [ -n "$mode" ] || [ -z "$postmake" ] || $postmake || \
  68. $err "$project/$target: !postmake: $postmake"; continue
  69. done; return 0
  70. }
  71. handle_defconfig()
  72. {
  73. target_dir="$configdir/$target"
  74. [ -f "CHANGELOG" ] || fetch_project "$project"
  75. configure_project "$target_dir" || return 0
  76. x_ mkdir -p "$elfdir/$target"
  77. chkvars tree; srcdir="src/$project/$tree"
  78. if [ "$mode" = "distclean" ] || [ "$mode" = "crossgcc-clean" ]; then
  79. [ -d "$srcdir" ] || return 0
  80. fi
  81. [ -z "$mode" ] && $dry check_cross_compiler
  82. for y in "$target_dir/config"/*; do
  83. [ "$_f" = "-d" ] || [ -f "$y" ] || continue
  84. [ "$_f" = "-d" ] || defconfig="$y"
  85. [ -n "$mode" ] || check_defconfig || continue
  86. handle_makefile
  87. [ -n "$mode" ] || $dry copy_elf
  88. done; return 0
  89. }
  90. configure_project()
  91. {
  92. eval `setvars "" xarch xlang build_depend autoconfargs xtree postmake \
  93. tree_depend makeargs btype mkhelper bootstrapargs premake release`
  94. _tcfg="$1/target.cfg"; badhash="n"; [ -f "$_tcfg" ] || btype="auto"
  95. [ -f "$datadir/mkhelper.cfg" ] && eval `setcfg "$datadir/mkhelper.cfg"`
  96. while [ -f "$_tcfg" ] || [ "$cmd" != "build_project" ]; do
  97. eval `setvars "" rev tree`; eval `setcfg "$_tcfg"`
  98. printf "Loading %s config: %s\n" "$project" "$_tcfg"
  99. [ "$_f" = "-d" ] && build_depend="" # dry run
  100. [ "$cmd" = "build_project" ] && break
  101. [ "$mode" = "fetch" ] || break
  102. [ "${_tcfg%/*/target.cfg}" = "${_tcfg%"/$tree/target.cfg"}" ] \
  103. && break; _tcfg="${_tcfg%/*/target.cfg}/$tree/target.cfg"
  104. done
  105. [ "$XBMK_RELEASE" = "y" ] && [ "$release" = "n" ] && return 1
  106. [ -z "$btype" ] || [ "${mode%config}" = "$mode" ] || return 1
  107. [ -z "$mode" ] && build_dependencies
  108. mdir="$PWD/config/submodule/$project"
  109. [ -n "$tree" ] && mdir="$mdir/$tree"
  110. [ -f "CHANGELOG" ] || check_project_hashes
  111. [ "$mode" = "fetch" ] || x_ ./mk -f "$project" $target
  112. [ "$mode" = "fetch" ] || return 0
  113. [ -f "CHANGELOG" ] && return 1; fetch_${cmd#build_}; return 1
  114. }
  115. build_dependencies()
  116. {
  117. for bd in $build_depend; do
  118. bd_p="${bd%%/*}"; bd_t="${bd##*/}"
  119. [ -z "$bd_p" ] && $dry $err "$project/$tree: !bd '$bd'"
  120. [ "${bd##*/}" = "$bd" ] && bd_t=""
  121. [ -z "$bd_p" ] || $dry ./mk -b $bd_p $bd_t \
  122. || $err "!mk $project/$tree $bd_p/$bd_t"; continue
  123. done; return 0
  124. }
  125. check_project_hashes()
  126. {
  127. mkdir -p "$XBMK_CACHE/hash" || $err "!mkdir '$XBMK_CACHE/hash'"
  128. old_pjhash=""; [ ! -f "$XBMK_CACHE/hash/$project$tree" ] || \
  129. read -r old_pjhash < "$XBMK_CACHE/hash/$project$tree"
  130. x_ rm -f "$TMPDIR/project.list" "$TMPDIR/project.hash" \
  131. "$TMPDIR/project.tmp"; x_ touch "$TMPDIR/project.tmp"
  132. x_ touch "$TMPDIR/project.hash"
  133. for rmchk in "$datadir" "$configdir/$tree" "$mdir"; do
  134. [ -d "$rmchk" ] || continue
  135. find "$rmchk" -type f -not -path "*/.git*/*" >> \
  136. "$TMPDIR/project.tmp" || $err "!find $rmchk > project.tmp"
  137. done; sort "$TMPDIR/project.tmp" > "$TMPDIR/project.list" || \
  138. $err "!sort project tmp/list"
  139. while read -r rmchk; do
  140. [ ! -f "$rmchk" ] || sha512sum "$rmchk" | awk \
  141. '{print $1}' >> "$TMPDIR/project.hash" || $err "!h $rmchk"
  142. done < "$TMPDIR/project.list"
  143. pjhash="$(sha512sum "$TMPDIR/project.hash" | awk '{print $1}')" || :
  144. badhash="y" && [ "$pjhash" = "$old_pjhash" ] && badhash="n"
  145. [ -f "$XBMK_CACHE/hash/$project$tree" ] || badhash="y"
  146. printf "%s\n" "$pjhash" > "$XBMK_CACHE/hash/$project$tree" || \
  147. $err "!mk $XBMK_CACHE/hash/$project$tree"
  148. [ "$badhash" = "n" ] || rm -Rf "src/$project/$tree" \
  149. "elf/$project/$tree" "elf/$project/$target" || \
  150. $err "!rm $project $tree"; :
  151. }
  152. check_cross_compiler()
  153. {
  154. xgccargs="UPDATED_SUBMODULES=1 CPUS=$XBMK_THREADS"
  155. for _xarch in $xarch; do
  156. cbdir="src/coreboot/$tree"
  157. [ "$project" != "coreboot" ] && cbdir="src/coreboot/default"
  158. [ -n "$xtree" ] && cbdir="src/coreboot/$xtree"
  159. x_ ./mk -f coreboot ${cbdir#src/coreboot/}
  160. export PATH="$PWD/$cbdir/util/crossgcc/xgcc/bin:$PATH"
  161. export CROSS_COMPILE="${xarch% *}-"
  162. [ -n "$xlang" ] && export BUILD_LANGUAGES="$xlang"
  163. # sometimes buildgcc fails for like no reason. try twice.
  164. make -C "$cbdir" crossgcc-${_xarch%-*} $xgccargs || \
  165. make -C "$cbdir" crossgcc-${_xarch%-*} $xgccargs || \
  166. $err "!mkxgcc $project/$xtree '${_xarch%-*}' '$xgccargs'"
  167. done; return 0
  168. }
  169. check_defconfig()
  170. {
  171. [ -f "$defconfig" ] || $dry $err "$project/$target: missing defconfig"
  172. dest_dir="$elfdir/$target/${defconfig#"$target_dir/config/"}"
  173. $dry elfcheck || return 1 # skip build if a previous one exists
  174. $dry x_ mkdir -p "$dest_dir"
  175. }
  176. elfcheck()
  177. {
  178. # TODO: very hacky check. do it properly (based on build.list)
  179. for elftest in "$dest_dir"/*; do
  180. [ -e "$elftest" ] && e "$elftest" f && return 1
  181. done; return 0
  182. }
  183. handle_makefile()
  184. {
  185. $dry check_makefile "$srcdir" && x_ make clean -C "$srcdir"
  186. [ -f "$defconfig" ] && x_ cp "$defconfig" "$srcdir/.config"
  187. [ -n "$mode" ] || [ -n "$btype" ] || $dry make -C \
  188. "$srcdir" silentoldconfig || make -C "$srcdir" oldconfig || :
  189. run_make_command || $err "handle_makefile $srcdir: no makefile!"
  190. _copy=".config" && [ "$mode" = "savedefconfig" ] && _copy="defconfig"
  191. [ "${mode%config}" = "$mode" ] || \
  192. $dry x_ cp "$srcdir/$_copy" "$defconfig"
  193. [ -e "$srcdir/.git" ] && [ "$project" = "u-boot" ] && \
  194. [ "$mode" = "distclean" ] && $dry x_ git -C "$srcdir" clean -fdx; :
  195. }
  196. run_make_command()
  197. {
  198. [ -z "$premake" ] || [ -n "$mode" ] || $premake || $err "!$premake"
  199. $dry check_cmake "$srcdir" && [ -z "$mode" ] && $dry check_autoconf \
  200. "$srcdir"; $dry check_makefile "$srcdir" || return 1
  201. $dry make -C "$srcdir" $mode -j$XBMK_THREADS $makeargs || $err "!$mode"
  202. [ -z "$mkhelper" ] || [ -n "$mode" ] || $mkhelper || $err "!$mkhelper"
  203. [ "$mode" = "clean" ] && $dry make -C "$srcdir" distclean || :; :
  204. }
  205. check_cmake()
  206. {
  207. [ -z "$cmakedir" ] || $dry check_makefile "$1" || cmake -B "$1" \
  208. "$1/$cmakedir" || $dry check_makefile "$1" || $err \
  209. "$1: !cmk $cmakedir"
  210. [ -z "$cmakedir" ] || $dry check_makefile "$1" || \
  211. $err "check_cmake $1: can't generate Makefile"; return 0
  212. }
  213. check_autoconf()
  214. {
  215. (
  216. cd "$1" || $err "!cd $1"
  217. [ -f "bootstrap" ] && x_ ./bootstrap $bootstrapargs
  218. [ -f "autogen.sh" ] && x_ ./autogen.sh $autogenargs
  219. [ -f "configure" ] && x_ ./configure $autoconfargs; return 0
  220. ) || $err "can't bootstrap project: $1"
  221. }
  222. check_makefile()
  223. {
  224. [ -f "$1/Makefile" ] || [ -f "$1/makefile" ] || \
  225. [ -f "$1/GNUmakefile" ] || return 1; return 0
  226. }
  227. copy_elf()
  228. {
  229. [ -f "$listfile" ] && x_ mkdir -p "$dest_dir" && while read -r f; do
  230. [ -f "$srcdir/$f" ] && x_ cp "$srcdir/$f" "$dest_dir"
  231. done < "$listfile"; x_ make clean -C "$srcdir"
  232. }
  233. main $@ || exit 0
  234. . "$mkhelpercfg"
  235. $cmd