lib.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # SPDX-License-Identifier: GPL-3.0-only
  2. # Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  3. # Copyright (c) 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. # Copyright (c) 2020-2024 Leah Rowe <leah@libreboot.org>
  5. export LC_COLLATE=C
  6. export LC_ALL=C
  7. _ua="Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"
  8. tmpdir_was_set="y"
  9. cbdir="src/coreboot/default"
  10. ifdtool="elf/ifdtool/default/ifdtool"
  11. cbfstool="elf/cbfstool/default/cbfstool"
  12. tmpgit="$PWD/tmp/gitclone"
  13. grubdata="config/data/grub"
  14. err="err_"
  15. badcmd()
  16. {
  17. errmsg="Bad command"
  18. [ $# -gt 0 ] && errmsg="Bad command ($1)"
  19. dstr="See $projectname build system docs: ${projectsite}docs/maintain/"
  20. [ -d "docs" ] && dstr="$dstr (local docs available via docs/)"
  21. $err "$errmsg. $dstr"
  22. }
  23. err_()
  24. {
  25. printf "ERROR %s: %s\n" "$0" "$1" 1>&2
  26. exit 1
  27. }
  28. setvars()
  29. {
  30. _setvars=""
  31. [ $# -lt 2 ] && $err "setvars: too few arguments"
  32. val="$1" && shift 1
  33. for var in $@; do
  34. _setvars="$var=\"$val\"; $_setvars"
  35. done
  36. printf "%s\n" "${_setvars% }"
  37. }
  38. eval "$(setvars "" xbmk_release tmpdir _nogit version board boarddir relname \
  39. versiondate threads projectname projectsite aur_notice cfgsdir datadir)"
  40. read -r projectname < projectname || :
  41. read -r projectsite < projectsite || :
  42. install_packages()
  43. {
  44. [ $# -lt 2 ] && badcmd "fewer than two arguments"
  45. [ -f "config/dependencies/$2" ] || badcmd "unsupported target"
  46. . "config/dependencies/$2" || $err "! . config/dependencies/$2"
  47. $pkg_add $pkglist || $err "Cannot install packages"
  48. [ -n "$aur_notice" ] && \
  49. printf "You need AUR packages: %s\n" "$aur_notice" 1>&2; return 0
  50. }
  51. [ $# -gt 0 ] && [ "$1" = "dependencies" ] && install_packages $@ && return 0
  52. id -u 1>/dev/null 2>/dev/null || $err "suid check failed (id -u)"
  53. [ "$(id -u)" != "0" ] || $err "this command as root is not permitted"
  54. [ -z "${TMPDIR+x}" ] && tmpdir_was_set="n"
  55. if [ "$tmpdir_was_set" = "y" ]; then
  56. [ "${TMPDIR%_*}" = "/tmp/xbmk" ] || tmpdir_was_set="n"
  57. fi
  58. if [ "$tmpdir_was_set" = "n" ]; then
  59. [ -f "lock" ] && \
  60. $err "$PWD/lock exists. If a build isn't going, delete and re-run."
  61. export TMPDIR="/tmp"
  62. tmpdir="$(mktemp -d -t xbmk_XXXXXXXX)"
  63. export TMPDIR="$tmpdir"
  64. touch lock || $err "cannot create 'lock' file"
  65. else
  66. export TMPDIR="$TMPDIR"
  67. tmpdir="$TMPDIR"
  68. fi
  69. # if "y": a coreboot target won't be built if target.cfg says release="n"
  70. # (this is used to exclude certain build targets from releases)
  71. [ -z "${XBMK_RELEASE+x}" ] && xbmk_release="n"
  72. [ -z "$xbmk_release" ] && xbmk_release="$XBMK_RELEASE"
  73. [ "$xbmk_release" = "n" ] || [ "$xbmk_release" = "y" ] || xbmk_release="n"
  74. export XBMK_RELEASE="$xbmk_release"
  75. [ -z "${XBMK_THREADS+x}" ] || threads="$XBMK_THREADS"
  76. [ -z "$threads" ] && threads=1
  77. expr "X$threads" : "X-\{0,1\}[0123456789][0123456789]*$" \
  78. 1>/dev/null 2>/dev/null || threads=1 # user specified a non-integer
  79. export XBMK_THREADS="$threads"
  80. x_() {
  81. [ $# -lt 1 ] || $@ || $err "Unhandled non-zero exit: $@"; return 0
  82. }
  83. [ -e ".git" ] || [ -f "version" ] || printf "unknown\n" > version || \
  84. $err "Cannot generate unknown version file"
  85. [ -e ".git" ] || [ -f "versiondate" ] || printf "1716415872\n" > versiondate || \
  86. $err "Cannot generate unknown versiondate file"
  87. [ ! -f version ] || read -r version < version || :
  88. version_="$version"
  89. [ ! -e ".git" ] || version="$(git describe --tags HEAD 2>&1)" || \
  90. version="git-$(git rev-parse HEAD 2>&1)" || version="$version_"
  91. [ ! -f versiondate ] || read -r versiondate < versiondate || :
  92. versiondate_="$versiondate"
  93. [ ! -e ".git" ] || versiondate="$(git show --no-patch --no-notes \
  94. --pretty='%ct' HEAD)" || versiondate="$versiondate_"
  95. for p in projectname version versiondate projectsite; do
  96. eval "[ -n \"\$$p\" ] || $err \"$p unset\""
  97. eval "x_ printf \"%s\\n\" \"\$$p\" > $p"
  98. done
  99. relname="$projectname-$version"
  100. export LOCALVERSION="-$projectname-${version%%-*}"
  101. items()
  102. {
  103. rval=1
  104. e "$1" d not && return 1
  105. for x in "$1/"*; do
  106. # -e used because this is for files *or* directories
  107. [ -e "$x" ] || continue
  108. printf "%s\n" "${x##*/}" 2>/dev/null
  109. rval=0
  110. done
  111. return $rval
  112. }
  113. scan_config()
  114. {
  115. awkstr=" /\{.*$1.*}{/ {flag=1;next} /\}/{flag=0} flag { print }"
  116. confdir="$2"
  117. revfile="$(mktemp -t sources.XXXXXXXXXX)"
  118. cat "$confdir/"* > "$revfile" || $err "$confdir: can't cat files"
  119. while read -r line ; do
  120. set $line 1>/dev/null 2>/dev/null || :
  121. if [ "${1%:}" = "depend" ]; then
  122. depend="$depend $2"
  123. else
  124. eval "${1%:}=\"$2\""
  125. fi
  126. done << EOF
  127. $(eval "awk '$awkstr' \"$revfile\"")
  128. EOF
  129. rm -f "$revfile" || $err "scan_config: Cannot remove tmpfile"
  130. }
  131. check_defconfig()
  132. {
  133. [ -d "$1" ] || $err "Target '$1' not defined."
  134. for x in "$1"/config/*; do
  135. [ -f "$x" ] && printf "%s\n" "$x" && return 1
  136. done
  137. }
  138. remkdir()
  139. {
  140. rm -Rf "$1" || $err "remkdir: !rm -Rf \"$1\""
  141. mkdir -p "$1" || $err "remkdir: !mkdir -p \"$1\""
  142. }
  143. git_err()
  144. {
  145. printf "You need to set git name/email, like so:\n%s\n\n" "$1" 1>&2
  146. $err "Git name/email not configured"
  147. }
  148. mkrom_tarball()
  149. {
  150. printf "%s\n" "$version" > "$1/version" || $err "$1 !version"
  151. printf "%s\n" "$versiondate" > "$1/versiondate" || $err "$1 !vdate"
  152. printf "%s\n" "$projectname" > "$1/projectname" || $err "$1 !pname"
  153. mktarball "$1" "${1%/*}/${relname}_${1##*/}.tar.xz"
  154. x_ rm -Rf "$1"
  155. }
  156. mktarball()
  157. {
  158. [ "${2%/*}" = "$2" ] || \
  159. mkdir -p "${2%/*}" || $err "mk, !mkdir -p \"${2%/*}\""
  160. printf "\nCreating archive: %s\n\n" "$2"
  161. tar -c "$1" | xz -T$threads -9e > "$2" || \
  162. $err "mktarball 2, $1"
  163. mksha512sum "$2" "${2##*/}.sha512"
  164. }
  165. mksha512sum()
  166. {
  167. (
  168. [ "${1%/*}" != "$1" ] && x_ cd "${1%/*}"
  169. sha512sum ./"${1##*/}" >> "$2" || \
  170. $err "!sha512sum \"$1\" > \"$2\""
  171. ) || $err "failed to create tarball checksum"
  172. }
  173. rmgit()
  174. {
  175. (
  176. cd "$1" || $err "!cd gitrepo $1"
  177. find . -name ".git" -exec rm -Rf {} + || $err "!rm .git $1"
  178. find . -name ".gitmodules" -exec rm -Rf {} + || $err "!rm .gitmod $1"
  179. ) || $err "Cannot remove .git/.gitmodules in $1"
  180. }
  181. e()
  182. {
  183. es_t="e"
  184. [ $# -gt 1 ] && es_t="$2"
  185. es2="already exists"
  186. estr="[ -$es_t \"\$1\" ] || return 1"
  187. [ $# -gt 2 ] && estr="[ -$es_t \"\$1\" ] && return 1" && es2="missing"
  188. eval "$estr"
  189. printf "%s %s\n" "$1" "$es2" 1>&2
  190. }
  191. # return 0 if project is single-tree, otherwise 1
  192. # e.g. coreboot is multi-tree, so 1
  193. singletree()
  194. {
  195. for targetfile in "config/${1}/"*/target.cfg; do
  196. [ -e "$targetfile" ] || continue
  197. [ -f "$targetfile" ] && return 1
  198. done
  199. }
  200. download()
  201. {
  202. dl_fail="y" # 1 url, 2 url backup, 3 destination, 4 checksum
  203. vendor_checksum "$4" "$3" 2>/dev/null || dl_fail="n"
  204. [ "$dl_fail" = "n" ] && e "$3" f && return 0
  205. x_ mkdir -p "${3%/*}" && for url in "$1" "$2"; do
  206. [ "$dl_fail" = "n" ] && break
  207. [ -z "$url" ] && continue
  208. x_ rm -f "$3"
  209. curl --location --retry 3 -A "$_ua" "$url" -o "$3" || \
  210. wget --tries 3 -U "$_ua" "$url" -O "$3" || continue
  211. vendor_checksum "$4" "$3" || dl_fail="n"
  212. done;
  213. [ "$dl_fail" = "y" ] && $err "$1 $2 $3 $4: not downloaded"; return 0
  214. }
  215. vendor_checksum()
  216. {
  217. [ "$(sha512sum "$2" | awk '{print $1}')" != "$1" ] || return 1
  218. printf "Bad checksum for file: %s\n" "$2" 1>&2
  219. rm -f "$2" || :
  220. }