scripts/build-go.sh
2025-10-11 14:37:14 +02:00

163 lines
6.2 KiB
Bash
Executable file

#!/bin/bash
set -e
errx() { echo "error: $1">&2; exit 1; }
[[ $(id -u) -ne 0 ]] && errx "this script must be run as root"
install_dir="/usr/local"
go_dir="${install_dir}/go"
url="https://github.com/golang/go/archive"
v14="go1.4.3"
v14_sum="65e8d2ea08447f02f8b6ab63778e4d98586ac4fd676401dae0eda494c7829965"
v14_bootstrap="${install_dir}/bootstrap-${v14}"
tgz14="${v14}.tar.gz"
path_v14="${install_dir}/${tgz14}"
v19="go1.19.7"
v19_sum="1fe106fce9342c2a7ceeb04477341457cc9f0a3c07b9f2a993af09d654307f56"
v19_bootstrap="${install_dir}/bootstrap-${v19}"
tgz19="${v19}.tar.gz"
path_v19="${install_dir}/${tgz19}"
v20="go1.20.6"
v20_sum="baaebb7ad7f0054ac4f7fd39ec6e1fec17e187301cc479f499fab7ac61ca58c7"
v20_bootstrap="${install_dir}/bootstrap-${v20}"
tgz20="${v20}.tar.gz"
path_v20="${install_dir}/${tgz20}"
v22="go1.22.6"
v22_sum="71cbf4f555da51df93f71b1726c69f2e80f62bbe2e7ce1c5c39c3d57fa385b14"
v22_bootstrap="${install_dir}/bootstrap-${v22}"
tgz22="${v22}.tar.gz"
path_v22="${install_dir}/${tgz22}"
v="${1-1.25.2}"
case "${v}" in
1.16.7) new_sum="07502f16366fbc697b1b09b842d29116e6d6607c9dc42b1d7c54b96f528c7952";;
1.17.1) new_sum="670ce36e100669f9be572a1603858ce4a64eb4b21a31fa5cdefb778609c40337";;
1.17.2) new_sum="022fe1f533432a305db705448f50f9a3ea369586dc32d07219b1bc7db1e01577";;
1.17.4) new_sum="91736588a47917aaa424dfdf482d8efcb44403a768e7a947c272a98881053386";;
1.17.6) new_sum="de00bf818cda9e4712e9b3438c9c3d4f8318beb598d11abb0ba9d515883f6320";;
1.18) new_sum="63e5cab48f97c63dc2737eb35582316acf77084d109ac0571651cedaf40987c0";;
1.18.2) new_sum="b76e590857060fdc575a37d4204c15df33de6d23e49d5cf9bdce93083e4dcddb";;
1.18.3) new_sum="cc0e73239f5bc1521de9ddc54f46eb0e69cc41faabe3f468865483bb0f844b68";;
1.18.4) new_sum="283442519c28f5c24dd8c849ebe0570a3ad92cd94610378b33b2053f60391fdf";;
1.18.5) new_sum="30c07c08c9b9a79d652272ac3298c249a810a4c908e8937cf5e58d2414af1cc3";;
1.19) new_sum="6ac64f39e3f477ac793896437237e84b06d67c7657fbb34abcdbf73c77f6b7a3";;
1.19.1) new_sum="884439c3de751c705e47a120af6b92b65770a952a7b8788244bc6d73b2548c3e";;
1.19.5) new_sum="1c24a6a2bf71d64d0ca8e228028d6108521f06b6edc7bf6b34ed6d767a795809";;
1.19.6) new_sum="0c7f4e6d2de3ae07d60377754955842ad357b0366df01116554fc619b1e99672";;
1.19.7) new_sum="1fe106fce9342c2a7ceeb04477341457cc9f0a3c07b9f2a993af09d654307f56";;
1.20.1) new_sum="5db737840a740457c1b4b73b90dad6daa57af0da420f3ea51c39a1a81abbcc41" v1_20=1;;
1.20.2) new_sum="176969bb14216dd45121e46d5d0f63d339edb9042d1389d485fb6b21ee172388" v1_20=1;;
1.20.6) new_sum="baaebb7ad7f0054ac4f7fd39ec6e1fec17e187301cc479f499fab7ac61ca58c7" v1_20=1;;
1.21.0) new_sum="cf1d9b0504983c1a4c044856c389555070fbc58c3ce1703193a2c3ffe877a8b9" v1_20=1;;
1.21.5) new_sum="20acc9b538ccc9368ca3061849d8ff466f10c71fd858af5a0940fce3c3dbe4db" v1_20=1;;
1.22.1) new_sum="e6dbc2e591b577ef7d3cb0f6572568521c2dcfe8c7521f1253fe23fd624cdc56" v1_22=1;;
1.23.0) new_sum="0bc1065ced4718d43c8cf945e7c321b6a8814defd4f7cae9588719494021bddf" v1_22=1;;
1.23.2) new_sum="fc3448a68fca887ceb0e0d4357d0ecd05d54a78e052f8667b283e745c87e7f2e" v1_22=1;;
1.23.5) new_sum="742aa265a47019d2b87bd3d9c42fd7f1e2fed133557e64b0a3e8749925ce5123" v1_22=1;;
1.25.2) new_sum="301f8d65edb32ab2080fbe80b559a6369c0e3f792d2b0d1078a2764f8b579bcb" v1_25=1;;
*) errx "no hash found for version ${v}";;
esac
[[ "${v1_25}" -eq 1 ]] && v1_22=1
[[ "${v1_22}" -eq 1 ]] && v1_20=1
new="go${v}"
new_dir="${install_dir}/go-${new}"
tgznew="${new}.tar.gz"
path_new="${install_dir}/${tgznew}"
rm -f ${go_dir}
if [[ ! -d "${v14_bootstrap}" ]]; then
curl -sSL "${url}/${tgz14}" >${path_v14}
[[ "$(sha256sum ${path_v14}|cut -d" " -f1)" != "${v14_sum}" ]] && errx "wrong checksum: ${path_v14}"
tar xfz ${path_v14} -C ${install_dir}
mv ${install_dir}/go-${v14} ${v14_bootstrap}
fi
if [[ "${v1_20}" -eq 1 ]] && [[ ! -d "${v19_bootstrap}" ]]; then
curl -sSL "${url}/${tgz19}" >${path_v19}
[[ "$(sha256sum ${path_v19}|cut -d" " -f1)" != "${v19_sum}" ]] && errx "wrong checksum: ${path_v19}"
tar xfz ${path_v19} -C ${install_dir}
mv ${install_dir}/go-${v19} ${v19_bootstrap}
fi
if [[ "${v1_22}" -eq 1 ]] && [[ ! -d "${v20_bootstrap}" ]]; then
curl -sSL "${url}/${tgz20}" >${path_v20}
[[ "$(sha256sum ${path_v20}|cut -d" " -f1)" != "${v20_sum}" ]] && errx "wrong checksum: ${path_v20}"
tar xfz ${path_v20} -C ${install_dir}
mv ${install_dir}/go-${v20} ${v20_bootstrap}
fi
if [[ "${v1_25}" -eq 1 ]] && [[ ! -d "${v22_bootstrap}" ]]; then
curl -sSL "${url}/${tgz22}" >${path_v22}
[[ "$(sha256sum ${path_v22}|cut -d" " -f1)" != "${v22_sum}" ]] && errx "wrong checksum: ${path_v22}"
tar xfz ${path_v22} -C ${install_dir}
mv ${install_dir}/go-${v22} ${v22_bootstrap}
fi
if [[ ! -d "${new_dir}" ]]; then
curl -sSL "${url}/${tgznew}" >${path_new}
[[ -z "${new_sum}" ]] && { echo "hashsum for version ${new}:"; sha256sum ${path_new}|cut -d" " -f1; exit 0; }
[[ "$(sha256sum ${path_new}|cut -d" " -f1)" != "${new_sum}" ]] && errx "wrong checksum: ${path_new}"
tar xfz ${path_new} -C ${install_dir}
fi
export CGO_ENABLED=0
if [[ ! -x "${v14_bootstrap}/bin/go" ]]; then
cd ${v14_bootstrap}/src
./make.bash
fi
if [[ "${v1_20}" -eq 1 ]] && [[ ! -x "${v19_bootstrap}/bin/go" ]]; then
export GOROOT_BOOTSTRAP=${v14_bootstrap}
cd ${v19_bootstrap}/src
./make.bash
fi
if [[ "${v1_22}" -eq 1 ]] && [[ ! -x "${v20_bootstrap}/bin/go" ]]; then
export GOROOT_BOOTSTRAP=${v19_bootstrap}
cd ${v20_bootstrap}/src
./make.bash
fi
if [[ "${v1_25}" -eq 1 ]] && [[ ! -x "${v22_bootstrap}/bin/go" ]]; then
export GOROOT_BOOTSTRAP=${v20_bootstrap}
cd ${v22_bootstrap}/src
./make.bash
fi
if [[ ! -x "${new_dir}/bin/go" ]]; then
if [[ "${v1_25}" -eq 1 ]]; then
bootstrap=${v22_bootstrap}
elif [[ "${v1_22}" -eq 1 ]]; then
bootstrap=${v20_bootstrap}
elif [[ "${v1_20}" -eq 1 ]]; then
bootstrap=${v19_bootstrap}
else
bootstrap=${v14_bootstrap}
fi
export GOROOT_BOOTSTRAP=${bootstrap}
[[ "$(uname -m)" == "x86_64" ]] && export GOARCH=amd64 || export GOARCH=386
cd ${new_dir}/src
./make.bash
fi
ln -s ${new_dir} ${go_dir}
export GOROOT=${go_dir}
export PATH=${PATH}:${GOROOT}/bin:~/go/bin
cat <<EOF
add this to your .bashrc/.profile:
export CGO_ENABLED=${CGO_ENABLED}
export GOPATH=~/go
export GOROOT=${GOROOT}
export GO111MODULE=on
export PATH=\${PATH}:\${GOROOT}/bin:~/go/bin
EOF
go version