blob: 3c0f7ff2222823de90978d4a416b5a4c5d1c6ab4 [file] [log] [blame]
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +00001#!/bin/bash
2
3# Copyright 2015 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
17# Use bazel to bootstrap various tools
18# Configuration:
19# BAZEL: path to the bazel binary
20# EMBED_LABEL: the label to embed in tools using --embed_label (optional)
21# BAZEL_ARGS: list of other arguments to pass to bazel (optional)
22# BAZELRC: the rc file to use
23
24: ${BAZELRC:="/dev/null"}
25: ${EMBED_LABEL:=""}
26
27EMBED_LABEL_ARG=()
28if [ -n "${EMBED_LABEL}" ]; then
29 EMBED_LABEL_ARG=(--stamp --embed_label "${EMBED_LABEL}")
30fi
31
32: ${JAVA_VERSION:="1.8"}
33: ${BAZEL_ARGS="--singlejar_top=//src/java_tools/singlejar:bootstrap_deploy.jar \
34 --javabuilder_top=//src/java_tools/buildjar:bootstrap_deploy.jar \
Alex Humeskyd3f7eda2015-07-08 18:18:33 +000035 --genclass_top=//src/java_tools/buildjar:bootstrap_genclass_deploy.jar \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000036 --ijar_top=//third_party/ijar"}
37
38function bazel_bootstrap() {
39 local mode=${3:-"0644"}
40 if [[ ! ${BAZEL_SKIP_TOOL_COMPILATION-} =~ "$2" ]]; then
41 log "Building $2"
42 if [ -n "${4-}" ]; then
Damien Martin-Guillerez50d124b2015-06-24 15:20:09 +000043 ${BAZEL} --nomaster_bazelrc --bazelrc=${BAZELRC} \
Damien Martin-Guillerez43b2ea72015-06-15 10:40:07 +000044 build ${BAZEL_ARGS} \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000045 --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
46 "${EMBED_LABEL_ARG[@]}" $1
47 else
Damien Martin-Guillerez50d124b2015-06-24 15:20:09 +000048 run_silent ${BAZEL} --nomaster_bazelrc --bazelrc=${BAZELRC} \
Damien Martin-Guillerez43b2ea72015-06-15 10:40:07 +000049 build ${BAZEL_ARGS} \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000050 --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
51 "${EMBED_LABEL_ARG[@]}" $1
52 fi
53 local file=bazel-bin/${1##//}
54 cp -f ${file/:/\/} $2
55 chmod ${mode} $2
56 fi
57}
58
59function md5_outputs() {
60 [ -n "${BAZEL_TEST_XTRACE:-}" ] && set +x # Avoid garbage in the output
61 # runfiles/MANIFEST & runfiles_manifest contain absolute path, ignore.
62 # ar on OS-X is non-deterministic, ignore .a files.
63 for i in $(find bazel-bin/ -type f -a \! -name MANIFEST -a \! -name '*.runfiles_manifest' -a \! -name '*.a'); do
64 md5_file $i
65 done
66 for i in $(find bazel-genfiles/ -type f); do
67 md5_file $i
68 done
69 [ -n "${BAZEL_TEST_XTRACE:-}" ] && set -x
70}
71
72function get_outputs_sum() {
73 md5_outputs | sort -k 2
74}
75
76function bootstrap_test() {
77 local BAZEL_BIN=$1
78 local BAZEL_SUM=$2
79 [ -x "${BAZEL_BIN}" ] || fail "syntax: bootstrap bazel-binary"
Damien Martin-Guillerez50d124b2015-06-24 15:20:09 +000080 run_silent ${BAZEL_BIN} --nomaster_bazelrc --bazelrc=${BAZELRC} clean \
Damien Martin-Guillerez841751e2015-06-19 12:20:48 +000081 --expunge || return $?
Damien Martin-Guillerez50d124b2015-06-24 15:20:09 +000082 run_silent ${BAZEL_BIN} --nomaster_bazelrc --bazelrc=${BAZELRC} build \
Damien Martin-Guillerez43b2ea72015-06-15 10:40:07 +000083 --fetch --nostamp \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000084 --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
85 //src:bazel //src:tools || return $?
86 if [ -n "${BAZEL_SUM}" ]; then
87 cat bazel-genfiles/src/java.version >${BAZEL_SUM}
88 get_outputs_sum >> ${BAZEL_SUM} || return $?
89 fi
90 if [ -z "${BOOTSTRAP:-}" ]; then
91 tempdir
92 BOOTSTRAP=${NEW_TMPDIR}/bazel
93 cp -f bazel-bin/src/bazel $BOOTSTRAP
94 chmod +x $BOOTSTRAP
95 fi
96}