Philipp Wollermann | 19f3413 | 2015-06-12 08:39:25 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 3 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 4 | # |
| 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 | |
| 19 | set -o errexit |
| 20 | |
Klaus Aehlig | 276a8cd | 2016-07-11 12:06:07 +0000 | [diff] [blame] | 21 | # If BAZEL_WRKDIR is set, default all variables to point into |
| 22 | # that directory |
| 23 | |
| 24 | if [ -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"} |
| 30 | fi |
| 31 | |
| 32 | |
dmarting | 511c35b | 2017-05-05 14:12:29 +0200 | [diff] [blame] | 33 | # We define the fail function early so we can use it when detecting the JDK |
| 34 | # See https://github.com/bazelbuild/bazel/issues/2949, |
| 35 | function 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 Aehlig | 276a8cd | 2016-07-11 12:06:07 +0000 | [diff] [blame] | 46 | # Set standard variables |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 47 | DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
Andrew Johnson | 1fccb31 | 2016-12-05 18:27:27 +0000 | [diff] [blame] | 48 | WORKSPACE_DIR="$(dirname "$(dirname "${DIR}")")" |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 49 | |
Damien Martin-Guillerez | 9c5deb6 | 2015-09-15 07:38:26 +0000 | [diff] [blame] | 50 | JAVA_VERSION=${JAVA_VERSION:-1.8} |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 51 | BAZELRC=${BAZELRC:-"/dev/null"} |
| 52 | PLATFORM="$(uname -s | tr 'A-Z' 'a-z')" |
| 53 | |
Philipp Wollermann | 19f3413 | 2015-06-12 08:39:25 +0000 | [diff] [blame] | 54 | MACHINE_TYPE="$(uname -m)" |
| 55 | MACHINE_IS_64BIT='no' |
namrata-ibm | 155bd64 | 2016-10-11 09:59:27 +0000 | [diff] [blame] | 56 | if [ "${MACHINE_TYPE}" = 'amd64' -o "${MACHINE_TYPE}" = 'x86_64' -o "${MACHINE_TYPE}" = 's390x' ]; then |
Philipp Wollermann | 19f3413 | 2015-06-12 08:39:25 +0000 | [diff] [blame] | 57 | MACHINE_IS_64BIT='yes' |
| 58 | fi |
| 59 | |
Zhong Wang | 8c28813 | 2015-08-12 15:06:08 +0000 | [diff] [blame] | 60 | MACHINE_IS_ARM='no' |
jmtatsch | 7c4afb6 | 2016-01-25 19:13:23 +0000 | [diff] [blame] | 61 | if [ "${MACHINE_TYPE}" = 'arm' -o "${MACHINE_TYPE}" = 'armv7l' -o "${MACHINE_TYPE}" = 'aarch64' ]; then |
Zhong Wang | 8c28813 | 2015-08-12 15:06:08 +0000 | [diff] [blame] | 62 | MACHINE_IS_ARM='yes' |
| 63 | fi |
| 64 | |
namrata-ibm | 155bd64 | 2016-10-11 09:59:27 +0000 | [diff] [blame] | 65 | MACHINE_IS_Z='no' |
| 66 | if [ "${MACHINE_TYPE}" = 's390x' ]; then |
| 67 | MACHINE_IS_Z='yes' |
| 68 | fi |
| 69 | |
Nishidha Panpaliya | aaee64e | 2016-12-20 18:19:43 +0000 | [diff] [blame] | 70 | if [ "${MACHINE_TYPE}" = 'ppc64' -o "${MACHINE_TYPE}" = 'ppc64le' ]; then |
| 71 | MACHINE_IS_64BIT='yes' |
| 72 | fi |
| 73 | |
László Csomor | 3c0c262 | 2017-02-13 15:46:28 +0000 | [diff] [blame] | 74 | PATHSEP=":" |
Lukacs T. Berki | b70343f | 2017-01-23 11:57:42 +0000 | [diff] [blame] | 75 | case "${PLATFORM}" in |
| 76 | linux) |
| 77 | # JAVA_HOME must point to a Java installation. |
| 78 | JAVA_HOME="${JAVA_HOME:-$(readlink -f $(which javac) | sed 's_/bin/javac__')}" |
| 79 | ;; |
| 80 | |
| 81 | freebsd) |
| 82 | # JAVA_HOME must point to a Java installation. |
| 83 | JAVA_HOME="${JAVA_HOME:-/usr/local/openjdk8}" |
| 84 | ;; |
| 85 | |
| 86 | darwin) |
| 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 Csomor | d0d7ef0 | 2017-04-26 10:48:00 +0200 | [diff] [blame] | 93 | msys*|mingw*|cygwin*) |
Lukacs T. Berki | b70343f | 2017-01-23 11:57:42 +0000 | [diff] [blame] | 94 | # Use a simplified platform string. |
Laszlo Csomor | d0d7ef0 | 2017-04-26 10:48:00 +0200 | [diff] [blame] | 95 | PLATFORM="windows" |
Lukacs T. Berki | b70343f | 2017-01-23 11:57:42 +0000 | [diff] [blame] | 96 | PATHSEP=";" |
| 97 | # Find the latest available version of the SDK. |
Laszlo Csomor | d0d7ef0 | 2017-04-26 10:48:00 +0200 | [diff] [blame] | 98 | JAVA_HOME="${JAVA_HOME:-$(ls -d C:/Program\ Files/Java/jdk* | sort | tail -n 1)}" |
László Csomor | 3c0c262 | 2017-02-13 15:46:28 +0000 | [diff] [blame] | 99 | # Replace backslashes with forward slashes. |
| 100 | JAVA_HOME="${JAVA_HOME//\\//}" |
Lukacs T. Berki | b70343f | 2017-01-23 11:57:42 +0000 | [diff] [blame] | 101 | esac |
| 102 | |
Dmitry Lomov | 9d40a60 | 2016-02-15 16:15:03 +0000 | [diff] [blame] | 103 | EXE_EXT="" |
Laszlo Csomor | d0d7ef0 | 2017-04-26 10:48:00 +0200 | [diff] [blame] | 104 | if [ "${PLATFORM}" == "windows" ]; then |
Laszlo Csomor | f5525e8 | 2017-01-30 18:11:58 +0000 | [diff] [blame] | 105 | # Extension for executables. |
Dmitry Lomov | 9d40a60 | 2016-02-15 16:15:03 +0000 | [diff] [blame] | 106 | EXE_EXT=".exe" |
Dmitry Lomov | 9d40a60 | 2016-02-15 16:15:03 +0000 | [diff] [blame] | 107 | |
Laszlo Csomor | d0d7ef0 | 2017-04-26 10:48:00 +0200 | [diff] [blame] | 108 | # Fix TMPDIR on windows |
Laszlo Csomor | f5525e8 | 2017-01-30 18:11:58 +0000 | [diff] [blame] | 109 | default_tmp=${TMP:-$(cygpath -mO)/Temp} |
Laszlo Csomor | 57db509 | 2016-09-05 07:37:44 +0000 | [diff] [blame] | 110 | TMPDIR=$(cygpath -ml "${TMPDIR:-$default_tmp}") |
Laszlo Csomor | f5525e8 | 2017-01-30 18:11:58 +0000 | [diff] [blame] | 111 | fi |
Dmitry Lomov | 19fd76f | 2016-06-24 11:52:50 +0000 | [diff] [blame] | 112 | |
Julio Merino | 5909d9d | 2016-02-25 21:44:31 +0000 | [diff] [blame] | 113 | # 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 Merino | 63e8d63 | 2016-02-25 18:17:53 +0000 | [diff] [blame] | 118 | # List of functions to invoke on exit. |
| 119 | ATEXIT_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-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 125 | function atexit() { |
Julio Merino | 63e8d63 | 2016-02-25 18:17:53 +0000 | [diff] [blame] | 126 | 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. |
| 137 | function 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-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | function tempdir() { |
| 159 | local tmp=${TMPDIR:-/tmp} |
Alexander Chung | fc07142 | 2017-02-07 12:48:26 +0000 | [diff] [blame] | 160 | mkdir -p "${tmp}" |
| 161 | local DIR="$(mktemp -d "${tmp%%/}/bazel_XXXXXXXX")" |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 162 | mkdir -p "${DIR}" |
Klaus Aehlig | f47fd83 | 2016-07-01 13:58:09 +0000 | [diff] [blame] | 163 | local DIRBASE=$(basename "${DIR}") |
László Csomor | aa02990 | 2017-10-09 09:11:08 +0200 | [diff] [blame] | 164 | eval "cleanup_tempdir_${DIRBASE}() { rm -rf '${DIR}' >&/dev/null || true ; }" |
Klaus Aehlig | f47fd83 | 2016-07-01 13:58:09 +0000 | [diff] [blame] | 165 | atexit cleanup_tempdir_${DIRBASE} |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 166 | NEW_TMPDIR="${DIR}" |
| 167 | } |
| 168 | tempdir |
| 169 | OUTPUT_DIR=${NEW_TMPDIR} |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 170 | phasefile=${OUTPUT_DIR}/phase |
Lukacs Berki | 7da21b1 | 2016-08-04 12:18:07 +0000 | [diff] [blame] | 171 | function cleanup_phasefile() { |
| 172 | if [ -f "${phasefile}" ]; then |
| 173 | echo 1>&2; |
| 174 | cat "${phasefile}" 1>&2; |
| 175 | fi; |
| 176 | } |
| 177 | |
Julio Merino | 63e8d63 | 2016-02-25 18:17:53 +0000 | [diff] [blame] | 178 | atexit cleanup_phasefile |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 179 | |
Julio Merino | 5909d9d | 2016-02-25 21:44:31 +0000 | [diff] [blame] | 180 | # 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. |
| 186 | function run() { |
| 187 | if [ "${VERBOSE}" = yes ]; then |
| 188 | echo "${@}" |
| 189 | "${@}" || exit $? |
| 190 | else |
Lukacs Berki | 7da21b1 | 2016-08-04 12:18:07 +0000 | [diff] [blame] | 191 | local errfile="${OUTPUT_DIR}/errors" |
| 192 | |
Julio Merino | 5909d9d | 2016-02-25 21:44:31 +0000 | [diff] [blame] | 193 | echo "${@}" >"${errfile}" |
Lukacs Berki | 7da21b1 | 2016-08-04 12:18:07 +0000 | [diff] [blame] | 194 | if ! "${@}" >>"${errfile}" 2>&1; then |
| 195 | local exitcode=$? |
| 196 | cat "${errfile}" 1>&2 |
| 197 | exit $exitcode |
| 198 | fi |
Julio Merino | 5909d9d | 2016-02-25 21:44:31 +0000 | [diff] [blame] | 199 | fi |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 202 | function display() { |
| 203 | if [[ -z "${QUIETMODE}" ]]; then |
| 204 | echo -e "$@" >&2 |
| 205 | fi |
| 206 | } |
| 207 | |
| 208 | function log() { |
| 209 | echo -n "." >&2 |
| 210 | echo "$1" >${phasefile} |
| 211 | } |
| 212 | |
| 213 | function clear_log() { |
| 214 | echo >&2 |
| 215 | rm -f ${phasefile} |
| 216 | } |
| 217 | |
| 218 | LEAVES="\xF0\x9F\x8D\x83" |
| 219 | INFO="\033[32mINFO\033[0m:" |
Damien Martin-Guillerez | 338dc56 | 2015-06-23 17:15:12 +0000 | [diff] [blame] | 220 | WARNING="\033[31mWARN\033[0m:" |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 221 | |
| 222 | first_step=1 |
| 223 | function 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 Lomov | 8d58ec1 | 2017-10-24 14:43:07 +0200 | [diff] [blame] | 231 | if [ -t 2 ]; then |
| 232 | display -n "$new_line$LEAVES $1" |
| 233 | else |
| 234 | display -n "$new_line$1" |
| 235 | fi |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | function git_sha1() { |
Laszlo Csomor | d0d7ef0 | 2017-04-26 10:48:00 +0200 | [diff] [blame] | 239 | if [ -x "$(which git 2>/dev/null)" ] && [ -d .git ]; then |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 240 | git rev-parse --short HEAD 2>/dev/null || true |
| 241 | fi |
| 242 | } |
| 243 | |
Damien Martin-Guillerez | 28e67b5 | 2016-03-14 11:01:31 +0000 | [diff] [blame] | 244 | function git_date() { |
Laszlo Csomor | d0d7ef0 | 2017-04-26 10:48:00 +0200 | [diff] [blame] | 245 | if [ -x "$(which git 2>/dev/null)" ] && [ -d .git ]; then |
Damien Martin-Guillerez | 28e67b5 | 2016-03-14 11:01:31 +0000 | [diff] [blame] | 246 | 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. |
| 252 | function get_last_version() { |
Damien Martin-Guillerez | b7e1b82 | 2016-03-17 20:52:36 +0000 | [diff] [blame] | 253 | 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-Guillerez | 28e67b5 | 2016-03-14 11:01:31 +0000 | [diff] [blame] | 260 | 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-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 270 | if [[ ${PLATFORM} == "darwin" ]]; then |
| 271 | function md5_file() { |
| 272 | echo $(cat $1 | md5) $1 |
| 273 | } |
| 274 | else |
| 275 | function md5_file() { |
| 276 | md5sum $1 |
| 277 | } |
| 278 | fi |
Damien Martin-Guillerez | 338dc56 | 2015-06-23 17:15:12 +0000 | [diff] [blame] | 279 | |
| 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. |
| 283 | function 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. Wiedemann | 5d3f4f1 | 2017-11-08 19:46:14 +0100 | [diff] [blame^] | 290 | if [[ "$JAVAC_VERSION" =~ javac\ ((1\.)?([789]|[1-9][0-9])).*$ ]]; then |
| 291 | JAVAC_VERSION=1.${BASH_REMATCH[3]} |
Damien Martin-Guillerez | 338dc56 | 2015-06-23 17:15:12 +0000 | [diff] [blame] | 292 | else |
John Cater | 6a87a7e | 2016-08-23 18:49:56 +0000 | [diff] [blame] | 293 | fail \ |
| 294 | "Cannot determine JDK version, please set \$JAVA_HOME.\n" \ |
| 295 | "\$JAVAC_VERSION is \"${JAVAC_VERSION}\"" |
Damien Martin-Guillerez | 338dc56 | 2015-06-23 17:15:12 +0000 | [diff] [blame] | 296 | fi |
| 297 | } |
Damien Martin-Guillerez | dbf5cad | 2015-09-04 12:46:23 +0000 | [diff] [blame] | 298 | |
| 299 | # Return the target that a bind point to, using Bazel query. |
| 300 | function get_bind_target() { |
Klaus Aehlig | 276a8cd | 2016-07-11 12:06:07 +0000 | [diff] [blame] | 301 | $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc ${BAZEL_DIR_STARTUP_OPTIONS} \ |
Damien Martin-Guillerez | dbf5cad | 2015-09-04 12:46:23 +0000 | [diff] [blame] | 302 | query "deps($1, 1) - $1" |
| 303 | } |