trees 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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-2025 Leah Rowe <leah@libreboot.org>
  6. set -u -e
  7. . "include/lib.sh"
  8. . "include/git.sh"
  9. XBMKPATH="$PATH"
  10. eval "`setvars "" xarch srcdir premake gnatdir xlang mode makeargs elfdir cmd \
  11. project target target_dir targets xtree _f release bootstrapargs mkhelper \
  12. autoconfargs listfile autogenargs btype tree rev tree_depend build_depend \
  13. defconfig postmake mkhelpercfg dry dest_dir mdir cleanargs gccver gccfull \
  14. gnatver gnatfull gccdir cmakedir`"; badhash="n"
  15. main()
  16. {
  17. while getopts f:b:m:u:c:x:s:l:n:d: option; do
  18. [ -n "$_f" ] && $err "only one flag is permitted"
  19. _f="$1" && [ "$_f" = "-d" ] && dry=":"
  20. case "$1" in
  21. -d) mode="" ;;
  22. -b) mode="" ;;
  23. -u) mode="oldconfig" ;;
  24. -m) mode="menuconfig" ;;
  25. -c) mode="distclean" ;;
  26. -x) mode="crossgcc-clean" ;;
  27. -f) mode="fetch" ;;
  28. -s) mode="savedefconfig" ;;
  29. -l) mode="olddefconfig" ;;
  30. -n) mode="nconfig" ;;
  31. *) $err "invalid option '-$option'" ;;
  32. esac
  33. [ -z "${OPTARG+x}" ] && shift 1 && break
  34. project="${OPTARG#src/}"; shift 2
  35. done
  36. [ -z "$_f" ] && $err "missing flag (-m/-u/-b/-c/-x/-f/-s/-l/-n)"
  37. [ -z "$project" ] && mk $_f $(ls -1 config/git) && 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="$*"; cmd="build_targets $targets"
  48. singletree "$project" && cmd="build_project"
  49. remkdir "${tmpgit%/*}"
  50. }
  51. build_project()
  52. {
  53. configure_project "$configdir" || return 0
  54. [ ! -f "$listfile" ] || $dry elfcheck || return 0
  55. [ "$mode" = "distclean" ] && mode="clean"
  56. run_make_command || return 0
  57. [ -n "$mode" ] || $dry copy_elf; return 0
  58. }
  59. build_targets()
  60. {
  61. [ -d "$configdir" ] || $err "directory, $configdir, does not exist"
  62. [ $# -gt 0 ] || targets="$(ls -1 "$configdir")" || $err "!o $configdir"
  63. for x in $targets; do
  64. unset CROSS_COMPILE
  65. export PATH="$XBMKPATH"
  66. [ "$x" = "list" ] && x_ ls -1 "config/$project" && \
  67. listfile="" && break
  68. target="$x"
  69. printf "'make %s', '%s', '%s'\n" "$mode" "$project" "$target"
  70. x_ handle_defconfig
  71. [ -n "$mode" ] || [ -z "$postmake" ] || $postmake || \
  72. $err "$project/$target: !postmake: $postmake"; continue
  73. done; return 0
  74. }
  75. handle_defconfig()
  76. {
  77. target_dir="$configdir/$target"
  78. [ -f "CHANGELOG" ] || fetch_project "$project"
  79. configure_project "$target_dir" || return 0
  80. x_ mkdir -p "$elfdir/$target"
  81. chkvars tree; 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 "" cleanargs build_depend autoconfargs xtree postmake \
  97. tree_depend makeargs btype mkhelper bootstrapargs premake release \
  98. xarch xlang`"
  99. _tcfg="$1/target.cfg"; badhash="n"; [ -f "$_tcfg" ] || btype="auto"
  100. [ -f "$datadir/mkhelper.cfg" ] && \
  101. eval "`setcfg "$datadir/mkhelper.cfg"`"
  102. while [ -f "$_tcfg" ] || [ "$cmd" != "build_project" ]; do
  103. eval "`setvars "" rev tree`"; eval "`setcfg "$_tcfg"`"
  104. printf "Loading %s config: %s\n" "$project" "$_tcfg"
  105. [ "$_f" = "-d" ] && build_depend="" # dry run
  106. [ "$cmd" = "build_project" ] && break
  107. [ "$mode" = "fetch" ] || break
  108. [ "${_tcfg%/*/target.cfg}" = "${_tcfg%"/$tree/target.cfg"}" ] \
  109. && break; _tcfg="${_tcfg%/*/target.cfg}/$tree/target.cfg"
  110. done
  111. [ "$XBMK_RELEASE" = "y" ] && [ "$release" = "n" ] && return 1
  112. [ -z "$btype" ] || [ "${mode%config}" = "$mode" ] || return 1
  113. [ -z "$mode" ] && build_dependencies
  114. mdir="$PWD/config/submodule/$project"
  115. [ -n "$tree" ] && mdir="$mdir/$tree"
  116. [ -f "CHANGELOG" ] || check_project_hashes
  117. [ "$mode" = "fetch" ] || x_ ./mk -f "$project" "$target"
  118. [ "$mode" = "fetch" ] || return 0
  119. [ -f "CHANGELOG" ] && return 1; fetch_${cmd#build_}; return 1
  120. }
  121. build_dependencies()
  122. {
  123. for bd in $build_depend; do
  124. bd_p="${bd%%/*}"; bd_t="${bd##*/}"
  125. [ -z "$bd_p" ] && $dry $err "$project/$tree: !bd '$bd'"
  126. [ "${bd##*/}" = "$bd" ] && bd_t=""
  127. [ -z "$bd_p" ] || $dry ./mk -b $bd_p $bd_t \
  128. || $err "!mk $project/$tree $bd_p/$bd_t"; continue
  129. done; return 0
  130. }
  131. check_project_hashes()
  132. {
  133. mkdir -p "$XBMK_CACHE/hash" || $err "!mkdir '$XBMK_CACHE/hash'"
  134. old_pjhash=""; [ ! -f "$XBMK_CACHE/hash/$project$tree" ] || \
  135. read -r old_pjhash < "$XBMK_CACHE/hash/$project$tree"
  136. x_ rm -f "$TMPDIR/project.list" "$TMPDIR/project.hash" \
  137. "$TMPDIR/project.tmp"; x_ touch "$TMPDIR/project.tmp"
  138. x_ touch "$TMPDIR/project.hash"
  139. for rmchk in "$datadir" "$configdir/$tree" "$mdir"; do
  140. [ -d "$rmchk" ] || continue
  141. find "$rmchk" -type f -not -path "*/.git*/*" >> \
  142. "$TMPDIR/project.tmp" || $err "!find $rmchk > project.tmp"
  143. done; sort "$TMPDIR/project.tmp" > "$TMPDIR/project.list" || \
  144. $err "!sort project tmp/list"
  145. while read -r rmchk; do
  146. [ ! -f "$rmchk" ] || sha512sum "$rmchk" | awk \
  147. '{print $1}' >> "$TMPDIR/project.hash" || $err "!h $rmchk"
  148. done < "$TMPDIR/project.list"
  149. pjhash="$(sha512sum "$TMPDIR/project.hash" | awk '{print $1}')" || :
  150. badhash="y" && [ "$pjhash" = "$old_pjhash" ] && badhash="n"
  151. [ -f "$XBMK_CACHE/hash/$project$tree" ] || badhash="y"
  152. printf "%s\n" "$pjhash" > "$XBMK_CACHE/hash/$project$tree" || \
  153. $err "!mk $XBMK_CACHE/hash/$project$tree"
  154. [ "$badhash" = "n" ] || rm -Rf "src/$project/$tree" \
  155. "elf/$project/$tree" "elf/$project/$target" || \
  156. $err "!rm $project $tree"; :
  157. }
  158. check_cross_compiler()
  159. {
  160. xgccargs="UPDATED_SUBMODULES=1 CPUS=$XBMK_THREADS"
  161. for _xarch in $xarch; do
  162. cbdir="src/coreboot/$tree"
  163. [ "$project" != "coreboot" ] && cbdir="src/coreboot/default"
  164. [ -n "$xtree" ] && cbdir="src/coreboot/$xtree"
  165. x_ ./mk -f coreboot "${cbdir#src/coreboot/}"
  166. export PATH="$PWD/$cbdir/util/crossgcc/xgcc/bin:$PATH"
  167. export CROSS_COMPILE="${xarch% *}-"
  168. [ -n "$xlang" ] && export BUILD_LANGUAGES="$xlang"
  169. xfix="${_xarch%-*}" && [ "$xfix" = "x86_64" ] && xfix="x64"
  170. # match gnat-X to gcc
  171. check_gnu_path gcc gnat || check_gnu_path gnat gcc || \
  172. $err "Cannot match host GCC/GNAT versions"
  173. # sometimes buildgcc fails for like no reason. try twice.
  174. make -C "$cbdir" crossgcc-$xfix $xgccargs || \
  175. make -C "$cbdir" crossgcc-$xfix $xgccargs || \
  176. $err "!mkxgcc $project/$xtree '$xfix' '$xgccargs'"
  177. # we only want to mess with hostcc to build xgcc
  178. rm -f "$XBMK_CACHE/gnupath/"* || \
  179. $err "Cannot clear gnupath/"; :
  180. done; return 0
  181. }
  182. # fix mismatching gcc/gnat versions on debian trixie/sid. as of december 2024,
  183. # trixie/sid had gnat-13 as gnat and gcc-14 as gcc, but has gnat-14 in apt. in
  184. # some cases, gcc 13+14 and gnat-13 are present; or gnat-14 and gcc-14, but
  185. # gnat in PATH never resolves to gnat-14, because gnat-14 was "experimental"
  186. check_gnu_path()
  187. {
  188. [ $# -lt 2 ] && $err "check_gnu_path: Too few arguments"
  189. [ "$1" = "$2" ] && $err "check_gnu_path: Both arguments identical"
  190. for _gnuarg in 1 2; do
  191. eval "[ \"\$$_gnuarg\" = \"gcc\" ] && continue"
  192. eval "[ \"\$$_gnuarg\" = \"gnat\" ] && continue"
  193. $err "check_gnu_path: Invalid argument \"$_gnuarg\""
  194. done
  195. command -v "$1" 1>/dev/null || $err "Host '$1' unavailable"
  196. eval "`setvars "" gccver gccfull gnatver gnatfull gccdir gnatdir`"
  197. gnu_setver "$1" "$1" || $err "Command '$1' unavailable."
  198. gnu_setver "$2" "$2" || :
  199. eval "[ -z \"\$$1ver\" ] && $err \"Cannot detect host '$1' version\""
  200. [ "$gnatfull" = "$gccfull" ] && return 0
  201. eval "$1dir=\"$(dirname "$(command -v "$1")")\""
  202. eval "_gnudir=\"\$$1dir\"; _gnuver=\"\$$1ver\""
  203. for _gnubin in "$_gnudir/$2-"*; do
  204. [ -f "$_gnubin" ] || continue
  205. [ "${_gnubin#"$_gnudir/$2-"}" = "$_gnuver" ] || continue
  206. _gnuver="${_gnubin#"$_gnudir/$2-"}"; break
  207. done
  208. gnu_setver "$2" "$_gnudir/$2-$_gnuver" || return 1
  209. [ "$gnatfull" = "$gccfull" ] || return 1
  210. (
  211. rm -f "$XBMK_CACHE/gnupath/"* || $err "Cannot clear gnupath/"
  212. cd "$XBMK_CACHE/gnupath" || $err "Can't cd to gnupath/"
  213. for _gnubin in "$_gnudir/$2"*"-$_gnuver"; do
  214. [ -e "$_gnubin" ] || continue; _gnuutil="${_gnubin##*/}"
  215. x_ ln -s "$_gnubin" "${_gnuutil%"-$_gnuver"}"
  216. done
  217. ) || $err "Cannot create $2-$_gnuver link in $_gnudir"; :
  218. }
  219. gnu_setver()
  220. {
  221. eval "$2 --version 1>/dev/null 2>/dev/null || return 1"
  222. eval "$1ver=\"`"$2" --version 2>/dev/null | head -n1`\""
  223. eval "$1ver=\"\${$1ver##* }\""
  224. eval "$1full=\"\$$1ver\""
  225. eval "$1ver=\"\${$1ver%%.*}\""; :
  226. }
  227. check_defconfig()
  228. {
  229. [ -f "$defconfig" ] || $dry $err "$project/$target: missing defconfig"
  230. dest_dir="$elfdir/$target/${defconfig#"$target_dir/config/"}"
  231. $dry elfcheck || return 1 # skip build if a previous one exists
  232. $dry x_ mkdir -p "$dest_dir"
  233. }
  234. elfcheck()
  235. {
  236. # TODO: very hacky check. do it properly (based on build.list)
  237. for elftest in "$dest_dir"/*; do
  238. [ -e "$elftest" ] && e "$elftest" f && return 1
  239. done; return 0
  240. }
  241. handle_makefile()
  242. {
  243. $dry check_makefile "$srcdir" && x_ make -C "$srcdir" $cleanargs clean
  244. [ -f "$defconfig" ] && x_ cp "$defconfig" "$srcdir/.config"
  245. [ -n "$mode" ] || [ -n "$btype" ] || $dry make -C \
  246. "$srcdir" silentoldconfig || make -C "$srcdir" oldconfig || :
  247. run_make_command || $err "handle_makefile $srcdir: no makefile!"
  248. _copy=".config" && [ "$mode" = "savedefconfig" ] && _copy="defconfig"
  249. [ "${mode%config}" = "$mode" ] || \
  250. $dry x_ cp "$srcdir/$_copy" "$defconfig"
  251. [ -e "$srcdir/.git" ] && [ "$project" = "u-boot" ] && \
  252. [ "$mode" = "distclean" ] && \
  253. $dry x_ git -C "$srcdir" $cleanargs clean -fdx; :
  254. }
  255. run_make_command()
  256. {
  257. [ -z "$premake" ] || [ -n "$mode" ] || $premake || $err "!$premake"
  258. $dry check_cmake "$srcdir" && [ -z "$mode" ] && $dry check_autoconf \
  259. "$srcdir"; $dry check_makefile "$srcdir" || return 1
  260. $dry make -C "$srcdir" $mode -j$XBMK_THREADS $makeargs || $err "!$mode"
  261. [ -z "$mkhelper" ] || [ -n "$mode" ] || $mkhelper || $err "!$mkhelper"
  262. [ "$mode" != "clean" ] || \
  263. $dry make -C "$srcdir" $cleanargs distclean || :; :
  264. }
  265. check_cmake()
  266. {
  267. [ -z "$cmakedir" ] || $dry check_makefile "$1" || cmake -B "$1" \
  268. "$1/$cmakedir" || $dry check_makefile "$1" || $err \
  269. "$1: !cmk $cmakedir"
  270. [ -z "$cmakedir" ] || $dry check_makefile "$1" || \
  271. $err "check_cmake $1: can't generate Makefile"; return 0
  272. }
  273. check_autoconf()
  274. {
  275. (
  276. cd "$1" || $err "!cd $1"
  277. [ -f "bootstrap" ] && x_ ./bootstrap $bootstrapargs
  278. [ -f "autogen.sh" ] && x_ ./autogen.sh $autogenargs
  279. [ -f "configure" ] && x_ ./configure $autoconfargs; return 0
  280. ) || $err "can't bootstrap project: $1"
  281. }
  282. check_makefile()
  283. {
  284. [ -f "$1/Makefile" ] || [ -f "$1/makefile" ] || \
  285. [ -f "$1/GNUmakefile" ] || return 1; return 0
  286. }
  287. copy_elf()
  288. {
  289. [ -f "$listfile" ] && x_ mkdir -p "$dest_dir" && while read -r f; do
  290. [ -f "$srcdir/$f" ] && x_ cp "$srcdir/$f" "$dest_dir"
  291. done < "$listfile"; x_ make clean -C "$srcdir" $cleanargs
  292. }
  293. main "$@" || exit 0
  294. . "$mkhelpercfg"
  295. $cmd