blob: c863c83d91a12acfb237ebd9c6c08c1b0b7504c0 [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
Lukacs Berki0caf3322015-07-21 08:47:08 +0000112 bazel_bootstrap //src/tools/xcode-common/java/com/google/devtools/build/xcode/actoolzip:actoolzip_deploy.jar \
113 tools/objc/precomp_actoolzip_deploy.jar
114 bazel_bootstrap //src/tools/xcode/ibtoolwrapper:ibtoolwrapper tools/objc/ibtoolwrapper 0755
115 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
Googlerad26d682015-06-18 17:13:05 +0000125 bazel_bootstrap //src/tools/xcode/stdredirect:StdRedirect.dylib \
Googlera4211f32015-07-14 18:02:00 +0000126 tools/objc/StdRedirect.dylib 0755
127 bazel_bootstrap //src/tools/xcode/realpath:realpath tools/objc/realpath 0755
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000128 fi
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100129fi
130
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000131#
132# Output is deterministic between two bootstrapped bazel binary using the actual tools and the
133# released binary.
134#
135if [ $DO_CHECKSUM ]; then
136 new_step "Determinism test"
Damien Martin-Guillerez4349f942015-06-24 13:28:08 +0000137 if [ ! -f ${BAZEL_SUM:-x} ]; then
138 BAZEL_SUM=bazel-out/bazel_checksum
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000139 log "First build"
140 bootstrap_test ${BAZEL} ${BAZEL_SUM}
141 else
142 BOOTSTRAP=${BAZEL}
143 fi
144 if [ "${BAZEL_SUM}" != "${OUTPUT_DIR}/bazel_checksum" ]; then
145 cp ${BAZEL_SUM} ${OUTPUT_DIR}/bazel_checksum
146 fi
147 if [ $DO_FULL_CHECKSUM ]; then
148 log "Second build"
149 bootstrap_test ${BOOTSTRAP} bazel-out/bazel_checksum
150 log "Comparing output"
151 (diff -U 0 ${OUTPUT_DIR}/bazel_checksum bazel-out/bazel_checksum >&2) \
152 || fail "Differences detected in outputs!"
153 fi
154fi
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100155
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000156#
157# Tests
158#
159if [ $DO_TESTS ]; then
160 new_step "Running tests"
161 display "."
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000162 [ -n "$JAVAC_VERSION" ] || get_java_version
163 if [[ ! "${BAZEL_TEST_FILTERS-}" =~ "-jdk8" ]] \
164 && [ "8" -gt ${JAVAC_VERSION#*.} ]; then
165 display "$WARNING Your version of Java is lower than 1.8!"
166 display "$WARNING Deactivating Java 8 tests, please use a JDK 8 to fully"
167 display "$WARNING test Bazel."
168 if [ -n "${BAZEL_TEST_FILTERS-}" ]; then
169 BAZEL_TEST_FILTERS="${BAZEL_TEST_FILTERS},-jdk8"
170 else
171 BAZEL_TEST_FILTERS="-jdk8"
172 fi
173 fi
Damien Martin-Guillerez50d124b2015-06-24 15:20:09 +0000174 $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc test \
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000175 --test_tag_filters="${BAZEL_TEST_FILTERS-}" \
Damien Martin-Guillerez7cf300e2015-06-16 12:31:03 +0000176 --build_tests_only \
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000177 --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000178 -k --test_output=errors //src/... //third_party/ijar/... //scripts/... \
179 || fail "Tests failed"
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000180fi
181
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000182#
183# Setup the base workspace
184#
185if [ $DO_BASE_WORKSPACE_INIT ]; then
186 new_step 'Setting up base workspace'
187 display "."
188 source scripts/bootstrap/init_workspace.sh
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000189fi
190
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000191clear_log
192display "Build successful! Binary is here: ${BAZEL}"