|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -x |
| 4 | +set -eo pipefail |
| 5 | + |
| 6 | +# shellcheck disable=SC1091 |
| 7 | +. lib.sh |
| 8 | + |
| 9 | +main() { |
| 10 | + local platform="${1}" |
| 11 | + install_packages ca-certificates curl xz-utils |
| 12 | + |
| 13 | + install_zig "${platform}" |
| 14 | + install_zigbuild "${platform}" |
| 15 | + |
| 16 | + purge_packages |
| 17 | + rm "${0}" |
| 18 | +} |
| 19 | + |
| 20 | +install_zig() { |
| 21 | + local platform="${1}" |
| 22 | + local version="0.9.1" |
| 23 | + local dst="/opt/zig" |
| 24 | + local arch= |
| 25 | + local os= |
| 26 | + local triple= |
| 27 | + |
| 28 | + case "${platform}" in |
| 29 | + 'linux/386') |
| 30 | + arch="i386" |
| 31 | + os="linux" |
| 32 | + ;; |
| 33 | + 'linux/amd64') |
| 34 | + arch="x86_64" |
| 35 | + os="linux" |
| 36 | + ;; |
| 37 | + 'linux/arm64') |
| 38 | + arch="aarch64" |
| 39 | + os="linux" |
| 40 | + ;; |
| 41 | + 'linux/riscv64') |
| 42 | + arch="riscv64" |
| 43 | + os="linux" |
| 44 | + ;; |
| 45 | + 'linux/ppc64le') |
| 46 | + triple="powerpc64le-linux-gnu" |
| 47 | + ;; |
| 48 | + 'linux/s390x') |
| 49 | + triple="s390x-linux-gnu" |
| 50 | + ;; |
| 51 | + 'darwin/amd64') |
| 52 | + arch="x86_64" |
| 53 | + os="macos" |
| 54 | + ;; |
| 55 | + 'darwin/arm64') |
| 56 | + arch="aarch64" |
| 57 | + os="macos" |
| 58 | + ;; |
| 59 | + # NOTE: explicitly don't support linux/arm/v6 |
| 60 | + *) |
| 61 | + echo "Unsupported target platform '${platform}'" 1>&2 |
| 62 | + exit 1 |
| 63 | + ;; |
| 64 | + esac |
| 65 | + |
| 66 | + if [[ -n "${arch}" ]]; then |
| 67 | + install_zig_tarball "${arch}" "${os}" "${version}" "${dst}" |
| 68 | + else |
| 69 | + install_zig_source "${triple}" "${version}" "${dst}" |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +install_zig_tarball() { |
| 74 | + local arch="${1}" |
| 75 | + local os="${2}" |
| 76 | + local version="${3}" |
| 77 | + local dst="${4}" |
| 78 | + local filename="zig-${os}-${arch}-${version}.tar.xz" |
| 79 | + |
| 80 | + local td |
| 81 | + td="$(mktemp -d)" |
| 82 | + |
| 83 | + pushd "${td}" |
| 84 | + |
| 85 | + curl --retry 3 -sSfL "https://ziglang.org/download/${version}/${filename}" -O |
| 86 | + mkdir -p "${dst}" |
| 87 | + tar --strip-components=1 -xJf "${filename}" --directory "${dst}" |
| 88 | + |
| 89 | + popd |
| 90 | + |
| 91 | + rm -rf "${td}" |
| 92 | +} |
| 93 | + |
| 94 | +install_zig_source() { |
| 95 | + local triple="${1}" |
| 96 | + local version="${2}" |
| 97 | + local dst="${3}" |
| 98 | + local filename="zig-bootstrap-${version}.tar.xz" |
| 99 | + |
| 100 | + local td |
| 101 | + td="$(mktemp -d)" |
| 102 | + |
| 103 | + pushd "${td}" |
| 104 | + |
| 105 | + curl --retry 3 -sSfL "https://ziglang.org/download/${version}/${filename}" -O |
| 106 | + mkdir zig |
| 107 | + tar --strip-components=1 -xJf "${filename}" --directory zig |
| 108 | + |
| 109 | + pushd zig |
| 110 | + install_packages python3 make g++ |
| 111 | + ./build -j5 "${triple}" native |
| 112 | + mv "out/zig-${triple}-native" /opt/zig |
| 113 | + |
| 114 | + popd |
| 115 | + popd |
| 116 | + |
| 117 | + rm -rf "${td}" |
| 118 | +} |
| 119 | + |
| 120 | +install_zigbuild() { |
| 121 | + local platform="${1}" |
| 122 | + local version=0.11.0 |
| 123 | + local dst="/usr/local" |
| 124 | + local triple= |
| 125 | + |
| 126 | + # we don't know if `linux/arm/v7` is hard-float, |
| 127 | + # and we don't know the the zigbuild `apple-darwin` |
| 128 | + # target doesn't manually specify the architecture. |
| 129 | + case "${platform}" in |
| 130 | + 'linux/386') |
| 131 | + triple="i686-unknown-linux-musl" |
| 132 | + ;; |
| 133 | + 'linux/amd64') |
| 134 | + triple="x86_64-unknown-linux-musl" |
| 135 | + ;; |
| 136 | + 'linux/arm64') |
| 137 | + triple="aarch64-unknown-linux-musl" |
| 138 | + ;; |
| 139 | + *) |
| 140 | + ;; |
| 141 | + esac |
| 142 | + |
| 143 | + if [[ -n "${triple}" ]]; then |
| 144 | + install_zigbuild_tarball "${triple}" "${version}" "${dst}" |
| 145 | + else |
| 146 | + install_zigbuild_source "${version}" "${dst}" |
| 147 | + fi |
| 148 | +} |
| 149 | + |
| 150 | +install_zigbuild_tarball() { |
| 151 | + local triple="${1}" |
| 152 | + local version="${2}" |
| 153 | + local dst="${3}" |
| 154 | + local repo="https://github.com/messense/cargo-zigbuild" |
| 155 | + local filename="cargo-zigbuild-v${version}.${triple}.tar.gz" |
| 156 | + |
| 157 | + local td |
| 158 | + td="$(mktemp -d)" |
| 159 | + |
| 160 | + pushd "${td}" |
| 161 | + |
| 162 | + curl --retry 3 -sSfL "${repo}/releases/download/v${version}/${filename}" -O |
| 163 | + mkdir -p "${dst}/bin" |
| 164 | + tar -xzf "${filename}" --directory "${dst}/bin" |
| 165 | + |
| 166 | + popd |
| 167 | + |
| 168 | + rm -rf "${td}" |
| 169 | +} |
| 170 | + |
| 171 | +install_zigbuild_source() { |
| 172 | + local version="${1}" |
| 173 | + local dst="${2}" |
| 174 | + |
| 175 | + local td |
| 176 | + td="$(mktemp -d)" |
| 177 | + |
| 178 | + pushd "${td}" |
| 179 | + |
| 180 | + export RUSTUP_HOME="${td}/rustup" |
| 181 | + export CARGO_HOME="${td}/cargo" |
| 182 | + |
| 183 | + curl --retry 3 -sSfL https://sh.rustup.rs -o rustup-init.sh |
| 184 | + sh rustup-init.sh -y --no-modify-path --profile minimal |
| 185 | + |
| 186 | + PATH="${CARGO_HOME}/bin:${PATH}" \ |
| 187 | + cargo install cargo-zigbuild \ |
| 188 | + --version "${version}" \ |
| 189 | + --root "${dst}" \ |
| 190 | + --locked |
| 191 | + |
| 192 | + popd |
| 193 | + |
| 194 | + rm -rf "${td}" |
| 195 | +} |
| 196 | + |
| 197 | +main "${@}" |
0 commit comments