lib.sh 6.7 KB

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