init.sh 7.1 KB

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