1#!/bin/bash 2# Copyright 2019 The gRPC Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16 17# Keeping up with Bazel's breaking changes is currently difficult. 18# This script wraps calling bazel by downloading the currently 19# supported version, and then calling it. This way, we can make sure 20# that running bazel will always get meaningful results, at least 21# until Bazel 1.0 is released. 22# NOTE: This script relies on bazel's feature where //tools/bazel 23# script can be used to hijack "bazel" invocations in given workspace. 24 25set -e 26 27# DISABLE_BAZEL_WRAPPER can be set to eliminate the wrapper logic 28if [ "${DISABLE_BAZEL_WRAPPER}" != "" ] && [ "${OVERRIDE_BAZEL_VERSION}" == "" ] 29then 30 if [ "${BAZEL_REAL}" != "" ] 31 then 32 # use BAZEL_REAL as set by 33 # https://github.com/bazelbuild/bazel/blob/master/scripts/packages/bazel.sh 34 # that originally invoked this script (this is what happens when you 35 # run "bazel" in our workspace) 36 exec -a "$0" "${BAZEL_REAL}" "$@" 37 else 38 # if BAZEL_REAL is not set, just invoke the default system bazel 39 exec bazel "$@" 40 fi 41fi 42 43# Read the bazel version from .bazelversion file in the root of the grpc repository 44BAZELVERSION_FILE_PATH="$(dirname $(readlink -f "$0"))/../.bazelversion" 45VERSION_FROM_BAZELVERSION_FILE=$(cat ${BAZELVERSION_FILE_PATH}) 46VERSION=${OVERRIDE_BAZEL_VERSION:-${VERSION_FROM_BAZELVERSION_FILE}} 47echo "INFO: Running bazel wrapper (see //tools/bazel for details), bazel version $VERSION will be used instead of system-wide bazel installation." >&2 48 49# update tools/update_mirror.sh to populate the mirror with new bazel archives 50BASEURL_MIRROR="https://storage.googleapis.com/grpc-bazel-mirror/github.com/bazelbuild/bazel/releases/download" 51BASEURL="https://github.com/bazelbuild/bazel/releases/download" 52pushd "$(dirname "$0")" >/dev/null 53# bazel binary will be downloaded to GRPC_REPO_ROOT/tools directory by default 54DOWNLOAD_DIR=${OVERRIDE_BAZEL_WRAPPER_DOWNLOAD_DIR:-$(pwd)} 55 56case $(uname -sm) in 57 "Linux x86_64") 58 suffix=linux-x86_64 59 ;; 60 "Linux aarch64") 61 suffix=linux-arm64 62 ;; 63 "Darwin x86_64") 64 suffix=darwin-x86_64 65 ;; 66 "Darwin arm64") 67 suffix=darwin-arm64 68 ;; 69 "MINGW"* | "MSYS_NT"*) 70 suffix=windows-x86_64.exe 71 ;; 72 *) 73 echo "Unsupported architecture: $(uname -sm)" >&2 74 exit 1 75 ;; 76esac 77 78filename="bazel-$VERSION-$suffix" 79filename_abs="${DOWNLOAD_DIR}/${filename}" 80 81if [ ! -x "${filename_abs}" ] ; then 82 # first try to download using mirror, fallback to download from github 83 echo "Downloading bazel, will try URLs: ${BASEURL_MIRROR}/${VERSION}/${filename} ${BASEURL}/${VERSION}/${filename}" >&2 84 curl --fail -L --output "${filename_abs}" "${BASEURL_MIRROR}/${VERSION}/${filename}" || curl --fail -L --output "${filename_abs}" "${BASEURL}/${VERSION}/${filename}" 85 chmod a+x "${filename_abs}" 86fi 87 88popd >/dev/null 89exec "${filename_abs}" "$@" 90