|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +GH_REPO="https://github.com/software-mansion/cairo-coverage" |
| 6 | +TOOL_NAME="cairo-coverage" |
| 7 | +TOOL_TEST="cairo-coverage --help" |
| 8 | + |
| 9 | +fail() { |
| 10 | + echo -e "asdf-$TOOL_NAME: $*" |
| 11 | + exit 1 |
| 12 | +} |
| 13 | + |
| 14 | +curl_opts=(-fsSL) |
| 15 | + |
| 16 | +if [ -n "${GITHUB_API_TOKEN:-}" ]; then |
| 17 | + curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITHUB_API_TOKEN") |
| 18 | +fi |
| 19 | + |
| 20 | +sort_versions() { |
| 21 | + sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' | |
| 22 | + LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}' |
| 23 | +} |
| 24 | + |
| 25 | +list_github_tags() { |
| 26 | + git ls-remote --tags --refs "$GH_REPO" | |
| 27 | + grep -o 'refs/tags/.*' | cut -d/ -f3- | |
| 28 | + sed 's/^v//' |
| 29 | +} |
| 30 | + |
| 31 | +list_all_versions() { |
| 32 | + list_github_tags |
| 33 | +} |
| 34 | + |
| 35 | +download_release() { |
| 36 | + local version filename url |
| 37 | + version="$1" |
| 38 | + filename="$2" |
| 39 | + |
| 40 | + get_architecture || fail "Could not determine system architecture." |
| 41 | + local _arch="$RETVAL" |
| 42 | + |
| 43 | + local repository tag |
| 44 | + |
| 45 | + repository=$GH_REPO |
| 46 | + tag="v$version" |
| 47 | + |
| 48 | + local _tarball="cairo-coverage-${tag}-${_arch}.tar.gz" |
| 49 | + url="${repository}/releases/download/${tag}/${_tarball}" |
| 50 | + |
| 51 | + echo "* Downloading $TOOL_NAME release $version..." |
| 52 | + curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url" |
| 53 | +} |
| 54 | + |
| 55 | +install_version() { |
| 56 | + local install_type="$1" |
| 57 | + local version="$2" |
| 58 | + local install_path="${3%/bin}" |
| 59 | + |
| 60 | + if [ "$install_type" != "version" ]; then |
| 61 | + fail "asdf-$TOOL_NAME supports release installs only" |
| 62 | + fi |
| 63 | + |
| 64 | + ( |
| 65 | + mkdir -p "$install_path" |
| 66 | + cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path" |
| 67 | + |
| 68 | + local tool_cmd |
| 69 | + tool_cmd="$(echo "$TOOL_TEST" | cut -d' ' -f1)" |
| 70 | + test -x "$install_path/bin/$tool_cmd" || fail "Expected $install_path/bin/$tool_cmd to be executable." |
| 71 | + |
| 72 | + echo "$TOOL_NAME $version installation was successful!" |
| 73 | + ) || ( |
| 74 | + rm -rf "$install_path" |
| 75 | + fail "An error occurred while installing $TOOL_NAME $version." |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +# This function has been copied verbatim from rustup install script. |
| 80 | +check_proc() { |
| 81 | + # Check for /proc by looking for the /proc/self/exe link |
| 82 | + # This is only run on Linux |
| 83 | + if ! test -L /proc/self/exe; then |
| 84 | + err "fatal: Unable to find /proc/self/exe. Is /proc mounted? Installation cannot proceed without /proc." |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +# This function has been copied verbatim from rustup install script. |
| 89 | +get_bitness() { |
| 90 | + # need_cmd head |
| 91 | + # Architecture detection without dependencies beyond coreutils. |
| 92 | + # ELF files start out "\x7fELF", and the following byte is |
| 93 | + # 0x01 for 32-bit and |
| 94 | + # 0x02 for 64-bit. |
| 95 | + # The printf builtin on some shells like dash only supports octal |
| 96 | + # escape sequences, so we use those. |
| 97 | + local _current_exe_head |
| 98 | + _current_exe_head=$(head -c 5 /proc/self/exe) |
| 99 | + if [ "$_current_exe_head" = "$(printf '\177ELF\001')" ]; then |
| 100 | + echo 32 |
| 101 | + elif [ "$_current_exe_head" = "$(printf '\177ELF\002')" ]; then |
| 102 | + echo 64 |
| 103 | + else |
| 104 | + err "unknown platform bitness" |
| 105 | + fi |
| 106 | +} |
| 107 | + |
| 108 | +# This function has been copied verbatim from rustup install script. |
| 109 | +get_architecture() { |
| 110 | + local _ostype _cputype _bitness _arch _clibtype |
| 111 | + _ostype="$(uname -s)" |
| 112 | + _cputype="$(uname -m)" |
| 113 | + _clibtype="gnu" |
| 114 | + |
| 115 | + if [ "$_ostype" = Linux ]; then |
| 116 | + if [ "$(uname -o)" = Android ]; then |
| 117 | + _ostype=Android |
| 118 | + fi |
| 119 | + if ldd --_requested_version 2>&1 | grep -q 'musl'; then |
| 120 | + _clibtype="musl" |
| 121 | + fi |
| 122 | + fi |
| 123 | + |
| 124 | + if [ "$_ostype" = Darwin ] && [ "$_cputype" = i386 ]; then |
| 125 | + # Darwin `uname -m` lies |
| 126 | + if sysctl hw.optional.x86_64 | grep -q ': 1'; then |
| 127 | + _cputype=x86_64 |
| 128 | + fi |
| 129 | + fi |
| 130 | + |
| 131 | + if [ "$_ostype" = SunOS ]; then |
| 132 | + # Both Solaris and illumos presently announce as "SunOS" in "uname -s" |
| 133 | + # so use "uname -o" to disambiguate. We use the full path to the |
| 134 | + # system uname in case the user has coreutils uname first in PATH, |
| 135 | + # which has historically sometimes printed the wrong value here. |
| 136 | + if [ "$(/usr/bin/uname -o)" = illumos ]; then |
| 137 | + _ostype=illumos |
| 138 | + fi |
| 139 | + |
| 140 | + # illumos systems have multi-arch userlands, and "uname -m" reports the |
| 141 | + # machine hardware name; e.g., "i86pc" on both 32- and 64-bit x86 |
| 142 | + # systems. Check for the native (widest) instruction set on the |
| 143 | + # running kernel: |
| 144 | + if [ "$_cputype" = i86pc ]; then |
| 145 | + _cputype="$(isainfo -n)" |
| 146 | + fi |
| 147 | + fi |
| 148 | + |
| 149 | + case "$_ostype" in |
| 150 | + Android) |
| 151 | + _ostype=linux-android |
| 152 | + ;; |
| 153 | + |
| 154 | + Linux) |
| 155 | + check_proc |
| 156 | + _ostype=unknown-linux-$_clibtype |
| 157 | + _bitness=$(get_bitness) |
| 158 | + ;; |
| 159 | + |
| 160 | + FreeBSD) |
| 161 | + _ostype=unknown-freebsd |
| 162 | + ;; |
| 163 | + |
| 164 | + NetBSD) |
| 165 | + _ostype=unknown-netbsd |
| 166 | + ;; |
| 167 | + |
| 168 | + DragonFly) |
| 169 | + _ostype=unknown-dragonfly |
| 170 | + ;; |
| 171 | + |
| 172 | + Darwin) |
| 173 | + _ostype=apple-darwin |
| 174 | + ;; |
| 175 | + |
| 176 | + illumos) |
| 177 | + _ostype=unknown-illumos |
| 178 | + ;; |
| 179 | + |
| 180 | + MINGW* | MSYS* | CYGWIN* | Windows_NT) |
| 181 | + _ostype=pc-windows-gnu |
| 182 | + ;; |
| 183 | + |
| 184 | + *) |
| 185 | + err "unrecognized OS type: $_ostype" |
| 186 | + ;; |
| 187 | + esac |
| 188 | + |
| 189 | + case "$_cputype" in |
| 190 | + i386 | i486 | i686 | i786 | x86) |
| 191 | + _cputype=i686 |
| 192 | + ;; |
| 193 | + |
| 194 | + xscale | arm) |
| 195 | + _cputype=arm |
| 196 | + if [ "$_ostype" = "linux-android" ]; then |
| 197 | + _ostype=linux-androideabi |
| 198 | + fi |
| 199 | + ;; |
| 200 | + |
| 201 | + armv6l) |
| 202 | + _cputype=arm |
| 203 | + if [ "$_ostype" = "linux-android" ]; then |
| 204 | + _ostype=linux-androideabi |
| 205 | + else |
| 206 | + _ostype="${_ostype}eabihf" |
| 207 | + fi |
| 208 | + ;; |
| 209 | + |
| 210 | + armv7l | armv8l) |
| 211 | + _cputype=armv7 |
| 212 | + if [ "$_ostype" = "linux-android" ]; then |
| 213 | + _ostype=linux-androideabi |
| 214 | + else |
| 215 | + _ostype="${_ostype}eabihf" |
| 216 | + fi |
| 217 | + ;; |
| 218 | + |
| 219 | + aarch64 | arm64) |
| 220 | + _cputype=aarch64 |
| 221 | + ;; |
| 222 | + |
| 223 | + x86_64 | x86-64 | x64 | amd64) |
| 224 | + _cputype=x86_64 |
| 225 | + ;; |
| 226 | + |
| 227 | + mips) |
| 228 | + _cputype=$(get_endianness mips '' el) |
| 229 | + ;; |
| 230 | + |
| 231 | + mips64) |
| 232 | + if [ "$_bitness" -eq 64 ]; then |
| 233 | + # only n64 ABI is supported for now |
| 234 | + _ostype="${_ostype}abi64" |
| 235 | + _cputype=$(get_endianness mips64 '' el) |
| 236 | + fi |
| 237 | + ;; |
| 238 | + |
| 239 | + ppc) |
| 240 | + _cputype=powerpc |
| 241 | + ;; |
| 242 | + |
| 243 | + ppc64) |
| 244 | + _cputype=powerpc64 |
| 245 | + ;; |
| 246 | + |
| 247 | + ppc64le) |
| 248 | + _cputype=powerpc64le |
| 249 | + ;; |
| 250 | + |
| 251 | + s390x) |
| 252 | + _cputype=s390x |
| 253 | + ;; |
| 254 | + riscv64) |
| 255 | + _cputype=riscv64gc |
| 256 | + ;; |
| 257 | + loongarch64) |
| 258 | + _cputype=loongarch64 |
| 259 | + ;; |
| 260 | + *) |
| 261 | + err "unknown CPU type: $_cputype" |
| 262 | + ;; |
| 263 | + esac |
| 264 | + |
| 265 | + # Detect 64-bit linux with 32-bit userland |
| 266 | + if [ "${_ostype}" = unknown-linux-gnu ] && [ "${_bitness}" -eq 32 ]; then |
| 267 | + case $_cputype in |
| 268 | + x86_64) |
| 269 | + if [ -n "${RUSTUP_CPUTYPE:-}" ]; then |
| 270 | + _cputype="$RUSTUP_CPUTYPE" |
| 271 | + else { |
| 272 | + # 32-bit executable for amd64 = x32 |
| 273 | + if is_host_amd64_elf; then |
| 274 | + err "x86_64 linux with x86 userland unsupported" |
| 275 | + else |
| 276 | + _cputype=i686 |
| 277 | + fi |
| 278 | + }; fi |
| 279 | + ;; |
| 280 | + mips64) |
| 281 | + _cputype=$(get_endianness mips '' el) |
| 282 | + ;; |
| 283 | + powerpc64) |
| 284 | + _cputype=powerpc |
| 285 | + ;; |
| 286 | + aarch64) |
| 287 | + _cputype=armv7 |
| 288 | + if [ "$_ostype" = "linux-android" ]; then |
| 289 | + _ostype=linux-androideabi |
| 290 | + else |
| 291 | + _ostype="${_ostype}eabihf" |
| 292 | + fi |
| 293 | + ;; |
| 294 | + riscv64gc) |
| 295 | + err "riscv64 with 32-bit userland unsupported" |
| 296 | + ;; |
| 297 | + esac |
| 298 | + fi |
| 299 | + |
| 300 | + # Detect armv7 but without the CPU features Rust needs in that build, |
| 301 | + # and fall back to arm. |
| 302 | + # See https://github.com/rust-lang/rustup.rs/issues/587. |
| 303 | + if [ "$_ostype" = "unknown-linux-gnueabihf" ] && [ "$_cputype" = armv7 ]; then |
| 304 | + if ensure grep '^Features' /proc/cpuinfo | grep -q -v neon; then |
| 305 | + # At least one processor does not have NEON. |
| 306 | + _cputype=arm |
| 307 | + fi |
| 308 | + fi |
| 309 | + |
| 310 | + _arch="${_cputype}-${_ostype}" |
| 311 | + |
| 312 | + RETVAL="$_arch" |
| 313 | +} |
0 commit comments