lib.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. ifdtool="elf/ifdtool/default/ifdtool"
  9. cbfstool="elf/cbfstool/default/cbfstool"
  10. tmpgit="$PWD/tmp/gitclone"
  11. grubdata="config/data/grub"
  12. err="err_"
  13. err_()
  14. {
  15. printf "ERROR %s: %s\n" "$0" "$1" 1>&2; exit 1
  16. }
  17. setvars()
  18. {
  19. _setvars="" && [ $# -lt 2 ] && $err "setvars: too few arguments"
  20. val="$1" && shift 1 && for var in $@; do
  21. _setvars="$var=\"$val\"; $_setvars"
  22. done; printf "%s\n" "${_setvars% }"
  23. }
  24. chkvars()
  25. {
  26. for var in $@; do
  27. eval "[ -n "\${$var+x}" ] || \$err \"$var unset\""
  28. eval "[ -n "\$$var" ] || \$err \"$var unset\""
  29. done; return 0
  30. }
  31. eval `setvars "" _nogit board xbmk_parent versiondate projectsite projectname \
  32. aur_notice configdir datadir version relname`
  33. for fv in projectname projectsite version versiondate; do
  34. eval "[ ! -f "$fv" ] || read -r $fv < \"$fv\" || :"
  35. done; chkvars projectname projectsite
  36. setcfg()
  37. {
  38. [ $# -gt 1 ] && printf "e \"%s\" f missing && return %s;\n" "$1" "$2"
  39. [ $# -gt 1 ] || \
  40. printf "e \"%s\" f not && %s \"Missing config\";\n" "$1" "$err"
  41. printf ". \"%s\" || %s \"Could not read config\";\n" "$1" "$err"
  42. }
  43. e()
  44. {
  45. es_t="e" && [ $# -gt 1 ] && es_t="$2"
  46. es2="already exists"
  47. estr="[ -$es_t \"\$1\" ] || return 1"
  48. [ $# -gt 2 ] && estr="[ -$es_t \"\$1\" ] && return 1" && es2="missing"
  49. eval "$estr"; printf "%s %s\n" "$1" "$es2" 1>&2
  50. }
  51. install_packages()
  52. {
  53. [ $# -lt 2 ] && $err "fewer than two arguments"
  54. eval `setcfg "config/dependencies/$2"`
  55. $pkg_add $pkglist || $err "Cannot install packages"
  56. [ -n "$aur_notice" ] && \
  57. printf "You need AUR packages: %s\n" "$aur_notice" 1>&2; return 0
  58. }
  59. [ $# -gt 0 ] && [ "$1" = "dependencies" ] && install_packages $@ && exit 0
  60. id -u 1>/dev/null 2>/dev/null || $err "suid check failed (id -u)"
  61. [ "$(id -u)" != "0" ] || $err "this command as root is not permitted"
  62. [ -z "${TMPDIR+x}" ] || [ "${TMPDIR%_*}" = "/tmp/xbmk" ] || unset TMPDIR
  63. [ -n "${TMPDIR+x}" ] && export TMPDIR="$TMPDIR"
  64. if [ -z "${TMPDIR+x}" ]; then
  65. [ -f "lock" ] && $err "$PWD/lock exists. Is a build running?"
  66. export TMPDIR="/tmp"
  67. export TMPDIR="$(mktemp -d -t xbmk_XXXXXXXX)"
  68. touch lock || $err "cannot create 'lock' file"
  69. xbmk_parent="y"
  70. fi
  71. # XBMK_CACHE is a directory, for caching downloads and git repositories
  72. [ -z "${XBMK_CACHE+x}" ] && export XBMK_CACHE="$PWD/cache"
  73. [ -z "$XBMK_CACHE" ] && export XBMK_CACHE="$PWD/cache"
  74. [ -L "$XBMK_CACHE" ] && [ "$XBMK_CACHE" = "$PWD/cache" ] && \
  75. $err "cachedir is default, $PWD/cache, but it exists and is a symlink"
  76. [ -L "$XBMK_CACHE" ] && export XBMK_CACHE="$PWD/cache"
  77. [ -f "$XBMK_CACHE" ] && $err "cachedir '$XBMK_CACHE' exists but it's a file"
  78. # if "y": a coreboot target won't be built if target.cfg says release="n"
  79. # (this is used to exclude certain build targets from releases)
  80. [ -z "${XBMK_RELEASE+x}" ] && export XBMK_RELEASE="n"
  81. [ "$XBMK_RELEASE" = "y" ] || export XBMK_RELEASE="n"
  82. [ -z "${XBMK_THREADS+x}" ] && export XBMK_THREADS=1
  83. expr "X$XBMK_THREADS" : "X-\{0,1\}[0123456789][0123456789]*$" \
  84. 1>/dev/null 2>/dev/null || export XBMK_THREADS=1 # user gave a non-integer
  85. x_() {
  86. [ $# -lt 1 ] || $@ || \
  87. $err "Unhandled non-zero exit: $(echo "$@")"; return 0
  88. }
  89. [ -e ".git" ] || [ -f "version" ] || printf "unknown\n" > version || \
  90. $err "Cannot generate unknown version file"
  91. [ -e ".git" ] || [ -f "versiondate" ] || printf "1716415872\n" > versiondate \
  92. || $err "Cannot generate unknown versiondate file"
  93. version_="$version"
  94. [ ! -e ".git" ] || version="$(git describe --tags HEAD 2>&1)" || \
  95. version="git-$(git rev-parse HEAD 2>&1)" || version="$version_"
  96. versiondate_="$versiondate"
  97. [ ! -e ".git" ] || versiondate="$(git show --no-patch --no-notes \
  98. --pretty='%ct' HEAD)" || versiondate="$versiondate_"
  99. for p in projectname version versiondate projectsite; do
  100. chkvars "$p"; eval "x_ printf \"%s\\n\" \"\$$p\" > $p"
  101. done
  102. relname="$projectname-$version"
  103. export LOCALVERSION="-$projectname-${version%%-*}"
  104. check_defconfig()
  105. {
  106. [ -d "$1" ] || $err "Target '$1' not defined."
  107. for x in "$1"/config/*; do
  108. [ -f "$x" ] && printf "%s\n" "$x" && return 1
  109. done; return 0
  110. }
  111. remkdir()
  112. {
  113. rm -Rf "$1" || $err "remkdir: !rm -Rf \"$1\""
  114. mkdir -p "$1" || $err "remkdir: !mkdir -p \"$1\""
  115. }
  116. mkrom_tarball()
  117. {
  118. printf "%s\n" "$version" > "$1/version" || $err "$1 !version"
  119. printf "%s\n" "$versiondate" > "$1/versiondate" || $err "$1 !vdate"
  120. printf "%s\n" "$projectname" > "$1/projectname" || $err "$1 !pname"
  121. mktarball "$1" "${1%/*}/${relname}_${1##*/}.tar.xz"; x_ rm -Rf "$1"; :
  122. }
  123. mktarball()
  124. {
  125. [ "${2%/*}" = "$2" ] || \
  126. mkdir -p "${2%/*}" || $err "mk, !mkdir -p \"${2%/*}\""
  127. printf "\nCreating archive: %s\n\n" "$2"
  128. tar -c "$1" | xz -T$XBMK_THREADS -9e > "$2" || $err "mktarball 2, $1"
  129. mksha512sum "$2" "${2##*/}.sha512"
  130. }
  131. mksha512sum()
  132. {
  133. (
  134. [ "${1%/*}" != "$1" ] && x_ cd "${1%/*}"
  135. sha512sum ./"${1##*/}" >> "$2" || $err "!sha512sum \"$1\" > \"$2\""
  136. ) || $err "failed to create tarball checksum"
  137. }
  138. rmgit()
  139. {
  140. (
  141. cd "$1" || $err "!cd gitrepo $1"
  142. find . -name ".git" -exec rm -Rf {} + || $err "!rm .git $1"
  143. find . -name ".gitmodules" -exec rm -Rf {} + || $err "!rm .gitmod $1"
  144. ) || $err "Cannot remove .git/.gitmodules in $1"
  145. }
  146. # return 0 if project is single-tree, otherwise 1
  147. # e.g. coreboot is multi-tree, so 1
  148. singletree()
  149. {
  150. for targetfile in "config/${1}/"*/target.cfg; do
  151. [ -e "$targetfile" ] && [ -f "$targetfile" ] && return 1; :
  152. done; return 0
  153. }
  154. download()
  155. {
  156. cached="$XBMK_CACHE/file/$4"
  157. dl_fail="n" # 1 url, 2 url backup, 3 destination, 4 checksum
  158. vendor_checksum "$4" "$cached" 2>/dev/null && dl_fail="y"
  159. [ "$dl_fail" = "n" ] && e "$3" f && return 0
  160. mkdir -p "${3%/*}" "$XBMK_CACHE/file" || \
  161. $err "!mkdir '$3' '$XBMK_CACHE/file'"
  162. for url in "$1" "$2"; do
  163. [ "$dl_fail" = "n" ] && break
  164. [ -z "$url" ] && continue
  165. rm -f "$cached" || $err "!rm -f '$cached'"
  166. curl --location --retry 3 -A "$_ua" "$url" -o "$cached" || \
  167. wget --tries 3 -U "$_ua" "$url" -O "$cached" || continue
  168. vendor_checksum "$4" "$cached" || dl_fail="n"
  169. done; [ "$dl_fail" = "y" ] && $err "$1 $2 $3 $4: not downloaded"
  170. [ "$cached" = "$3" ] || cp "$cached" "$3" || $err "!d cp $cached $3"; :
  171. }
  172. vendor_checksum()
  173. {
  174. [ "$(sha512sum "$2" | awk '{print $1}')" != "$1" ] || return 1
  175. printf "Bad checksum for file: %s\n" "$2" 1>&2; rm -f "$2" || :; :
  176. }
  177. cbfs()
  178. {
  179. ccmd="add-payload" && [ $# -gt 3 ] && ccmd="add"
  180. lzma="-c lzma" && [ $# -gt 3 ] && lzma="-t raw"
  181. x_ "$cbfstool" "$1" $ccmd -f "$2" -n "$3" $lzma
  182. }
  183. mk()
  184. {
  185. mk_flag="$1" || $err "No argument given"
  186. shift 1 && for mk_arg in $@; do
  187. ./mk $mk_flag $mk_arg || $err "./mk $mk_flag $mk_arg"; :
  188. done; :
  189. }