Skip to content

Commit 192dc41

Browse files
arvindbr8printchard
authored andcommitted
scripts: regenerate pbs with caching deps to a fixed tmp folder (grpc#7409)
1 parent acdaeb3 commit 192dc41

File tree

2 files changed

+83
-75
lines changed

2 files changed

+83
-75
lines changed

scripts/install-protoc.sh

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
#
2+
#
33
# Copyright 2024 gRPC authors.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,21 +14,17 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
# install-protoc.sh
18-
#
19-
# This script installs the Protocol Buffers compiler (protoc) on the local
20-
# machine. It is used to generate code from .proto files for gRPC communication.
21-
# The script downloads the protoc binary from the official GitHub repository and
22-
# installs it in the system.
23-
#
24-
# Usage: ./install-protoc.sh [INSTALL_PATH]
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
2525
#
26-
# Arguments:
27-
# INSTALL_PATH: The path where the protoc binary will be installed (optional).
28-
# If not provided, the script will install the binary in the the
29-
# directory named by the GOBIN environment variable, which
30-
# defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH
31-
# environment variable is not set.
26+
# Arguments:
27+
# INSTALL_PATH: The path where the protoc binary will be installed.
3228
#
3329
# Note: This script requires internet connectivity to download the protoc binary.
3430

@@ -39,53 +35,60 @@ source "$(dirname $0)/common.sh"
3935
# The version of protoc that will be installed.
4036
PROTOC_VERSION="27.1"
4137

42-
INSTALL_PATH="${1:-${GOBIN:-${GOPATH:-$HOME/go}}}"
38+
main() {
39+
if [[ "$#" -ne 1 ]]; then
40+
die "Usage: $0 INSTALL_PATH"
41+
fi
4342

44-
# downloads the pre-built binaries for Linux to $INSTALL_PATH.
45-
# Usage:
46-
# download_binary ARCH OS
47-
# Arguments:
48-
# ARCH: The architecture of the system. Accepted values: [x86_64, aarch_64]
49-
# OS: The operating system of the system. Accepted values: [osx, linux]
50-
download_binary() {
51-
DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-$2-$1.zip"
52-
# -L follows redirects.
53-
# -O writes output to a file.
54-
curl -LO "${DOWNLOAD_URL}"
55-
unzip "protoc-${PROTOC_VERSION}-$2-$1.zip" -d "${INSTALL_PATH}"
56-
rm "protoc-${PROTOC_VERSION}-$2-$1.zip"
57-
rm "${INSTALL_PATH}/readme.txt"
58-
}
43+
INSTALL_PATH="${1}"
5944

60-
main() {
61-
# Check if protoc is already available.
62-
if command -v protoc &> /dev/null; then
63-
if INSTALL_VERSION=$(protoc --version | cut -d' ' -f2 2>/dev/null); then
64-
if [ "$INSTALL_VERSION" = "$PROTOC_VERSION" ]; then
65-
echo "protoc version $PROTOC_VERSION is already installed."
66-
return
67-
else
68-
die "Existing protoc version ($INSTALL_VERSION) differs. Kindly make sure you have $PROTOC_VERSION installed."
69-
fi
70-
fi
45+
if [[ ! -d "${INSTALL_PATH}" ]]; then
46+
die "INSTALL_PATH (${INSTALL_PATH}) does not exist."
7147
fi
7248

73-
# Detect the architecture
49+
echo "Installing protoc version $PROTOC_VERSION to ${INSTALL_PATH}..."
50+
51+
# Detect the hardware platform.
7452
case "$(uname -m)" in
75-
"x86_64") ARCH="x86_64";;
76-
"aarch64") ARCH="aarch_64";;
77-
"arm64") ARCH="aarch_64";;
78-
*) die "Unsupported architecture. Please consider manual installation from "\
79-
"https://github.com/protocolbuffers/protobuf/releases/ and add to PATH."
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";;
8057
esac
8158

82-
# Detect the Operating System
59+
# Detect the Operating System.
8360
case "$(uname -s)" in
84-
"Darwin") download_binary $ARCH "osx";;
85-
"Linux") download_binary $ARCH "linux";;
86-
*) die "Unsupported OS. Please consider manual installation from "\
87-
"https://github.com/protocolbuffers/protobuf/releases/ and add to PATH" ;;
61+
"Darwin") OS="osx";;
62+
"Linux") OS="linux";;
63+
*) die "Install unsuccessful. OS not supported by installer: $2";;
8864
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"
8992
}
9093

9194
main "$@"

scripts/regenerate.sh

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
#
23
# Copyright 2020 gRPC authors.
34
#
45
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,39 +16,43 @@
1516

1617
set -eu -o pipefail
1718

18-
WORKDIR=$(mktemp -d)
19-
20-
function finish {
21-
rm -rf "$WORKDIR"
22-
}
23-
trap finish EXIT
19+
WORKDIR="/tmp/grpc-go-tools"
20+
mkdir -p "${WORKDIR}"
2421

25-
./scripts/install-protoc.sh "${WORKDIR}"
22+
"$(dirname "${0}")"/install-protoc.sh ${WORKDIR}
2623

