blob: 45782fd255ecb4415c26c7362d4175699a5299b7 [file] [log] [blame]
Chris Frantzd87e6122022-03-25 08:57:54 -07001#!/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 Sota369e0272022-04-21 13:14:23 -04009#
10# CI jobs should use ci/bazelisk.sh instead, which performs CI-friendly additional
11# setup.
Chris Frantzd87e6122022-03-25 08:57:54 -070012
13set -euo pipefail
14
15: "${CURL_FLAGS:=--silent}"
16: "${REPO_TOP:=$(git rev-parse --show-toplevel)}"
17: "${BINDIR:=.bin}"
18
19readonly release="v1.11.0"
20declare -A hashes=(
21 # sha256sums for v1.11.0. Update this if you update the release.
22 [linux-amd64]="231ec5ca8115e94c75a1f4fbada1a062b48822ca04f21f26e4cb1cd8973cd458"
23)
24
25declare -A architectures=(
26 # Map `uname -m -o` to bazelisk's precompiled binary target names.
27 [x86_64 GNU/Linux]="linux-amd64"
28)
29
30function os_arch() {
31 local arch="$(uname -m -o)"
32 echo "${architectures[$arch]:-${arch}}"
33}
34
35function 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
43function 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 Frantzd87e6122022-03-25 08:57:54 -070049 mkdir -p "$bindir"
Drew Macrae3b0dc222022-04-19 14:51:40 -040050 echo "Downloading bazelisk ${release} (${url})." >> $bindir/bazelisk.log
Chris Frantzd87e6122022-03-25 08:57:54 -070051 curl ${CURL_FLAGS} --location "$url" --output "$file"
52 chmod +x "$file"
53}
54
55function 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
67main "$@"