trees 9.0 KB

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