Chris Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright lowRISC contributors. |
| 3 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | # This is a wrapper script for `bazelisk` that downloads and executes bazelisk. |
| 7 | # Bazelisk is a wrapper for `bazel` that can download and execute the project's |
| 8 | # required bazel version. |
Miguel Young de la Sota | 369e027 | 2022-04-21 13:14:23 -0400 | [diff] [blame] | 9 | # |
| 10 | # CI jobs should use ci/bazelisk.sh instead, which performs CI-friendly additional |
| 11 | # setup. |
Chris Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 12 | |
| 13 | set -euo pipefail |
| 14 | |
| 15 | : "${CURL_FLAGS:=--silent}" |
| 16 | : "${REPO_TOP:=$(git rev-parse --show-toplevel)}" |
| 17 | : "${BINDIR:=.bin}" |
| 18 | |
| 19 | readonly release="v1.11.0" |
| 20 | declare -A hashes=( |
| 21 | # sha256sums for v1.11.0. Update this if you update the release. |
| 22 | [linux-amd64]="231ec5ca8115e94c75a1f4fbada1a062b48822ca04f21f26e4cb1cd8973cd458" |
| 23 | ) |
| 24 | |
| 25 | declare -A architectures=( |
| 26 | # Map `uname -m -o` to bazelisk's precompiled binary target names. |
| 27 | [x86_64 GNU/Linux]="linux-amd64" |
| 28 | ) |
| 29 | |
| 30 | function os_arch() { |
| 31 | local arch="$(uname -m -o)" |
| 32 | echo "${architectures[$arch]:-${arch}}" |
| 33 | } |
| 34 | |
| 35 | function check_hash() { |
| 36 | local file="$1" |
| 37 | local target="$(os_arch)" |
| 38 | local value="$(sha256sum "${file}" | cut -f1 -d' ')" |
| 39 | local expect="${hashes[$target]}" |
| 40 | return $(test "$value" == "$expect") |
| 41 | } |
| 42 | |
| 43 | function prepare() { |
| 44 | local target="$(os_arch)" |
| 45 | local bindir="${REPO_TOP}/${BINDIR}" |
| 46 | local file="${bindir}/bazelisk" |
| 47 | local url="https://github.com/bazelbuild/bazelisk/releases/download/${release}/bazelisk-${target}" |
| 48 | |
Chris Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 49 | mkdir -p "$bindir" |
Drew Macrae | 3b0dc22 | 2022-04-19 14:51:40 -0400 | [diff] [blame] | 50 | echo "Downloading bazelisk ${release} (${url})." >> $bindir/bazelisk.log |
Chris Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 51 | curl ${CURL_FLAGS} --location "$url" --output "$file" |
| 52 | chmod +x "$file" |
| 53 | } |
| 54 | |
| 55 | function main() { |
| 56 | local file="${REPO_TOP}/${BINDIR}/bazelisk" |
| 57 | if [[ ! -f "$file" ]] || ! check_hash "$file"; then |
| 58 | prepare |
| 59 | fi |
| 60 | if ! check_hash "$file"; then |
| 61 | echo "sha256sum doesn't match expected value" |
| 62 | exit 1 |
| 63 | fi |
| 64 | exec "$file" "$@" |
| 65 | } |
| 66 | |
| 67 | main "$@" |