1#!/bin/bash
2# Updater script for bazel prebuilt binaries in AOSP.
3#
4# This script handles both linux and darwin binaries in a single invocation. See
5# README.md for more details.
6#
7# Usage: update.sh <commit>
8
9set -euox pipefail
10
11function err() {
12    >&2 echo "$@"
13    exit 1
14}
15
16# Check that the necessary tools are installed.
17function check_prereqs() {
18    if ! [[ "${PWD}" =~ .*/prebuilts/bazel/common$ ]]; then
19        err "Error: Run this script from within the prebuilts/bazel/common directory."
20    fi
21
22    for cmd in jq curl; do
23        if ! command -v "${cmd}" &> /dev/null; then
24            err "Error: This script requires ${cmd}. Install it and ensure it is on your PATH."
25        fi
26    done
27}
28
29check_prereqs
30commit="$1"; shift
31bazel_src_dir="$1"; shift
32
33ci_url="https://storage.googleapis.com/bazel-builds/metadata/${commit}.json"
34platforms_json="$(curl -s "${ci_url}" | jq '{ platforms: .platforms }')"
35if [[ $? != 0 ]]; then
36    err "Failed to download or parse ${ci_url}. Exiting."
37fi
38
39darwin_nojdk_url="$(echo "${platforms_json}" | jq --raw-output '.[].macos.nojdk_url')"
40darwin_nojdk_sha256="$(echo "${platforms_json}" | jq --raw-output '.[].macos.nojdk_sha256')"
41
42linux_nojdk_url="$(echo "${platforms_json}" | jq --raw-output '.[].linux.nojdk_url')"
43linux_nojdk_sha256="$(echo "${platforms_json}" | jq --raw-output '.[].linux.nojdk_sha256')"
44
45function check_sha256() {
46  local sha256=$1; shift
47  local downloaded_file=$1; shift
48    echo "Verifying checksum for ${downloaded_file} to be ${sha256}.."
49    if ! echo "${sha256} ${downloaded_file}" | sha256sum --check --status ; then
50      echo "ERROR: checksum of ${downloaded_file} did not match!"
51      echo "${sha256} ${downloaded_file}" | sha256sum --check --status
52      echo "${sha256} ${downloaded_file}" | sha256sum --check
53      exit 1
54    fi
55}
56
57function download_and_verify() {
58    local os=$1; shift
59    local url=$1; shift
60    local sha256=$1; shift
61
62    echo "Cleaning previous ${os} bazel binary.."
63    rm -f bazel_nojdk-*-${os}-x86_64
64
65    echo "Downloading ${os} bazel binary for ${commit}.."
66    downloaded_file="bazel_nojdk-${commit}-${os}-x86_64"
67    curl "${url}" --output "${downloaded_file}"
68
69    check_sha256 "${sha256}" "${downloaded_file}"
70
71    echo "Setting up bazel symlink for ${os}.."
72    rm -f bazel && ln -s "${downloaded_file}" bazel
73    chmod +x "${downloaded_file}"
74}
75
76common_bazel_dir=$(pwd)
77# Update Linux binary.
78cd ../linux-x86_64
79linux_bazel_dir=$(pwd)
80download_and_verify "linux" "${linux_nojdk_url}" "${linux_nojdk_sha256}"
81linux_downloaded_file=${downloaded_file}
82./${downloaded_file} license > LICENSE
83cp LICENSE ../common/
84# Update macOS binary.
85cp LICENSE "../darwin-x86_64/"
86cd "../darwin-x86_64"
87download_and_verify "darwin" "${darwin_nojdk_url}" "${darwin_nojdk_sha256}"
88${common_bazel_dir}/update_tools.sh "$linux_bazel_dir/$linux_downloaded_file" "$common_bazel_dir" "$linux_bazel_dir" "$bazel_src_dir"
89
90
91echo "Done. This script may have affected all of prebuilts/bazel/common, prebuilts/bazel/linux-x86_64 and prebuilts/bazel/darwin-x86_64. Be sure to upload changes for all affected git repositories."
92