blob: 3b986abb626320f28adab8439e8006cfe8390fc6 [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.
Matt Stark45820112024-06-04 09:05:51 -070035PARENT_DIR="$(dirname "$(dirname "${BASH_SOURCE[0]}")")"
36BAZEL_DIR=${BAZEL_DIR:-${PARENT_DIR}}
John Cater0bf14132018-03-28 13:55:44 -070037# Bazel to use to build local bazel binaries.
38BAZEL_BINARY=${BAZEL_BINARY:-$(which bazel)}
39
40# The location of the resulting binary.
Jingwen Chen7c15c032020-01-16 11:54:44 -080041BAZEL_DEV="$BAZEL_DIR/bazel-bin/src/bazel-dev"
John Cater0bf14132018-03-28 13:55:44 -070042
43# First, check whether a rebuild is needed.
44REBUILD=0
45# If there is no built development Bazel, build it.
46if [ ! -x "$BAZEL_DEV" ]; then
47 REBUILD=1
48fi
49# If the current directory isn't the bazel working dir, always try to rebuild.
50if [ "$(pwd)" != "$BAZEL_DIR" ]; then
51 REBUILD=1
52fi
53
54# Perform a rebuild.
55if [ "$REBUILD" == 1 ]; then
56 echo -e "\033[31mBuilding dev version of bazel...\033[0m"
57 (
58 cd "$BAZEL_DIR"
59 result=0
Jingwen Chen7c15c032020-01-16 11:54:44 -080060 ${BAZEL_BINARY} build //src:bazel-dev || result=$?
John Cater0bf14132018-03-28 13:55:44 -070061 if [[ $result != 0 ]]; then
62 echo -e "\033[31mError building dev version of bazel.\033[0m"
63 exit $result
64 fi
65 )
66fi
67
68# Execute bazel command.
69echo -e "\e[31mExecuting bazel-dev...\e[0m"
70exec $BAZEL_DEV "$@"
71