trees 9.0 KB

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