blob: 7c2bfd8e573f091d83c2601a46e68a81dd7f1610 [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.
Rupert Swarbrickb4d3ded2022-05-30 14:55:00 +01009#
Miguel Young de la Sota369e0272022-04-21 13:14:23 -040010# 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
Rupert Swarbrickb4d3ded2022-05-30 14:55:00 +010055function up_to_date() {
56 local file="$1"
57 # We need an update if the file doesn't exist or it has the wrong hash
58 test -f "$file" || return 1
59 check_hash "$file" || return 1
60 return 0
61}
62
Chris Frantzd87e6122022-03-25 08:57:54 -070063function main() {
Rupert Swarbrickb4d3ded2022-05-30 14:55:00 +010064 local bindir="${REPO_TOP}/${BINDIR}"
65 local file="${bindir}/bazelisk"
66 local lockfile="${bindir}/bazelisk.lock"
67
68 if ! up_to_date "$file"; then
69 # Grab the lock, blocking until success. Upon success, check again
70 # whether we're up to date (because some other process might have
71 # downloaded bazelisk in the meantime). If not, download it ourselves.
72 mkdir -p "$bindir"
73 (flock -x 9; up_to_date "$file" || prepare) 9>>"$lockfile"
Chris Frantzd87e6122022-03-25 08:57:54 -070074 fi
75 if ! check_hash "$file"; then
76 echo "sha256sum doesn't match expected value"
77 exit 1
78 fi
79 exec "$file" "$@"
80}
81
82main "$@"