1#!/bin/bash 2# Copyright 2022 The Bazel Authors. All rights reserved. 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 17function DownloadBazel() { 18 # Utility function to download a specified version of bazel to a given 19 # installation directory. 20 # Positional arguments: 21 # ver: The version to install. Supports "latest" (major and minor releases), 22 # "latest-with-prereleases" (all versions from "latest" + prereleases), 23 # major/minor releases such as 5.2.0, and also prereleases such as 24 # 6.0.0-pre.20220720.3. Release candidates with "rc" in the name are NOT 25 # supported. 26 # platform: The platform to install. Currently only "linux" has been 27 # validated. 28 # arch: Architecture to install. Currently only "x86_64" has been validated. 29 # dest: Where to install Bazel. Must be a user-writeable directory, 30 # otherwise the root user must call this function through sudo. 31 # Returns: 32 # Echoes the installation directory at the end of installation. 33 ( 34 set -euxo pipefail 35 # Significantly cribbed from 36 # devtools/kokoro/vanadium/linux_scripts/usr/local/bin/use_bazel.sh 37 # Temporary workaround solution until use_bazel.sh can download prereleases. 38 39 # Positional arguments 40 local ver="$1" 41 local platform="$2" 42 local arch="$3" 43 local dest="$4" 44 45 # Function-local helper variables 46 local gcs_uri="" 47 local revision_identifier="" 48 if [[ "$ver" == "latest" || "$ver" == "latest-with-prereleases" ]]; then 49 # Query binary blob bucket to find the latest prerelease 50 if [[ "$ver" == "latest" ]]; then 51 # Filter out prereleases 52 ver=$(gsutil ls -l gs://bazel/**/*-installer-"${platform}"-"${arch}".sh | grep "gs://" | grep -v rc | grep -v pre | tail -n1 | awk '{print $NF}') 53 else 54 ver=$(gsutil ls -l gs://bazel/**/*-installer-"${platform}"-"${arch}".sh | grep "gs://" | grep -v rc | tail -n1 | awk '{print $NF}') 55 fi 56 ver=$(echo "$ver" | sed -n "s/.*bazel\-\(.*\)\-installer.*/\1/p") 57 fi 58 if [[ "$ver" =~ pre ]]; then 59 revision_identifier=$(echo "$ver" | awk -F"-" '{print $1}') 60 gcs_uri="gs://bazel/${revision_identifier}/rolling/${ver}/bazel-${ver}-installer-${platform}-${arch}.sh" 61 else 62 gcs_uri="gs://bazel/${ver}/release/bazel-${ver}-installer-${platform}-${arch}.sh" 63 fi 64 65 # Download the installer from GCS 66 gsutil -q cp "$gcs_uri" "$dest"/bazel_installer.sh 67 mkdir -p "$dest"/install 68 # Run the installer 69 bash "$dest"/bazel_installer.sh --prefix="$dest"/install > /dev/null 70 ls -d "$dest"/install 71 ) 72} 73 74 75