blob: 43151a171cb8eaaf8e26492a04dc94567e8ed13c [file] [log] [blame]
#!/bin/bash -e
#
# Copyright 2018 The Kythe Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Script to control which Bazel version is used through env variables.
#
# By default, this script just runs /usr/bin/bazel with "$@". The following
# environmental variables allow the caller to switch which Bazel binary is
# executed:
#
# BAZEL: override all other variables; run this binary
# BAZEL_VERSION: Bazel version to execute (e.g. 0.15.2)
# BAZEL_BIN: directory of bazel binaries (e.g. $BAZEL_BIN/bazel-0.15.2)
# DEFAULT_BAZEL: binary to use when BAZEL and BAZEL_VERSION are not set
#
# Examples:
# ./bazel # executes /usr/bin/bazel
# BAZEL_VERSION=0.15.2 ./bazel # executes /usr/bin/bazel-0.15.2
# BAZEL_VERSION=0.15.2 BAZEL_BIN=/opt/bazel ./bazel # executes /opt/bazel/bazel-0.15.2
# BAZEL=/bin/true ./bazel # executes /bin/true
# DEFAULT_BAZEL=/bin/true ./bazel # executes /bin/true
# DEFAULT_BAZEL=/bin/true BAZEL_VERSION=0.15.2 ./bazel # executes /usr/bin/bazel-0.15.2
SCRIPT="$(realpath "$0")"
DEFAULT_BAZEL="${DEFAULT_BAZEL=/usr/bin/bazel}"
BAZEL_BIN="${BAZEL_BIN:-/usr/bin}"
if [[ -n "$BAZEL" ]]; then
exec "$BAZEL" "$@"
fi
if [[ -z "$BAZEL_VERSION" ]]; then
if [[ -n "$DEFAULT_BAZEL" && -x "$DEFAULT_BAZEL" ]]; then
DEFAULT_BAZEL="$(realpath "$DEFAULT_BAZEL")"
if [[ "$DEFAULT_BAZEL" != "$SCRIPT" ]]; then
# Use default bazel version
exec "$DEFAULT_BAZEL" "$@"
fi
fi
# Select latest version available
VERSIONS=($(ls "$BAZEL_BIN"/bazel-* | xargs -L1 basename | sort -t. -h -k1,1 -k2,2 -k3,3))
LATEST="${VERSIONS[*]: -1}"
BAZEL_VERSION="${LATEST#bazel-}"
fi
exec "$BAZEL_BIN/bazel-$BAZEL_VERSION" "$@"