Skip to content

Commit

Permalink
Improve & simplify the protobuf generation script
Browse files Browse the repository at this point in the history
Specifically earlier we were not able to generate the protobuf
files on mac os or arm64 machines. This fixes that & overall simplifies
the script.

Signed-off-by: Malay Kumar Parida <mparida@redhat.com>
  • Loading branch information
malayparida2000 committed Sep 6, 2023
1 parent 8a18a74 commit 5656c14
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
13 changes: 8 additions & 5 deletions hack/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ OCS_MUST_GATHER_DIR="${OCS_MUST_GATHER_DIR:-ocs-must-gather}"
OCP_MUST_GATHER_DIR="${OCP_MUST_GATHER_DIR:-ocp-must-gather}"

# Protobuf
PROTOC_VERSION="3.14.0"
PROTOC_VERSION="3.20.0"
PROTOC_GEN_GO_VERSION="1.26.0"
PROTOC_GEN_GO_GRPC_VERSION="1.1.0"
OUTDIR_GRPC="build/_output/grpc"
OUTDIR_PROTO_DIST="build/_output/grpc/dist"
OUTDIR_PROTO_GOOGLE="build/_output/grpc/google/protobuf"

GRPC_BIN="${LOCALBIN}/grpc"
PROTOC="${GRPC_BIN}/protoc"
PROTO_GOOGLE="${GRPC_BIN}/google/protobuf"
PROTOC_GEN_GO="${GRPC_BIN}/protoc-gen-go"
PROTOC_GEN_GO_GRPC="${GRPC_BIN}/protoc-gen-go-grpc"

# gRPC services
declare -a SERVICES=("provider")
SERVICES=("provider")
67 changes: 39 additions & 28 deletions hack/gen-protobuf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,54 @@ set -o pipefail

source hack/common.sh

mkdir -p "${LOCALBIN}" "${GRPC_BIN}" "${PROTO_GOOGLE}"

mkdir -p ${OUTDIR} ${OUTDIR_GRPC} ${OUTDIR_PROTO_DIST} ${OUTDIR_PROTO_GOOGLE}

EXISTING_PROTOC=$(${OUTDIR_GRPC}/protoc --version 2> /dev/null)
EXISTING_PROTOC_GEN_GO=$(${OUTDIR_GRPC}/protoc-gen-go --version 2>&1 | grep protoc-gen-go)
EXISTING_PROTOC_GEN_GO_GRPC=$(${OUTDIR_GRPC}/protoc-gen-go-grpc --version 2> /dev/null)

if [[ $EXISTING_PROTOC != "libprotoc ${PROTOC_VERSION}" ]]; then
# download protoc
wget -P ${OUTDIR_PROTO_DIST} --backups=1 \
https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.zip
unzip -jod ${OUTDIR_GRPC} ${OUTDIR_PROTO_DIST}/protoc-${PROTOC_VERSION}-linux-x86_64.zip bin/protoc
if [[ ${GOHOSTOS} == "linux" ]]; then
PROTOC_OS="linux"
elif [[ ${GOHOSTOS} == "darwin" ]]; then
PROTOC_OS="osx"
fi

# extract descriptor.proto and wrappers.proto
unzip -jod ${OUTDIR_PROTO_GOOGLE} ${OUTDIR_PROTO_DIST}/protoc-${PROTOC_VERSION}-linux-x86_64.zip \
include/google/protobuf/descriptor.proto \
include/google/protobuf/wrappers.proto
if [[ ${GOHOSTARCH} == "amd64" ]]; then
PROTOC_ARCH="x86_64"
elif [[ ${GOHOSTARCH} == "arm64" ]]; then
PROTOC_ARCH="aarch_64"
fi

if [[ $EXISTING_PROTOC_GEN_GO != "protoc-gen-go v${PROTOC_GEN_GO_VERSION}" ]]; then
# download protoc-gen-go
wget -P ${OUTDIR_PROTO_DIST} --backups=1 \
https://github.com/protocolbuffers/protobuf-go/releases/download/v${PROTOC_GEN_GO_VERSION}/protoc-gen-go.v${PROTOC_GEN_GO_VERSION}.linux.386.tar.gz
tar -C ${OUTDIR_GRPC} -zxvf ${OUTDIR_PROTO_DIST}/protoc-gen-go.v${PROTOC_GEN_GO_VERSION}.linux.386.tar.gz protoc-gen-go
PROTOC_ZIP="protoc-${PROTOC_VERSION}-${PROTOC_OS}-${PROTOC_ARCH}.zip"
PROTOC_DL_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/${PROTOC_ZIP}"

