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 27, 2024
1 parent fc3f327 commit a880e4d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 46 deletions.
65 changes: 65 additions & 0 deletions protoc_installer.sh
@@ -0,0 +1,65 @@
#!/bin/bash
# Install protoc
echo "Installing protoc"
PROTOC_VERSION="25.2" # Use quotes for consistency
echo "Protobuf version: ${PROTOC_VERSION}"


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) # Quotes within command substitution
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" # Quotes around the URL
echo "Downloaded"
if [ -z "$WORKDIR" ]; then
unzip "protoc-${PROTOC_VERSION}-linux-$ARCH.zip"
else
unzip "protoc-${PROTOC_VERSION}-linux-$ARCH.zip" -d "${WORKDIR}" # Quotes around WORKDIR
rm "protoc-${PROTOC_VERSION}-linux-$ARCH.zip"
fi
}


download_protoc() {
# Detect the Operating System
OS=$(uname -s)

case "$OS" in # Quotes around $OS
"Darwin") download_macos ;;
"Linux") download_linux ;;
*) echo "Unsupported operating system. Please consider manual installation." ;;
esac
}
97 changes: 55 additions & 42 deletions regenerate.sh
Expand Up @@ -15,16 +15,16 @@

set -eu -o pipefail

WORKDIR=$(mktemp -d)
export WORKDIR=$(mktemp -d)

function finish {
rm -rf "$WORKDIR"
}
trap finish EXIT

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

echo "remove existing generated files"
# grpc_testing_not_regenerate/*.pb.go is not re-generated,
Expand All @@ -34,43 +34,56 @@ rm -f $(find . -name '*.pb.go' | grep -v 'grpc_testing_not_regenerate')
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/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
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
# 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

mkdir -p ${WORKDIR}/out
source ./protoc_installer.sh
download_protoc

export PATH="$PATH:$GOBIN"

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

# 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\|reflection/grpc_reflection_v1alpha/reflection.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
Expand All @@ -93,31 +106,31 @@ 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 \
-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/grpc_testing_not_regenerate/*.pb.go
rm "${WORKDIR}"/out/google.golang.org/grpc/reflection/grpc_testing_not_regenerate/*.pb.go

cp -R ${WORKDIR}/out/google.golang.org/grpc/* .
cp -R "${WORKDIR}"/out/google.golang.org/grpc/* .
6 changes: 2 additions & 4 deletions vet.sh
Expand Up @@ -41,11 +41,9 @@ 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
source ./protoc_installer.sh
pushd /home/runner/go
wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME}
unzip ${PROTOC_FILENAME}
download_protoc
bin/protoc --version
popd
elif not which protoc > /dev/null; then
Expand Down

0 comments on commit a880e4d

Please sign in to comment.