blob: 304814f880599b3756c7a239162585677bd7b876 [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001#! /bin/bash
2
3set -eu
4
5function usage() {
6 [ -n "${1:-}" ] && echo "Invalid command(s): $1" >&2
7 echo "syntax: $0 command[,command]* [BAZEL_BIN [BAZEL_SUM]]" >&2
8 echo " Available commands: bootstrap, determinism, test, all" >&2
9 exit 1
10}
11
12function parse_options() {
13 local keywords="(bootstrap|test|determinism|all)"
14 [[ "${1:-}" =~ ^$keywords(,$keywords)*$ ]] || usage "$@"
15 DO_BOOTSTRAP=
16 DO_CHECKSUM=
17 DO_TESTS=
18 [[ "$1" =~ (bootstrap|all) ]] && DO_BOOTSTRAP=1
19 [[ "$1" =~ (determinism|all) ]] && DO_CHECKSUM=1
20 [[ "$1" =~ (test|all) ]] && DO_TESTS=1
21
22 BAZEL_BIN=${2:-"bazel-bin/src/bazel"}
23 BAZEL_SUM=${3:-"bazel-out/bazel_checksum"}
24}
25
26PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027if [[ ${PLATFORM} == "darwin" ]]; then
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028 function md5_file() {
29 md5 $1 | sed 's|^MD5 (\(.*\)) =|\1|'
30 }
31else
32 function md5_file() {
33 md5sum $1
34 }
35fi
36
37function md5_outputs() {
38 [ -n "${BAZEL_TEST_XTRACE:-}" ] && set +x # Avoid garbage in the output
39 # genproto does not strip out timestamp, let skip it for now
40 # runfiles/MANIFEST & runfiles_manifest contain absolute path, ignore.
41 # ar on OS-X is non-deterministic, ignore .a files.
42 for i in $(find bazel-bin/ -type f -a \! -name 'libproto_*.jar' -a \! -name MANIFEST -a \! -name '*.runfiles_manifest' -a \! -name '*.a'); do
43 md5_file $i
44 done
45 for i in $(find bazel-genfiles/ -type f); do
46 md5_file $i
47 done
48 [ -n "${BAZEL_TEST_XTRACE:-}" ] && set -x
49}
50
51function get_outputs_sum() {
52 md5_outputs | sort -k 2
53}
54
55function fail() {
56 echo $1 >&2
57 exit 1
58}
59
60function bootstrap() {
61 local BAZEL_BIN=$1
62 local BAZEL_SUM=$2
63 [ -x "${BAZEL_BIN}" ] || fail "syntax: bootstrap bazel-binary"
Damien Martin-Guillereze1349ec2015-03-12 11:00:44 +000064 ${BAZEL_BIN} --blazerc=/dev/null clean || return $?
65 ${BAZEL_BIN} --blazerc=/dev/null build --nostamp //src:bazel //src:tools || return $?
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010066
67 if [ -n "${BAZEL_SUM}" ]; then
68 get_outputs_sum > ${BAZEL_SUM} || return $?
69 fi
70}
71
72function copy_bootstrap() {
73 if [ -z "${BOOTSTRAP:-}" ]; then
74 BOOTSTRAP=$(mktemp /tmp/bootstrap.XXXXXXXXXX)
75 trap '{ rm -f $BOOTSTRAP; }' EXIT
76 cp -f $BAZEL_BIN $BOOTSTRAP
77 chmod +x $BOOTSTRAP
78 fi
79}
80
81function start_test() {
82 echo "****${1//?/*}****"
83 echo "*** $1 ***"
84 echo "****${1//?/*}****"
85}
86
87function end_test() {
88 echo " ==> $1 passed"
89 echo
90 echo
91}
92
93parse_options "$@"
94
95if [ -n "${DO_BOOTSTRAP}" -o ! -x ${BAZEL_BIN} ]; then
96 start_test bootstrap
97 [ -x "output/bazel" ] || ./compile.sh || fail "Compilation failed"
98 bootstrap output/bazel ${BAZEL_SUM} || fail "Bootstrap failed"
99 end_test bootstrap
100fi
101
102# check that bootstrapped binary actually runs correctly
103copy_bootstrap
Han-Wen Nienhuys24e95712015-02-20 13:32:49 +0000104$BOOTSTRAP >/dev/null || fail "Bootstrapped binary is non-functional"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100105
106if [ $DO_CHECKSUM ]; then
107 start_test checksum
108
109 SUM1=$(mktemp /tmp/bootstrap-sum.XXXXXXXXXX)
110 SUM2=$(mktemp /tmp/bootstrap-sum.XXXXXXXXXX)
111
112 trap '{ rm -f $BOOTSTRAP $SUM1 $SUM2; }' EXIT
113 cat $BAZEL_SUM > $SUM1
114
115 # Second run
116 bootstrap $BOOTSTRAP $SUM2 || fail "Bootstrap failed"
117 (diff -U 0 $SUM1 $SUM2 >&2) || fail "Differences detected in outputs!"
118
119 end_test checksum
120fi
121
122if [ $DO_TESTS ]; then
123 start_test "test"
Han-Wen Nienhuys24e95712015-02-20 13:32:49 +0000124
Damien Martin-Guillereze1349ec2015-03-12 11:00:44 +0000125 $BOOTSTRAP --blazerc=/dev/null test -k --test_output=errors //src/... || fail "Tests failed"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100126 end_test "test"
127fi
128
129echo "Bootstrap tests succeeded (tested: $1)"