blob: f8af2269c834a3dc8c2c7bb3340b9088ee7bf2a0 [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
Laszlo Csomorf5525e82017-01-30 18:11:58 +000025# Correct PATH on Windows, to avoid using "FIND.EXE" instead of "/usr/bin/find"
26# etc, leading to confusing errors.
27export BAZEL_OLD_PATH=$PATH
28case "$(uname -s | tr [:upper:] [:lower:])" in
Laszlo Csomord0d7ef02017-04-26 10:48:00 +020029msys*|mingw*|cygwin*)
Laszlo Csomorf5525e82017-01-30 18:11:58 +000030 # Check that the PATH is set up correctly by attempting to locate `[`.
31 # This ensures that `which` is installed correctly and can succeed, while
32 # also avoids accidentally locating a tool that exists in plain Windows too
33 # (like "find" for "FIND.EXE").
34 which [ >&/dev/null || export PATH="/bin:/usr/bin:$PATH"
35esac
36
37# Check that the bintools can be found, otherwise we would see very confusing
38# error messages.
Klaus Aehlig27db2d82017-08-28 12:41:52 +020039hash tr >&/dev/null || {
40 echo >&2 "ERROR: cannot locate GNU coreutils; check your PATH."
Laszlo Csomorf5525e82017-01-30 18:11:58 +000041 echo >&2 " (You may need to run 'export PATH=/bin:/usr/bin:\$PATH)'"
42 exit 1
43}
44
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045cd "$(dirname "$0")"
46
Julio Merino5909d9d2016-02-25 21:44:31 +000047# Set the default verbose mode in buildenv.sh so that we do not display command
48# output unless there is a failure. We do this conditionally to offer the user
49# a chance of overriding this in case they want to do so.
50: ${VERBOSE:=no}
51
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000052source scripts/bootstrap/buildenv.sh
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000054function usage() {
Kristina Chodorow84450b82016-01-28 15:16:02 +000055 [ -n "${1:-compile}" ] && echo "Invalid command(s): $1" >&2
Damien Martin-Guillerezda1d4162017-08-23 12:19:25 +020056 echo "syntax: $0 [command[,command]* [BAZEL_BIN]]" >&2
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000057 echo " General purpose commands:" >&2
Kristina Chodorow84450b82016-01-28 15:16:02 +000058 echo " compile = compile the bazel binary (default)" >&2
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000059 echo " Commands for developers:" >&2
Damien Martin-Guillerezda1d4162017-08-23 12:19:25 +020060 echo " all = compile,srcs,test" >&2
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +000061 echo " srcs = test that //:srcs contains all the sources" >&2
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000062 echo " test = run the full test suite of Bazel" >&2
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010063 exit 1
64}
65
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000066function parse_options() {
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +000067 local keywords="(compile|all|determinism|bootstrap|srcs|test)"
Kristina Chodorow84450b82016-01-28 15:16:02 +000068 COMMANDS="${1:-compile}"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000069 [[ "${COMMANDS}" =~ ^$keywords(,$keywords)*$ ]] || usage "$@"
70 DO_COMPILE=
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000071 DO_TESTS=
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +000072 DO_SRCS_TEST=
Kristina Chodorow84450b82016-01-28 15:16:02 +000073 [[ "${COMMANDS}" =~ (compile|all) ]] && DO_COMPILE=1
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +000074 [[ "${COMMANDS}" =~ (srcs|all) ]] && DO_SRCS_TEST=1
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000075 [[ "${COMMANDS}" =~ (test|all) ]] && DO_TESTS=1
76
77 BAZEL_BIN=${2:-"bazel-bin/src/bazel"}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010078}
79
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000080parse_options "${@}"
Kristina Chodorow8c6b3bb2015-04-09 19:05:34 +000081
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000082mkdir -p output
83: ${BAZEL:=${2-}}
Kristina Chodorowac271142015-03-06 16:00:46 +000084
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000085#
86# Create an initial binary so we can host ourself
87#
88if [ ! -x "${BAZEL}" ]; then
89 display "$INFO You can skip this first step by providing a path to the bazel binary as second argument:"
90 display "$INFO $0 ${COMMANDS} /path/to/bazel"
91 new_step 'Building Bazel from scratch'
92 source scripts/bootstrap/compile.sh
Kristina Chodorowdccd81f2016-02-01 17:42:07 +000093 # The DO_COMPILE flow will actually create the bazel binary and set BAZEL.
94 DO_COMPILE=1
Daniel Wagner-Hall13ba3312015-03-20 05:45:45 +000095fi
96
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000097#
98# Bootstrap bazel using the previous bazel binary = release binary
99#
Damien Martin-Guillereza377af02015-06-24 12:04:27 +0000100if [ "${EMBED_LABEL-x}" = "x" ]; then
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000101 # Add a default label when unspecified
102 git_sha1=$(git_sha1)
Damien Martin-Guillerez28e67b52016-03-14 11:01:31 +0000103 EMBED_LABEL="$(get_last_version) (@${git_sha1:-non-git})"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100104fi
105
Damien Martin-Guillerez3d796fe2016-01-11 10:07:57 +0000106if [[ $PLATFORM == "darwin" ]] && \
107 xcodebuild -showsdks 2> /dev/null | grep -q '\-sdk iphonesimulator'; then
Damien Martin-Guillerez1540d8f2016-09-07 11:49:39 +0000108 EXTRA_BAZEL_ARGS="${EXTRA_BAZEL_ARGS-} --define IPHONE_SDK=1"
Dmitry Lomovac6ed792015-12-22 19:24:00 +0000109fi
Yun Pengcb8a5e22017-03-08 14:30:49 +0000110
Damien Martin-Guillerez3d796fe2016-01-11 10:07:57 +0000111source scripts/bootstrap/bootstrap.sh
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100112
Lukacs Berki0d5d5222015-10-12 13:24:44 +0000113if [ $DO_COMPILE ]; then
114 new_step 'Building Bazel with Bazel'
115 display "."
Damien Martin-Guillerez50fce862016-01-18 11:30:10 +0000116 log "Building output/bazel"
Laszlo Csomor9f7180f2016-09-27 13:07:43 +0000117 bazel_build "src:bazel${EXE_EXT}" \
118 || fail "Could not build Bazel"
119 bazel_bin_path="$(get_bazel_bin_path)/src/bazel${EXE_EXT}"
120 [ -e "$bazel_bin_path" ] \
121 || fail "Could not find freshly built Bazel binary at '$bazel_bin_path'"
122 cp -f "$bazel_bin_path" "output/bazel${EXE_EXT}" \
123 || fail "Could not copy '$bazel_bin_path' to 'output/bazel${EXE_EXT}'"
Dmitry Lomov9d40a602016-02-15 16:15:03 +0000124 chmod 0755 "output/bazel${EXE_EXT}"
125 BAZEL="$(pwd)/output/bazel${EXE_EXT}"
Lukacs Berki0d5d5222015-10-12 13:24:44 +0000126fi
127
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000128#
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +0000129# Test that //:srcs contains all the sources
130#
131if [ $DO_SRCS_TEST ]; then
132 new_step "Checking that //:srcs contains all the sources"
133 log "Querying //:srcs"
aehlig5ede79a2017-07-31 17:45:47 +0200134 ${BAZEL} query 'kind("source file", deps(//:srcs))' \
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +0000135 | grep -v '^@' \
Ulf Adams161c4a32016-12-05 17:25:21 +0000136 | sed -e 's|^//||' | sed -e 's|^:||' | sed -e 's|:|/|' \
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +0000137 | sort -u >"${OUTPUT_DIR}/srcs-query"
138
139 log "Finding all files"
Damien Martin-Guillerezca407f02016-07-04 13:07:08 +0000140 # SRCS_EXCLUDES can be overriden to adds some more exceptions for the find
141 # commands (for CI systems).
142 SRCS_EXCLUDES=${SRCS_EXCLUDES-XXXXXXXXXXXXXX1268778dfsdf4}
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +0000143 # See file BUILD for the list of grep -v exceptions.
144 # tools/defaults package is hidden by Bazel so cannot be put in the srcs.
Ulf Adams161c4a32016-12-05 17:25:21 +0000145 find . -type f | sed -e 's|./||' \
Adam Michael9b7330f2017-03-23 18:40:51 +0000146 | grep -v '^bazel-' \
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +0000147 | grep -v '^\.' | grep -v '^out/' | grep -v '^output/' \
Klaus Aehlig0a050862016-11-11 17:26:44 +0000148 | grep -v '^derived' \
Damien Martin-Guillerezca407f02016-07-04 13:07:08 +0000149 | grep -Ev "${SRCS_EXCLUDES}" \
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +0000150 | grep -v '^tools/defaults/BUILD' \
151 | sort -u >"${OUTPUT_DIR}/srcs-find"
152
153 log "Diffing"
Ulf Adams161c4a32016-12-05 17:25:21 +0000154 res="$(diff -U 0 "${OUTPUT_DIR}/srcs-find" "${OUTPUT_DIR}/srcs-query" | sed -e 's|^-||' | grep -Ev '^(@@|\+\+|--)' || true)"
Damien Martin-Guillerez7d265e02016-07-01 13:33:48 +0000155
156 if [ -n "${res}" ]; then
157 fail "//:srcs filegroup do not contains all the sources, missing:
158${res}"
159 fi
160fi
161
162#
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000163# Tests
164#
165if [ $DO_TESTS ]; then
166 new_step "Running tests"
167 display "."
Lukacs Berki678ba232015-09-03 13:28:55 +0000168
Damien Martin-Guillerezb81f90c2015-09-04 15:52:26 +0000169 ndk_target="$(get_bind_target //external:android_ndk_for_testing)"
170 sdk_target="$(get_bind_target //external:android_sdk_for_testing)"
Androbincfb2ec02017-06-27 13:47:43 +0200171 if [ "$ndk_target" = "//:dummy" ] || [ "$sdk_target" = "//:dummy" ]; then
Lukacs Berki678ba232015-09-03 13:28:55 +0000172 display "$WARNING Android SDK or NDK are not set in the WORKSPACE file. Android tests will not be run."
173 fi
174
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000175 [ -n "$JAVAC_VERSION" ] || get_java_version
Damien Martin-Guillerez0ad9f5e2016-04-20 13:58:08 +0000176 if [[ ! "${BAZEL_TEST_FILTERS-}" =~ "-jdk8" ]]; then
177 if [ "8" -gt ${JAVAC_VERSION#*.} ] || [ "${JAVA_VERSION}" = "1.7" ]; then
178 display "$WARNING Your version of Java is lower than 1.8!"
179 display "$WARNING Deactivating Java 8 tests, please use a JDK 8 to fully"
180 display "$WARNING test Bazel."
181 if [ -n "${BAZEL_TEST_FILTERS-}" ]; then
182 BAZEL_TEST_FILTERS="${BAZEL_TEST_FILTERS},-jdk8"
183 else
184 BAZEL_TEST_FILTERS="-jdk8"
185 fi
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000186 fi
187 fi
Klaus Aehlig276a8cd2016-07-11 12:06:07 +0000188 $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc \
189 ${BAZEL_DIR_STARTUP_OPTIONS} \
190 test \
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000191 --test_tag_filters="${BAZEL_TEST_FILTERS-}" \
Damien Martin-Guillerez7cf300e2015-06-16 12:31:03 +0000192 --build_tests_only \
Damien Martin-Guillerez04d46ab2016-04-13 19:27:56 +0000193 --nolegacy_bazel_java_test \
Damien Martin-Guillerez0ad9f5e2016-04-20 13:58:08 +0000194 --define JAVA_VERSION=${JAVA_VERSION} \
Damien Martin-Guillerez3d796fe2016-01-11 10:07:57 +0000195 ${EXTRA_BAZEL_ARGS} \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000196 -k --test_output=errors //src/... //third_party/ijar/... //scripts/... \
197 || fail "Tests failed"
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000198fi
199
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000200clear_log
201display "Build successful! Binary is here: ${BAZEL}"