Skip to content

Commit

Permalink
Improve regenerate.sh to use the correct proto compiler version
Browse files Browse the repository at this point in the history
  • Loading branch information
aranjans committed Mar 26, 2024
1 parent b78c0eb commit e404049
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
93 changes: 79 additions & 14 deletions regenerate.sh
Expand Up @@ -24,29 +24,94 @@ trap finish EXIT

export GOBIN=${WORKDIR}/bin
export PATH=${GOBIN}:${PATH}
mkdir -p ${GOBIN}
mkdir -p "${GOBIN}"

echo "remove existing generated files"
# grpc_testing_not_regenerate/*.pb.go is not re-generated,
# see grpc_testing_not_regenerate/README.md for details.
rm -f $(find . -name '*.pb.go' | grep -v 'grpc_testing_not_regenerate')
find . -name '*.pb.go' | grep -v 'grpc_testing_not_regenerate' | xargs -I{} sh -c 'rm -f {} || true'

echo "go install google.golang.org/protobuf/cmd/protoc-gen-go"
(cd test/tools && go install google.golang.org/protobuf/cmd/protoc-gen-go)

echo "go install cmd/protoc-gen-go-grpc"
(cd cmd/protoc-gen-go-grpc && go install .)
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

echo "git clone https://github.com/protocolbuffers/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
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

# Install protoc
echo "Install protoc"
PROTOC_VERSION=25.2
echo "Protobuf version: ${PROTOC_VERSION}"
# Function to download for macOS using Homebrew
download_macos() {
echo "Attempting to install libprotoc ${PROTOC_VERSION} using Homebrew (macOS)..."
brew search libprotoc >/dev/null 2>&1 # Check if Homebrew has the formula
if [[ $? -eq 0 ]]; then
PROTOC_VERSION=$(echo $PROTOC_VERSION/1 | bc)
brew install protobuf@${PROTOC_VERSION}
if [[ $? -eq 0 ]]; then
echo "libprotoc ${PROTOC_VERSION} successfully installed on macOS"
else
echo "Error installing libprotoc on macOS"
fi
else
echo "Could not find libprotoc ${PROTOC_VERSION} formula in Homebrew. Please consider manual installation."
fi
}

# Function to download pre-built binaries for Linux
download_linux() {
echo "Attempting to download pre-built libprotoc ${PROTOC_VERSION} binary (Linux)..."

# Determine architecture
if [[ $(uname -m) == "x86_64" ]]; then
ARCH="x86_64"
elif [[ $(uname -m) == "aarch64" ]]; then
ARCH="aarch_64"
else
echo "Unsupported architecture. Please consider manual installation."
return
fi

# Download URL (adjust if a newer release is available)
DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-$ARCH.zip"

echo "Download URL: ${DOWNLOAD_URL}"

# Download and unzip
curl -LO $DOWNLOAD_URL
echo "Downloaded"
unzip "protoc-${PROTOC_VERSION}-linux-$ARCH.zip" -d ${WORKDIR}
echo "libprotoc ${PROTOC_VERSION} installed in ${WORKDIR} (Linux)"
export PATH="$PATH:$GOBIN"
}

echo "git clone https://github.com/grpc/grpc-proto"
git clone --quiet https://github.com/grpc/grpc-proto ${WORKDIR}/grpc-proto
# Detect the Operating System
OS=$(uname -s)

echo "git clone https://github.com/protocolbuffers/protobuf"
git clone --quiet https://github.com/protocolbuffers/protobuf ${WORKDIR}/protobuf
case $OS in
"Darwin") download_macos ;;
"Linux") download_linux ;;
*) echo "Unsupported operating system. Please consider manual installation." ;;
esac

# Pull in code.proto as a proto dependency
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
PROTOC_PATH="/usr/local/bin"
if [[ ":$PATH:" != *":$PROTOC_PATH:"* ]]; then
export PATH="$PATH:$PROTOC_PATH"
echo "protoc added to your PATH. You might need to open a new terminal"
else
echo "protoc already appears to be on your PATH"
fi

mkdir -p ${WORKDIR}/out

Expand Down Expand Up @@ -93,7 +158,7 @@ Mgrpc/testing/empty.proto=google.golang.org/grpc/interop/grpc_testing

for src in ${SOURCES[@]}; do
echo "protoc ${src}"
protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS}:${WORKDIR}/out \
protoc --go_out=${OPTS}:"${WORKDIR}"/out --go-grpc_out=${OPTS}:${WORKDIR}/out \
-I"." \
-I${WORKDIR}/grpc-proto \
-I${WORKDIR}/googleapis \
Expand All @@ -103,7 +168,7 @@ 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 \
Expand Down
2 changes: 1 addition & 1 deletion vet.sh
Expand Up @@ -31,6 +31,7 @@ trap cleanup EXIT
PATH="${HOME}/go/bin:${GOROOT}/bin:${PATH}"
go version

export PROTOBUF_VERSION=25.2 # a.k.a. v4.22.0 in pb.go files.
if [[ "$1" = "-install" ]]; then
# Install the pinned versions as defined in module tools.
pushd ./test/tools
Expand All @@ -41,7 +42,6 @@ if [[ "$1" = "-install" ]]; then
popd
if [[ -z "${VET_SKIP_PROTO}" ]]; then
if [[ "${GITHUB_ACTIONS}" = "true" ]]; then
PROTOBUF_VERSION=25.2 # a.k.a. v4.22.0 in pb.go files.
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}
Expand Down

0 comments on commit e404049

Please sign in to comment.