blob: fb54ed4edd2d472e118c9ff402c7d44b3a8cf3b0 [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
21DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
22WORKSPACE_DIR="$(dirname $(dirname ${DIR}))"
23
Damien Martin-Guillerez9c5deb62015-09-15 07:38:26 +000024JAVA_VERSION=${JAVA_VERSION:-1.8}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000025BAZELRC=${BAZELRC:-"/dev/null"}
26PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
27
Philipp Wollermann19f34132015-06-12 08:39:25 +000028MACHINE_TYPE="$(uname -m)"
29MACHINE_IS_64BIT='no'
30if [ "${MACHINE_TYPE}" = 'amd64' -o "${MACHINE_TYPE}" = 'x86_64' ]; then
31 MACHINE_IS_64BIT='yes'
32fi
33
Zhong Wang8c288132015-08-12 15:06:08 +000034MACHINE_IS_ARM='no'
35if [ "${MACHINE_TYPE}" = 'arm' -o "${MACHINE_TYPE}" = 'armv7l' ]; then
36 MACHINE_IS_ARM='yes'
37fi
38
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000039ATEXIT_=""
40function atexit() {
41 ATEXIT_="$1; ${ATEXIT_}"
42 trap "{ ${ATEXIT_} }" EXIT
43}
44
45function 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}
52tempdir
53OUTPUT_DIR=${NEW_TMPDIR}
54errfile=${OUTPUT_DIR}/errors
55atexit "if [ -f ${errfile} ]; then cat ${errfile} >&2; fi"
56phasefile=${OUTPUT_DIR}/phase
57atexit "if [ -f ${phasefile} ]; then echo >&2; cat ${phasefile} >&2; fi"
58
59function run_silent() {
60 echo "${@}" >${errfile}
Kristina Chodorow78f2e942015-06-24 16:48:47 +000061 # 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-Guillerezd47a8ef2015-06-10 11:54:50 +000064 rm ${errfile}
65}
66
67function fail() {
68 echo >&2
69 echo "$1" >&2
70 exit 1
71}
72
73function display() {
74 if [[ -z "${QUIETMODE}" ]]; then
75 echo -e "$@" >&2
76 fi
77}
78
79function log() {
80 echo -n "." >&2
81 echo "$1" >${phasefile}
82}
83
84function clear_log() {
85 echo >&2
86 rm -f ${phasefile}
87}
88
89LEAVES="\xF0\x9F\x8D\x83"
90INFO="\033[32mINFO\033[0m:"
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +000091WARNING="\033[31mWARN\033[0m:"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000092
93first_step=1
94function 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
105function 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
111if [[ ${PLATFORM} == "darwin" ]]; then
112 function md5_file() {
113 echo $(cat $1 | md5) $1
114 }
115else
116 function md5_file() {
117 md5sum $1
118 }
119fi
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000120
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.
124function 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-Guillerezdbf5cad2015-09-04 12:46:23 +0000137
138# Return the target that a bind point to, using Bazel query.
139function get_bind_target() {
Damien Martin-Guillerez311801a2015-09-04 14:01:01 +0000140 $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc \
Damien Martin-Guillerezdbf5cad2015-09-04 12:46:23 +0000141 query "deps($1, 1) - $1"
142}