trees 9.0 KB

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