trees 8.9 KB

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