forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bazel
executable file
·210 lines (175 loc) · 5.94 KB
/
bazel
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
if [ ! -z "$LOCAL_BAZEL_OVERRIDE" ]; then
exec $LOCAL_BAZEL_OVERRIDE "$@"
fi
# update this to move to later versions of this repo:
# https://github.com/bazelbuild/bazel
ORIGINAL_PWD=$PWD
TMPDIR="${TMPDIR:-/tmp}"
RND_UID=$(date "+%s")
function try_goto_cmd_location() {
BASH_LOC=${BASH_SOURCE[0]}
if [ ! -z "$BASH_LOC" ]; then
DIR_NAME=$(dirname "$BASH_LOC")
CMD_LOC="$( cd "$DIR_NAME" && pwd )"
if [ ! -z "$CMD_LOC" ]; then
cd $CMD_LOC
fi
fi
}
# If we are executed as a sub-process we won't be able to do this
# So we do it as a try
try_goto_cmd_location
set -eo pipefail
if [ "$(uname -s)" == "Linux" ]; then
export BAZEL_LAUNCHER_PLATFORM_NAME='linux'
elif [ "$(uname -s)" == "Darwin" ]; then
export BAZEL_LAUNCHER_PLATFORM_NAME='darwin'
else
"Your platform $(uname -s) is unsupported, sorry"
exit 1
fi
if [ -z "$BAZEL_REMOTE_SOURCE" ]; then
export BAZEL_REMOTE_SOURCE=https://github.com/bazelbuild/bazel/releases/download
fi
findWorkspace() {
OLD_PWD=$PWD
if [ ! -f WORKSPACE ]; then
cd ..
if [ "$PWD" = "$OLD_PWD" ]; then
echo "Didn't find the workspace"
exit 1
fi
findWorkspace
fi
}
findWorkspace
REPO_ROOT=$PWD
cd $ORIGINAL_PWD
if [ -f $REPO_ROOT/stripe-build.yaml ]
then
# version is pulled out of stripe-build.yaml
bazel_version_regex='version: "(([0-9]+\.[0-9]+)\.[0-9]+)"'
stripe_build_yaml=`cat $REPO_ROOT/stripe-build.yaml`
if [[ $stripe_build_yaml =~ $bazel_version_regex ]]
then
export BAZEL_VERSION=${BASH_REMATCH[1]}
export BAZEL_MAJOR_MINOR=${BASH_REMATCH[2]}
else
echo "$0: Failed to extract Bazel version from stripe-build.yaml" >&2
exit 1
fi
else
# version is pulled out of WORKSPACE
bazel_version_regex='BAZEL_VERSION = "(([0-9]+\.[0-9]+)\.[0-9]+)"'
stripe_build_yaml=`cat $REPO_ROOT/WORKSPACE`
if [[ $stripe_build_yaml =~ $bazel_version_regex ]]
then
export BAZEL_VERSION=${BASH_REMATCH[1]}
export BAZEL_MAJOR_MINOR=${BASH_REMATCH[2]}
else
echo "$0: Failed to extract Bazel version from WORKSPACE" >&2
exit 1
fi
fi
# in some cases (like building the kube image), we want to use the host
# bazel. however, this should ideally be the same bazel version, specified
# in stripe-build.yaml
if ! [ -z "$STRIPE_BUILD_JENKINS_BAZEL" ] && ! [ -z "$BAZEL_REAL" ]; then
# first check that the version is expected
actual_bazel_version_regex='Build label: (([0-9]+\.[0-9]+)\.[0-9]+)'
bazel_version_output=`$BAZEL_REAL version`
if [[ $bazel_version_output =~ $actual_bazel_version_regex ]]
then
ACTUAL_BAZEL_VERSION=${BASH_REMATCH[1]}
ACTUAL_BAZEL_MAJOR_MINOR=${BASH_REMATCH[2]}
if [ "$ACTUAL_BAZEL_MAJOR_MINOR" != "$BAZEL_MAJOR_MINOR" ]; then
echo "$0: Expected bazel version $BAZEL_VERSION but got $ACTUAL_BAZEL_VERSION (major.minor mismatch)" >&2
exit 1
fi
else
echo "$0: Failed to extract Bazel version from stripe-build.yaml" >&2
exit 1
fi
exec $BAZEL_REAL "$@"
fi
BAZEL_RC_PATH=${TMPDIR}/bazel_bazel_rc_${RND_UID}
echo "" > $BAZEL_RC_PATH
if [ -f $REPO_ROOT/shared_tools/default_bazel_rc ]; then
cat $REPO_ROOT/shared_tools/default_bazel_rc >> $BAZEL_RC_PATH
fi
if [ -f /etc/bazelrc ]; then
cat /etc/bazelrc >> $BAZEL_RC_PATH
fi
if [ -f $HOME/.bazelrc ]; then
cat $HOME/.bazelrc >> $BAZEL_RC_PATH
fi
if [ -f $REPO_ROOT/.bazelrc ]; then
cat $REPO_ROOT/.bazelrc >> $BAZEL_RC_PATH
fi
IS_CACHE_WORTHY_COMMAND=false
for var in "$@"
do
case "$var" in
build|test|run)
IS_CACHE_WORTHY_COMMAND=true
break
;;
*)
if [[ ! $var == --* ]]; then
break;
fi
;;
esac
done
if [ -z "$BAZEL_INSTALLER_VERSION_SHA" ]; then
## TODO(gkk): load shas from stripe-build.yaml instead of WORKSPACE once the yaml file supports it
SHA_VARIABLE_NAME="BAZEL_INSTALLER_VERSION_${BAZEL_LAUNCHER_PLATFORM_NAME}_SHA"
export BAZEL_INSTALLER_VERSION_SHA=$(cat $REPO_ROOT/WORKSPACE | egrep "^${SHA_VARIABLE_NAME}\s*=\s*" | sed -e 's/.*=//g' -e 's/ //g' -e 's/"//g')
fi
# So we can compare what we download against an expected SHA 256
# export BAZEL_INSTALLER_VERSION_SHA=$(ruby -ryaml -e 'puts YAML.load_file("'$REPO_ROOT'/stripe-build.yaml")["bazel"]["dist_sha256_'$BAZEL_LAUNCHER_PLATFORM_NAME'"]')
export BAZEL_ZIP_PATH=$(cat $REPO_ROOT/WORKSPACE | egrep '^BAZEL_ZIP_PATH\s*=\s*' | sed -e 's/.*=//g' -e 's/ //g' -e 's/"//g')
if [ -z "$BAZEL_VERSION" ]; then
echo "Must supply a BAZEL_VERSION in the workspace or in the stripe-build.yaml"
exit 1
fi
if [ -z "$BAZEL_INSTALLER_VERSION_SHA" ]; then
echo "Must supply a 'dist_sha256_$BAZEL_LAUNCHER_PLATFORM_NAME' in the workspace or in the stripe-build.yaml"
exit 1
fi
if [ -z "$BAZEL_BIN_LOC" ]; then
BAZEL_BIN_LOC=~/.bazel_binaries
fi
mkdir -p $BAZEL_BIN_LOC
export BAZEL_EXEC_PATH=$BAZEL_BIN_LOC/$BAZEL_VERSION/bin/bazel-real
if [ -f "$BAZEL_EXEC_PATH" ]; then
exec $BAZEL_EXEC_PATH --bazelrc $BAZEL_RC_PATH "$@"
fi
RND_UID=$(date "+%s")
export BUILD_DIR=${TMPDIR}/bazel_b_${RND_UID}
mkdir -p $BUILD_DIR
( # Opens a subshell
set -e
echo "Installing Bazel, this will take ~1min"
cd $BUILD_DIR
INSTALLER_NAME="bazel-${BAZEL_VERSION}-installer-${BAZEL_LAUNCHER_PLATFORM_NAME}-x86_64.sh"
echo $PWD
if [ -z $BAZEL_INSTALLER_PATH ]; then
BAZEL_INSTALLER_PATH=$BAZEL_REMOTE_SOURCE/${BAZEL_VERSION}/$INSTALLER_NAME
fi
curl -O -L $BAZEL_INSTALLER_PATH
GENERATED_SHA_256=$(shasum -a 256 $INSTALLER_NAME | awk '{print $1}')
if [ "$GENERATED_SHA_256" != "$BAZEL_INSTALLER_VERSION_SHA" ]; then
echo "Sha 256 does not match, expected: $BAZEL_INSTALLER_VERSION_SHA"
echo "But found $GENERATED_SHA_256"
echo "Recommend you: update the sha to the expected"
echo "and then re-run this script"
exit 1
fi
chmod +x ${INSTALLER_NAME}
./${INSTALLER_NAME} --base=${BAZEL_BIN_LOC}/${BAZEL_VERSION} --bin=${BAZEL_BIN_LOC}/${BAZEL_VERSION}/bin_t
)
rm -rf $BUILD_DIR
cd $ORIGINAL_PWD
exec $BAZEL_EXEC_PATH --bazelrc $BAZEL_RC_PATH "$@"