|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright (c) 2025 Uber Technologies, Inc. |
| 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 | +# Hermetic protoc wrapper. Downloads and pins protoc to the version in |
| 18 | +# .protocversion, then execs it. This mirrors ./tool/bazel (Bazelisk) so that |
| 19 | +# `make proto` produces identical output regardless of any host/homebrew protoc. |
| 20 | +# |
| 21 | +# The binary is cached under ${XDG_CACHE_HOME:-$HOME/.cache}/submitqueue-protoc/ |
| 22 | +# and verified against a pinned SHA-256 before use. |
| 23 | + |
| 24 | +set -euo pipefail |
| 25 | + |
| 26 | +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 27 | +version="$(tr -d '[:space:]' <"${repo_root}/.protocversion")" |
| 28 | + |
| 29 | +# Map the host OS/arch onto the protobuf release asset suffix. |
| 30 | +os="$(uname -s)" |
| 31 | +arch="$(uname -m)" |
| 32 | +case "${os}" in |
| 33 | + Darwin) os_part="osx" ;; |
| 34 | + Linux) os_part="linux" ;; |
| 35 | + *) echo "tool/protoc: unsupported OS: ${os}" >&2; exit 1 ;; |
| 36 | +esac |
| 37 | +case "${arch}" in |
| 38 | + arm64 | aarch64) arch_part="aarch_64" ;; |
| 39 | + x86_64 | amd64) arch_part="x86_64" ;; |
| 40 | + *) echo "tool/protoc: unsupported arch: ${arch}" >&2; exit 1 ;; |
| 41 | +esac |
| 42 | +plat="${os_part}-${arch_part}" |
| 43 | + |
| 44 | +# Pinned SHA-256 of protoc-${version}-${plat}.zip. Add new entries when bumping |
| 45 | +# .protocversion (see the release page on github.com/protocolbuffers/protobuf). |
| 46 | +checksum="" |
| 47 | +case "${version}:${plat}" in |
| 48 | + "29.3:osx-aarch_64") checksum="2b8a3403cd097f95f3ba656e14b76c732b6b26d7f183330b11e36ef2bc028765" ;; |
| 49 | + "29.3:osx-x86_64") checksum="9a788036d8f9854f7b03c305df4777cf0e54e5b081e25bf15252da87e0e90875" ;; |
| 50 | + "29.3:linux-x86_64") checksum="3e866620c5be27664f3d2fa2d656b5f3e09b5152b42f1bedbf427b333e90021a" ;; |
| 51 | + "29.3:linux-aarch_64") checksum="6427349140e01f06e049e707a58709a4f221ae73ab9a0425bc4a00c8d0e1ab32" ;; |
| 52 | +esac |
| 53 | + |
| 54 | +cache_root="${XDG_CACHE_HOME:-${HOME}/.cache}/submitqueue-protoc" |
| 55 | +install_dir="${cache_root}/${version}/${plat}" |
| 56 | +protoc_bin="${install_dir}/bin/protoc" |
| 57 | + |
| 58 | +sha256() { |
| 59 | + if command -v sha256sum >/dev/null 2>&1; then |
| 60 | + sha256sum "$1" | awk '{print $1}' |
| 61 | + else |
| 62 | + shasum -a 256 "$1" | awk '{print $1}' |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +if [[ ! -x "${protoc_bin}" ]]; then |
| 67 | + url="https://github.com/protocolbuffers/protobuf/releases/download/v${version}/protoc-${version}-${plat}.zip" |
| 68 | + tmp="$(mktemp -d)" |
| 69 | + trap 'rm -rf "${tmp}"' EXIT |
| 70 | + echo "tool/protoc: downloading protoc ${version} (${plat})..." >&2 |
| 71 | + curl -fsSL -o "${tmp}/protoc.zip" "${url}" |
| 72 | + if [[ -n "${checksum}" ]]; then |
| 73 | + got="$(sha256 "${tmp}/protoc.zip")" |
| 74 | + if [[ "${got}" != "${checksum}" ]]; then |
| 75 | + echo "tool/protoc: checksum mismatch for protoc-${version}-${plat}.zip" >&2 |
| 76 | + echo " expected ${checksum}" >&2 |
| 77 | + echo " got ${got}" >&2 |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | + else |
| 81 | + echo "tool/protoc: no pinned checksum for ${version}:${plat}; add one to tool/protoc" >&2 |
| 82 | + echo " (downloaded sha256: $(sha256 "${tmp}/protoc.zip"))" >&2 |
| 83 | + fi |
| 84 | + rm -rf "${install_dir}" |
| 85 | + mkdir -p "${install_dir}" |
| 86 | + unzip -q "${tmp}/protoc.zip" -d "${install_dir}" |
| 87 | +fi |
| 88 | + |
| 89 | +exec "${protoc_bin}" "$@" |
0 commit comments