Skip to content

Commit

Permalink
hack: fix broken build for ARM64-arch
Browse files Browse the repository at this point in the history
When building over AWS-Graviton, need to use ARM64 (aarch64) protoc
binaries. Upstream project does not provides proper ARM64 binaries for
protoc-gen-go and protoc-gen-go-grpc so we need to build it from source
within temporary directory.

Refs RHSTOR-2044

Signed-off-by: Shachar Sharon <[email protected]>
  • Loading branch information
synarete committed Jan 9, 2023
1 parent 02fd448 commit 7d3573b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
4 changes: 2 additions & 2 deletions hack/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

GO111MODULE="on"
GOPROXY="https://proxy.golang.org"
GOROOT="${GOROOT:-go env GOROOT}"
GOROOT="${GOROOT:-$(go env GOROOT)}"
GOOS="${GOOS:-linux}"
GOARCH="${GOARCH:-amd64}"
GOARCH="${GOARCH:-$(go env GOARCH)}"

GO_LINT_IMG_LOCATION="${GO_LINT_IMG_LOCATION:-golangci/golangci-lint}"
GO_LINT_IMG_TAG="${GO_LINT_IMG_TAG:-v1.49.0}"
Expand Down
52 changes: 35 additions & 17 deletions hack/gen-protobuf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,61 @@ set -o pipefail

source hack/common.sh

case "${GOARCH}" in
"arm64"|"aarch64")
PROTOC_ARCH="aarch_64"
PROTOC_GEN_GO_ARCH="arm64"
;;
"x86_64"|"amd64")
PROTOC_ARCH="x86_64"
PROTOC_GEN_GO_ARCH="amd64"
;;
*)
echo "unknown ARCH=${GOARCH}; using default x86_64/amd64"
PROTOC_ARCH="x86_64"
PROTOC_GEN_GO_ARCH="amd64"
;;
esac

mkdir -p ${OUTDIR} ${OUTDIR_GRPC} ${OUTDIR_PROTO_DIST} ${OUTDIR_PROTO_GOOGLE}
OUTDIR_GRPC_TMP=$(realpath ${OUTDIR_GRPC}/tmp/)

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
# download protoc
wget -P ${OUTDIR_PROTO_DIST} --backups=1 \
https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-${PROTOC_ARCH}.zip
unzip -jod ${OUTDIR_GRPC} ${OUTDIR_PROTO_DIST}/protoc-${PROTOC_VERSION}-linux-${PROTOC_ARCH}.zip bin/protoc

# extract descriptor.proto and wrappers.proto
unzip -jod ${OUTDIR_PROTO_GOOGLE} ${OUTDIR_PROTO_DIST}/protoc-${PROTOC_VERSION}-linux-x86_64.zip \
unzip -jod ${OUTDIR_PROTO_GOOGLE} ${OUTDIR_PROTO_DIST}/protoc-${PROTOC_VERSION}-linux-${PROTOC_ARCH}.zip \
include/google/protobuf/descriptor.proto \
include/google/protobuf/wrappers.proto
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
# install protoc-gen-go
mkdir -p "${OUTDIR_GRPC_TMP}"
GOBIN="${OUTDIR_GRPC_TMP}" go install "google.golang.org/protobuf/cmd/protoc-gen-go@v${PROTOC_GEN_GO_VERSION}"
mv "${OUTDIR_GRPC_TMP}/protoc-gen-go" "${OUTDIR_GRPC}"
rm -rf "${OUTDIR_GRPC_TMP}"
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-grpc
mkdir -p "${OUTDIR_GRPC_TMP}"
GOBIN="${OUTDIR_GRPC_TMP}" go install "google.golang.org/grpc/cmd/protoc-gen-go-grpc@v${PROTOC_GEN_GO_GRPC_VERSION}"
mv "${OUTDIR_GRPC_TMP}/protoc-gen-go-grpc" "${OUTDIR_GRPC}"
rm -rf "${OUTDIR_GRPC_TMP}"
fi


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"
# 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"
done

0 comments on commit 7d3573b

Please sign in to comment.