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. |
Rupert Swarbrick | b4d3ded | 2022-05-30 14:55:00 +0100 | [diff] [blame] | 9 | # |
Miguel Young de la Sota | 369e027 | 2022-04-21 13:14:23 -0400 | [diff] [blame] | 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 | |
Chris Frantz | 2f640af | 2022-06-08 08:20:41 -0700 | [diff] [blame] | 15 | # Change to this script's directory, as it is the location of the bazel workspace. |
| 16 | cd "$(dirname "$0")" |
| 17 | |
Chris Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 18 | : "${CURL_FLAGS:=--silent}" |
| 19 | : "${REPO_TOP:=$(git rev-parse --show-toplevel)}" |
| 20 | : "${BINDIR:=.bin}" |
| 21 | |
| 22 | readonly release="v1.11.0" |
| 23 | declare -A hashes=( |
| 24 | # sha256sums for v1.11.0. Update this if you update the release. |
| 25 | [linux-amd64]="231ec5ca8115e94c75a1f4fbada1a062b48822ca04f21f26e4cb1cd8973cd458" |
| 26 | ) |
| 27 | |
| 28 | declare -A architectures=( |
| 29 | # Map `uname -m -o` to bazelisk's precompiled binary target names. |
| 30 | [x86_64 GNU/Linux]="linux-amd64" |
| 31 | ) |
| 32 | |
| 33 | function os_arch() { |
Miles Dai | 7e07af1 | 2022-07-07 16:27:33 -0400 | [diff] [blame] | 34 | local arch |
| 35 | arch="$(uname -m -o)" |
Chris Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 36 | echo "${architectures[$arch]:-${arch}}" |
| 37 | } |
| 38 | |
| 39 | function check_hash() { |
Miles Dai | 2885dc9 | 2022-07-27 14:35:03 -0400 | [diff] [blame] | 40 | local file target |
Miles Dai | 7e07af1 | 2022-07-07 16:27:33 -0400 | [diff] [blame] | 41 | file="$1" |
| 42 | target="$(os_arch)" |
Miles Dai | 2885dc9 | 2022-07-27 14:35:03 -0400 | [diff] [blame] | 43 | echo "${hashes[$target]} $file" | sha256sum --check --quiet |
Chris Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | function prepare() { |
Miles Dai | 7e07af1 | 2022-07-07 16:27:33 -0400 | [diff] [blame] | 47 | local target |
| 48 | target="$(os_arch)" |
Chris Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 49 | 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 Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 53 | mkdir -p "$bindir" |
Drew Macrae | 3b0dc22 | 2022-04-19 14:51:40 -0400 | [diff] [blame] | 54 | echo "Downloading bazelisk ${release} (${url})." >> $bindir/bazelisk.log |
Chris Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 55 | curl ${CURL_FLAGS} --location "$url" --output "$file" |
| 56 | chmod +x "$file" |
| 57 | } |
| 58 | |
Rupert Swarbrick | b4d3ded | 2022-05-30 14:55:00 +0100 | [diff] [blame] | 59 | function 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 Frantz | 4f8abe1 | 2022-07-18 12:57:06 -0700 | [diff] [blame] | 67 | function 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 Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 92 | function main() { |
Rupert Swarbrick | b4d3ded | 2022-05-30 14:55:00 +0100 | [diff] [blame] | 93 | 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 Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 103 | fi |
| 104 | if ! check_hash "$file"; then |
| 105 | echo "sha256sum doesn't match expected value" |
| 106 | exit 1 |
| 107 | fi |
Chris Frantz | 4f8abe1 | 2022-07-18 12:57:06 -0700 | [diff] [blame] | 108 | |
| 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 Frantz | d87e612 | 2022-03-25 08:57:54 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | main "$@" |