blob: 3560187b299170545f413eb245764ee050ea65e1 [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001#! /bin/bash
2
Damien Martin-Guillerezf90b4ac2015-03-13 16:32:01 +00003# 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
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017set -eu
18
19function usage() {
20 [ -n "${1:-}" ] && echo "Invalid command(s): $1" >&2
21 echo "syntax: $0 command[,command]* [BAZEL_BIN [BAZEL_SUM]]" >&2
22 echo " Available commands: bootstrap, determinism, test, all" >&2
23 exit 1
24}
25
26function parse_options() {
27 local keywords="(bootstrap|test|determinism|all)"
28 [[ "${1:-}" =~ ^$keywords(,$keywords)*$ ]] || usage "$@"
29 DO_BOOTSTRAP=
30 DO_CHECKSUM=
31 DO_TESTS=
32 [[ "$1" =~ (bootstrap|all) ]] && DO_BOOTSTRAP=1
33 [[ "$1" =~ (determinism|all) ]] && DO_CHECKSUM=1
34 [[ "$1" =~ (test|all) ]] && DO_TESTS=1
35
36 BAZEL_BIN=${2:-"bazel-bin/src/bazel"}
37 BAZEL_SUM=${3:-"bazel-out/bazel_checksum"}
38}
39
40PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041if [[ ${PLATFORM} == "darwin" ]]; then
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010042 function md5_file() {
43 md5 $1 | sed 's|^MD5 (\(.*\)) =|\1|'
44 }
45else
46 function md5_file() {
47 md5sum $1
48 }
49fi
50
51function md5_outputs() {
52 [ -n "${BAZEL_TEST_XTRACE:-}" ] && set +x # Avoid garbage in the output
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053 # runfiles/MANIFEST & runfiles_manifest contain absolute path, ignore.
54 # ar on OS-X is non-deterministic, ignore .a files.
Damien Martin-Guillerez1b0a8b32015-03-31 10:15:21 +000055 for i in $(find bazel-bin/ -type f -a \! -name MANIFEST -a \! -name '*.runfiles_manifest' -a \! -name '*.a'); do
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010056 md5_file $i
57 done
58 for i in $(find bazel-genfiles/ -type f); do
59 md5_file $i
60 done
61 [ -n "${BAZEL_TEST_XTRACE:-}" ] && set -x
62}
63
64function get_outputs_sum() {
65 md5_outputs | sort -k 2
66}
67
68function fail() {
69 echo $1 >&2
70 exit 1
71}
72
73function bootstrap() {
74 local BAZEL_BIN=$1
75 local BAZEL_SUM=$2
76 [ -x "${BAZEL_BIN}" ] || fail "syntax: bootstrap bazel-binary"
Damien Martin-Guillereze1349ec2015-03-12 11:00:44 +000077 ${BAZEL_BIN} --blazerc=/dev/null clean || return $?
Kristina Chodorow4fa24ee2015-05-13 17:08:29 +000078 ${BAZEL_BIN} --blazerc=/dev/null fetch //... || return $?
Damien Martin-Guillereze1349ec2015-03-12 11:00:44 +000079 ${BAZEL_BIN} --blazerc=/dev/null build --nostamp //src:bazel //src:tools || return $?
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010080
81 if [ -n "${BAZEL_SUM}" ]; then
82 get_outputs_sum > ${BAZEL_SUM} || return $?
83 fi
84}
85
86function copy_bootstrap() {
87 if [ -z "${BOOTSTRAP:-}" ]; then
88 BOOTSTRAP=$(mktemp /tmp/bootstrap.XXXXXXXXXX)
89 trap '{ rm -f $BOOTSTRAP; }' EXIT
90 cp -f $BAZEL_BIN $BOOTSTRAP
91 chmod +x $BOOTSTRAP
92 fi
93}
94
95function start_test() {
96 echo "****${1//?/*}****"
97 echo "*** $1 ***"
98 echo "****${1//?/*}****"
99}
100
101function end_test() {
102 echo " ==> $1 passed"
103 echo
104 echo
105}
106
107parse_options "$@"
108
109if [ -n "${DO_BOOTSTRAP}" -o ! -x ${BAZEL_BIN} ]; then
110 start_test bootstrap
111 [ -x "output/bazel" ] || ./compile.sh || fail "Compilation failed"
112 bootstrap output/bazel ${BAZEL_SUM} || fail "Bootstrap failed"
113 end_test bootstrap
114fi
115
116# check that bootstrapped binary actually runs correctly
117copy_bootstrap
Han-Wen Nienhuys24e95712015-02-20 13:32:49 +0000118$BOOTSTRAP >/dev/null || fail "Bootstrapped binary is non-functional"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100119
120if [ $DO_CHECKSUM ]; then
121 start_test checksum
122
123 SUM1=$(mktemp /tmp/bootstrap-sum.XXXXXXXXXX)
124 SUM2=$(mktemp /tmp/bootstrap-sum.XXXXXXXXXX)
125
126 trap '{ rm -f $BOOTSTRAP $SUM1 $SUM2; }' EXIT
127 cat $BAZEL_SUM > $SUM1
128
129 # Second run
130 bootstrap $BOOTSTRAP $SUM2 || fail "Bootstrap failed"
131 (diff -U 0 $SUM1 $SUM2 >&2) || fail "Differences detected in outputs!"
132
133 end_test checksum
134fi
135
136if [ $DO_TESTS ]; then
137 start_test "test"
Han-Wen Nienhuys24e95712015-02-20 13:32:49 +0000138
Damien Martin-Guillereze1349ec2015-03-12 11:00:44 +0000139 $BOOTSTRAP --blazerc=/dev/null test -k --test_output=errors //src/... || fail "Tests failed"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100140 end_test "test"
141fi
142
143echo "Bootstrap tests succeeded (tested: $1)"