John Cater | 0bf1413 | 2018-03-28 13:55:44 -0700 | [diff] [blame] | 1 | #!/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 | |
| 18 | set -eu |
| 19 | |
| 20 | USAGE="$0 [<bazel arguments>...]" |
| 21 | DESCRIPTION=' |
| 22 | Rebuilds a development version of Bazel, if necessary, and then runs the |
| 23 | given Bazel command using that binary.' |
| 24 | |
| 25 | function 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. |
| 32 | BAZEL_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. |
| 35 | BAZEL_DIR=${BAZEL_DIR:-$HOME/os-bazel} |
| 36 | # Bazel to use to build local bazel binaries. |
| 37 | BAZEL_BINARY=${BAZEL_BINARY:-$(which bazel)} |
| 38 | |
| 39 | # The location of the resulting binary. |
Jingwen Chen | 7c15c03 | 2020-01-16 11:54:44 -0800 | [diff] [blame] | 40 | BAZEL_DEV="$BAZEL_DIR/bazel-bin/src/bazel-dev" |
John Cater | 0bf1413 | 2018-03-28 13:55:44 -0700 | [diff] [blame] | 41 | |
| 42 | # First, check whether a rebuild is needed. |
| 43 | REBUILD=0 |
| 44 | # If there is no built development Bazel, build it. |
| 45 | if [ ! -x "$BAZEL_DEV" ]; then |
| 46 | REBUILD=1 |
| 47 | fi |
| 48 | # If the current directory isn't the bazel working dir, always try to rebuild. |
| 49 | if [ "$(pwd)" != "$BAZEL_DIR" ]; then |
| 50 | REBUILD=1 |
| 51 | fi |
| 52 | |
| 53 | # Perform a rebuild. |
| 54 | if [ "$REBUILD" == 1 ]; then |
| 55 | echo -e "\033[31mBuilding dev version of bazel...\033[0m" |
| 56 | ( |
| 57 | cd "$BAZEL_DIR" |
| 58 | result=0 |
Jingwen Chen | 7c15c03 | 2020-01-16 11:54:44 -0800 | [diff] [blame] | 59 | ${BAZEL_BINARY} build //src:bazel-dev || result=$? |
John Cater | 0bf1413 | 2018-03-28 13:55:44 -0700 | [diff] [blame] | 60 | if [[ $result != 0 ]]; then |
| 61 | echo -e "\033[31mError building dev version of bazel.\033[0m" |
| 62 | exit $result |
| 63 | fi |
| 64 | ) |
| 65 | fi |
| 66 | |
| 67 | # Execute bazel command. |
| 68 | echo -e "\e[31mExecuting bazel-dev...\e[0m" |
| 69 | exec $BAZEL_DEV "$@" |
| 70 | |