blob: 75daee351aba30ebce14a68630a79590cca5df1e [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
Chris Frantz2f640af2022-06-08 08:20:41 -070015# Change to this script's directory, as it is the location of the bazel workspace.
16cd "$(dirname "$0")"
17
Chris Frantzd87e6122022-03-25 08:57:54 -070018: "${CURL_FLAGS:=--silent}"
19: "${REPO_TOP:=$(git rev-parse --show-toplevel)}"
20: "${BINDIR:=.bin}"
21
22readonly release="v1.11.0"
23declare -A hashes=(
24 # sha256sums for v1.11.0. Update this if you update the release.
25 [linux-amd64]="231ec5ca8115e94c75a1f4fbada1a062b48822ca04f21f26e4cb1cd8973cd458"
26)
27
28declare -A architectures=(
29 # Map `uname -m -o` to bazelisk's precompiled binary target names.
30 [x86_64 GNU/Linux]="linux-amd64"
31)
32
33function os_arch() {
Miles Dai7e07af12022-07-07 16:27:33 -040034 local arch
35 arch="$(uname -m -o)"
Chris Frantzd87e6122022-03-25 08:57:54 -070036 echo "${architectures[$arch]:-${arch}}"
37}
38
39function check_hash() {
Miles Dai2885dc92022-07-27 14:35:03 -040040 local file target
Miles Dai7e07af12022-07-07 16:27:33 -040041 file="$1"
42 target="$(os_arch)"
Miles Dai2885dc92022-07-27 14:35:03 -040043 echo "${hashes[$target]} $file" | sha256sum --check --quiet
Chris Frantzd87e6122022-03-25 08:57:54 -070044}
45
46function prepare() {
Miles Dai7e07af12022-07-07 16:27:33 -040047 local target
48 target="$(os_arch)"
Chris Frantzd87e6122022-03-25 08:57:54 -070049 local bindir="${REPO_TOP}/${BINDIR}"
50 local file="${bindir}/bazelisk"
51 local url="https://github.com/bazelbuild/bazelisk/releases/download/${release}/bazelisk-${target}"
52
Chris Frantzd87e6122022-03-25 08:57:54 -070053 mkdir -p "$bindir"
Drew Macrae3b0dc222022-04-19 14:51:40 -040054 echo "Downloading bazelisk ${release} (${url})." >> $bindir/bazelisk.log
Chris Frantzd87e6122022-03-25 08:57:54 -070055 curl ${CURL_FLAGS} --location "$url" --output "$file"
56 chmod +x "$file"
57}
58
Rupert Swarbrickb4d3ded2022-05-30 14:55:00 +010059function up_to_date() {
60 local file="$1"
61 # We need an update if the file doesn't exist or it has the wrong hash
62 test -f "$file" || return 1
63 check_hash "$file" || return 1
64 return 0
65}
66
Chris Frantz4f8abe12022-07-18 12:57:06 -070067function outquery_starlark_expr() {
68 local query="$1"
69 shift
70 if [[ ${query} == "outquery" ]]; then
71 q="-one"
72 else
73 q=${query#outquery}
74 fi
75
76 case "$q" in
77 -one)
78 echo "target.files.to_list()[0].path"
79 ;;
80 -all)
81 echo "\"\\n\".join([f.path for f in target.files.to_list()])"
82 ;;
83 -*)
84 echo "\"\\n\".join([f.path for f in target.files.to_list() if \"$q\"[1:] in f.path])"
85 ;;
86 .*)
87 echo "\"\\n\".join([f.path for f in target.files.to_list() if f.path.endswith(\"$q\")])"
88 ;;
89 esac
90}
91
Chris Frantzd87e6122022-03-25 08:57:54 -070092function main() {
Rupert Swarbrickb4d3ded2022-05-30 14:55:00 +010093 local bindir="${REPO_TOP}/${BINDIR}"
94 local file="${bindir}/bazelisk"
95 local lockfile="${bindir}/bazelisk.lock"
96
97 if ! up_to_date "$file"; then
98 # Grab the lock, blocking until success. Upon success, check again
99 # whether we're up to date (because some other process might have
100 # downloaded bazelisk in the meantime). If not, download it ourselves.
101 mkdir -p "$bindir"
102 (flock -x 9; up_to_date "$file" || prepare) 9>>"$lockfile"
Chris Frantzd87e6122022-03-25 08:57:54 -0700103 fi
104 if ! check_hash "$file"; then
105 echo "sha256sum doesn't match expected value"
106 exit 1
107 fi
Chris Frantz4f8abe12022-07-18 12:57:06 -0700108
109 case "$1" in
110 outquery*)
111 # The custom 'outquery' command can be used to query bazel for the
112 # outputs associated with labels.
113 # The outquery command can take several forms:
114 # outquery: return one output file associated with the label.
115 # outquery-all: return all output files associated with the label.
116 # outquery-x: return output files containing the substring "x".
117 # outquery.x: return output files ending with the substring ".x".
118 QEXPR="$(outquery_starlark_expr "$1")"
119 shift
120 exec "$file" cquery "$@" --output=starlark --starlark:expr="$QEXPR"
121 ;;
122 *)
123 exec "$file" "$@"
124 ;;
125 esac
Chris Frantzd87e6122022-03-25 08:57:54 -0700126}
127
128main "$@"