xref: /aosp_15_r20/external/tink/kokoro/testutils/install_protoc.sh (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1#!/bin/bash
2# Copyright 2022 Google LLC
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# This scripts installs the protocol buffer compiler at a given version.
18#
19# NOTEs:
20#   * If not running on Kokoro, this script will do nothing.
21#   * This script MUST be sourced to update the environment of the calling
22#     script.
23#
24# Usage:
25#   source ./kokoro/testutils/install_protoc.sh [version]
26
27## Per default, use X.21.9.
28readonly DEFAULT_PROTOC_VERSION="21.9"
29
30install_temp_protoc() {
31  local protoc_version="${1:-${DEFAULT_PROTOC_VERSION}}"
32  local platform="$(uname | tr '[:upper:]' '[:lower:]')"
33  local protoc_zip="protoc-${protoc_version}-linux-x86_64.zip"
34  if [[ "${platform}" == 'darwin' ]]; then
35    protoc_zip="protoc-${protoc_version}-osx-x86_64.zip"
36  fi
37  local protoc_url="https://github.com/protocolbuffers/protobuf/releases/download/v${protoc_version}/${protoc_zip}"
38  local -r protoc_tmpdir="$(mktemp -dt tink-protoc.XXXXXX)"
39  (
40    cd "${protoc_tmpdir}"
41    curl -OLsS "${protoc_url}"
42    unzip "${protoc_zip}" bin/protoc
43  )
44  export PATH="${protoc_tmpdir}/bin:${PATH}"
45}
46
47if [[ -n "${KOKORO_ROOT:-}" ]]; then
48  install_temp_protoc "$@"
49fi
50