blob: 609031a11df3999810126c242cd64c3e641a7487 [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')"
27CPU_FLAG=""
28if [[ ${PLATFORM} == "darwin" ]]; then
29 CPU_FLAG="--cpu=darwin"
30 function md5_file() {
31 md5 $1 | sed 's|^MD5 (\(.*\)) =|\1|'
32 }
33else
34 function md5_file() {
35 md5sum $1
36 }
37fi
38
39function md5_outputs() {
40 [ -n "${BAZEL_TEST_XTRACE:-}" ] && set +x # Avoid garbage in the output
41 # genproto does not strip out timestamp, let skip it for now
42 # runfiles/MANIFEST & runfiles_manifest contain absolute path, ignore.
43 # ar on OS-X is non-deterministic, ignore .a files.
44 for i in $(find bazel-bin/ -type f -a \! -name 'libproto_*.jar' -a \! -name MANIFEST -a \! -name '*.runfiles_manifest' -a \! -name '*.a'); do
45 md5_file $i
46 done
47 for i in $(find bazel-genfiles/ -type f); do
48 md5_file $i
49 done
50 [ -n "${BAZEL_TEST_XTRACE:-}" ] && set -x
51}
52
53function get_outputs_sum() {
54 md5_outputs | sort -k 2
55}
56
57function fail() {
58 echo $1 >&2
59 exit 1
60}
61
62function bootstrap() {
63 local BAZEL_BIN=$1
64 local BAZEL_SUM=$2
65 [ -x "${BAZEL_BIN}" ] || fail "syntax: bootstrap bazel-binary"
66 ${BAZEL_BIN} --blazerc=/dev/null clean ${CPU_FLAG} || return $?
67 ${BAZEL_BIN} --blazerc=/dev/null build ${CPU_FLAG} --nostamp //src:bazel //src:tools || return $?
68
69 if [ -n "${BAZEL_SUM}" ]; then
70 get_outputs_sum > ${BAZEL_SUM} || return $?
71 fi
72}
73
74function copy_bootstrap() {
75 if [ -z "${BOOTSTRAP:-}" ]; then
76 BOOTSTRAP=$(mktemp /tmp/bootstrap.XXXXXXXXXX)
77 trap '{ rm -f $BOOTSTRAP; }' EXIT
78 cp -f $BAZEL_BIN $BOOTSTRAP
79 chmod +x $BOOTSTRAP
80 fi
81}
82
83function start_test() {
84 echo "****${1//?/*}****"
85 echo "*** $1 ***"
86 echo "****${1//?/*}****"
87}
88
89function end_test() {
90 echo " ==> $1 passed"
91 echo
92 echo
93}
94
95parse_options "$@"
96
97if [ -n "${DO_BOOTSTRAP}" -o ! -x ${BAZEL_BIN} ]; then
98 start_test bootstrap
99 [ -x "output/bazel" ] || ./compile.sh || fail "Compilation failed"
100 bootstrap output/bazel ${BAZEL_SUM} || fail "Bootstrap failed"
101 end_test bootstrap
102fi
103
104# check that bootstrapped binary actually runs correctly
105copy_bootstrap
106$BOOTSTRAP >/dev/null || fail "Boostraped binary is non-functionnal!"
107
108if [ $DO_CHECKSUM ]; then
109 start_test checksum
110
111 SUM1=$(mktemp /tmp/bootstrap-sum.XXXXXXXXXX)
112 SUM2=$(mktemp /tmp/bootstrap-sum.XXXXXXXXXX)
113
114 trap '{ rm -f $BOOTSTRAP $SUM1 $SUM2; }' EXIT
115 cat $BAZEL_SUM > $SUM1
116
117 # Second run
118 bootstrap $BOOTSTRAP $SUM2 || fail "Bootstrap failed"
119 (diff -U 0 $SUM1 $SUM2 >&2) || fail "Differences detected in outputs!"
120
121 end_test checksum
122fi
123
124if [ $DO_TESTS ]; then
125 start_test "test"
126 $BOOTSTRAP --blazerc=/dev/null test ${CPU_FLAG} -k --test_output=errors //src/... || fail "Tests failed"
127 end_test "test"
128fi
129
130echo "Bootstrap tests succeeded (tested: $1)"