-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
buildenv.sh
executable file
·376 lines (327 loc) · 10.4 KB
/
buildenv.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/bin/bash
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# General purpose method and values for bootstrapping bazel.
set -o errexit
# Check if all necessary tools are available.
# List: https://github.com/bazelbuild/bazel/issues/7641#issuecomment-472344261
for tool in basename cat chmod comm cp dirname find grep ln ls mkdir mktemp \
readlink rm sed sort tail touch tr uname unzip which; do
if ! hash "$tool" >/dev/null; then
echo >&2 "ERROR: cannot find \"$tool\"; check your PATH."
echo >&2 " You may need to run the following command or similar:"
echo >&2 " export PATH=\"/bin:/usr/bin:\$PATH\""
exit 1
fi
done
# Ensure Python is on the PATH on Windows, otherwise we would see
# "LAUNCHER ERROR" messages from py_binary exe launchers.
case "$(uname -s | tr "[:upper:]" "[:lower:]")" in
msys*|mingw*|cygwin*)
# Ensure Python is on the PATH, otherwise the bootstrapping fails later.
if ! hash python.exe >/dev/null; then
echo >&2 "ERROR: cannot locate python.exe; check your PATH."
echo >&2 " You may need to run the following command, or something"
echo >&2 " similar, depending on where you installed Python:"
echo >&2 " export PATH=\"/c/Python27:\$PATH\""
exit 1
fi
# Ensure TMPDIR uses the user-specified TMPDIR or TMP or TEMP.
# This is necessary to avoid overly longs paths during bootstrapping, see for
# example https://github.com/bazelbuild/bazel/issues/4536
export TMPDIR="${TMPDIR:-${TMP:-${TEMP:-}}}"
esac
# If BAZEL_WRKDIR is set, default all variables to point into
# that directory
if [ -n "${BAZEL_WRKDIR}" ] ; then
mkdir -p "${BAZEL_WRKDIR}/tmp"
mkdir -p "${BAZEL_WRKDIR}/user_root"
: ${TMPDIR:=${BAZEL_WRKDIR}/tmp}
export TMPDIR
: ${BAZEL_DIR_STARTUP_OPTIONS:="--output_user_root=${BAZEL_WRKDIR}/user_root"}
fi
# We define the fail function early so we can use it when detecting the JDK
# See https://github.com/bazelbuild/bazel/issues/2949,
function fail() {
local exitCode=$?
if [[ "$exitCode" = "0" ]]; then
exitCode=1
fi
echo >&2
echo "ERROR: $*" >&2
exit $exitCode
}
# Set standard variables
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
WORKSPACE_DIR="$(dirname "$(dirname "${DIR}")")"
JAVA_VERSION=${JAVA_VERSION:-21}
BAZELRC=${BAZELRC:-"/dev/null"}
PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
PATHSEP=":"
case "${PLATFORM}" in
linux)
# JAVA_HOME must point to a Java installation.
JAVA_HOME="${JAVA_HOME:-$(readlink -f $(which javac) | sed 's_/bin/javac__')}"
;;
freebsd)
# JAVA_HOME must point to a Java installation.
JAVA_HOME="${JAVA_HOME:-/usr/local/openjdk11}"
;;
openbsd)
# JAVA_HOME must point to a Java installation.
JAVA_HOME="${JAVA_HOME:-/usr/local/jdk-11}"
;;
darwin)
if [[ -z "$JAVA_HOME" ]]; then
JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" \
|| fail "Could not find JAVA_HOME, please ensure a JDK (version ${JAVA_VERSION}+) is installed."
fi
;;
msys*|mingw*|cygwin*)
# Use a simplified platform string.
PLATFORM="windows"
PATHSEP=";"
# Find the latest available version of the SDK.
JAVA_HOME="${JAVA_HOME:-$(ls -d C:/Program\ Files/Java/jdk* | sort | tail -n 1)}"
# Replace backslashes with forward slashes.
JAVA_HOME="${JAVA_HOME//\\//}"
esac
EXE_EXT=""
if [ "${PLATFORM}" == "windows" ]; then
# Extension for executables.
EXE_EXT=".exe"
# Fix TMPDIR on windows
default_tmp=${TMP:-$(cygpath -mO)/Temp}
TMPDIR=$(cygpath -ml "${TMPDIR:-$default_tmp}")
fi
# Whether we display build messages or not. We set this conditionally because
# the file including us or the user may already have defined VERBOSE to their
# liking.
: ${VERBOSE:=yes}
# List of functions to invoke on exit.
ATEXIT_HANDLERS=
# Registers a function to be invoked on exit.
#
# The handlers will be invoked at exit time in the order they were registered.
# See comments in run_atexit for more details.
function atexit() {
local handler="${1}"; shift
[ -n "${ATEXIT_HANDLERS}" ] || trap 'run_atexit_handlers $?' EXIT
ATEXIT_HANDLERS="${ATEXIT_HANDLERS} ${handler}"
}
# Exit routine to run all registered atexit handlers.
#
# If the program exited with an error, this exit routine will also exit with the
# same error. However, if the program exited successfully, this exit routine
# will only exit successfully if the atexit handlers succeed.
function run_atexit_handlers() {
local exit_code="$?"
local failed=no
for handler in ${ATEXIT_HANDLERS}; do
eval "${handler}" || failed=yes
done
trap - EXIT # Reset exit handler to prevent double execution.
if [ ${exit_code} -ne 0 ]; then
exit ${exit_code}
else
if [ "${failed}" = yes ]; then
echo "Program tried to exit successfully but atexit routines failed" 1>&2
exit 1
else
exit 0
fi
fi
}
function tempdir() {
local tmp=${TMPDIR:-/tmp}
mkdir -p "${tmp}"
local DIR="$(mktemp -d "${tmp%%/}/bazel_XXXXXXXX")"
mkdir -p "${DIR}"
local DIRBASE=$(basename "${DIR}")
eval "cleanup_tempdir_${DIRBASE}() { rm -rf '${DIR}' >&/dev/null || true ; }"
atexit cleanup_tempdir_${DIRBASE}
NEW_TMPDIR="${DIR}"
}
tempdir
OUTPUT_DIR=${NEW_TMPDIR}
phasefile=${OUTPUT_DIR}/phase
function cleanup_phasefile() {
if [ -f "${phasefile}" ]; then
echo 1>&2;
cat "${phasefile}" 1>&2;
fi;
}
atexit cleanup_phasefile
# Executes a command respecting the current verbosity settings.
#
# If VERBOSE is yes, the command itself and its output are printed.
# If VERBOSE is no, the command's output is only displayed in case of failure.
#
# Exits the script if the command fails.
function run() {
if [ "${VERBOSE}" = yes ]; then
echo "${@}"
"${@}" || exit $?
else
local errfile="${OUTPUT_DIR}/errors"
echo "${@}" >"${errfile}"
if ! "${@}" >>"${errfile}" 2>&1; then
local exitcode=$?
cat "${errfile}" 1>&2
exit $exitcode
fi
fi
}
function display() {
if [[ -z "${QUIETMODE}" ]]; then
echo -e "$@" >&2
fi
}
function log() {
echo -n "." >&2
echo "$1" >${phasefile}
}
function clear_log() {
echo >&2
rm -f ${phasefile}
}
LEAVES="\xF0\x9F\x8D\x83"
INFO="\033[32mINFO\033[0m:"
WARNING="\033[31mWARN\033[0m:"
first_step=1
function new_step() {
rm -f ${phasefile}
local new_line=
if [ -n "${first_step}" ]; then
first_step=
else
new_line="\n"
fi
if [ -t 2 ]; then
display -n "$new_line$LEAVES $1"
else
display -n "$new_line$1"
fi
}
function git_sha1() {
if [ -x "$(which git 2>/dev/null)" ] && [ -d .git ]; then
git rev-parse --short HEAD 2>/dev/null || true
fi
}
function git_date() {
if [ -x "$(which git 2>/dev/null)" ] && [ -d .git ]; then
git log -1 --pretty=%ai | cut -d " " -f 1 || true
fi
}
# Get the latest release version and append the date of
# the last commit if any.
function get_last_version() {
if [ -f "MODULE.bazel" ]; then
local version=$(grep "version =" MODULE.bazel | head -n 1 | sed 's/.*version = "\(.*\)".*/\1/' | cut -d '"' -f2)
else
local version=""
fi
local date="$(git_date)"
if [ -z "${version-}" ]; then
version="unknown"
fi
if [ -n "${date-}" ]; then
date="$(date +%Y-%m-%d)"
fi
echo "${version}-${date}"
}
if [[ ${PLATFORM} == "darwin" ]]; then
function md5_file() {
echo $(cat $1 | md5) $1
}
else
function md5_file() {
md5sum $1
}
fi
# Gets the java version from JAVA_HOME
# Sets JAVAC and JAVAC_VERSION with respectively the path to javac and
# the version of javac.
function get_java_version() {
test -z "$JAVA_HOME" && fail "JDK not found, please set \$JAVA_HOME."
JAVAC="${JAVA_HOME}/bin/javac"
[[ -x "${JAVAC}" ]] \
|| fail "JAVA_HOME ($JAVA_HOME) is not a path to a working JDK."
JAVAC_VERSION=$("${JAVAC}" -version 2>&1)
if [[ "$JAVAC_VERSION" =~ javac\ ((1\.)?([789]|[1-9][0-9])).*$ ]]; then
JAVAC_VERSION=1.${BASH_REMATCH[3]}
else
fail \
"Cannot determine JDK version, please set \$JAVA_HOME.\n" \
"\$JAVAC_VERSION is \"${JAVAC_VERSION}\""
fi
}
# Return the target that a bind point to, using Bazel query.
function get_bind_target() {
$BAZEL --bazelrc=${BAZELRC} ${BAZEL_DIR_STARTUP_OPTIONS} \
query "deps($1, 1) - $1"
}
# Create a link for a directory on the filesystem
function link_dir() {
local source=$1
local dest=$2
if [[ "${PLATFORM}" == "windows" ]]; then
local -r s="$(cygpath -w "$source")"
local -r d="$(cygpath -w "$dest")"
powershell -command "New-Item -ItemType Junction -Path '$d' -Value '$s'"
else
ln -s "${source}" "${dest}"
fi
}
function link_file() {
local source=$1
local dest=$2
if [[ "${PLATFORM}" == "windows" ]]; then
# Attempt creating a symlink to the file. This is supported without
# elevation (Administrator privileges) on Windows 10 version 1709 when
# Developer Mode is enabled.
local -r s="$(cygpath -w "$source")"
local -r d="$(cygpath -w "$dest")"
if ! powershell -command "New-Item -ItemType SymbolicLink -Path '$d' -Value '$s'"; then
# If the previous call failed to create a symlink, just copy the file.
cp "$source" "$dest"
fi
else
ln -s "${source}" "${dest}"
fi
}
# Link direct children (subdirectories and files) of a directory.
# Usage:
# link_children "$PWD" "tools" "${BAZEL_TOOLS_REPO}"
# This creates:
# ${BAZEL_TOOLS_REPO}/tools/android -> $PWD/tools/android
# ${BAZEL_TOOLS_REPO}/tools/bash -> $PWD/tools/bash
# ... and so on for all files and directories directly under "tools".
function link_children() {
local -r source_dir=${1%/}
local -r source_subdir=${2%/}
local -r dest_dir=${3%/}
for e in $(find "${source_dir}/${source_subdir}" -mindepth 1 -maxdepth 1 -type d); do
local dest_path="${dest_dir}/${e#$source_dir/}"
if [[ ! -d "$dest_path" ]]; then
link_dir "$e" "$dest_path"
fi
done
for e in $(find "${source_dir}/${source_subdir}" -mindepth 1 -maxdepth 1 -type f); do
local dest_path="${dest_dir}/${e#$source_dir/}"
if [[ ! -f "$dest_path" ]]; then
link_file "$e" "$dest_path"
fi
done
}