blob: 3f157e2ec71350a2c8a7d97fa799e594666d4142 [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001#!/bin/bash
2
3# Copyright 2014 Google Inc. All rights reserved.
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
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
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000027source scripts/bootstrap/buildenv.sh
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000029function usage() {
30 [ -n "${1:-build}" ] && echo "Invalid command(s): $1" >&2
31 echo "syntax: $0 [command[,command]* [BAZEL_BIN [BAZEL_SUM]]]" >&2
32 echo " General purpose commands:" >&2
33 echo " build = compile,tools,init (default)" >&2
34 echo " compile = compile a Bazel binary for usage" >&2
35 echo " tools = compile and install tooling for Bazel" >&2
36 echo " init = initialize the base workspace" >&2
37 echo " Commands for developers:" >&2
38 echo " all = build,determinism,test" >&2
39 echo " determinism = test for stability of Bazel builds" >&2
40 echo " test = run the full test suite of Bazel" >&2
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041 exit 1
42}
43
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000044function parse_options() {
45 local keywords="(build|compile|tools|init|all|determinism|bootstrap|test)"
46 COMMANDS="${1:-build}"
47 [[ "${COMMANDS}" =~ ^$keywords(,$keywords)*$ ]] || usage "$@"
48 DO_COMPILE=
49 DO_TOOLS_COMPILATION=
50 DO_CHECKSUM=
51 DO_FULL_CHECKSUM=1
52 DO_TESTS=
53 DO_BASE_WORKSPACE_INIT=
54 [[ "${COMMANDS}" =~ (compile|build|all) ]] && DO_COMPILE=1
55 [[ "${COMMANDS}" =~ (tools|build|all) ]] && DO_TOOLS_COMPILATION=1
56 [[ "${COMMANDS}" =~ (init|build|all) ]] && DO_BASE_WORKSPACE_INIT=1
57 [[ "${COMMANDS}" =~ (bootstrap|determinism|all) ]] && DO_CHECKSUM=1
58 [[ "${COMMANDS}" =~ (bootstrap) ]] && DO_FULL_CHECKSUM=
59 [[ "${COMMANDS}" =~ (test|all) ]] && DO_TESTS=1
60
61 BAZEL_BIN=${2:-"bazel-bin/src/bazel"}
Damien Martin-Guillerez4f892812015-07-02 08:38:58 +000062 BAZEL_SUM=${3:-"x"}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010063}
64
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000065parse_options "${@}"
Kristina Chodorow8c6b3bb2015-04-09 19:05:34 +000066
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000067mkdir -p output
68: ${BAZEL:=${2-}}
Kristina Chodorowac271142015-03-06 16:00:46 +000069
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000070#
71# Create an initial binary so we can host ourself
72#
73if [ ! -x "${BAZEL}" ]; then
74 display "$INFO You can skip this first step by providing a path to the bazel binary as second argument:"
75 display "$INFO $0 ${COMMANDS} /path/to/bazel"
76 new_step 'Building Bazel from scratch'
77 source scripts/bootstrap/compile.sh
78 cp ${OUTPUT_DIR}/bazel output/bazel
79 BAZEL=$(pwd)/output/bazel
Daniel Wagner-Hall13ba3312015-03-20 05:45:45 +000080fi
81
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000082#
83# Bootstrap bazel using the previous bazel binary = release binary
84#
Damien Martin-Guillereza377af02015-06-24 12:04:27 +000085if [ "${EMBED_LABEL-x}" = "x" ]; then
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000086 # Add a default label when unspecified
87 git_sha1=$(git_sha1)
88 EMBED_LABEL="head (@${git_sha1:-non-git})"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010089fi
90
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000091source scripts/bootstrap/bootstrap.sh
92if [ $DO_COMPILE ]; then
93 new_step 'Building Bazel with Bazel'
94 display "."
95 bazel_bootstrap //src:bazel output/bazel 0755 1
96 BAZEL=$(pwd)/output/bazel
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010097fi
98
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000099#
100# Bootstrap tools using the release binary
101#
102if [ $DO_TOOLS_COMPILATION ]; then
103 new_step 'Building Bazel tools'
Googlera4211f32015-07-14 18:02:00 +0000104 bazel_bootstrap //third_party/ijar:ijar tools/jdk/ijar 0755
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000105 bazel_bootstrap //src/java_tools/singlejar:SingleJar_deploy.jar \
106 tools/jdk/SingleJar_deploy.jar
107 bazel_bootstrap //src/java_tools/buildjar:JavaBuilder_deploy.jar \
108 tools/jdk/JavaBuilder_deploy.jar
Alex Humeskyd3f7eda2015-07-08 18:18:33 +0000109 bazel_bootstrap //src/java_tools/buildjar/java/com/google/devtools/build/buildjar/genclass:GenClass_deploy.jar \
110 tools/jdk/GenClass_deploy.jar
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000111 if [[ $PLATFORM == "darwin" ]]; then
Googler46e80572015-08-03 18:23:11 +0000112 bazel_bootstrap //src/tools/xcode-common/java/com/google/devtools/build/xcode/actoolzip:actoolzip_deploy.jar \
113 tools/objc/precomp_actoolzip_deploy.jar
Chris Parsonsb47cb492015-08-07 00:23:00 +0000114 bazel_bootstrap //src/tools/xcode/ibtoolwrapper:ibtoolwrapper tools/objc/ibtoolwrapper.sh 0755
Googler46e80572015-08-03 18:23:11 +0000115 bazel_bootstrap //src/tools/xcode-common/java/com/google/devtools/build/xcode/swiftstdlibtoolzip:swiftstdlibtoolzip_deploy.jar \
116 tools/objc/precomp_swiftstdlibtoolzip_deploy.jar
117 bazel_bootstrap //src/objc_tools/momczip:momczip_deploy.jar \
118 tools/objc/precomp_momczip_deploy.jar
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000119 bazel_bootstrap //src/objc_tools/bundlemerge:bundlemerge_deploy.jar \
120 tools/objc/precomp_bundlemerge_deploy.jar
121 bazel_bootstrap //src/objc_tools/plmerge:plmerge_deploy.jar \
122 tools/objc/precomp_plmerge_deploy.jar
123 bazel_bootstrap //src/objc_tools/xcodegen:xcodegen_deploy.jar \
124 tools/objc/precomp_xcodegen_deploy.jar
Kristina Chodorow5b378db2015-08-20 15:49:46 +0000125 if xcodebuild -showsdks 2> /dev/null | grep -q '\-sdk iphonesimulator'; then
126 bazel_bootstrap //src/tools/xcode/stdredirect:StdRedirect.dylib \
127 tools/objc/StdRedirect.dylib 0755
128 fi
Googlera4211f32015-07-14 18:02:00 +0000129 bazel_bootstrap //src/tools/xcode/realpath:realpath tools/objc/realpath 0755
Anastasios Kakalis27404a92015-09-15 10:36:26 +0000130 bazel_bootstrap //src/tools/xcode/environment:environment_plist \
131 tools/objc/environment_plist.sh 0755
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000132 fi
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100133fi
134
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000135#
136# Output is deterministic between two bootstrapped bazel binary using the actual tools and the
137# released binary.
138#
139if [ $DO_CHECKSUM ]; then
140 new_step "Determinism test"
Damien Martin-Guillerez4349f942015-06-24 13:28:08 +0000141 if [ ! -f ${BAZEL_SUM:-x} ]; then
142 BAZEL_SUM=bazel-out/bazel_checksum
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000143 log "First build"
144 bootstrap_test ${BAZEL} ${BAZEL_SUM}
145 else
146 BOOTSTRAP=${BAZEL}
147 fi
148 if [ "${BAZEL_SUM}" != "${OUTPUT_DIR}/bazel_checksum" ]; then
149 cp ${BAZEL_SUM} ${OUTPUT_DIR}/bazel_checksum
150 fi
151 if [ $DO_FULL_CHECKSUM ]; then
152 log "Second build"
153 bootstrap_test ${BOOTSTRAP} bazel-out/bazel_checksum
154 log "Comparing output"
155 (diff -U 0 ${OUTPUT_DIR}/bazel_checksum bazel-out/bazel_checksum >&2) \
156 || fail "Differences detected in outputs!"
157 fi
158fi
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100159
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000160#
161# Tests
162#
163if [ $DO_TESTS ]; then
164 new_step "Running tests"
165 display "."
Lukacs Berki678ba232015-09-03 13:28:55 +0000166
Damien Martin-Guillerezb81f90c2015-09-04 15:52:26 +0000167 ndk_target="$(get_bind_target //external:android_ndk_for_testing)"
168 sdk_target="$(get_bind_target //external:android_sdk_for_testing)"
169 if [ "$ndk_target" = "//:dummy" -o "$sdk_target" = "//:dummy" ]; then
Lukacs Berki678ba232015-09-03 13:28:55 +0000170 display "$WARNING Android SDK or NDK are not set in the WORKSPACE file. Android tests will not be run."
171 fi
172
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000173 [ -n "$JAVAC_VERSION" ] || get_java_version
174 if [[ ! "${BAZEL_TEST_FILTERS-}" =~ "-jdk8" ]] \
175 && [ "8" -gt ${JAVAC_VERSION#*.} ]; then
176 display "$WARNING Your version of Java is lower than 1.8!"
177 display "$WARNING Deactivating Java 8 tests, please use a JDK 8 to fully"
178 display "$WARNING test Bazel."
179 if [ -n "${BAZEL_TEST_FILTERS-}" ]; then
180 BAZEL_TEST_FILTERS="${BAZEL_TEST_FILTERS},-jdk8"
181 else
182 BAZEL_TEST_FILTERS="-jdk8"
183 fi
184 fi
Damien Martin-Guillerez50d124b2015-06-24 15:20:09 +0000185 $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc test \
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000186 --test_tag_filters="${BAZEL_TEST_FILTERS-}" \
Damien Martin-Guillerez7cf300e2015-06-16 12:31:03 +0000187 --build_tests_only \
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000188 --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000189 -k --test_output=errors //src/... //third_party/ijar/... //scripts/... \
190 || fail "Tests failed"
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000191fi
192
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000193#
194# Setup the base workspace
195#
196if [ $DO_BASE_WORKSPACE_INIT ]; then
197 new_step 'Setting up base workspace'
198 display "."
199 source scripts/bootstrap/init_workspace.sh
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000200fi
201
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000202clear_log
203display "Build successful! Binary is here: ${BAZEL}"