2724
export GOBIN="${WORKDIR}"/bin
2825
export PATH="${GOBIN}:${PATH}"
2926
mkdir -p "${GOBIN}"
3027

31-
echo "remove existing generated files"
28+
echo "removing existing generated files..."
3229
# grpc_testing_not_regenerate/*.pb.go is not re-generated,
3330
# see grpc_testing_not_regenerate/README.md for details.
34-
rm -f $(find . -name '*.pb.go' | grep -v 'grpc_testing_not_regenerate')
31+
find . -name '*.pb.go' | grep -v 'grpc_testing_not_regenerate' | xargs rm -f || true
3532

36-
echo "go install google.golang.org/protobuf/cmd/protoc-gen-go"
33+
echo "Executing: go install google.golang.org/protobuf/cmd/protoc-gen-go..."
3734
(cd test/tools && go install google.golang.org/protobuf/cmd/protoc-gen-go)
3835

39-
echo "go install cmd/protoc-gen-go-grpc"
36+
echo "Executing: go install cmd/protoc-gen-go-grpc..."
4037
(cd cmd/protoc-gen-go-grpc && go install .)
4138

42-
echo "git clone https://github.com/grpc/grpc-proto"
43-
git clone --quiet https://github.com/grpc/grpc-proto "${WORKDIR}/grpc-proto"
39+
echo "Pulling protos from https://github.com/grpc/grpc-proto..."
40+
if [ -d "${WORKDIR}/grpc-proto" ]; then
41+
(cd "${WORKDIR}/grpc-proto" && git pull)
42+
else
43+
git clone --quiet https://github.com/grpc/grpc-proto "${WORKDIR}/grpc-proto"
44+
fi
4445

45-
echo "git clone https://github.com/protocolbuffers/protobuf"
46-
git clone --quiet https://github.com/protocolbuffers/protobuf "${WORKDIR}/protobuf"
46+
echo "Pulling protos from https://github.com/protocolbuffers/protobuf..."
47+
if [ -d "${WORKDIR}/protobuf" ]; then
48+
(cd "${WORKDIR}/protobuf" && git pull)
49+
else
50+
git clone --quiet https://github.com/protocolbuffers/protobuf "${WORKDIR}/protobuf"
51+
fi
4752

4853
# Pull in code.proto as a proto dependency
4954
mkdir -p "${WORKDIR}/googleapis/google/rpc"
50-
echo "curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto"
55+
echo "Pulling code.proto from https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto..."
5156
curl --silent https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto > "${WORKDIR}/googleapis/google/rpc/code.proto"
5257

5358

@@ -72,8 +77,8 @@ SOURCES=(
7277
"${WORKDIR}/grpc-proto/grpc/gcp/transport_security_common.proto"
7378
"${WORKDIR}/grpc-proto/grpc/lookup/v1/rls.proto"
7479
"${WORKDIR}/grpc-proto/grpc/lookup/v1/rls_config.proto"
75-
"${WORKDIR}/grpc-proto/grpc/testing/*.proto"
76-
"${WORKDIR}/grpc-proto/grpc/core/*.proto"
80+
"${WORKDIR}"/grpc-proto/grpc/testing/*.proto
81+
"${WORKDIR}"/grpc-proto/grpc/core/*.proto
7782
)
7883

7984
# These options of the form 'Mfoo.proto=bar' instruct the codegen to use an
@@ -94,19 +99,19 @@ Mgrpc/testing/test.proto=google.golang.org/grpc/interop/grpc_testing,\
9499
Mgrpc/testing/payloads.proto=google.golang.org/grpc/interop/grpc_testing,\
95100
Mgrpc/testing/empty.proto=google.golang.org/grpc/interop/grpc_testing
96101

97-
for src in ${SOURCES[@]}; do
102+
for src in "${SOURCES[@]}"; do
98103
echo "protoc ${src}"
99-
protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS}:${WORKDIR}/out \
104+
protoc --go_out="${OPTS}:${WORKDIR}/out" --go-grpc_out="${OPTS}:${WORKDIR}/out" \
100105
-I"." \
101106
-I"${WORKDIR}/grpc-proto" \
102107
-I"${WORKDIR}/googleapis" \
103108
-I"${WORKDIR}/protobuf/src" \
104109
"${src}"
105110
done
106111

107-
for src in ${LEGACY_SOURCES[@]}; do
112+
for src in "${LEGACY_SOURCES[@]}"; do
108113
echo "protoc ${src}"
109-
protoc --go_out="${OPTS}:${WORKDIR}/out" --go-grpc_out="${OPTS}",require_unimplemented_servers=false:"${WORKDIR}/out" \
114+
protoc --go_out="${OPTS}:${WORKDIR}/out" --go-grpc_out="${OPTS},require_unimplemented_servers=false:${WORKDIR}/out" \
110115
-I"." \
111116
-I"${WORKDIR}/grpc-proto" \
112117
-I"${WORKDIR}/googleapis" \

0 commit comments

Comments
 (0)