Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 3 | # Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 17 | # 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 Mankin | 5cafe13 | 2015-05-28 20:20:56 +0000 | [diff] [blame] | 22 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | set -o errexit |
| 24 | |
Laszlo Csomor | f5525e8 | 2017-01-30 18:11:58 +0000 | [diff] [blame] | 25 | # Correct PATH on Windows, to avoid using "FIND.EXE" instead of "/usr/bin/find" |
| 26 | # etc, leading to confusing errors. |
| 27 | export BAZEL_OLD_PATH=$PATH |
| 28 | case "$(uname -s | tr [:upper:] [:lower:])" in |
Laszlo Csomor | d0d7ef0 | 2017-04-26 10:48:00 +0200 | [diff] [blame] | 29 | msys*|mingw*|cygwin*) |
Laszlo Csomor | f5525e8 | 2017-01-30 18:11:58 +0000 | [diff] [blame] | 30 | # 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" |
| 35 | esac |
| 36 | |
| 37 | # Check that the bintools can be found, otherwise we would see very confusing |
| 38 | # error messages. |
| 39 | which [ >&/dev/null || { |
| 40 | echo >&2 "ERROR: cannot locate GNU bintools; check your PATH." |
| 41 | echo >&2 " (You may need to run 'export PATH=/bin:/usr/bin:\$PATH)'" |
| 42 | exit 1 |
| 43 | } |
| 44 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | cd "$(dirname "$0")" |
| 46 | |
Julio Merino | 5909d9d | 2016-02-25 21:44:31 +0000 | [diff] [blame] | 47 | # 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-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 52 | source scripts/bootstrap/buildenv.sh |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 54 | function usage() { |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 55 | [ -n "${1:-compile}" ] && echo "Invalid command(s): $1" >&2 |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 56 | echo "syntax: $0 [command[,command]* [BAZEL_BIN [BAZEL_SUM]]]" >&2 |
| 57 | echo " General purpose commands:" >&2 |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 58 | echo " compile = compile the bazel binary (default)" >&2 |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 59 | echo " Commands for developers:" >&2 |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 60 | echo " all = compile,determinism,test" >&2 |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 61 | echo " determinism = test for stability of Bazel builds" >&2 |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 62 | echo " srcs = test that //:srcs contains all the sources" >&2 |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 63 | echo " test = run the full test suite of Bazel" >&2 |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | exit 1 |
| 65 | } |
| 66 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 67 | function parse_options() { |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 68 | local keywords="(compile|all|determinism|bootstrap|srcs|test)" |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 69 | COMMANDS="${1:-compile}" |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 70 | [[ "${COMMANDS}" =~ ^$keywords(,$keywords)*$ ]] || usage "$@" |
| 71 | DO_COMPILE= |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 72 | DO_CHECKSUM= |
| 73 | DO_FULL_CHECKSUM=1 |
| 74 | DO_TESTS= |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 75 | DO_SRCS_TEST= |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 76 | [[ "${COMMANDS}" =~ (compile|all) ]] && DO_COMPILE=1 |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 77 | [[ "${COMMANDS}" =~ (bootstrap|determinism|all) ]] && DO_CHECKSUM=1 |
| 78 | [[ "${COMMANDS}" =~ (bootstrap) ]] && DO_FULL_CHECKSUM= |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 79 | [[ "${COMMANDS}" =~ (srcs|all) ]] && DO_SRCS_TEST=1 |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 80 | [[ "${COMMANDS}" =~ (test|all) ]] && DO_TESTS=1 |
| 81 | |
| 82 | BAZEL_BIN=${2:-"bazel-bin/src/bazel"} |
Damien Martin-Guillerez | 4f89281 | 2015-07-02 08:38:58 +0000 | [diff] [blame] | 83 | BAZEL_SUM=${3:-"x"} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 86 | parse_options "${@}" |
Kristina Chodorow | 8c6b3bb | 2015-04-09 19:05:34 +0000 | [diff] [blame] | 87 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 88 | mkdir -p output |
| 89 | : ${BAZEL:=${2-}} |
Kristina Chodorow | ac27114 | 2015-03-06 16:00:46 +0000 | [diff] [blame] | 90 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 91 | # |
| 92 | # Create an initial binary so we can host ourself |
| 93 | # |
| 94 | if [ ! -x "${BAZEL}" ]; then |
| 95 | display "$INFO You can skip this first step by providing a path to the bazel binary as second argument:" |
| 96 | display "$INFO $0 ${COMMANDS} /path/to/bazel" |
| 97 | new_step 'Building Bazel from scratch' |
| 98 | source scripts/bootstrap/compile.sh |
Kristina Chodorow | dccd81f | 2016-02-01 17:42:07 +0000 | [diff] [blame] | 99 | # The DO_COMPILE flow will actually create the bazel binary and set BAZEL. |
| 100 | DO_COMPILE=1 |
Daniel Wagner-Hall | 13ba331 | 2015-03-20 05:45:45 +0000 | [diff] [blame] | 101 | fi |
| 102 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 103 | # |
| 104 | # Bootstrap bazel using the previous bazel binary = release binary |
| 105 | # |
Damien Martin-Guillerez | a377af0 | 2015-06-24 12:04:27 +0000 | [diff] [blame] | 106 | if [ "${EMBED_LABEL-x}" = "x" ]; then |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 107 | # Add a default label when unspecified |
| 108 | git_sha1=$(git_sha1) |
Damien Martin-Guillerez | 28e67b5 | 2016-03-14 11:01:31 +0000 | [diff] [blame] | 109 | EMBED_LABEL="$(get_last_version) (@${git_sha1:-non-git})" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | fi |
| 111 | |
Damien Martin-Guillerez | 3d796fe | 2016-01-11 10:07:57 +0000 | [diff] [blame] | 112 | if [[ $PLATFORM == "darwin" ]] && \ |
| 113 | xcodebuild -showsdks 2> /dev/null | grep -q '\-sdk iphonesimulator'; then |
Damien Martin-Guillerez | 1540d8f | 2016-09-07 11:49:39 +0000 | [diff] [blame] | 114 | EXTRA_BAZEL_ARGS="${EXTRA_BAZEL_ARGS-} --define IPHONE_SDK=1" |
Dmitry Lomov | ac6ed79 | 2015-12-22 19:24:00 +0000 | [diff] [blame] | 115 | fi |
Yun Peng | cb8a5e2 | 2017-03-08 14:30:49 +0000 | [diff] [blame] | 116 | |
Damien Martin-Guillerez | 3d796fe | 2016-01-11 10:07:57 +0000 | [diff] [blame] | 117 | source scripts/bootstrap/bootstrap.sh |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | |
Lukacs Berki | 0d5d522 | 2015-10-12 13:24:44 +0000 | [diff] [blame] | 119 | if [ $DO_COMPILE ]; then |
| 120 | new_step 'Building Bazel with Bazel' |
| 121 | display "." |
Damien Martin-Guillerez | 50fce86 | 2016-01-18 11:30:10 +0000 | [diff] [blame] | 122 | log "Building output/bazel" |
Laszlo Csomor | 9f7180f | 2016-09-27 13:07:43 +0000 | [diff] [blame] | 123 | bazel_build "src:bazel${EXE_EXT}" \ |
| 124 | || fail "Could not build Bazel" |
| 125 | bazel_bin_path="$(get_bazel_bin_path)/src/bazel${EXE_EXT}" |
| 126 | [ -e "$bazel_bin_path" ] \ |
| 127 | || fail "Could not find freshly built Bazel binary at '$bazel_bin_path'" |
| 128 | cp -f "$bazel_bin_path" "output/bazel${EXE_EXT}" \ |
| 129 | || fail "Could not copy '$bazel_bin_path' to 'output/bazel${EXE_EXT}'" |
Dmitry Lomov | 9d40a60 | 2016-02-15 16:15:03 +0000 | [diff] [blame] | 130 | chmod 0755 "output/bazel${EXE_EXT}" |
| 131 | BAZEL="$(pwd)/output/bazel${EXE_EXT}" |
Lukacs Berki | 0d5d522 | 2015-10-12 13:24:44 +0000 | [diff] [blame] | 132 | fi |
| 133 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 134 | # |
| 135 | # Output is deterministic between two bootstrapped bazel binary using the actual tools and the |
| 136 | # released binary. |
| 137 | # |
| 138 | if [ $DO_CHECKSUM ]; then |
| 139 | new_step "Determinism test" |
Damien Martin-Guillerez | 4349f94 | 2015-06-24 13:28:08 +0000 | [diff] [blame] | 140 | if [ ! -f ${BAZEL_SUM:-x} ]; then |
| 141 | BAZEL_SUM=bazel-out/bazel_checksum |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 142 | log "First build" |
| 143 | bootstrap_test ${BAZEL} ${BAZEL_SUM} |
| 144 | else |
| 145 | BOOTSTRAP=${BAZEL} |
| 146 | fi |
| 147 | if [ "${BAZEL_SUM}" != "${OUTPUT_DIR}/bazel_checksum" ]; then |
| 148 | cp ${BAZEL_SUM} ${OUTPUT_DIR}/bazel_checksum |
| 149 | fi |
| 150 | if [ $DO_FULL_CHECKSUM ]; then |
| 151 | log "Second build" |
| 152 | bootstrap_test ${BOOTSTRAP} bazel-out/bazel_checksum |
| 153 | log "Comparing output" |
| 154 | (diff -U 0 ${OUTPUT_DIR}/bazel_checksum bazel-out/bazel_checksum >&2) \ |
| 155 | || fail "Differences detected in outputs!" |
| 156 | fi |
| 157 | fi |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 158 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 159 | # |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 160 | # Test that //:srcs contains all the sources |
| 161 | # |
| 162 | if [ $DO_SRCS_TEST ]; then |
| 163 | new_step "Checking that //:srcs contains all the sources" |
| 164 | log "Querying //:srcs" |
aehlig | 5ede79a | 2017-07-31 17:45:47 +0200 | [diff] [blame] | 165 | ${BAZEL} query 'kind("source file", deps(//:srcs))' \ |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 166 | | grep -v '^@' \ |
Ulf Adams | 161c4a3 | 2016-12-05 17:25:21 +0000 | [diff] [blame] | 167 | | sed -e 's|^//||' | sed -e 's|^:||' | sed -e 's|:|/|' \ |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 168 | | sort -u >"${OUTPUT_DIR}/srcs-query" |
| 169 | |
| 170 | log "Finding all files" |
Damien Martin-Guillerez | ca407f0 | 2016-07-04 13:07:08 +0000 | [diff] [blame] | 171 | # SRCS_EXCLUDES can be overriden to adds some more exceptions for the find |
| 172 | # commands (for CI systems). |
| 173 | SRCS_EXCLUDES=${SRCS_EXCLUDES-XXXXXXXXXXXXXX1268778dfsdf4} |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 174 | # See file BUILD for the list of grep -v exceptions. |
| 175 | # tools/defaults package is hidden by Bazel so cannot be put in the srcs. |
Ulf Adams | 161c4a3 | 2016-12-05 17:25:21 +0000 | [diff] [blame] | 176 | find . -type f | sed -e 's|./||' \ |
Adam Michael | 9b7330f | 2017-03-23 18:40:51 +0000 | [diff] [blame] | 177 | | grep -v '^bazel-' \ |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 178 | | grep -v '^\.' | grep -v '^out/' | grep -v '^output/' \ |
Klaus Aehlig | 0a05086 | 2016-11-11 17:26:44 +0000 | [diff] [blame] | 179 | | grep -v '^derived' \ |
Damien Martin-Guillerez | ca407f0 | 2016-07-04 13:07:08 +0000 | [diff] [blame] | 180 | | grep -Ev "${SRCS_EXCLUDES}" \ |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 181 | | grep -v '^tools/defaults/BUILD' \ |
| 182 | | sort -u >"${OUTPUT_DIR}/srcs-find" |
| 183 | |
| 184 | log "Diffing" |
Ulf Adams | 161c4a3 | 2016-12-05 17:25:21 +0000 | [diff] [blame] | 185 | res="$(diff -U 0 "${OUTPUT_DIR}/srcs-find" "${OUTPUT_DIR}/srcs-query" | sed -e 's|^-||' | grep -Ev '^(@@|\+\+|--)' || true)" |
Damien Martin-Guillerez | 7d265e0 | 2016-07-01 13:33:48 +0000 | [diff] [blame] | 186 | |
| 187 | if [ -n "${res}" ]; then |
| 188 | fail "//:srcs filegroup do not contains all the sources, missing: |
| 189 | ${res}" |
| 190 | fi |
| 191 | fi |
| 192 | |
| 193 | # |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 194 | # Tests |
| 195 | # |
| 196 | if [ $DO_TESTS ]; then |
| 197 | new_step "Running tests" |
| 198 | display "." |
Lukacs Berki | 678ba23 | 2015-09-03 13:28:55 +0000 | [diff] [blame] | 199 | |
Damien Martin-Guillerez | b81f90c | 2015-09-04 15:52:26 +0000 | [diff] [blame] | 200 | ndk_target="$(get_bind_target //external:android_ndk_for_testing)" |
| 201 | sdk_target="$(get_bind_target //external:android_sdk_for_testing)" |
Androbin | cfb2ec0 | 2017-06-27 13:47:43 +0200 | [diff] [blame] | 202 | if [ "$ndk_target" = "//:dummy" ] || [ "$sdk_target" = "//:dummy" ]; then |
Lukacs Berki | 678ba23 | 2015-09-03 13:28:55 +0000 | [diff] [blame] | 203 | display "$WARNING Android SDK or NDK are not set in the WORKSPACE file. Android tests will not be run." |
| 204 | fi |
| 205 | |
Damien Martin-Guillerez | 338dc56 | 2015-06-23 17:15:12 +0000 | [diff] [blame] | 206 | [ -n "$JAVAC_VERSION" ] || get_java_version |
Damien Martin-Guillerez | 0ad9f5e | 2016-04-20 13:58:08 +0000 | [diff] [blame] | 207 | if [[ ! "${BAZEL_TEST_FILTERS-}" =~ "-jdk8" ]]; then |
| 208 | if [ "8" -gt ${JAVAC_VERSION#*.} ] || [ "${JAVA_VERSION}" = "1.7" ]; then |
| 209 | display "$WARNING Your version of Java is lower than 1.8!" |
| 210 | display "$WARNING Deactivating Java 8 tests, please use a JDK 8 to fully" |
| 211 | display "$WARNING test Bazel." |
| 212 | if [ -n "${BAZEL_TEST_FILTERS-}" ]; then |
| 213 | BAZEL_TEST_FILTERS="${BAZEL_TEST_FILTERS},-jdk8" |
| 214 | else |
| 215 | BAZEL_TEST_FILTERS="-jdk8" |
| 216 | fi |
Damien Martin-Guillerez | 338dc56 | 2015-06-23 17:15:12 +0000 | [diff] [blame] | 217 | fi |
| 218 | fi |
Klaus Aehlig | 276a8cd | 2016-07-11 12:06:07 +0000 | [diff] [blame] | 219 | $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc \ |
| 220 | ${BAZEL_DIR_STARTUP_OPTIONS} \ |
| 221 | test \ |
Damien Martin-Guillerez | 338dc56 | 2015-06-23 17:15:12 +0000 | [diff] [blame] | 222 | --test_tag_filters="${BAZEL_TEST_FILTERS-}" \ |
Damien Martin-Guillerez | 7cf300e | 2015-06-16 12:31:03 +0000 | [diff] [blame] | 223 | --build_tests_only \ |
Damien Martin-Guillerez | 04d46ab | 2016-04-13 19:27:56 +0000 | [diff] [blame] | 224 | --nolegacy_bazel_java_test \ |
Damien Martin-Guillerez | 0ad9f5e | 2016-04-20 13:58:08 +0000 | [diff] [blame] | 225 | --define JAVA_VERSION=${JAVA_VERSION} \ |
Damien Martin-Guillerez | 3d796fe | 2016-01-11 10:07:57 +0000 | [diff] [blame] | 226 | ${EXTRA_BAZEL_ARGS} \ |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 227 | -k --test_output=errors //src/... //third_party/ijar/... //scripts/... \ |
| 228 | || fail "Tests failed" |
Damien Martin-Guillerez | d6f4808 | 2015-06-05 10:50:43 +0000 | [diff] [blame] | 229 | fi |
| 230 | |
Damien Martin-Guillerez | d47a8ef | 2015-06-10 11:54:50 +0000 | [diff] [blame] | 231 | clear_log |
| 232 | display "Build successful! Binary is here: ${BAZEL}" |