blob: d08489019c9f163c8b2755c437c3923990e39748 [file] [log] [blame]
John Cater0bf14132018-03-28 13:55:44 -07001#!/bin/bash
2#
3# Copyright 2018 The Bazel Authors. 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
18set -eu
19
20USAGE="$0 [<bazel arguments>...]"
21DESCRIPTION='
22 Rebuilds a development version of Bazel, if necessary, and then runs the
23 given Bazel command using that binary.'
24
25function usage() {
26 echo "$USAGE" "$DESCRIPTION" >&2
27}
28
29# Configuration params. Export these in your bashrc to set personal defaults.
30
31# The source of Bazel code.
32BAZEL_REPO=${BAZEL_REPO:-https://github.com/bazelbuild/bazel}
33# Where to keep the Bazel repository. If you make changes here, be warned that
34# this script may overwrite or lose them.
35BAZEL_DIR=${BAZEL_DIR:-$HOME/os-bazel}
36# Bazel to use to build local bazel binaries.
37BAZEL_BINARY=${BAZEL_BINARY:-$(which bazel)}
38
39# The location of the resulting binary.
Jingwen Chen7c15c032020-01-16 11:54:44 -080040BAZEL_DEV="$BAZEL_DIR/bazel-bin/src/bazel-dev"
John Cater0bf14132018-03-28 13:55:44 -070041
42# First, check whether a rebuild is needed.
43REBUILD=0
44# If there is no built development Bazel, build it.
45if [ ! -x "$BAZEL_DEV" ]; then
46 REBUILD=1
47fi
48# If the current directory isn't the bazel working dir, always try to rebuild.
49if [ "$(pwd)" != "$BAZEL_DIR" ]; then
50 REBUILD=1
51fi
52
53# Perform a rebuild.
54if [ "$REBUILD" == 1 ]; then
55 echo -e "\033[31mBuilding dev version of bazel...\033[0m"
56 (
57 cd "$BAZEL_DIR"
58 result=0
Jingwen Chen7c15c032020-01-16 11:54:44 -080059 ${BAZEL_BINARY} build //src:bazel-dev || result=$?
John Cater0bf14132018-03-28 13:55:44 -070060 if [[ $result != 0 ]]; then
61 echo -e "\033[31mError building dev version of bazel.\033[0m"
62 exit $result
63 fi
64 )
65fi
66
67# Execute bazel command.
68echo -e "\e[31mExecuting bazel-dev...\e[0m"
69exec $BAZEL_DEV "$@"
70