lib_govm.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. #
  4. # Go versions manager to install and maintain golang [1] binaries & packages.
  5. #
  6. # [1] https://golang.org/doc/devel/release#policy
  7. # shellcheck source=utils/lib.sh
  8. . /dev/null
  9. # configure golang environment for go.vm
  10. # --------------------------------------
  11. _GO_DL_URL="https://go.dev/dl"
  12. GOVERSION="${GOVERSION:-go$(awk '/^go /{print $2}' "${REPO_ROOT}/go.mod")}"
  13. GOTOOLCHAIN=local
  14. GOROOT="${REPO_ROOT}/.govm/${GOVERSION}"
  15. GOENV="${GOROOT}/.config/go.env"
  16. GOVM_EXE="${GOROOT}/bin/go"
  17. GOPATH="${REPO_ROOT}/local/${GOVERSION}" # no support for multiple path names!!
  18. GOCACHE="${GOPATH}/.cache/go-build"
  19. GOMODCACHE="${GOPATH}/pkg/mod"
  20. # implement go functions
  21. # -----------------------
  22. go.help() {
  23. cat <<EOF
  24. go: GOROOT=${GOROOT}
  25. install : compiles and installs packages
  26. EOF
  27. }
  28. go.tool() {
  29. # shortcut for "go tool .." in the Go environment
  30. go.env.dev
  31. "${GOVM_EXE}" tool "$@"
  32. }
  33. go.env.dev() {
  34. if [ -z "$_GO_DEVTOOLS_INSTALLED" ]; then
  35. build_msg INSTALL "[pkg.go.dev] ./go.mod: developer and CI tools"
  36. go.tidy
  37. else
  38. go.vm.ensure
  39. _GO_DEVTOOLS_INSTALLED=1
  40. fi
  41. }
  42. go.tidy() {
  43. go.vm.ensure
  44. "${GOVM_EXE}" mod tidy
  45. chmod -R u+w "${GOMODCACHE}"
  46. }
  47. go.clean() {
  48. if ! go.vm.is_installed; then
  49. build_msg CLEAN "[Go] not installed"
  50. return 0
  51. fi
  52. build_msg CLEAN "[Go] drop folders ${GOROOT} and ${GOPATH}"
  53. rm -rf "${GOROOT}" "${GOPATH}"
  54. }
  55. go.install() {
  56. go.vm.ensure
  57. GOENV="${GOENV}" "${GOVM_EXE}" install "$@"
  58. # not sure why, but go installs some files without setting the write access
  59. # for the file owner
  60. chmod -R u+w "${GOMODCACHE}"
  61. }
  62. go.os() {
  63. local OS
  64. case "$(command uname -a)xx" in
  65. Linux\ *) OS=linux ;;
  66. Darwin\ *) OS=darwin ;;
  67. FreeBSD\ *) OS=freebsd ;;
  68. CYGWIN* | MSYS* | MINGW*) OS=windows ;;
  69. *) die 42 "OS is unknown: $(command uname -a)" ;;
  70. esac
  71. echo "${OS}"
  72. }
  73. go.arch() {
  74. local ARCH
  75. case "$(command uname -m)" in
  76. "x86_64") ARCH=amd64 ;;
  77. "aarch64") ARCH=arm64 ;;
  78. "armv6" | "armv7l") ARCH=armv6l ;;
  79. "armv8") ARCH=arm64 ;;
  80. .*386.*) ARCH=386 ;;
  81. ppc64*) ARCH=ppc64le ;;
  82. *) die 42 "ARCH is unknown: $(command uname -m)" ;;
  83. esac
  84. echo "${ARCH}"
  85. }
  86. # Go version management (go.vm)
  87. # -----------------------------
  88. go.vm.ensure() {
  89. if ! go.vm.is_installed; then
  90. # shellcheck disable=SC2119
  91. go.vm.install
  92. fi
  93. }
  94. go.vm.is_installed() {
  95. # is true if "go" command is installed
  96. [[ -f "${GOROOT}/bin/go" ]]
  97. }
  98. # shellcheck disable=SC2120
  99. go.vm.install() {
  100. python -m pip install -U requests
  101. # Go versions manager; to install Go at arbitrary place:
  102. #
  103. # usage: go.vm.install <version> <dest>
  104. local version dest fname sha size tmp
  105. version="${1:-$GOVERSION}"
  106. dest="${2:-$GOROOT}"
  107. info_msg "Install Go in ${dest}"
  108. # HINT: the python requirements needed by go.vm.version are taken from the
  109. # developer environment. If it is not yet installed, install it now ..
  110. pyenv.install
  111. # fetch go version ..
  112. local buf=()
  113. mapfile -t buf < <(
  114. go.vm.version "${version}" archive "$(go.os)" "$(go.arch)" filename sha256 size
  115. )
  116. if [ ${#buf[@]} -eq 0 ]; then
  117. die 42 "can't find info of golang version: ${version}"
  118. fi
  119. fname="${buf[0]}"
  120. sha="${buf[1]}"
  121. size="$(numfmt --to=iec "${buf[2]}")"
  122. info_msg "Download go binary ${fname} (${size}B)"
  123. cache_download "${_GO_DL_URL}/${fname}" "${fname}"
  124. pushd "${CACHE}" &>/dev/null
  125. echo "${sha} ${fname}" >"${fname}.sha256"
  126. if ! sha256sum -c "${fname}.sha256" >/dev/null; then
  127. die 42 "downloaded file ${fname} checksum does not match"
  128. else
  129. info_msg "${fname} checksum OK"
  130. fi
  131. popd &>/dev/null
  132. info_msg "install golang"
  133. tmp="$(mktemp -d)"
  134. tar -C "${tmp}" -xzf "${CACHE}/${fname}"
  135. rm -rf "${dest}"
  136. mkdir -p "$(dirname "${dest}")"
  137. mv "${tmp}/go" "${dest}"
  138. mkdir -p "$(dirname "$GOENV")"
  139. export GOENV
  140. "${GOVM_EXE}" telemetry off
  141. "${GOVM_EXE}" env -w \
  142. GOBIN="$GOBIN" \
  143. GOTOOLCHAIN="$GOTOOLCHAIN" \
  144. GOCACHE="$GOCACHE" \
  145. GOPATH="$GOPATH" \
  146. GOMODCACHE="$GOMODCACHE"
  147. mkdir -p "${GOMODCACHE}"
  148. }
  149. go.vm.list() {
  150. # Go versions manager; list Go versions (stable)
  151. python <<EOF
  152. import sys, json, requests
  153. resp = requests.get("${_GO_DL_URL}/?mode=json&include=all")
  154. for ver in json.loads(resp.text):
  155. if not ver['stable']:
  156. continue
  157. for f in ver['files']:
  158. if f['kind'] != 'archive' or not f['size'] or not f['sha256'] or len(f['os']) < 2:
  159. continue
  160. print(" %(version)-10s|%(os)-8s|%(arch)-8s|%(filename)-30s|%(size)-10s|%(sha256)s" % f)
  161. EOF
  162. }
  163. go.vm.version() {
  164. # Print information about a Go distribution. To print filename sha256 and
  165. # size of the archive that fits to your OS and host:
  166. #
  167. # go.ver_info "${GOVERSION}" archive "$(go.os)" "$(go.arch)" filename sha256 size
  168. #
  169. # usage: go.vm.version <go-vers> <kind> <os> <arch> [filename|sha256|size]
  170. #
  171. # kind: [archive|source|installer]
  172. # os: [darwin|freebsd|linux|windows]
  173. # arch: [amd64|arm64|386|armv6l|ppc64le|s390x]
  174. python - "$@" <<EOF
  175. import sys, json, requests
  176. resp = requests.get("${_GO_DL_URL}/?mode=json&include=all")
  177. for ver in json.loads(resp.text):
  178. if ver['version'] != sys.argv[1]:
  179. continue
  180. for f in ver['files']:
  181. if (f['kind'] != sys.argv[2] or f['os'] != sys.argv[3] or f['arch'] != sys.argv[4]):
  182. continue
  183. for x in sys.argv[5:]:
  184. print(f[x])
  185. sys.exit(0)
  186. sys.exit(42)
  187. EOF
  188. }