blob: 5c5061b45dff2f1478f485d19c6501dcb3d9145e [file] [log] [blame]
Philipp Wollermann19f34132015-06-12 08:39:25 +00001#!/bin/bash
2
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2015 The Bazel Authors. All rights reserved.
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +00004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# General purpose method and values for bootstrapping bazel.
18
19set -o errexit
20
Klaus Aehlig276a8cd2016-07-11 12:06:07 +000021# If BAZEL_WRKDIR is set, default all variables to point into
22# that directory
23
24if [ -n "${BAZEL_WRKDIR}" ] ; then
25 mkdir -p "${BAZEL_WRKDIR}/tmp"
26 mkdir -p "${BAZEL_WRKDIR}/user_root"
27 : ${TMPDIR:=${BAZEL_WRKDIR}/tmp}
28 export TMPDIR
29 : ${BAZEL_DIR_STARTUP_OPTIONS:="--output_user_root=${BAZEL_WRKDIR}/user_root"}
30fi
31
32
dmarting511c35b2017-05-05 14:12:29 +020033# We define the fail function early so we can use it when detecting the JDK
34# See https://github.com/bazelbuild/bazel/issues/2949,
35function fail() {
36 local exitCode=$?
37 if [[ "$exitCode" = "0" ]]; then
38 exitCode=1
39 fi
40 echo >&2
41 echo "ERROR: $@" >&2
42 exit $exitCode
43}
44
45
Klaus Aehlig276a8cd2016-07-11 12:06:07 +000046# Set standard variables
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000047DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
Andrew Johnson1fccb312016-12-05 18:27:27 +000048WORKSPACE_DIR="$(dirname "$(dirname "${DIR}")")"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000049
Damien Martin-Guillerez9c5deb62015-09-15 07:38:26 +000050JAVA_VERSION=${JAVA_VERSION:-1.8}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000051BAZELRC=${BAZELRC:-"/dev/null"}
52PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
53
Philipp Wollermann19f34132015-06-12 08:39:25 +000054MACHINE_TYPE="$(uname -m)"
55MACHINE_IS_64BIT='no'
namrata-ibm155bd642016-10-11 09:59:27 +000056if [ "${MACHINE_TYPE}" = 'amd64' -o "${MACHINE_TYPE}" = 'x86_64' -o "${MACHINE_TYPE}" = 's390x' ]; then
Philipp Wollermann19f34132015-06-12 08:39:25 +000057 MACHINE_IS_64BIT='yes'
58fi
59
Zhong Wang8c288132015-08-12 15:06:08 +000060MACHINE_IS_ARM='no'
jmtatsch7c4afb62016-01-25 19:13:23 +000061if [ "${MACHINE_TYPE}" = 'arm' -o "${MACHINE_TYPE}" = 'armv7l' -o "${MACHINE_TYPE}" = 'aarch64' ]; then
Zhong Wang8c288132015-08-12 15:06:08 +000062 MACHINE_IS_ARM='yes'
63fi
64
namrata-ibm155bd642016-10-11 09:59:27 +000065MACHINE_IS_Z='no'
66if [ "${MACHINE_TYPE}" = 's390x' ]; then
67 MACHINE_IS_Z='yes'
68fi
69
Nishidha Panpaliyaaaee64e2016-12-20 18:19:43 +000070if [ "${MACHINE_TYPE}" = 'ppc64' -o "${MACHINE_TYPE}" = 'ppc64le' ]; then
71 MACHINE_IS_64BIT='yes'
72fi
73
László Csomor3c0c2622017-02-13 15:46:28 +000074PATHSEP=":"
Lukacs T. Berkib70343f2017-01-23 11:57:42 +000075case "${PLATFORM}" in
76linux)
77 # JAVA_HOME must point to a Java installation.
78 JAVA_HOME="${JAVA_HOME:-$(readlink -f $(which javac) | sed 's_/bin/javac__')}"
79 ;;
80
81freebsd)
82 # JAVA_HOME must point to a Java installation.
83 JAVA_HOME="${JAVA_HOME:-/usr/local/openjdk8}"
84 ;;
85
86darwin)
87 if [[ -z "$JAVA_HOME" ]]; then
88 JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" \
89 || fail "Could not find JAVA_HOME, please ensure a JDK (version ${JAVA_VERSION}+) is installed."
90 fi
91 ;;
92
Laszlo Csomord0d7ef02017-04-26 10:48:00 +020093msys*|mingw*|cygwin*)
Lukacs T. Berkib70343f2017-01-23 11:57:42 +000094 # Use a simplified platform string.
Laszlo Csomord0d7ef02017-04-26 10:48:00 +020095 PLATFORM="windows"
Lukacs T. Berkib70343f2017-01-23 11:57:42 +000096 PATHSEP=";"
97 # Find the latest available version of the SDK.
Laszlo Csomord0d7ef02017-04-26 10:48:00 +020098 JAVA_HOME="${JAVA_HOME:-$(ls -d C:/Program\ Files/Java/jdk* | sort | tail -n 1)}"
László Csomor3c0c2622017-02-13 15:46:28 +000099 # Replace backslashes with forward slashes.
100 JAVA_HOME="${JAVA_HOME//\\//}"
Lukacs T. Berkib70343f2017-01-23 11:57:42 +0000101esac
102
Dmitry Lomov9d40a602016-02-15 16:15:03 +0000103EXE_EXT=""
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200104if [ "${PLATFORM}" == "windows" ]; then
Laszlo Csomorf5525e82017-01-30 18:11:58 +0000105 # Extension for executables.
Dmitry Lomov9d40a602016-02-15 16:15:03 +0000106 EXE_EXT=".exe"
Dmitry Lomov9d40a602016-02-15 16:15:03 +0000107
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200108 # Fix TMPDIR on windows
Laszlo Csomorf5525e82017-01-30 18:11:58 +0000109 default_tmp=${TMP:-$(cygpath -mO)/Temp}
Laszlo Csomor57db5092016-09-05 07:37:44 +0000110 TMPDIR=$(cygpath -ml "${TMPDIR:-$default_tmp}")
Laszlo Csomorf5525e82017-01-30 18:11:58 +0000111fi
Dmitry Lomov19fd76f2016-06-24 11:52:50 +0000112
Julio Merino5909d9d2016-02-25 21:44:31 +0000113# Whether we display build messages or not. We set this conditionally because
114# the file including us or the user may already have defined VERBOSE to their
115# liking.
116: ${VERBOSE:=yes}
117
Julio Merino63e8d632016-02-25 18:17:53 +0000118# List of functions to invoke on exit.
119ATEXIT_HANDLERS=
120
121# Registers a function to be invoked on exit.
122#
123# The handlers will be invoked at exit time in the order they were registered.
124# See comments in run_atexit for more details.
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000125function atexit() {
Julio Merino63e8d632016-02-25 18:17:53 +0000126 local handler="${1}"; shift
127
128 [ -n "${ATEXIT_HANDLERS}" ] || trap 'run_atexit_handlers $?' EXIT
129 ATEXIT_HANDLERS="${ATEXIT_HANDLERS} ${handler}"
130}
131
132# Exit routine to run all registered atexit handlers.
133#
134# If the program exited with an error, this exit routine will also exit with the
135# same error. However, if the program exited successfully, this exit routine
136# will only exit successfully if the atexit handlers succeed.
137function run_atexit_handlers() {
138 local exit_code="$?"
139
140 local failed=no
141 for handler in ${ATEXIT_HANDLERS}; do
142 eval "${handler}" || failed=yes
143 done
144
145 trap - EXIT # Reset exit handler to prevent double execution.
146 if [ ${exit_code} -ne 0 ]; then
147 exit ${exit_code}
148 else
149 if [ "${failed}" = yes ]; then
150 echo "Program tried to exit successfully but atexit routines failed" 1>&2
151 exit 1
152 else
153 exit 0
154 fi
155 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000156}
157
158function tempdir() {
159 local tmp=${TMPDIR:-/tmp}
Alexander Chungfc071422017-02-07 12:48:26 +0000160 mkdir -p "${tmp}"
161 local DIR="$(mktemp -d "${tmp%%/}/bazel_XXXXXXXX")"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000162 mkdir -p "${DIR}"
Klaus Aehligf47fd832016-07-01 13:58:09 +0000163 local DIRBASE=$(basename "${DIR}")
László Csomoraa029902017-10-09 09:11:08 +0200164 eval "cleanup_tempdir_${DIRBASE}() { rm -rf '${DIR}' >&/dev/null || true ; }"
Klaus Aehligf47fd832016-07-01 13:58:09 +0000165 atexit cleanup_tempdir_${DIRBASE}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000166 NEW_TMPDIR="${DIR}"
167}
168tempdir
169OUTPUT_DIR=${NEW_TMPDIR}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000170phasefile=${OUTPUT_DIR}/phase
Lukacs Berki7da21b12016-08-04 12:18:07 +0000171function cleanup_phasefile() {
172 if [ -f "${phasefile}" ]; then
173 echo 1>&2;
174 cat "${phasefile}" 1>&2;
175 fi;
176}
177
Julio Merino63e8d632016-02-25 18:17:53 +0000178atexit cleanup_phasefile
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000179
Julio Merino5909d9d2016-02-25 21:44:31 +0000180# Excutes a command respecting the current verbosity settings.
181#
182# If VERBOSE is yes, the command itself and its output are printed.
183# If VERBOSE is no, the command's output is only displayed in case of failure.
184#
185# Exits the script if the command fails.
186function run() {
187 if [ "${VERBOSE}" = yes ]; then
188 echo "${@}"
189 "${@}" || exit $?
190 else
Lukacs Berki7da21b12016-08-04 12:18:07 +0000191 local errfile="${OUTPUT_DIR}/errors"
192
Julio Merino5909d9d2016-02-25 21:44:31 +0000193 echo "${@}" >"${errfile}"
Lukacs Berki7da21b12016-08-04 12:18:07 +0000194 if ! "${@}" >>"${errfile}" 2>&1; then
195 local exitcode=$?
196 cat "${errfile}" 1>&2
197 exit $exitcode
198 fi
Julio Merino5909d9d2016-02-25 21:44:31 +0000199 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000200}
201
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000202function display() {
203 if [[ -z "${QUIETMODE}" ]]; then
204 echo -e "$@" >&2
205 fi
206}
207
208function log() {
209 echo -n "." >&2
210 echo "$1" >${phasefile}
211}
212
213function clear_log() {
214 echo >&2
215 rm -f ${phasefile}
216}
217
218LEAVES="\xF0\x9F\x8D\x83"
219INFO="\033[32mINFO\033[0m:"
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000220WARNING="\033[31mWARN\033[0m:"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000221
222first_step=1
223function new_step() {
224 rm -f ${phasefile}
225 local new_line=
226 if [ -n "${first_step}" ]; then
227 first_step=
228 else
229 new_line="\n"
230 fi
Dmitry Lomov8d58ec12017-10-24 14:43:07 +0200231 if [ -t 2 ]; then
232 display -n "$new_line$LEAVES $1"
233 else
234 display -n "$new_line$1"
235 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000236}
237
238function git_sha1() {
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200239 if [ -x "$(which git 2>/dev/null)" ] && [ -d .git ]; then
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000240 git rev-parse --short HEAD 2>/dev/null || true
241 fi
242}
243
Damien Martin-Guillerez28e67b52016-03-14 11:01:31 +0000244function git_date() {
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200245 if [ -x "$(which git 2>/dev/null)" ] && [ -d .git ]; then
Damien Martin-Guillerez28e67b52016-03-14 11:01:31 +0000246 git log -1 --pretty=%ai | cut -d " " -f 1 || true
247 fi
248}
249
250# Get the latest release version and append the date of
251# the last commit if any.
252function get_last_version() {
Damien Martin-Guillerezb7e1b822016-03-17 20:52:36 +0000253 if [ -f "CHANGELOG.md" ]; then
254 local version="$(fgrep -m 1 '## Release' CHANGELOG.md \
255 | sed -E 's|.*Release (.*) \(.*\)|\1|')"
256 else
257 local version=""
258 fi
259
Damien Martin-Guillerez28e67b52016-03-14 11:01:31 +0000260 local date="$(git_date)"
261 if [ -z "${version-}" ]; then
262 version="unknown"
263 fi
264 if [ -n "${date-}" ]; then
265 date="$(date +%Y-%m-%d)"
266 fi
267 echo "${version}-${date}"
268}
269
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000270if [[ ${PLATFORM} == "darwin" ]]; then
271 function md5_file() {
272 echo $(cat $1 | md5) $1
273 }
274else
275 function md5_file() {
276 md5sum $1
277 }
278fi
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000279
280# Gets the java version from JAVA_HOME
281# Sets JAVAC and JAVAC_VERSION with respectively the path to javac and
282# the version of javac.
283function get_java_version() {
284 test -z "$JAVA_HOME" && fail "JDK not found, please set \$JAVA_HOME."
285 JAVAC="${JAVA_HOME}/bin/javac"
286 [[ -x "${JAVAC}" ]] \
287 || fail "JAVA_HOME ($JAVA_HOME) is not a path to a working JDK."
288
289 JAVAC_VERSION=$("${JAVAC}" -version 2>&1)
Bernhard M. Wiedemann5d3f4f12017-11-08 19:46:14 +0100290 if [[ "$JAVAC_VERSION" =~ javac\ ((1\.)?([789]|[1-9][0-9])).*$ ]]; then
291 JAVAC_VERSION=1.${BASH_REMATCH[3]}
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000292 else
John Cater6a87a7e2016-08-23 18:49:56 +0000293 fail \
294 "Cannot determine JDK version, please set \$JAVA_HOME.\n" \
295 "\$JAVAC_VERSION is \"${JAVAC_VERSION}\""
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000296 fi
297}
Damien Martin-Guillerezdbf5cad2015-09-04 12:46:23 +0000298
299# Return the target that a bind point to, using Bazel query.
300function get_bind_target() {
Klaus Aehlig276a8cd2016-07-11 12:06:07 +0000301 $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc ${BAZEL_DIR_STARTUP_OPTIONS} \
Damien Martin-Guillerezdbf5cad2015-09-04 12:46:23 +0000302 query "deps($1, 1) - $1"
303}