# install protoc
if [ ! -x "${PROTOC}" ] || [ "$(${PROTOC} --version)" != "libprotoc ${PROTOC_VERSION}" ]; then
echo "Installing protoc at ${PROTOC}"
echo "Downloading ${PROTOC_DL_URL}"
curl -LO "${PROTOC_DL_URL}"
unzip -jod "${GRPC_BIN}" "${PROTOC_ZIP}" bin/protoc
unzip -jod "${PROTO_GOOGLE}" "${PROTOC_ZIP}" include/google/protobuf/descriptor.proto include/google/protobuf/wrappers.proto
rm "${PROTOC_ZIP}"
else
echo "protoc already present at ${PROTOC}"
fi

if [[ $EXISTING_PROTOC_GEN_GO_GRPC != "protoc-gen-go-grpc ${PROTOC_GEN_GO_GRPC_VERSION}" ]]; then
# download protoc-gen-go-grpc
wget -P ${OUTDIR_PROTO_DIST} --backups=1 \
https://github.com/grpc/grpc-go/releases/download/cmd%2Fprotoc-gen-go-grpc%2Fv${PROTOC_GEN_GO_GRPC_VERSION}/protoc-gen-go-grpc.v${PROTOC_GEN_GO_GRPC_VERSION}.linux.386.tar.gz
tar -C ${OUTDIR_GRPC} -zxvf ${OUTDIR_PROTO_DIST}/protoc-gen-go-grpc.v${PROTOC_GEN_GO_GRPC_VERSION}.linux.386.tar.gz ./protoc-gen-go-grpc
# install protoc-gen-go
if [ ! -x "${PROTOC_GEN_GO}" ] || [ "$(${PROTOC_GEN_GO} --version)" != "protoc-gen-go v${PROTOC_GEN_GO_VERSION}" ]; then
echo "Installing protoc-gen-go at ${PROTOC_GEN_GO}"
GOBIN=${GRPC_BIN} go install google.golang.org/protobuf/cmd/protoc-gen-go@v${PROTOC_GEN_GO_VERSION}
else
echo "protoc-gen-go already present at ${PROTOC_GEN_GO}"
fi

# install protoc-gen-go-grpc
if [ ! -x "${PROTOC_GEN_GO_GRPC}" ] || [ "$(${PROTOC_GEN_GO_GRPC} --version)" != "protoc-gen-go-grpc ${PROTOC_GEN_GO_GRPC_VERSION}" ]; then
echo "Installing protoc-gen-go-grpc at ${PROTOC_GEN_GO_GRPC}"
GOBIN=${GRPC_BIN} go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v${PROTOC_GEN_GO_GRPC_VERSION}
else
echo "protoc-gen-go-grpc already present at ${PROTOC_GEN_GO_GRPC}"
fi

# generate code
for service in "${SERVICES[@]}"
do
# generate code
./${OUTDIR_GRPC}/protoc --proto_path="services/${service}/proto" --go_out="services/${service}/pb" --plugin=${OUTDIR_GRPC}/protoc-gen-go "${service}.proto"
./${OUTDIR_GRPC}/protoc --proto_path="services/${service}/proto" --go-grpc_out="services/${service}/pb" --plugin=${OUTDIR_GRPC}/protoc-gen-go-grpc "${service}.proto"
${PROTOC} --proto_path="services/${service}/proto" --go_out="services/${service}/pb" --plugin="${PROTOC_GEN_GO}" "${service}.proto"
${PROTOC} --proto_path="services/${service}/proto" --go-grpc_out="services/${service}/pb" --plugin="${PROTOC_GEN_GO_GRPC}" "${service}.proto"
done
2 changes: 1 addition & 1 deletion services/provider/pb/provider.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5656c14

Please sign in to comment.