google.golang.org/grpc@v1.72.2/scripts/install-protoc.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright 2024 gRPC authors.
     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  # install-protoc.sh
    18  #
    19  # This script installs the Protocol Buffers compiler (protoc) to the specified
    20  # directory.  It is used to generate code from .proto files for gRPC
    21  # communication. The script downloads the protoc binary from the official GitHub
    22  # repository and installs it in the system.
    23  #
    24  # Usage: ./install-protoc.sh INSTALL_PATH
    25  #
    26  # Arguments:
    27  #   INSTALL_PATH: The path where the protoc binary will be installed.
    28  #
    29  # Note: This script requires internet connectivity to download the protoc binary.
    30  
    31  set -eu -o pipefail
    32  
    33  source "$(dirname $0)/common.sh"
    34  
    35  # The version of protoc that will be installed.
    36  PROTOC_VERSION="27.1"
    37  
    38  main() {
    39    if [[ "$#" -ne 1 ]]; then
    40      die "Usage: $0 INSTALL_PATH"
    41    fi
    42  
    43    INSTALL_PATH="${1}"
    44  
    45    if [[ ! -d "${INSTALL_PATH}" ]]; then
    46      die "INSTALL_PATH (${INSTALL_PATH}) does not exist."
    47    fi
    48  
    49    echo "Installing protoc version $PROTOC_VERSION to ${INSTALL_PATH}..."
    50  
    51    # Detect the hardware platform.
    52    case "$(uname -m)" in
    53      "x86_64")   ARCH="x86_64";;
    54      "aarch64")  ARCH="aarch_64";;
    55      "arm64")    ARCH="aarch_64";;
    56      *)          die "Install unsuccessful. Hardware platform not supported by installer: $1";;
    57    esac
    58  
    59    # Detect the Operating System.
    60    case "$(uname -s)" in
    61      "Darwin") OS="osx";;
    62      "Linux")  OS="linux";;
    63      *)        die "Install unsuccessful. OS not supported by installer: $2";;
    64    esac
    65  
    66    # Check if the protoc binary with the right version is already installed.
    67    if [[ -f "${INSTALL_PATH}/bin/protoc" ]]; then
    68      if [[ "$("${INSTALL_PATH}/bin/protoc" --version)" == "libprotoc ${PROTOC_VERSION}" ]]; then
    69        echo "protoc version ${PROTOC_VERSION} is already installed in ${INSTALL_PATH}"
    70        return
    71      fi
    72    fi
    73  
    74    DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-${OS}-${ARCH}.zip"
    75  
    76    # -L follows redirects.
    77    # -O writes output to a file.
    78    curl -LO "${DOWNLOAD_URL}"
    79  
    80    # Unzip the downloaded file and except readme.txt.
    81    # The file structure should look like:
    82    # INSTALL_PATH
    83    # ├── bin
    84    # │   └── protoc
    85    # └── include
    86    #     └── other files...
    87    unzip "protoc-${PROTOC_VERSION}-${OS}-${ARCH}.zip" -d "${INSTALL_PATH}" -x "readme.txt"
    88    rm "protoc-${PROTOC_VERSION}-${OS}-${ARCH}.zip"
    89  
    90    # Make the protoc binary executable. ¯\_(ツ)_/¯  crazy, right?
    91    chmod +x "${INSTALL_PATH}/bin/protoc"
    92  }
    93  
    94  main "$@"