From 621d5106d2b8e254e667c7a0dcfe4dfbdc468b2b Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Tue, 11 Feb 2025 09:26:40 +1000 Subject: [PATCH 1/3] Duplicate protobuf v2 into v3 --- pkg/protobuf/v3/Makefile | 142 ++++++++++++++++++ pkg/protobuf/v3/bin/filter-grpc | 9 ++ pkg/protobuf/v3/bin/generate-include-paths | 17 +++ pkg/protobuf/v3/bin/generate-run-protoc | 18 +++ pkg/protobuf/v3/bin/install-protoc | 45 ++++++ pkg/protobuf/v3/bin/install-protoc-gen-go | 31 ++++ .../v3/bin/install-protoc-gen-go-primo | 30 ++++ pkg/protobuf/v3/bin/install-protoc-gen-swift | 21 +++ pkg/protobuf/v3/with-primo.mk | 17 +++ 9 files changed, 330 insertions(+) create mode 100644 pkg/protobuf/v3/Makefile create mode 100755 pkg/protobuf/v3/bin/filter-grpc create mode 100755 pkg/protobuf/v3/bin/generate-include-paths create mode 100755 pkg/protobuf/v3/bin/generate-run-protoc create mode 100755 pkg/protobuf/v3/bin/install-protoc create mode 100755 pkg/protobuf/v3/bin/install-protoc-gen-go create mode 100755 pkg/protobuf/v3/bin/install-protoc-gen-go-primo create mode 100755 pkg/protobuf/v3/bin/install-protoc-gen-swift create mode 100644 pkg/protobuf/v3/with-primo.mk diff --git a/pkg/protobuf/v3/Makefile b/pkg/protobuf/v3/Makefile new file mode 100644 index 0000000..b7aaf36 --- /dev/null +++ b/pkg/protobuf/v3/Makefile @@ -0,0 +1,142 @@ +MF_LANGUAGES += proto + +# PROTO_FILES is a space separated list of Protocol Buffers files. +PROTO_FILES += $(shell PATH="$(PATH)" git-find . -type f -name '*.proto' -not -regex '^_.*' -not -regex '.*\/_.*') + +# PROTO_GRPC_FILES is the subset of PROTO_FILES that contain gRPC service +# definitions. +PROTO_GRPC_FILES ?= $(shell $(MF_ROOT)/pkg/protobuf/v2/bin/filter-grpc $(PROTO_FILES)) + +# PROTO_INCLUDE_PATHS is a space separated list of include paths to use when +# building the .proto files from this repository. +# +# NOTE: Please avoid adding the current directory (.) in this variable as it may +# cause a "type redefinition" error from the protobuf compiler. The absolute +# path to the current repository is already added to the list of include paths +# via the 'artifacts/protobuf/args/common' file. +PROTO_INCLUDE_PATHS ?= + +# PROTOC_COMMAND is the path to the protoc command, if this is not specified +# the latest version of protoc is installed for the host operating system. +PROTOC_COMMAND ?= $(MF_PROJECT_ROOT)/artifacts/protobuf/bin/protoc + +# PROTO_PROTOC_PATH is the path to the script file that includes +# $(MF_PROJECT_ROOT)/artifacts/protobuf/bin directory in the PATH environment +# variable when the script is executed. +PROTO_PROTOC_PATH = $(MF_PROJECT_ROOT)/artifacts/protobuf/bin/run-protoc + +# PROTO_SWIFT_OUT is the relative path to the directory into which generated +# Swift files are placed. +PROTO_SWIFT_OUT ?= . + +################################################################################ + +$(MF_PROJECT_ROOT)/artifacts/protobuf/bin/protoc: + $(MF_ROOT)/pkg/protobuf/v2/bin/install-protoc "$(MF_PROJECT_ROOT)/artifacts/protobuf" + +artifacts/protobuf/bin/run-protoc: $(PROTOC_COMMAND) + $(MF_ROOT)/pkg/protobuf/v2/bin/generate-run-protoc $(PROTOC_COMMAND) "$@" + +artifacts/protobuf/bin/protoc-gen-go artifacts/protobuf/bin/protoc-gen-go-grpc: go.mod + $(MF_ROOT)/pkg/protobuf/v2/bin/install-protoc-gen-go "$(MF_PROJECT_ROOT)/$(@D)" + +artifacts/protobuf/bin/protoc-gen-swift artifacts/protobuf/bin/protoc-gen-grpc-swift: + $(MF_ROOT)/pkg/protobuf/v2/bin/install-protoc-gen-swift "$(MF_PROJECT_ROOT)/artifacts/protobuf/bin" + +artifacts/protobuf/args/common: + @mkdir -p "$(@D)" + echo $(addprefix --proto_path=,$(PROTO_INCLUDE_PATHS)) > "$@" + +# This Makefile provides the recipes used to build each language's source files +# from the .proto files, but it does NOT automatically add these source files to +# the GENERATED_FILES list. This is the responsibility of each language-specific +# Makefile; otherwise any project that included the protobuf Makefile would +# attempt to build source files for every supported language. +# +# NOTE: The $$(cat ...) syntax can NOT be swapped to $$(< ...). For reasons +# unknown this syntax does NOT work under Travis CI. + +# GO ########################################################################### + +# The recipes below includes each Go module available through `go list -m all` +# command as an import path for the protoc compiler. +# +# The path of the module is used as a virtual path to make the import in the +# .proto file look more natural. For example, with the module's path +# 'github.com/foo/bar' and the proto file 'dir/file.proto' in that module, the +# import statement becomes `import "github.com/foo/bar/dir/file.proto";` in the +# target .proto file. +# +# Please note that relative import paths are strongly discouraged as they +# require adding the current directory (.) to protoc's include path via a +# --proto_path parameter. This may cause "type redefinition" errors during +# protobuf file compilation because the same file is reachable via different +# paths. +# +# The absolute path to the current repository is already added to the list of +# include paths via the 'artifacts/protobuf/args/go' file. +# +# It is also critical to supply absolute paths to the .proto files when running +# the recipe below so that protoc can detect those files as part of this module +# (it uses a simple string prefix comparison). This works because the path to +# this module in the 'artifacts/protobuf/args/go' file is absolute. +# +# The --go_opt=module=... parameter strips the absolute module path prefix off +# the name of the generated files, ensuring they are placed relative to the root +# of the repository. +# +# For more information follow this link: +# https://developers.google.com/protocol-buffers/docs/reference/go-generated#invocation + +%.pb.go: %.proto artifacts/protobuf/bin/protoc-gen-go artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/go + $(PROTO_PROTOC_PATH) \ + $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/go) \ + $(MF_PROJECT_ROOT)/$(@D)/*.proto + +%_grpc.pb.go: %.proto artifacts/protobuf/bin/protoc-gen-go-grpc artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/go-grpc + $(PROTO_PROTOC_PATH) \ + $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/go-grpc) \ + $(MF_PROJECT_ROOT)/$(@D)/*.proto + +artifacts/protobuf/args/go: go.mod + go mod download all + @mkdir -p "$(@D)" + echo "--go_opt=module=$$(go list -m)" > "$@" + echo "--go_out=:." >> "$@" + $(MF_ROOT)/pkg/protobuf/v2/bin/generate-include-paths >> "$@" + +artifacts/protobuf/args/go-grpc: go.mod + go mod download all + @mkdir -p "$(@D)" + echo "--go-grpc_opt=module=$$(go list -m)" > "$@" + echo "--go-grpc_out=." >> "$@" + echo "--go-grpc_opt=require_unimplemented_servers=false" >> "$@" + $(MF_ROOT)/pkg/protobuf/v2/bin/generate-include-paths >> "$@" + +# SWIFT ######################################################################## + +$(PROTO_SWIFT_OUT)/%.pb.swift: %.proto artifacts/protobuf/bin/protoc-gen-swift artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/swift + @mkdir -p "$(PROTO_SWIFT_OUT)" + $(PROTO_PROTOC_PATH) \ + $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/swift) \ + $(MF_PROJECT_ROOT)/$(dir $<)*.proto + +$(PROTO_SWIFT_OUT)/%.grpc.swift: %.proto artifacts/protobuf/bin/protoc-gen-grpc-swift artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/swift-grpc + @mkdir -p "$(PROTO_SWIFT_OUT)" + $(PROTO_PROTOC_PATH) \ + $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/swift-grpc) \ + $(MF_PROJECT_ROOT)/$(dir $<)*.proto + +artifacts/protobuf/args/swift: + @mkdir -p "$(@D)" + echo "--proto_path=$(MF_PROJECT_ROOT)" > "$@" + echo "--plugin=$(MF_PROJECT_ROOT)/artifacts/protobuf/bin/proto-gen-swift" >> "$@" + echo "--swift_opt=Visibility=Public" >> "$@" + echo "--swift_out=$(PROTO_SWIFT_OUT)" >> "$@" + +artifacts/protobuf/args/swift-grpc: + @mkdir -p "$(@D)" + echo "--proto_path=$(MF_PROJECT_ROOT)" > "$@" + echo "--plugin=$(MF_PROJECT_ROOT)/artifacts/protobuf/bin/proto-gen-grpc-swift" >> "$@" + echo "--grpc-swift_opt=Visibility=Public" >> "$@" + echo "--grpc-swift_out=$(PROTO_SWIFT_OUT)" >> "$@" diff --git a/pkg/protobuf/v3/bin/filter-grpc b/pkg/protobuf/v3/bin/filter-grpc new file mode 100755 index 0000000..2465880 --- /dev/null +++ b/pkg/protobuf/v3/bin/filter-grpc @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -euo pipefail + +# This script accepts .proto filenames as arguments and prints the names of any +# of those files that contain at least one gRPC service definition. + +if [ $# -ne 0 ]; then + egrep --files-with-match '\bservice\s+\w+\s+{' "$@" || true +fi diff --git a/pkg/protobuf/v3/bin/generate-include-paths b/pkg/protobuf/v3/bin/generate-include-paths new file mode 100755 index 0000000..26849af --- /dev/null +++ b/pkg/protobuf/v3/bin/generate-include-paths @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail + +go_modules=($(go list -f '{{.Path}}={{.Dir}} ' -m all)) + +for i in "${go_modules[@]}" +do + path="${i%=*}" + dir="${i#*=}" + proto_file_count=$(find $dir -type f -iname '*.proto' | wc -l) + + if [[ $proto_file_count -gt 0 ]]; then + # Print the protobuf include path to the standard output + # if the Go module directory contains *.proto files. + echo "--proto_path=$path=$(realpath "$dir")" + fi +done diff --git a/pkg/protobuf/v3/bin/generate-run-protoc b/pkg/protobuf/v3/bin/generate-run-protoc new file mode 100755 index 0000000..d7c7b55 --- /dev/null +++ b/pkg/protobuf/v3/bin/generate-run-protoc @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euo pipefail + +protoc_path=$1 +run_protoc_path=$2 +run_protoc_dir=$(dirname "${run_protoc_path}") + +$(cat < "$run_protoc_path" +#!/usr/bin/env bash +set -euo pipefail + +# Configure PATH so that the protoc plugins installed by the Makefiles take +# precedence. +PATH="${run_protoc_dir}:\$PATH" "$protoc_path" "\${@}" +EOF +) + +chmod +x "$run_protoc_path" diff --git a/pkg/protobuf/v3/bin/install-protoc b/pkg/protobuf/v3/bin/install-protoc new file mode 100755 index 0000000..4641fe0 --- /dev/null +++ b/pkg/protobuf/v3/bin/install-protoc @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -euo pipefail + +PROTOC_ROOT="${1}" +shift + +case "$(uname -s)" in + "Darwin") + OS="osx";; + "Linux") + OS="linux";; + *) + OS="linux";; +esac + +ARCH="$(uname -m)" + +if [[ "$OS" == "osx" && "$ARCH" == "arm64" ]]; then + ARCH="aarch_64" +fi + +if [[ "$OS" == "linux" && "$ARCH" == "aarch64" ]]; then + ARCH="aarch_64" +fi + +VERSION="$(curl --head --silent https://github.com/protocolbuffers/protobuf/releases/latest | grep -i '^Location:' | egrep -o '[0-9]+.[0-9]+')" + +if [[ -f "${PROTOC_ROOT}/bin/protoc" ]]; then + LOCAL_VERSION="$(${PROTOC_ROOT}/bin/protoc --version | sed -e 's/.* //')" + [[ "${VERSION}" == "${LOCAL_VERSION}" ]] && exit 0 +fi + +ARCHIVE="protoc-${VERSION}-${OS}-${ARCH}.zip" +URL="https://github.com/protocolbuffers/protobuf/releases/download/v${VERSION}/${ARCHIVE}" + +mkdir -p "${PROTOC_ROOT}" +pushd "${PROTOC_ROOT}" +curl --fail --silent --show-error --location --output "${ARCHIVE}" "${URL}" +unzip -o "${ARCHIVE}" +if [[ ! -x bin/protoc ]]; then ## if bin/protoc already executable, skip chmod, see PR#107. + chmod +x bin/protoc +fi +find include/google -type d -exec chmod 755 {} \; +find include/google -type f -exec chmod 644 {} \; +popd diff --git a/pkg/protobuf/v3/bin/install-protoc-gen-go b/pkg/protobuf/v3/bin/install-protoc-gen-go new file mode 100755 index 0000000..b5d7c4e --- /dev/null +++ b/pkg/protobuf/v3/bin/install-protoc-gen-go @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Find the version of google.golang.org/protobuf in go.mod so we can install the +# same version of the protoc-gen-go tool. +if version="$(go list -f '{{.Version}}' -m "google.golang.org/protobuf" 2> /dev/null)"; then + echo "found google.golang.org/protobuf@$version in go.mod" + +# Otherwise, install the latest version. +else + version=latest + echo "did not find google.golang.org/protobuf in go.mod, defaulting to latest version" +fi + +export GOBIN="$1" +mkdir -p "$GOBIN" +cd "$GOBIN" + +# Create a go mod just for this binaries, to avoid messing with the project's +# own go.mod. We ignore +if [ ! -f go.mod ]; then + go mod init bin +fi + +# Install the code generators into this "bin" module. +go install "google.golang.org/protobuf/cmd/protoc-gen-go@$version" +go install "google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest" + +# Touch the go.mod file so that it is no longer considered out of date by make, +# even if nothing actually changed. +touch go.mod diff --git a/pkg/protobuf/v3/bin/install-protoc-gen-go-primo b/pkg/protobuf/v3/bin/install-protoc-gen-go-primo new file mode 100755 index 0000000..14eb372 --- /dev/null +++ b/pkg/protobuf/v3/bin/install-protoc-gen-go-primo @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Find the version of google.golang.org/protobuf in go.mod so we can install the +# same version of the protoc-gen-go tool. +if version="$(go list -f '{{.Version}}' -m "github.com/dogmatiq/primo" 2> /dev/null)"; then + echo "found github.com/dogmatiq/primo@$version in go.mod" + +# Otherwise, install the latest version. +else + version=latest + echo "did not find github.com/dogmatiq/primo in go.mod, defaulting to latest version" +fi + +export GOBIN="$1" +mkdir -p "$GOBIN" +cd "$GOBIN" + +# Create a go mod just for this binaries, to avoid messing with the project's +# own go.mod. We ignore +if [ ! -f go.mod ]; then + go mod init bin +fi + +# Install the code generators into this "bin" module. +go install "github.com/dogmatiq/primo/cmd/protoc-gen-go-primo@$version" + +# Touch the go.mod file so that it is no longer considered out of date by make, +# even if nothing actually changed. +touch go.mod diff --git a/pkg/protobuf/v3/bin/install-protoc-gen-swift b/pkg/protobuf/v3/bin/install-protoc-gen-swift new file mode 100755 index 0000000..343fedd --- /dev/null +++ b/pkg/protobuf/v3/bin/install-protoc-gen-swift @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -euo pipefail + +PROTOC_ROOT="${1}" + +case "$(uname -s)" in + "Darwin") + IS_LINUX=false;; + *) + IS_LINUX=true;; +esac + +QUERY=".assets[] | select(.name | contains(\"linux-x86_64\") == ${IS_LINUX}) | .browser_download_url" +URL=$(curl https://api.github.com/repos/grpc/grpc-swift/releases/184697892 | jq -r "${QUERY}") +ARCHIVE="protoc-gen-swift.zip" + +mkdir -p "${PROTOC_ROOT}" +pushd "${PROTOC_ROOT}" +curl --fail --silent --show-error --location --output "${ARCHIVE}" "${URL}" +unzip -oj "${ARCHIVE}" +popd diff --git a/pkg/protobuf/v3/with-primo.mk b/pkg/protobuf/v3/with-primo.mk new file mode 100644 index 0000000..e876540 --- /dev/null +++ b/pkg/protobuf/v3/with-primo.mk @@ -0,0 +1,17 @@ +GENERATED_FILES += $(foreach f,$(PROTO_FILES:.proto=_primo.pb.go),$(if $(findstring /_,/$f),,$f)) + +artifacts/protobuf/bin/protoc-gen-go-primo: go.mod + $(MF_ROOT)/pkg/protobuf/v2/bin/install-protoc-gen-go-primo "$(MF_PROJECT_ROOT)/$(@D)" + +%_primo.pb.go: %.proto artifacts/protobuf/bin/protoc-gen-go-primo artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/go-primo + $(PROTO_PROTOC_PATH) \ + $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/go-primo) \ + $(MF_PROJECT_ROOT)/$(@D)/*.proto + +artifacts/protobuf/args/go-primo: go.mod + go mod download all + @mkdir -p "$(@D)" + echo "--plugin protoc-gen-go-primo=artifacts/protobuf/bin/protoc-gen-go-primo" > "$@" + echo "--go-primo_opt=module=$$(go list -m)" >> "$@" + echo "--go-primo_out=." >> "$@" + $(MF_ROOT)/pkg/protobuf/v2/bin/generate-include-paths >> "$@" From a1380cff4f0eab0151440d183dd8ac64aaede6d9 Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Tue, 11 Feb 2025 09:30:19 +1000 Subject: [PATCH 2/3] Replace instances of v2 with v3 --- pkg/protobuf/v3/Makefile | 14 +++++++------- pkg/protobuf/v3/with-primo.mk | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/protobuf/v3/Makefile b/pkg/protobuf/v3/Makefile index b7aaf36..204cc6f 100644 --- a/pkg/protobuf/v3/Makefile +++ b/pkg/protobuf/v3/Makefile @@ -5,7 +5,7 @@ PROTO_FILES += $(shell PATH="$(PATH)" git-find . -type f -name '*.proto' -not -r # PROTO_GRPC_FILES is the subset of PROTO_FILES that contain gRPC service # definitions. -PROTO_GRPC_FILES ?= $(shell $(MF_ROOT)/pkg/protobuf/v2/bin/filter-grpc $(PROTO_FILES)) +PROTO_GRPC_FILES ?= $(shell $(MF_ROOT)/pkg/protobuf/v3/bin/filter-grpc $(PROTO_FILES)) # PROTO_INCLUDE_PATHS is a space separated list of include paths to use when # building the .proto files from this repository. @@ -32,16 +32,16 @@ PROTO_SWIFT_OUT ?= . ################################################################################ $(MF_PROJECT_ROOT)/artifacts/protobuf/bin/protoc: - $(MF_ROOT)/pkg/protobuf/v2/bin/install-protoc "$(MF_PROJECT_ROOT)/artifacts/protobuf" + $(MF_ROOT)/pkg/protobuf/v3/bin/install-protoc "$(MF_PROJECT_ROOT)/artifacts/protobuf" artifacts/protobuf/bin/run-protoc: $(PROTOC_COMMAND) - $(MF_ROOT)/pkg/protobuf/v2/bin/generate-run-protoc $(PROTOC_COMMAND) "$@" + $(MF_ROOT)/pkg/protobuf/v3/bin/generate-run-protoc $(PROTOC_COMMAND) "$@" artifacts/protobuf/bin/protoc-gen-go artifacts/protobuf/bin/protoc-gen-go-grpc: go.mod - $(MF_ROOT)/pkg/protobuf/v2/bin/install-protoc-gen-go "$(MF_PROJECT_ROOT)/$(@D)" + $(MF_ROOT)/pkg/protobuf/v3/bin/install-protoc-gen-go "$(MF_PROJECT_ROOT)/$(@D)" artifacts/protobuf/bin/protoc-gen-swift artifacts/protobuf/bin/protoc-gen-grpc-swift: - $(MF_ROOT)/pkg/protobuf/v2/bin/install-protoc-gen-swift "$(MF_PROJECT_ROOT)/artifacts/protobuf/bin" + $(MF_ROOT)/pkg/protobuf/v3/bin/install-protoc-gen-swift "$(MF_PROJECT_ROOT)/artifacts/protobuf/bin" artifacts/protobuf/args/common: @mkdir -p "$(@D)" @@ -103,7 +103,7 @@ artifacts/protobuf/args/go: go.mod @mkdir -p "$(@D)" echo "--go_opt=module=$$(go list -m)" > "$@" echo "--go_out=:." >> "$@" - $(MF_ROOT)/pkg/protobuf/v2/bin/generate-include-paths >> "$@" + $(MF_ROOT)/pkg/protobuf/v3/bin/generate-include-paths >> "$@" artifacts/protobuf/args/go-grpc: go.mod go mod download all @@ -111,7 +111,7 @@ artifacts/protobuf/args/go-grpc: go.mod echo "--go-grpc_opt=module=$$(go list -m)" > "$@" echo "--go-grpc_out=." >> "$@" echo "--go-grpc_opt=require_unimplemented_servers=false" >> "$@" - $(MF_ROOT)/pkg/protobuf/v2/bin/generate-include-paths >> "$@" + $(MF_ROOT)/pkg/protobuf/v3/bin/generate-include-paths >> "$@" # SWIFT ######################################################################## diff --git a/pkg/protobuf/v3/with-primo.mk b/pkg/protobuf/v3/with-primo.mk index e876540..7ac1d10 100644 --- a/pkg/protobuf/v3/with-primo.mk +++ b/pkg/protobuf/v3/with-primo.mk @@ -1,7 +1,7 @@ GENERATED_FILES += $(foreach f,$(PROTO_FILES:.proto=_primo.pb.go),$(if $(findstring /_,/$f),,$f)) artifacts/protobuf/bin/protoc-gen-go-primo: go.mod - $(MF_ROOT)/pkg/protobuf/v2/bin/install-protoc-gen-go-primo "$(MF_PROJECT_ROOT)/$(@D)" + $(MF_ROOT)/pkg/protobuf/v3/bin/install-protoc-gen-go-primo "$(MF_PROJECT_ROOT)/$(@D)" %_primo.pb.go: %.proto artifacts/protobuf/bin/protoc-gen-go-primo artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/go-primo $(PROTO_PROTOC_PATH) \ @@ -14,4 +14,4 @@ artifacts/protobuf/args/go-primo: go.mod echo "--plugin protoc-gen-go-primo=artifacts/protobuf/bin/protoc-gen-go-primo" > "$@" echo "--go-primo_opt=module=$$(go list -m)" >> "$@" echo "--go-primo_out=." >> "$@" - $(MF_ROOT)/pkg/protobuf/v2/bin/generate-include-paths >> "$@" + $(MF_ROOT)/pkg/protobuf/v3/bin/generate-include-paths >> "$@" From 919260225069f6859625bbec7f8865d6eaf3435b Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Tue, 11 Feb 2025 09:46:52 +1000 Subject: [PATCH 3/3] Remove hopefully unused code from protobuf v3 --- pkg/protobuf/v3/Makefile | 39 ++++------------ pkg/protobuf/v3/bin/generate-run-protoc | 18 -------- pkg/protobuf/v3/bin/install-protoc | 45 ------------------- pkg/protobuf/v3/bin/install-protoc-gen-go | 31 ------------- .../v3/bin/install-protoc-gen-go-primo | 30 ------------- pkg/protobuf/v3/bin/install-protoc-gen-swift | 21 --------- pkg/protobuf/v3/with-primo.mk | 8 +--- 7 files changed, 10 insertions(+), 182 deletions(-) delete mode 100755 pkg/protobuf/v3/bin/generate-run-protoc delete mode 100755 pkg/protobuf/v3/bin/install-protoc delete mode 100755 pkg/protobuf/v3/bin/install-protoc-gen-go delete mode 100755 pkg/protobuf/v3/bin/install-protoc-gen-go-primo delete mode 100755 pkg/protobuf/v3/bin/install-protoc-gen-swift diff --git a/pkg/protobuf/v3/Makefile b/pkg/protobuf/v3/Makefile index 204cc6f..52d7cd9 100644 --- a/pkg/protobuf/v3/Makefile +++ b/pkg/protobuf/v3/Makefile @@ -16,33 +16,12 @@ PROTO_GRPC_FILES ?= $(shell $(MF_ROOT)/pkg/protobuf/v3/bin/filter-grpc $(PROTO_F # via the 'artifacts/protobuf/args/common' file. PROTO_INCLUDE_PATHS ?= -# PROTOC_COMMAND is the path to the protoc command, if this is not specified -# the latest version of protoc is installed for the host operating system. -PROTOC_COMMAND ?= $(MF_PROJECT_ROOT)/artifacts/protobuf/bin/protoc - -# PROTO_PROTOC_PATH is the path to the script file that includes -# $(MF_PROJECT_ROOT)/artifacts/protobuf/bin directory in the PATH environment -# variable when the script is executed. -PROTO_PROTOC_PATH = $(MF_PROJECT_ROOT)/artifacts/protobuf/bin/run-protoc - # PROTO_SWIFT_OUT is the relative path to the directory into which generated # Swift files are placed. PROTO_SWIFT_OUT ?= . ################################################################################ -$(MF_PROJECT_ROOT)/artifacts/protobuf/bin/protoc: - $(MF_ROOT)/pkg/protobuf/v3/bin/install-protoc "$(MF_PROJECT_ROOT)/artifacts/protobuf" - -artifacts/protobuf/bin/run-protoc: $(PROTOC_COMMAND) - $(MF_ROOT)/pkg/protobuf/v3/bin/generate-run-protoc $(PROTOC_COMMAND) "$@" - -artifacts/protobuf/bin/protoc-gen-go artifacts/protobuf/bin/protoc-gen-go-grpc: go.mod - $(MF_ROOT)/pkg/protobuf/v3/bin/install-protoc-gen-go "$(MF_PROJECT_ROOT)/$(@D)" - -artifacts/protobuf/bin/protoc-gen-swift artifacts/protobuf/bin/protoc-gen-grpc-swift: - $(MF_ROOT)/pkg/protobuf/v3/bin/install-protoc-gen-swift "$(MF_PROJECT_ROOT)/artifacts/protobuf/bin" - artifacts/protobuf/args/common: @mkdir -p "$(@D)" echo $(addprefix --proto_path=,$(PROTO_INCLUDE_PATHS)) > "$@" @@ -88,13 +67,13 @@ artifacts/protobuf/args/common: # For more information follow this link: # https://developers.google.com/protocol-buffers/docs/reference/go-generated#invocation -%.pb.go: %.proto artifacts/protobuf/bin/protoc-gen-go artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/go - $(PROTO_PROTOC_PATH) \ +%.pb.go: %.proto artifacts/protobuf/args/common artifacts/protobuf/args/go + protoc \ $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/go) \ $(MF_PROJECT_ROOT)/$(@D)/*.proto -%_grpc.pb.go: %.proto artifacts/protobuf/bin/protoc-gen-go-grpc artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/go-grpc - $(PROTO_PROTOC_PATH) \ +%_grpc.pb.go: %.proto artifacts/protobuf/args/common artifacts/protobuf/args/go-grpc + protoc \ $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/go-grpc) \ $(MF_PROJECT_ROOT)/$(@D)/*.proto @@ -115,28 +94,26 @@ artifacts/protobuf/args/go-grpc: go.mod # SWIFT ######################################################################## -$(PROTO_SWIFT_OUT)/%.pb.swift: %.proto artifacts/protobuf/bin/protoc-gen-swift artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/swift +$(PROTO_SWIFT_OUT)/%.pb.swift: %.proto artifacts/protobuf/args/common artifacts/protobuf/args/swift @mkdir -p "$(PROTO_SWIFT_OUT)" - $(PROTO_PROTOC_PATH) \ + protoc \ $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/swift) \ $(MF_PROJECT_ROOT)/$(dir $<)*.proto -$(PROTO_SWIFT_OUT)/%.grpc.swift: %.proto artifacts/protobuf/bin/protoc-gen-grpc-swift artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/swift-grpc +$(PROTO_SWIFT_OUT)/%.grpc.swift: %.proto artifacts/protobuf/args/common artifacts/protobuf/args/swift-grpc @mkdir -p "$(PROTO_SWIFT_OUT)" - $(PROTO_PROTOC_PATH) \ + protoc \ $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/swift-grpc) \ $(MF_PROJECT_ROOT)/$(dir $<)*.proto artifacts/protobuf/args/swift: @mkdir -p "$(@D)" echo "--proto_path=$(MF_PROJECT_ROOT)" > "$@" - echo "--plugin=$(MF_PROJECT_ROOT)/artifacts/protobuf/bin/proto-gen-swift" >> "$@" echo "--swift_opt=Visibility=Public" >> "$@" echo "--swift_out=$(PROTO_SWIFT_OUT)" >> "$@" artifacts/protobuf/args/swift-grpc: @mkdir -p "$(@D)" echo "--proto_path=$(MF_PROJECT_ROOT)" > "$@" - echo "--plugin=$(MF_PROJECT_ROOT)/artifacts/protobuf/bin/proto-gen-grpc-swift" >> "$@" echo "--grpc-swift_opt=Visibility=Public" >> "$@" echo "--grpc-swift_out=$(PROTO_SWIFT_OUT)" >> "$@" diff --git a/pkg/protobuf/v3/bin/generate-run-protoc b/pkg/protobuf/v3/bin/generate-run-protoc deleted file mode 100755 index d7c7b55..0000000 --- a/pkg/protobuf/v3/bin/generate-run-protoc +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -protoc_path=$1 -run_protoc_path=$2 -run_protoc_dir=$(dirname "${run_protoc_path}") - -$(cat < "$run_protoc_path" -#!/usr/bin/env bash -set -euo pipefail - -# Configure PATH so that the protoc plugins installed by the Makefiles take -# precedence. -PATH="${run_protoc_dir}:\$PATH" "$protoc_path" "\${@}" -EOF -) - -chmod +x "$run_protoc_path" diff --git a/pkg/protobuf/v3/bin/install-protoc b/pkg/protobuf/v3/bin/install-protoc deleted file mode 100755 index 4641fe0..0000000 --- a/pkg/protobuf/v3/bin/install-protoc +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -PROTOC_ROOT="${1}" -shift - -case "$(uname -s)" in - "Darwin") - OS="osx";; - "Linux") - OS="linux";; - *) - OS="linux";; -esac - -ARCH="$(uname -m)" - -if [[ "$OS" == "osx" && "$ARCH" == "arm64" ]]; then - ARCH="aarch_64" -fi - -if [[ "$OS" == "linux" && "$ARCH" == "aarch64" ]]; then - ARCH="aarch_64" -fi - -VERSION="$(curl --head --silent https://github.com/protocolbuffers/protobuf/releases/latest | grep -i '^Location:' | egrep -o '[0-9]+.[0-9]+')" - -if [[ -f "${PROTOC_ROOT}/bin/protoc" ]]; then - LOCAL_VERSION="$(${PROTOC_ROOT}/bin/protoc --version | sed -e 's/.* //')" - [[ "${VERSION}" == "${LOCAL_VERSION}" ]] && exit 0 -fi - -ARCHIVE="protoc-${VERSION}-${OS}-${ARCH}.zip" -URL="https://github.com/protocolbuffers/protobuf/releases/download/v${VERSION}/${ARCHIVE}" - -mkdir -p "${PROTOC_ROOT}" -pushd "${PROTOC_ROOT}" -curl --fail --silent --show-error --location --output "${ARCHIVE}" "${URL}" -unzip -o "${ARCHIVE}" -if [[ ! -x bin/protoc ]]; then ## if bin/protoc already executable, skip chmod, see PR#107. - chmod +x bin/protoc -fi -find include/google -type d -exec chmod 755 {} \; -find include/google -type f -exec chmod 644 {} \; -popd diff --git a/pkg/protobuf/v3/bin/install-protoc-gen-go b/pkg/protobuf/v3/bin/install-protoc-gen-go deleted file mode 100755 index b5d7c4e..0000000 --- a/pkg/protobuf/v3/bin/install-protoc-gen-go +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -# Find the version of google.golang.org/protobuf in go.mod so we can install the -# same version of the protoc-gen-go tool. -if version="$(go list -f '{{.Version}}' -m "google.golang.org/protobuf" 2> /dev/null)"; then - echo "found google.golang.org/protobuf@$version in go.mod" - -# Otherwise, install the latest version. -else - version=latest - echo "did not find google.golang.org/protobuf in go.mod, defaulting to latest version" -fi - -export GOBIN="$1" -mkdir -p "$GOBIN" -cd "$GOBIN" - -# Create a go mod just for this binaries, to avoid messing with the project's -# own go.mod. We ignore -if [ ! -f go.mod ]; then - go mod init bin -fi - -# Install the code generators into this "bin" module. -go install "google.golang.org/protobuf/cmd/protoc-gen-go@$version" -go install "google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest" - -# Touch the go.mod file so that it is no longer considered out of date by make, -# even if nothing actually changed. -touch go.mod diff --git a/pkg/protobuf/v3/bin/install-protoc-gen-go-primo b/pkg/protobuf/v3/bin/install-protoc-gen-go-primo deleted file mode 100755 index 14eb372..0000000 --- a/pkg/protobuf/v3/bin/install-protoc-gen-go-primo +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -# Find the version of google.golang.org/protobuf in go.mod so we can install the -# same version of the protoc-gen-go tool. -if version="$(go list -f '{{.Version}}' -m "github.com/dogmatiq/primo" 2> /dev/null)"; then - echo "found github.com/dogmatiq/primo@$version in go.mod" - -# Otherwise, install the latest version. -else - version=latest - echo "did not find github.com/dogmatiq/primo in go.mod, defaulting to latest version" -fi - -export GOBIN="$1" -mkdir -p "$GOBIN" -cd "$GOBIN" - -# Create a go mod just for this binaries, to avoid messing with the project's -# own go.mod. We ignore -if [ ! -f go.mod ]; then - go mod init bin -fi - -# Install the code generators into this "bin" module. -go install "github.com/dogmatiq/primo/cmd/protoc-gen-go-primo@$version" - -# Touch the go.mod file so that it is no longer considered out of date by make, -# even if nothing actually changed. -touch go.mod diff --git a/pkg/protobuf/v3/bin/install-protoc-gen-swift b/pkg/protobuf/v3/bin/install-protoc-gen-swift deleted file mode 100755 index 343fedd..0000000 --- a/pkg/protobuf/v3/bin/install-protoc-gen-swift +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -PROTOC_ROOT="${1}" - -case "$(uname -s)" in - "Darwin") - IS_LINUX=false;; - *) - IS_LINUX=true;; -esac - -QUERY=".assets[] | select(.name | contains(\"linux-x86_64\") == ${IS_LINUX}) | .browser_download_url" -URL=$(curl https://api.github.com/repos/grpc/grpc-swift/releases/184697892 | jq -r "${QUERY}") -ARCHIVE="protoc-gen-swift.zip" - -mkdir -p "${PROTOC_ROOT}" -pushd "${PROTOC_ROOT}" -curl --fail --silent --show-error --location --output "${ARCHIVE}" "${URL}" -unzip -oj "${ARCHIVE}" -popd diff --git a/pkg/protobuf/v3/with-primo.mk b/pkg/protobuf/v3/with-primo.mk index 7ac1d10..0bd39ec 100644 --- a/pkg/protobuf/v3/with-primo.mk +++ b/pkg/protobuf/v3/with-primo.mk @@ -1,17 +1,13 @@ GENERATED_FILES += $(foreach f,$(PROTO_FILES:.proto=_primo.pb.go),$(if $(findstring /_,/$f),,$f)) -artifacts/protobuf/bin/protoc-gen-go-primo: go.mod - $(MF_ROOT)/pkg/protobuf/v3/bin/install-protoc-gen-go-primo "$(MF_PROJECT_ROOT)/$(@D)" - -%_primo.pb.go: %.proto artifacts/protobuf/bin/protoc-gen-go-primo artifacts/protobuf/bin/run-protoc artifacts/protobuf/args/common artifacts/protobuf/args/go-primo - $(PROTO_PROTOC_PATH) \ +%_primo.pb.go: %.proto artifacts/protobuf/args/common artifacts/protobuf/args/go-primo + protoc \ $$(cat artifacts/protobuf/args/common artifacts/protobuf/args/go-primo) \ $(MF_PROJECT_ROOT)/$(@D)/*.proto artifacts/protobuf/args/go-primo: go.mod go mod download all @mkdir -p "$(@D)" - echo "--plugin protoc-gen-go-primo=artifacts/protobuf/bin/protoc-gen-go-primo" > "$@" echo "--go-primo_opt=module=$$(go list -m)" >> "$@" echo "--go-primo_out=." >> "$@" $(MF_ROOT)/pkg/protobuf/v3/bin/generate-include-paths >> "$@"