blob: 56299b8f1bd72662b6c3a07068d2ce97cb82b2ad [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001#!/bin/bash
2
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01004#
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
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000017# This script bootstraps building a Bazel binary without Bazel then
18# use this compiled Bazel to bootstrap Bazel itself. It can also
19# be provided with a previous version of Bazel to bootstrap Bazel
20# itself.
21# The resulting binary can be found at output/bazel.
David Mankin5cafe132015-05-28 20:20:56 +000022
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023set -o errexit
24
25cd "$(dirname "$0")"
26
Julio Merino5909d9d2016-02-25 21:44:31 +000027# Set the default verbose mode in buildenv.sh so that we do not display command
28# output unless there is a failure. We do this conditionally to offer the user
29# a chance of overriding this in case they want to do so.
30: ${VERBOSE:=no}
31
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000032source scripts/bootstrap/buildenv.sh
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000034function usage() {
Kristina Chodorow84450b82016-01-28 15:16:02 +000035 [ -n "${1:-compile}" ] && echo "Invalid command(s): $1" >&2
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000036 echo "syntax: $0 [command[,command]* [BAZEL_BIN [BAZEL_SUM]]]" >&2
37 echo " General purpose commands:" >&2
Kristina Chodorow84450b82016-01-28 15:16:02 +000038 echo " compile = compile the bazel binary (default)" >&2
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000039 echo " Commands for developers:" >&2
Kristina Chodorow84450b82016-01-28 15:16:02 +000040 echo " all = compile,determinism,test" >&2
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000041 echo " determinism = test for stability of Bazel builds" >&2
42 echo " test = run the full test suite of Bazel" >&2
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043 exit 1
44}
45
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000046function parse_options() {
Kristina Chodorow84450b82016-01-28 15:16:02 +000047 local keywords="(compile|all|determinism|bootstrap|test)"
48 COMMANDS="${1:-compile}"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000049 [[ "${COMMANDS}" =~ ^$keywords(,$keywords)*$ ]] || usage "$@"
50 DO_COMPILE=
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000051 DO_CHECKSUM=
52 DO_FULL_CHECKSUM=1
53 DO_TESTS=
Kristina Chodorow84450b82016-01-28 15:16:02 +000054 [[ "${COMMANDS}" =~ (compile|all) ]] && DO_COMPILE=1
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000055 [[ "${COMMANDS}" =~ (bootstrap|determinism|all) ]] && DO_CHECKSUM=1
56 [[ "${COMMANDS}" =~ (bootstrap) ]] && DO_FULL_CHECKSUM=
57 [[ "${COMMANDS}" =~ (test|all) ]] && DO_TESTS=1
58
59 BAZEL_BIN=${2:-"bazel-bin/src/bazel"}
Damien Martin-Guillerez4f892812015-07-02 08:38:58 +000060 BAZEL_SUM=${3:-"x"}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010061}
62
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000063parse_options "${@}"
Kristina Chodorow8c6b3bb2015-04-09 19:05:34 +000064
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000065mkdir -p output
66: ${BAZEL:=${2-}}
Kristina Chodorowac271142015-03-06 16:00:46 +000067
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000068#
69# Create an initial binary so we can host ourself
70#
71if [ ! -x "${BAZEL}" ]; then
72 display "$INFO You can skip this first step by providing a path to the bazel binary as second argument:"
73 display "$INFO $0 ${COMMANDS} /path/to/bazel"
74 new_step 'Building Bazel from scratch'
75 source scripts/bootstrap/compile.sh
Kristina Chodorowdccd81f2016-02-01 17:42:07 +000076 # The DO_COMPILE flow will actually create the bazel binary and set BAZEL.
77 DO_COMPILE=1
Daniel Wagner-Hall13ba3312015-03-20 05:45:45 +000078fi
79
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000080#
81# Bootstrap bazel using the previous bazel binary = release binary
82#
Damien Martin-Guillereza377af02015-06-24 12:04:27 +000083if [ "${EMBED_LABEL-x}" = "x" ]; then
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000084 # Add a default label when unspecified
85 git_sha1=$(git_sha1)
Damien Martin-Guillerez28e67b52016-03-14 11:01:31 +000086 EMBED_LABEL="$(get_last_version) (@${git_sha1:-non-git})"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010087fi
88
Damien Martin-Guillerez3d796fe2016-01-11 10:07:57 +000089if [[ $PLATFORM == "darwin" ]] && \
90 xcodebuild -showsdks 2> /dev/null | grep -q '\-sdk iphonesimulator'; then
91 EXTRA_BAZEL_ARGS="${EXTRA_BAZEL_ARGS-} --define IPHONE_SDK=1"
Dmitry Lomovac6ed792015-12-22 19:24:00 +000092fi
Damien Martin-Guillerez3d796fe2016-01-11 10:07:57 +000093source scripts/bootstrap/bootstrap.sh
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010094
Lukacs Berki0d5d5222015-10-12 13:24:44 +000095if [ $DO_COMPILE ]; then
96 new_step 'Building Bazel with Bazel'
97 display "."
Damien Martin-Guillerez50fce862016-01-18 11:30:10 +000098 log "Building output/bazel"
Damien Martin-Guillerez04d46ab2016-04-13 19:27:56 +000099 bazel_build "src:bazel${EXE_EXT}"
100 cp -f "bazel-bin/src/bazel${EXE_EXT}" "output/bazel${EXE_EXT}"
Dmitry Lomov9d40a602016-02-15 16:15:03 +0000101 chmod 0755 "output/bazel${EXE_EXT}"
102 BAZEL="$(pwd)/output/bazel${EXE_EXT}"
Lukacs Berki0d5d5222015-10-12 13:24:44 +0000103fi
104
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000105#
106# Output is deterministic between two bootstrapped bazel binary using the actual tools and the
107# released binary.
108#
109if [ $DO_CHECKSUM ]; then
110 new_step "Determinism test"
Damien Martin-Guillerez4349f942015-06-24 13:28:08 +0000111 if [ ! -f ${BAZEL_SUM:-x} ]; then
112 BAZEL_SUM=bazel-out/bazel_checksum
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000113 log "First build"
114 bootstrap_test ${BAZEL} ${BAZEL_SUM}
115 else
116 BOOTSTRAP=${BAZEL}
117 fi
118 if [ "${BAZEL_SUM}" != "${OUTPUT_DIR}/bazel_checksum" ]; then
119 cp ${BAZEL_SUM} ${OUTPUT_DIR}/bazel_checksum
120 fi
121 if [ $DO_FULL_CHECKSUM ]; then
122 log "Second build"
123 bootstrap_test ${BOOTSTRAP} bazel-out/bazel_checksum
124 log "Comparing output"
125 (diff -U 0 ${OUTPUT_DIR}/bazel_checksum bazel-out/bazel_checksum >&2) \
126 || fail "Differences detected in outputs!"
127 fi
128fi
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100129
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000130#
131# Tests
132#
133if [ $DO_TESTS ]; then
134 new_step "Running tests"
135 display "."
Lukacs Berki678ba232015-09-03 13:28:55 +0000136
Damien Martin-Guillerezb81f90c2015-09-04 15:52:26 +0000137 ndk_target="$(get_bind_target //external:android_ndk_for_testing)"
138 sdk_target="$(get_bind_target //external:android_sdk_for_testing)"
139 if [ "$ndk_target" = "//:dummy" -o "$sdk_target" = "//:dummy" ]; then
Lukacs Berki678ba232015-09-03 13:28:55 +0000140 display "$WARNING Android SDK or NDK are not set in the WORKSPACE file. Android tests will not be run."
141 fi
142
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000143 [ -n "$JAVAC_VERSION" ] || get_java_version
144 if [[ ! "${BAZEL_TEST_FILTERS-}" =~ "-jdk8" ]] \
145 && [ "8" -gt ${JAVAC_VERSION#*.} ]; then
146 display "$WARNING Your version of Java is lower than 1.8!"
147 display "$WARNING Deactivating Java 8 tests, please use a JDK 8 to fully"
148 display "$WARNING test Bazel."
149 if [ -n "${BAZEL_TEST_FILTERS-}" ]; then
150 BAZEL_TEST_FILTERS="${BAZEL_TEST_FILTERS},-jdk8"
151 else
152 BAZEL_TEST_FILTERS="-jdk8"
153 fi
154 fi
Damien Martin-Guillerez50d124b2015-06-24 15:20:09 +0000155 $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc test \
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000156 --test_tag_filters="${BAZEL_TEST_FILTERS-}" \
Damien Martin-Guillerez7cf300e2015-06-16 12:31:03 +0000157 --build_tests_only \
Damien Martin-Guillerez04d46ab2016-04-13 19:27:56 +0000158 --nolegacy_bazel_java_test \
Damien Martin-Guillerez3d796fe2016-01-11 10:07:57 +0000159 ${EXTRA_BAZEL_ARGS} \
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000160 --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000161 -k --test_output=errors //src/... //third_party/ijar/... //scripts/... \
162 || fail "Tests failed"
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000163fi
164
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000165clear_log
166display "Build successful! Binary is here: ${BAZEL}"