-
Notifications
You must be signed in to change notification settings - Fork 771
/
bazelisk.sh
executable file
·178 lines (158 loc) · 5.79 KB
/
bazelisk.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
# This is a wrapper script for `bazelisk` that downloads and executes bazelisk.
# Bazelisk is a wrapper for `bazel` that can download and execute the project's
# required bazel version.
#
# CI jobs should use ci/bazelisk.sh instead, which performs CI-friendly additional
# setup.
set -eo pipefail
# Change to this script's directory, as it is the location of the bazel workspace.
cd "$(dirname "$0")"
: "${CURL_FLAGS:=--silent}"
: "${REPO_TOP:=$(git rev-parse --show-toplevel)}"
: "${BINDIR:=.bin}"
: "${BAZEL_BIN:=$(which bazel 2>/dev/null)}"
readonly release="v1.11.0"
declare -A hashes=(
# sha256sums for v1.11.0. Update this if you update the release.
[linux-amd64]="231ec5ca8115e94c75a1f4fbada1a062b48822ca04f21f26e4cb1cd8973cd458"
)
declare -A architectures=(
# Map `uname -m -o` to bazelisk's precompiled binary target names.
[x86_64 GNU/Linux]="linux-amd64"
)
function os_arch() {
local arch
arch="$(uname -m -o)"
echo "${architectures[$arch]:-${arch}}"
}
function check_hash() {
local file target
file="$1"
target="$(os_arch)"
echo "${hashes[$target]} $file" | sha256sum --check --quiet
}
function prepare() {
local target
target="$(os_arch)"
local bindir="${REPO_TOP}/${BINDIR}"
local file="${bindir}/bazelisk"
local url="https://github.com/bazelbuild/bazelisk/releases/download/${release}/bazelisk-${target}"
mkdir -p "$bindir"
echo "Downloading bazelisk ${release} (${url})." >> $bindir/bazelisk.log
curl ${CURL_FLAGS} --location "$url" --output "$file"
chmod +x "$file"
}
function up_to_date() {
local file="$1"
# We need an update if the file doesn't exist or it has the wrong hash
test -f "$file" || return 1
check_hash "$file" || return 1
return 0
}
function outquery_starlark_expr() {
local query="$1"
shift
if [[ ${query} == "outquery" ]]; then
q="-one"
else
q=${query#outquery}
fi
case "$q" in
-one)
echo "target.files.to_list()[0].path"
;;
-all)
echo "\"\\n\".join([f.path for f in depset(transitive=[target.files, target.default_runfiles.files]).to_list()])"
;;
-providers)
echo "providers(target)"
;;
-*)
echo "\"\\n\".join([f.path for f in depset(transitive=[target.files, target.default_runfiles.files]).to_list() if \"$q\"[1:] in f.path])"
;;
.*)
echo "\"\\n\".join([f.path for f in depset(transitive=[target.files, target.default_runfiles.files]).to_list() if f.path.endswith(\"$q\")])"
;;
esac
}
# Arguments:
# $qexpr: starlark expression - see `outquery_starlark_expr`
# $name: name of an array containing Bazel arguments that should come _before_
# the subcommand (e.g. `--bazelrc=...`).
function do_outquery() {
local qexpr="$1"
shift
"$file" "${pre_cmd_args[@]}" cquery "$@" \
--output=starlark --starlark:expr="$qexpr" \
--ui_event_filters=-info --noshow_progress \
| sort | uniq
}
function main() {
local bindir="${REPO_TOP}/${BINDIR}"
local file="${BAZEL_BIN:-${bindir}/bazelisk}"
local lockfile="${bindir}/bazelisk.lock"
# Are we using bazel from the user's PATH or using bazelisk?
if expr match "${file}" ".*bazelisk$" >/dev/null; then
if ! up_to_date "$file"; then
# Grab the lock, blocking until success. Upon success, check again
# whether we're up to date (because some other process might have
# downloaded bazelisk in the meantime). If not, download it ourselves.
mkdir -p "$bindir"
(flock -x 9; up_to_date "$file" || prepare) 9>>"$lockfile"
fi
if ! check_hash "$file"; then
echo "sha256sum doesn't match expected value"
exit 1
fi
fi
# Shift all flags (starting with `-`) that come before the subcommand
# into an array.
pre_cmd_args=()
while [[ "${1-}" == -* ]]; do
pre_cmd_args+=("$1")
shift
done
case "${1-}" in
outquery*)
# The custom 'outquery' command can be used to query bazel for the
# outputs associated with labels.
# The outquery command can take several forms:
# outquery: return one output file associated with the label.
# outquery-all: return all output files associated with the label.
# outquery-x: return output files containing the substring "x".
# outquery.x: return output files ending with the substring ".x".
local qexpr
qexpr="$(outquery_starlark_expr "$1")"
shift
do_outquery "$qexpr" "$@"
;;
build-then)
# The 'build-then' command builds the requested targets and then
# evaluates the given command template, replacing "%s" with the path
# to an output file.
#
# For example, the command below would build "//:foo" and run "less"
# on one of the output files.
#
# ./bazelisk.sh build-then "less %s" //:foo
shift
local command_template="$1"
shift
local qexpr outfile
qexpr="$(outquery_starlark_expr outquery)"
outfile=$(do_outquery "$qexpr" "$@")
"$file" "${pre_cmd_args[@]}" build "$@"
# shellcheck disable=SC2059
# We are intentionally using $command_template as a format string.
eval "$(printf "$command_template" "$outfile")"
;;
*)
exec "$file" "${pre_cmd_args[@]}" "$@"
;;
esac
}
main "$@"