init.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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-2025 Leah Rowe <leah@libreboot.org>
  5. # Copyright (c) 2025 Alper Nebi Yasak <alpernebiyasak@gmail.com>
  6. export LC_COLLATE=C
  7. export LC_ALL=C
  8. projectname="canoeboot"
  9. projectsite="https://canoeboot.org/"
  10. [ -z "${PATH+x}" ] && \
  11. export PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
  12. xbmkpath="$PATH"
  13. err="err_"
  14. setvars()
  15. {
  16. _setvars=""
  17. if [ $# -lt 2 ]; then
  18. printf "\$err \"setvars: too few args\\n\""
  19. return 0
  20. fi
  21. val="$1"
  22. shift 1
  23. for var in "$@"; do
  24. _setvars="$var=\"$val\"; $_setvars"
  25. done
  26. printf "%s\n" "${_setvars% }"
  27. }
  28. err_()
  29. {
  30. printf "ERROR %s: %s\n" "$0" "$1" 1>&2
  31. exit 1
  32. }
  33. eval "`setvars "" _nogit board reinstall versiondate aur_notice configdir \
  34. datadir version xbmkpwd relname xbmkpwd xbmktmp python pyver`"
  35. xbmk_init()
  36. {
  37. xbmkpwd="`pwd`" || $err "Cannot generate PWD"
  38. export PWD="$xbmkpwd"
  39. if [ $# -gt 0 ] && [ "$1" = "dependencies" ]; then
  40. install_packages "$@" || exit 1
  41. exit 0
  42. fi
  43. id -u 1>/dev/null 2>/dev/null || $err "suid check failed (id -u)"
  44. [ "$(id -u)" != "0" ] || $err "this command as root is not permitted"
  45. for init_cmd in set_pyver set_env set_version git_init create_tmpdir \
  46. lock create_pathdirs child_exec; do
  47. xbmk_$init_cmd "$@" || break
  48. done
  49. }
  50. install_packages()
  51. {
  52. [ $# -lt 2 ] && $err "fewer than two arguments"
  53. [ $# -gt 2 ] && reinstall="$3"
  54. eval "`setcfg "config/dependencies/$2"`"
  55. chkvars pkg_add pkglist
  56. $pkg_add $pkglist || $err "Cannot install packages"
  57. [ -n "$aur_notice" ] && \
  58. printf "You need AUR packages: %s\n" "$aur_notice" 1>&2; :
  59. }
  60. setcfg()
  61. {
  62. [ $# -gt 1 ] && printf "e \"%s\" f missing && return %s;\n" "$1" "$2"
  63. [ $# -gt 1 ] || \
  64. printf "e \"%s\" f not && %s \"Missing config\";\n" "$1" "$err"
  65. printf ". \"%s\" || %s \"Could not read config\";\n" "$1" "$err"
  66. }
  67. chkvars()
  68. {
  69. for var in "$@"; do
  70. eval "[ -n \"\${$var+x}\" ] || \$err \"$var unset\""
  71. eval "[ -n \"\$$var\" ] || \$err \"$var unset\""
  72. done; :
  73. }
  74. xbmk_set_pyver()
  75. {
  76. pyv="import sys; print(sys.version_info[:])"
  77. python="python3"
  78. pybin python3 1>/dev/null || python="python"
  79. pyver="2" && [ "$python" = "python3" ] && pyver="3"
  80. pybin "$python" 1>/dev/null || pyver=""
  81. [ -z "$pyver" ] || "`pybin "$python"`" -c "$pyv" 1>/dev/null \
  82. 2>/dev/null || $err "Cannot detect host Python version."
  83. if [ -n "$pyver" ]; then
  84. pyver="$("$(pybin "$python")" -c "$pyv" | awk '{print $1}')"
  85. pyver="${pyver#(}"
  86. pyver="${pyver%,}"
  87. fi
  88. [ "${pyver%%.*}" = "3" ] || $err "Bad python version (must by 3.x)"; :
  89. }
  90. # Use direct path, to prevent a hang if Python is using a virtual environment,
  91. # not command -v, to prevent a hang when checking python's version
  92. # See: https://docs.python.org/3/library/venv.html#how-venvs-work
  93. pybin()
  94. {
  95. py="import sys; quit(1) if sys.prefix == sys.base_prefix else quit(0)"
  96. venv=1
  97. command -v "$1" 1>/dev/null 2>/dev/null || venv=0
  98. [ $venv -lt 1 ] || "$1" -c "$py" 1>/dev/null 2>/dev/null || venv=0
  99. # ideally, don't rely on PATH or hardcoded paths if python venv.
  100. # use the *real*, direct executable linked to by the venv symlink
  101. if [ $venv -gt 0 ] && [ -L "`command -v "$1" 2>/dev/null`" ]; then
  102. # realpath isn't posix, but available mostly universally
  103. pypath="$(realpath \
  104. "$(command -v "$1" 2>/dev/null)" 2>/dev/null || :)"
  105. [ -e "$pypath" ] && [ ! -d "$pypath" ] && \
  106. [ -x "$pypath" ] && printf "%s\n" "$pypath" && return 0; :
  107. fi
  108. # if python venv: fall back to common PATH directories for checking
  109. [ $venv -gt 0 ] && for pypath in "/usr/local/bin" "/usr/bin"; do
  110. [ -e "$pypath/$1" ] && [ ! -d "$pypath/$1" ] && \
  111. [ -x "$pypath/$1" ] && printf "%s/%s\n" "$pypath" "$1" && \
  112. return 0
  113. done
  114. [ $venv -gt 0 ] && return 1
  115. # Defer to normal command -v if not a venv
  116. command -v "$1" 2>/dev/null || return 1
  117. }
  118. xbmk_set_env()
  119. {
  120. # XBMK_CACHE is a directory, for caching downloads and git repon
  121. [ -z "${XBMK_CACHE+x}" ] && export XBMK_CACHE="$xbmkpwd/cache"
  122. [ -z "$XBMK_CACHE" ] && export XBMK_CACHE="$xbmkpwd/cache"
  123. [ -L "$XBMK_CACHE" ] && [ "$XBMK_CACHE" = "$xbmkpwd/cache" ] && \
  124. $err "cachedir '$xbmkpwd/cache' is a symlink"
  125. [ -L "$XBMK_CACHE" ] && export XBMK_CACHE="$xbmkpwd/cache"
  126. [ -f "$XBMK_CACHE" ] && $err "cachedir '$XBMK_CACHE' is a file"; :
  127. # if "y": a coreboot target won't be built if target.cfg says release="n"
  128. # (this is used to exclude certain build targets from releases)
  129. [ -z "${XBMK_RELEASE+x}" ] && export XBMK_RELEASE="n"
  130. [ "$XBMK_RELEASE" = "y" ] || export XBMK_RELEASE="n"
  131. [ -z "${XBMK_THREADS+x}" ] && export XBMK_THREADS=1
  132. expr "X$XBMK_THREADS" : "X-\{0,1\}[0123456789][0123456789]*$" \
  133. 1>/dev/null 2>/dev/null || export XBMK_THREADS=1; :
  134. }
  135. xbmk_set_version()
  136. {
  137. [ ! -f ".version" ] || read -r version < ".version" || :; :
  138. [ ! -f ".versiondate" ] || read -r versiondate < ".versiondate" || :; :
  139. [ -e ".git" ] || [ -f ".version" ] || printf "unknown\n" > ".version" \
  140. || $err "Cannot generate unknown .version file"
  141. [ -e ".git" ] || [ -f ".versiondate" ] || printf "1716415872\n" > \
  142. ".versiondate" || $err "Can't generate unknown versiondate file"; :
  143. version_="$version"
  144. [ ! -e ".git" ] || version="$(git describe --tags HEAD 2>&1)" || \
  145. version="git-$(git rev-parse HEAD 2>&1)" || version="$version_"
  146. versiondate_="$versiondate"
  147. [ ! -e ".git" ] || versiondate="$(git show --no-patch --no-notes \
  148. --pretty='%ct' HEAD)" || versiondate="$versiondate_"
  149. chkvars version versiondate
  150. printf "%s\n" "$version" > ".version" || $err "can't save version"
  151. printf "%s\n" "$versiondate" > ".versiondate" || $err "can't save date"
  152. relname="$projectname-$version"
  153. export LOCALVERSION="-$projectname-${version%%-*}"
  154. }
  155. xbmk_git_init()
  156. {
  157. [ -L ".git" ] && return 1
  158. [ -e ".git" ] && return 0
  159. eval "`setvars "$(date -Rud @$versiondate)" cdate _nogit`"
  160. x_ git init 1>/dev/null 2>/dev/null
  161. x_ git add -A . 1>/dev/null 2>/dev/null
  162. x_ git commit -m "$projectname $version" --date "$cdate" \
  163. --author="xbmk <xbmk@example.com>" 1>/dev/null 2>/dev/null
  164. x_ git tag -a "$version" -m "$projectname $version" 1>/dev/null \
  165. 2>/dev/null
  166. for gitarg in "--global user.name" "--global user.email"; do
  167. gitcmd="git config $gitarg"; $gitcmd || $err \
  168. "Please run this first: $gitcmd \"your ${gitcmd##*.}\""
  169. done
  170. }
  171. xbmk_create_tmpdir()
  172. {
  173. # unify all temporary files/directories in a single TMPDIR
  174. [ -z "${TMPDIR+x}" ] || [ "${TMPDIR%_*}" = "/tmp/xbmk" ] || \
  175. unset TMPDIR
  176. [ -n "${TMPDIR+x}" ] && export TMPDIR="$TMPDIR" && xbmktmp="$TMPDIR"
  177. [ -z "${TMPDIR+x}" ] || return 1 # child instance, so return
  178. # parent instance of xbmk, so don't return. set up TMPDIR
  179. export TMPDIR="/tmp"
  180. export TMPDIR="$(mktemp -d -t xbmk_XXXXXXXX)"
  181. xbmktmp="$TMPDIR"
  182. }
  183. xbmk_lock()
  184. {
  185. [ -f "lock" ] && $err "$xbmkpwd/lock exists. Is a build running?"
  186. touch lock || $err "cannot create 'lock' file"; :
  187. }
  188. xbmk_create_pathdirs()
  189. {
  190. x_ rm -Rf "$XBMK_CACHE/xbmkpath" "$XBMK_CACHE/gnupath"
  191. x_ mkdir -p "$XBMK_CACHE/gnupath" "$XBMK_CACHE/xbmkpath"
  192. export PATH="$XBMK_CACHE/xbmkpath:$XBMK_CACHE/gnupath:$PATH"
  193. (
  194. # set up python v3.x in PATH, in case it's not set up correctly.
  195. # see code above that detected the correct python3 command.
  196. cd "$XBMK_CACHE/xbmkpath" || $err "can't cd $XBMK_CACHE/xbmkpath"
  197. x_ ln -s "`pybin "$python"`" python
  198. ) || $err "Can't set up python symlink in $XBMK_CACHE/xbmkpath"; :
  199. }
  200. xbmk_child_exec()
  201. {
  202. xbmk_rval=0
  203. ( x_ ./mk "$@" ) || xbmk_rval=1
  204. rm -Rf "$xbmktmp" || xbmk_rval=1
  205. rm -f lock || xbmk_rval=1
  206. exit $xbmk_rval
  207. }
  208. x_()
  209. {
  210. [ $# -lt 1 ] || "$@" || $err "Unhandled error for: $(echo "$@")"; :
  211. }
  212. xbmk_init "$@"