init.sh 6.8 KB

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