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 | |
| 21 | DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
| 22 | WORKSPACE_DIR="$(dirname $(dirname ${DIR}))" |
| 23 | |
Damien Martin-Guillerez | 9c5deb6 | 2015-09-15 07:38:26 +0000 | [diff] [blame] | 24 | JAVA_VERSION=${JAVA_VERSION:-1.8} |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 25 | BAZELRC=${BAZELRC:-"/dev/null"} |
| 26 | PLATFORM="$(uname -s | tr 'A-Z' 'a-z')" |
| 27 | |
Philipp Wollermann | 19f3413 | 2015-06-12 08:39:25 +0000 | [diff] [blame] | 28 | MACHINE_TYPE="$(uname -m)" |
| 29 | MACHINE_IS_64BIT='no' |
| 30 | if [ "${MACHINE_TYPE}" = 'amd64' -o "${MACHINE_TYPE}" = 'x86_64' ]; then |
| 31 | MACHINE_IS_64BIT='yes' |
| 32 | fi |
| 33 | |
Zhong Wang | 8c28813 | 2015-08-12 15:06:08 +0000 | [diff] [blame] | 34 | MACHINE_IS_ARM='no' |
| 35 | if [ "${MACHINE_TYPE}" = 'arm' -o "${MACHINE_TYPE}" = 'armv7l' ]; then |
| 36 | MACHINE_IS_ARM='yes' |
| 37 | fi |
| 38 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 39 | ATEXIT_="" |
| 40 | function atexit() { |
| 41 | ATEXIT_="$1; ${ATEXIT_}" |
| 42 | trap "{ ${ATEXIT_} }" EXIT |
| 43 | } |
| 44 | |
| 45 | function tempdir() { |
| 46 | local tmp=${TMPDIR:-/tmp} |
| 47 | local DIR="$(mktemp -d ${tmp%%/}/bazel.XXXXXXXX)" |
| 48 | mkdir -p "${DIR}" |
| 49 | atexit "rm -fr ${DIR}" |
| 50 | NEW_TMPDIR="${DIR}" |
| 51 | } |
| 52 | tempdir |
| 53 | OUTPUT_DIR=${NEW_TMPDIR} |
| 54 | errfile=${OUTPUT_DIR}/errors |
| 55 | atexit "if [ -f ${errfile} ]; then cat ${errfile} >&2; fi" |
| 56 | phasefile=${OUTPUT_DIR}/phase |
| 57 | atexit "if [ -f ${phasefile} ]; then echo >&2; cat ${phasefile} >&2; fi" |
| 58 | |
| 59 | function run_silent() { |
| 60 | echo "${@}" >${errfile} |
Kristina Chodorow | 78f2e94 | 2015-06-24 16:48:47 +0000 | [diff] [blame] | 61 | # TODO(kchodorow): figure out why this doesn't exit on a non-zero exit code, |
| 62 | # even though errexit is set. |
| 63 | "${@}" >>${errfile} 2>&1 || exit $? |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 64 | rm ${errfile} |
| 65 | } |
| 66 | |
| 67 | function fail() { |
| 68 | echo >&2 |
| 69 | echo "$1" >&2 |
| 70 | exit 1 |
| 71 | } |
| 72 | |
| 73 | function display() { |
| 74 | if [[ -z "${QUIETMODE}" ]]; then |
| 75 | echo -e "$@" >&2 |
| 76 | fi |
| 77 | } |
| 78 | |
| 79 | function log() { |
| 80 | echo -n "." >&2 |
| 81 | echo "$1" >${phasefile} |
| 82 | } |
| 83 | |
| 84 | function clear_log() { |
| 85 | echo >&2 |
| 86 | rm -f ${phasefile} |
| 87 | } |
| 88 | |
| 89 | LEAVES="\xF0\x9F\x8D\x83" |
| 90 | INFO="\033[32mINFO\033[0m:" |
Damien Martin-Guillerez | 338dc56 | 2015-06-23 17:15:12 +0000 | [diff] [blame] | 91 | WARNING="\033[31mWARN\033[0m:" |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 92 | |
| 93 | first_step=1 |
| 94 | function new_step() { |
| 95 | rm -f ${phasefile} |
| 96 | local new_line= |
| 97 | if [ -n "${first_step}" ]; then |
| 98 | first_step= |
| 99 | else |
| 100 | new_line="\n" |
| 101 | fi |
| 102 | display -n "$new_line$LEAVES $1" |
| 103 | } |
| 104 | |
| 105 | function git_sha1() { |
| 106 | if [ -x "$(which git || true)" ] && [ -d .git ]; then |
| 107 | git rev-parse --short HEAD 2>/dev/null || true |
| 108 | fi |
| 109 | } |
| 110 | |
| 111 | if [[ ${PLATFORM} == "darwin" ]]; then |
| 112 | function md5_file() { |
| 113 | echo $(cat $1 | md5) $1 |
| 114 | } |
| 115 | else |
| 116 | function md5_file() { |
| 117 | md5sum $1 |
| 118 | } |
| 119 | fi |
Damien Martin-Guillerez | 338dc56 | 2015-06-23 17:15:12 +0000 | [diff] [blame] | 120 | |
| 121 | # Gets the java version from JAVA_HOME |
| 122 | # Sets JAVAC and JAVAC_VERSION with respectively the path to javac and |
| 123 | # the version of javac. |
| 124 | function get_java_version() { |
| 125 | test -z "$JAVA_HOME" && fail "JDK not found, please set \$JAVA_HOME." |
| 126 | JAVAC="${JAVA_HOME}/bin/javac" |
| 127 | [[ -x "${JAVAC}" ]] \ |
| 128 | || fail "JAVA_HOME ($JAVA_HOME) is not a path to a working JDK." |
| 129 | |
| 130 | JAVAC_VERSION=$("${JAVAC}" -version 2>&1) |
| 131 | if [[ "$JAVAC_VERSION" =~ ^"javac "(1\.([789]|[1-9][0-9])).*$ ]]; then |
| 132 | JAVAC_VERSION=${BASH_REMATCH[1]} |
| 133 | else |
| 134 | fail "Cannot determine JDK version, please set \$JAVA_HOME." |
| 135 | fi |
| 136 | } |
Damien Martin-Guillerez | dbf5cad | 2015-09-04 12:46:23 +0000 | [diff] [blame] | 137 | |
| 138 | # Return the target that a bind point to, using Bazel query. |
| 139 | function get_bind_target() { |
Damien Martin-Guillerez | 311801a | 2015-09-04 14:01:01 +0000 | [diff] [blame] | 140 | $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc \ |
Damien Martin-Guillerez | dbf5cad | 2015-09-04 12:46:23 +0000 | [diff] [blame] | 141 | query "deps($1, 1) - $1" |
| 142 | } |