Skip to content

Commit c4753c3

Browse files
authored
scripts: improve regenerate.sh to use the correct proto compiler version (#7064)
1 parent e2e7a51 commit c4753c3

File tree

4 files changed

+114
-47
lines changed

4 files changed

+114
-47
lines changed

doc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
//go:generate ./regenerate.sh
19+
//go:generate ./scripts/regenerate.sh
2020

2121
/*
2222
Package grpc implements an RPC system called gRPC.

scripts/install_protoc.sh

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
# Copyright 2024 gRPC authors.
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+
# This script ensures the installation of protobuf on client machine.
17+
# In case of manual run of this script, make sure you pass the args
18+
# expected at
19+
# https://github.com/grpc/grpc-go/blob/master/scripts/install_protoc.sh#L60
20+
21+
set -eu -o pipefail
22+
23+
source "$(dirname $0)/vet-common.sh"
24+
25+
# The version of protoc that will be installed.
26+
PROTOC_VERSION="25.2"
27+
28+
# Function to download pre-built binaries for Linux with
29+
# ARCH as $1, OS as $2, and INSTALL_PATH as $3 arguments.
30+
download_binary() {
31+
# Check if protoc is already available.
32+
if command -v protoc &> /dev/null; then
33+
if INSTALL_VERSION=$(protoc --version | cut -d' ' -f2 2>/dev/null); then
34+
if [ "$INSTALL_VERSION" = "$PROTOC_VERSION" ]; then
35+
echo "protoc version $PROTOC_VERSION is already installed."
36+
return
37+
else
38+
die "Existing protoc version ($INSTALL_VERSION) differs. Kindly make sure you have $PROTOC_VERSION installed."
39+
fi
40+
else
41+
echo "Unable to determine installed protoc version. Starting the installation."
42+
fi
43+
fi
44+
DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-$2-$1.zip"
45+
# Download and unzip
46+
curl -LO "$DOWNLOAD_URL"
47+
INSTALL_DIR="${3:-${GOBIN:-${GOPATH:-$HOME/go}}}"
48+
unzip "protoc-${PROTOC_VERSION}-$2-$1.zip" -d $INSTALL_DIR
49+
rm "protoc-${PROTOC_VERSION}-$2-$1.zip"
50+
rm "${INSTALL_DIR}/readme.txt"
51+
}
52+
53+
# Detect the architecture
54+
case "$(uname -m)" in
55+
"x86_64") ARCH="x86_64";;
56+
"aarch64") ARCH="aarch_64";;
57+
"arm64") ARCH="aarch_64";;
58+
*) die "Unsupported architecture. Please consider manual installation from \
59+
https://github.com/protocolbuffers/protobuf/releases/ and add to PATH."
60+
esac
61+
62+
# Detect the Operating System
63+
INSTALL_PATH=${1:+"$1"}
64+
case "$(uname -s)" in
65+
"Darwin") download_binary $ARCH "osx" "$INSTALL_PATH";;
66+
"Linux") download_binary $ARCH "linux" "$INSTALL_PATH";;
67+
*) die "Unsupported OS. Please consider manual installation from \
68+
https://github.com/protocolbuffers/protobuf/releases/ and add to PATH" ;;
69+
esac

regenerate.sh scripts/regenerate.sh

+39-34
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ WORKDIR=$(mktemp -d)
1919

2020
function finish {
2121
rm -rf "$WORKDIR"
22+
# Revert back the PATH to client's original value
23+
export PATH=$ORIGINAL_PATH
2224
}
2325
trap finish EXIT
2426

25-
export GOBIN=${WORKDIR}/bin
26-
export PATH=${GOBIN}:${PATH}
27-
mkdir -p ${GOBIN}
27+
GOBIN="${WORKDIR}"/bin
28+
ORIGINAL_PATH=$PATH
29+
export PATH="${GOBIN}:${PATH}"
30+
mkdir -p "${GOBIN}"
2831

2932
echo "remove existing generated files"
3033
# grpc_testing_not_regenerate/*.pb.go is not re-generated,
@@ -38,39 +41,41 @@ echo "go install cmd/protoc-gen-go-grpc"
3841
(cd cmd/protoc-gen-go-grpc && go install .)
3942

4043
echo "git clone https://github.com/grpc/grpc-proto"
41-
git clone --quiet https://github.com/grpc/grpc-proto ${WORKDIR}/grpc-proto
44+
git clone --quiet https://github.com/grpc/grpc-proto "${WORKDIR}/grpc-proto"
4245

4346
echo "git clone https://github.com/protocolbuffers/protobuf"
44-
git clone --quiet https://github.com/protocolbuffers/protobuf ${WORKDIR}/protobuf
47+
git clone --quiet https://github.com/protocolbuffers/protobuf "${WORKDIR}/protobuf"
4548

4649
# Pull in code.proto as a proto dependency
47-
mkdir -p ${WORKDIR}/googleapis/google/rpc
50+
mkdir -p "${WORKDIR}/googleapis/google/rpc"
4851
echo "curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto"
49-
curl --silent https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto > ${WORKDIR}/googleapis/google/rpc/code.proto
52+
curl --silent https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto > "${WORKDIR}/googleapis/google/rpc/code.proto"
5053

51-
mkdir -p ${WORKDIR}/out
54+
source ./scripts/install_protoc.sh $WORKDIR
55+
56+
mkdir -p "${WORKDIR}/out"
5257

5358
# Generates sources without the embed requirement
5459
LEGACY_SOURCES=(
55-
${WORKDIR}/grpc-proto/grpc/binlog/v1/binarylog.proto
56-
${WORKDIR}/grpc-proto/grpc/channelz/v1/channelz.proto
57-
${WORKDIR}/grpc-proto/grpc/health/v1/health.proto
58-
${WORKDIR}/grpc-proto/grpc/lb/v1/load_balancer.proto
60+
"${WORKDIR}/grpc-proto/grpc/binlog/v1/binarylog.proto"
61+
"${WORKDIR}/grpc-proto/grpc/channelz/v1/channelz.proto"
62+
"${WORKDIR}/grpc-proto/grpc/health/v1/health.proto"
63+
"${WORKDIR}/grpc-proto/grpc/lb/v1/load_balancer.proto"
5964
profiling/proto/service.proto
60-
${WORKDIR}/grpc-proto/grpc/reflection/v1alpha/reflection.proto
61-
${WORKDIR}/grpc-proto/grpc/reflection/v1/reflection.proto
65+
"${WORKDIR}/grpc-proto/grpc/reflection/v1alpha/reflection.proto"
66+
"${WORKDIR}/grpc-proto/grpc/reflection/v1/reflection.proto"
6267
)
6368

6469
# Generates only the new gRPC Service symbols
6570
SOURCES=(
6671
$(git ls-files --exclude-standard --cached --others "*.proto" | grep -v '^profiling/proto/service.proto$')
67-
${WORKDIR}/grpc-proto/grpc/gcp/altscontext.proto
68-
${WORKDIR}/grpc-proto/grpc/gcp/handshaker.proto
69-
${WORKDIR}/grpc-proto/grpc/gcp/transport_security_common.proto
70-
${WORKDIR}/grpc-proto/grpc/lookup/v1/rls.proto
71-
${WORKDIR}/grpc-proto/grpc/lookup/v1/rls_config.proto
72-
${WORKDIR}/grpc-proto/grpc/testing/*.proto
73-
${WORKDIR}/grpc-proto/grpc/core/*.proto
72+
"${WORKDIR}/grpc-proto/grpc/gcp/altscontext.proto"
73+
"${WORKDIR}/grpc-proto/grpc/gcp/handshaker.proto"
74+
"${WORKDIR}/grpc-proto/grpc/gcp/transport_security_common.proto"
75+
"${WORKDIR}/grpc-proto/grpc/lookup/v1/rls.proto"
76+
"${WORKDIR}/grpc-proto/grpc/lookup/v1/rls_config.proto"
77+
"${WORKDIR}/grpc-proto/grpc/testing/*.proto"
78+
"${WORKDIR}/grpc-proto/grpc/core/*.proto"
7479
)
7580

7681
# These options of the form 'Mfoo.proto=bar' instruct the codegen to use an
@@ -95,29 +100,29 @@ for src in ${SOURCES[@]}; do
95100
echo "protoc ${src}"
96101
protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS},use_generic_streams_experimental=true:${WORKDIR}/out \
97102
-I"." \
98-
-I${WORKDIR}/grpc-proto \
99-
-I${WORKDIR}/googleapis \
100-
-I${WORKDIR}/protobuf/src \
101-
${src}
103+
-I"${WORKDIR}/grpc-proto" \
104+
-I"${WORKDIR}/googleapis" \
105+
-I"${WORKDIR}/protobuf/src" \
106+
"${src}"
102107
done
103108

104109
for src in ${LEGACY_SOURCES[@]}; do
105110
echo "protoc ${src}"
106-
protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS},require_unimplemented_servers=false:${WORKDIR}/out \
111+
protoc --go_out="${OPTS}:${WORKDIR}/out" --go-grpc_out="${OPTS}",require_unimplemented_servers=false:"${WORKDIR}/out" \
107112
-I"." \
108-
-I${WORKDIR}/grpc-proto \
109-
-I${WORKDIR}/googleapis \
110-
-I${WORKDIR}/protobuf/src \
111-
${src}
113+
-I"${WORKDIR}/grpc-proto" \
114+
-I"${WORKDIR}/googleapis" \
115+
-I"${WORKDIR}/protobuf/src" \
116+
"${src}"
112117
done
113118

114119
# The go_package option in grpc/lookup/v1/rls.proto doesn't match the
115120
# current location. Move it into the right place.
116-
mkdir -p ${WORKDIR}/out/google.golang.org/grpc/internal/proto/grpc_lookup_v1
117-
mv ${WORKDIR}/out/google.golang.org/grpc/lookup/grpc_lookup_v1/* ${WORKDIR}/out/google.golang.org/grpc/internal/proto/grpc_lookup_v1
121+
mkdir -p "${WORKDIR}/out/google.golang.org/grpc/internal/proto/grpc_lookup_v1"
122+
mv "${WORKDIR}"/out/google.golang.org/grpc/lookup/grpc_lookup_v1/* "${WORKDIR}/out/google.golang.org/grpc/internal/proto/grpc_lookup_v1"
118123

119124
# grpc_testing_not_regenerate/*.pb.go are not re-generated,
120125
# see grpc_testing_not_regenerate/README.md for details.
121-
rm ${WORKDIR}/out/google.golang.org/grpc/reflection/test/grpc_testing_not_regenerate/*.pb.go
126+
rm "${WORKDIR}"/out/google.golang.org/grpc/reflection/test/grpc_testing_not_regenerate/*.pb.go
122127

123-
cp -R ${WORKDIR}/out/google.golang.org/grpc/* .
128+
cp -R "${WORKDIR}"/out/google.golang.org/grpc/* .

scripts/vet-proto.sh

+5-12
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,11 @@ trap cleanup EXIT
2020
# consistent with the place where all binaries installed by scripts in this repo
2121
# go.)
2222
if [[ "$1" = "-install" ]]; then
23-
if [[ "${GITHUB_ACTIONS}" = "true" ]]; then
24-
PROTOBUF_VERSION=25.2 # Shows up in pb.go files as v4.22.0
25-
PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip
26-
pushd /home/runner/go
27-
wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME}
28-
unzip ${PROTOC_FILENAME}
29-
protoc --version # Check that the binary works.
30-
popd
31-
else
32-
# TODO: replace with install protoc when https://github.com/grpc/grpc-go/pull/7064 is merged.
33-
die "-install currently intended for use in CI only."
34-
fi
23+
if [[ "${GITHUB_ACTIONS}" = "true" ]]; then
24+
source ./scripts/install_protoc.sh "/home/runner/go"
25+
else
26+
die "run protoc installer https://github.com/grpc/grpc-go/blob/master/scripts/install_protoc.sh"
27+
fi
3528
echo SUCCESS
3629
exit 0
3730
elif [[ "$#" -ne 0 ]]; then

0 commit comments

Comments
 (0)