From c4753c39391bf30c3f07303e9d1e03816f1e7f72 Mon Sep 17 00:00:00 2001 From: Abhishek Ranjan <159750762+aranjans@users.noreply.github.com> Date: Mon, 10 Jun 2024 23:26:01 +0530 Subject: [PATCH] scripts: improve regenerate.sh to use the correct proto compiler version (#7064) --- doc.go | 2 +- scripts/install_protoc.sh | 69 ++++++++++++++++++++++++ regenerate.sh => scripts/regenerate.sh | 73 ++++++++++++++------------ scripts/vet-proto.sh | 17 ++---- 4 files changed, 114 insertions(+), 47 deletions(-) create mode 100755 scripts/install_protoc.sh rename regenerate.sh => scripts/regenerate.sh (63%) diff --git a/doc.go b/doc.go index 0022859ad746..e7b532b6f806 100644 --- a/doc.go +++ b/doc.go @@ -16,7 +16,7 @@ * */ -//go:generate ./regenerate.sh +//go:generate ./scripts/regenerate.sh /* Package grpc implements an RPC system called gRPC. diff --git a/scripts/install_protoc.sh b/scripts/install_protoc.sh new file mode 100755 index 000000000000..f0b8aef56178 --- /dev/null +++ b/scripts/install_protoc.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# Copyright 2024 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# This script ensures the installation of protobuf on client machine. +# In case of manual run of this script, make sure you pass the args +# expected at +# https://github.com/grpc/grpc-go/blob/master/scripts/install_protoc.sh#L60 + +set -eu -o pipefail + +source "$(dirname $0)/vet-common.sh" + +# The version of protoc that will be installed. +PROTOC_VERSION="25.2" + +# Function to download pre-built binaries for Linux with +# ARCH as $1, OS as $2, and INSTALL_PATH as $3 arguments. +download_binary() { + # Check if protoc is already available. + if command -v protoc &> /dev/null; then + if INSTALL_VERSION=$(protoc --version | cut -d' ' -f2 2>/dev/null); then + if [ "$INSTALL_VERSION" = "$PROTOC_VERSION" ]; then + echo "protoc version $PROTOC_VERSION is already installed." + return + else + die "Existing protoc version ($INSTALL_VERSION) differs. Kindly make sure you have $PROTOC_VERSION installed." + fi + else + echo "Unable to determine installed protoc version. Starting the installation." + fi + fi + DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-$2-$1.zip" + # Download and unzip + curl -LO "$DOWNLOAD_URL" + INSTALL_DIR="${3:-${GOBIN:-${GOPATH:-$HOME/go}}}" + unzip "protoc-${PROTOC_VERSION}-$2-$1.zip" -d $INSTALL_DIR + rm "protoc-${PROTOC_VERSION}-$2-$1.zip" + rm "${INSTALL_DIR}/readme.txt" +} + +# Detect the architecture +case "$(uname -m)" in + "x86_64") ARCH="x86_64";; + "aarch64") ARCH="aarch_64";; + "arm64") ARCH="aarch_64";; +*) die "Unsupported architecture. Please consider manual installation from \ + https://github.com/protocolbuffers/protobuf/releases/ and add to PATH." +esac + +# Detect the Operating System +INSTALL_PATH=${1:+"$1"} +case "$(uname -s)" in + "Darwin") download_binary $ARCH "osx" "$INSTALL_PATH";; + "Linux") download_binary $ARCH "linux" "$INSTALL_PATH";; +*) die "Unsupported OS. Please consider manual installation from \ + https://github.com/protocolbuffers/protobuf/releases/ and add to PATH" ;; +esac diff --git a/regenerate.sh b/scripts/regenerate.sh similarity index 63% rename from regenerate.sh rename to scripts/regenerate.sh index 3edca296c224..c5d7b1a645f0 100755 --- a/regenerate.sh +++ b/scripts/regenerate.sh @@ -19,12 +19,15 @@ WORKDIR=$(mktemp -d) function finish { rm -rf "$WORKDIR" + # Revert back the PATH to client's original value + export PATH=$ORIGINAL_PATH } trap finish EXIT -export GOBIN=${WORKDIR}/bin -export PATH=${GOBIN}:${PATH} -mkdir -p ${GOBIN} +GOBIN="${WORKDIR}"/bin +ORIGINAL_PATH=$PATH +export PATH="${GOBIN}:${PATH}" +mkdir -p "${GOBIN}" echo "remove existing generated files" # grpc_testing_not_regenerate/*.pb.go is not re-generated, @@ -38,39 +41,41 @@ echo "go install cmd/protoc-gen-go-grpc" (cd cmd/protoc-gen-go-grpc && go install .) echo "git clone https://github.com/grpc/grpc-proto" -git clone --quiet https://github.com/grpc/grpc-proto ${WORKDIR}/grpc-proto +git clone --quiet https://github.com/grpc/grpc-proto "${WORKDIR}/grpc-proto" echo "git clone https://github.com/protocolbuffers/protobuf" -git clone --quiet https://github.com/protocolbuffers/protobuf ${WORKDIR}/protobuf +git clone --quiet https://github.com/protocolbuffers/protobuf "${WORKDIR}/protobuf" # Pull in code.proto as a proto dependency -mkdir -p ${WORKDIR}/googleapis/google/rpc +mkdir -p "${WORKDIR}/googleapis/google/rpc" echo "curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto" -curl --silent https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto > ${WORKDIR}/googleapis/google/rpc/code.proto +curl --silent https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto > "${WORKDIR}/googleapis/google/rpc/code.proto" -mkdir -p ${WORKDIR}/out +source ./scripts/install_protoc.sh $WORKDIR + +mkdir -p "${WORKDIR}/out" # Generates sources without the embed requirement LEGACY_SOURCES=( - ${WORKDIR}/grpc-proto/grpc/binlog/v1/binarylog.proto - ${WORKDIR}/grpc-proto/grpc/channelz/v1/channelz.proto - ${WORKDIR}/grpc-proto/grpc/health/v1/health.proto - ${WORKDIR}/grpc-proto/grpc/lb/v1/load_balancer.proto + "${WORKDIR}/grpc-proto/grpc/binlog/v1/binarylog.proto" + "${WORKDIR}/grpc-proto/grpc/channelz/v1/channelz.proto" + "${WORKDIR}/grpc-proto/grpc/health/v1/health.proto" + "${WORKDIR}/grpc-proto/grpc/lb/v1/load_balancer.proto" profiling/proto/service.proto - ${WORKDIR}/grpc-proto/grpc/reflection/v1alpha/reflection.proto - ${WORKDIR}/grpc-proto/grpc/reflection/v1/reflection.proto + "${WORKDIR}/grpc-proto/grpc/reflection/v1alpha/reflection.proto" + "${WORKDIR}/grpc-proto/grpc/reflection/v1/reflection.proto" ) # Generates only the new gRPC Service symbols SOURCES=( $(git ls-files --exclude-standard --cached --others "*.proto" | grep -v '^profiling/proto/service.proto$') - ${WORKDIR}/grpc-proto/grpc/gcp/altscontext.proto - ${WORKDIR}/grpc-proto/grpc/gcp/handshaker.proto - ${WORKDIR}/grpc-proto/grpc/gcp/transport_security_common.proto - ${WORKDIR}/grpc-proto/grpc/lookup/v1/rls.proto - ${WORKDIR}/grpc-proto/grpc/lookup/v1/rls_config.proto - ${WORKDIR}/grpc-proto/grpc/testing/*.proto - ${WORKDIR}/grpc-proto/grpc/core/*.proto + "${WORKDIR}/grpc-proto/grpc/gcp/altscontext.proto" + "${WORKDIR}/grpc-proto/grpc/gcp/handshaker.proto" + "${WORKDIR}/grpc-proto/grpc/gcp/transport_security_common.proto" + "${WORKDIR}/grpc-proto/grpc/lookup/v1/rls.proto" + "${WORKDIR}/grpc-proto/grpc/lookup/v1/rls_config.proto" + "${WORKDIR}/grpc-proto/grpc/testing/*.proto" + "${WORKDIR}/grpc-proto/grpc/core/*.proto" ) # These options of the form 'Mfoo.proto=bar' instruct the codegen to use an @@ -95,29 +100,29 @@ for src in ${SOURCES[@]}; do echo "protoc ${src}" protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS},use_generic_streams_experimental=true:${WORKDIR}/out \ -I"." \ - -I${WORKDIR}/grpc-proto \ - -I${WORKDIR}/googleapis \ - -I${WORKDIR}/protobuf/src \ - ${src} + -I"${WORKDIR}/grpc-proto" \ + -I"${WORKDIR}/googleapis" \ + -I"${WORKDIR}/protobuf/src" \ + "${src}" done for src in ${LEGACY_SOURCES[@]}; do echo "protoc ${src}" - protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS},require_unimplemented_servers=false:${WORKDIR}/out \ + protoc --go_out="${OPTS}:${WORKDIR}/out" --go-grpc_out="${OPTS}",require_unimplemented_servers=false:"${WORKDIR}/out" \ -I"." \ - -I${WORKDIR}/grpc-proto \ - -I${WORKDIR}/googleapis \ - -I${WORKDIR}/protobuf/src \ - ${src} + -I"${WORKDIR}/grpc-proto" \ + -I"${WORKDIR}/googleapis" \ + -I"${WORKDIR}/protobuf/src" \ + "${src}" done # The go_package option in grpc/lookup/v1/rls.proto doesn't match the # current location. Move it into the right place. -mkdir -p ${WORKDIR}/out/google.golang.org/grpc/internal/proto/grpc_lookup_v1 -mv ${WORKDIR}/out/google.golang.org/grpc/lookup/grpc_lookup_v1/* ${WORKDIR}/out/google.golang.org/grpc/internal/proto/grpc_lookup_v1 +mkdir -p "${WORKDIR}/out/google.golang.org/grpc/internal/proto/grpc_lookup_v1" +mv "${WORKDIR}"/out/google.golang.org/grpc/lookup/grpc_lookup_v1/* "${WORKDIR}/out/google.golang.org/grpc/internal/proto/grpc_lookup_v1" # grpc_testing_not_regenerate/*.pb.go are not re-generated, # see grpc_testing_not_regenerate/README.md for details. -rm ${WORKDIR}/out/google.golang.org/grpc/reflection/test/grpc_testing_not_regenerate/*.pb.go +rm "${WORKDIR}"/out/google.golang.org/grpc/reflection/test/grpc_testing_not_regenerate/*.pb.go -cp -R ${WORKDIR}/out/google.golang.org/grpc/* . +cp -R "${WORKDIR}"/out/google.golang.org/grpc/* . diff --git a/scripts/vet-proto.sh b/scripts/vet-proto.sh index 26c1ac65885a..56b423593cb9 100755 --- a/scripts/vet-proto.sh +++ b/scripts/vet-proto.sh @@ -20,18 +20,11 @@ trap cleanup EXIT # consistent with the place where all binaries installed by scripts in this repo # go.) if [[ "$1" = "-install" ]]; then - if [[ "${GITHUB_ACTIONS}" = "true" ]]; then - PROTOBUF_VERSION=25.2 # Shows up in pb.go files as v4.22.0 - PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip - pushd /home/runner/go - wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME} - unzip ${PROTOC_FILENAME} - protoc --version # Check that the binary works. - popd - else - # TODO: replace with install protoc when https://github.com/grpc/grpc-go/pull/7064 is merged. - die "-install currently intended for use in CI only." - fi + if [[ "${GITHUB_ACTIONS}" = "true" ]]; then + source ./scripts/install_protoc.sh "/home/runner/go" + else + die "run protoc installer https://github.com/grpc/grpc-go/blob/master/scripts/install_protoc.sh" + fi echo SUCCESS exit 0 elif [[ "$#" -ne 0 ]]; then