|
13 | 13 | # See the License for the specific language governing permissions and
|
14 | 14 | # limitations under the License.
|
15 | 15 | #
|
16 |
| -# This Makefile.proto has rules to generate *.pb.go files in |
17 |
| -# `exporters/otlp/internal/opentelemetry-proto-gen` from the .proto files in |
18 |
| -# `exporters/otlp/internal/opentelemetry-proto` using protoc with a go plugin. |
| 16 | +# This Makefile.proto has rules to generate go code for otlp |
| 17 | +# exporter. It does it by copying the proto files from |
| 18 | +# `exporters/otlp/internal/opentelemetry-proto` (which is a |
| 19 | +# submodule that needs to be checked out) into `gen/proto`, changing |
| 20 | +# the go_package option to a valid string, generating the go files and |
| 21 | +# finally copying the files into the module. The files are not |
| 22 | +# generated in place, because protoc generates a too-deep directory |
| 23 | +# structure. |
19 | 24 | #
|
20 |
| -# The protoc binary and other tools are sourced from a docker image |
21 |
| -# `PROTOC_IMAGE`. |
| 25 | +# Currently, all the generated code is in |
| 26 | +# `exporters/otlp/internal/opentelemetry-proto-gen`. |
22 | 27 | #
|
23 |
| -# Prereqs: The archiving utility `pax` is installed. |
| 28 | +# Prereqs: wget (for downloading the zip file with protoc binary), |
| 29 | +# unzip (for unpacking the archive), rsync (for copying back the |
| 30 | +# generated files). |
24 | 31 |
|
25 |
| -PROTOC_IMAGE := namely/protoc-all:1.29_2 |
26 |
| -PROTOBUF_VERSION := v1 |
27 |
| -OTEL_PROTO_SUBMODULE := exporters/otlp/internal/opentelemetry-proto |
28 |
| -PROTOBUF_GEN_DIR := exporters/otlp/internal/opentelemetry-proto-gen |
29 |
| -PROTOBUF_TEMP_DIR := gen/pb-go |
30 |
| -PROTO_SOURCE_DIR := gen/proto |
31 |
| -SUBMODULE_PROTO_FILES := $(wildcard $(OTEL_PROTO_SUBMODULE)/opentelemetry/proto/*/$(PROTOBUF_VERSION)/*.proto \ |
32 |
| - $(OTEL_PROTO_SUBMODULE)/opentelemetry/proto/collector/*/$(PROTOBUF_VERSION)/*.proto) |
33 |
| -SOURCE_PROTO_FILES := $(subst $(OTEL_PROTO_SUBMODULE),$(PROTO_SOURCE_DIR),$(SUBMODULE_PROTO_FILES)) |
| 32 | +PROTOC_VERSION := 3.14.0 |
34 | 33 |
|
35 |
| -default: protobuf |
| 34 | +TOOLS_DIR := $(abspath ./.tools) |
| 35 | +TOOLS_MOD_DIR := ./internal/tools |
| 36 | +PROTOBUF_VERSION := v1 |
| 37 | +OTEL_PROTO_SUBMODULE := exporters/otlp/internal/opentelemetry-proto |
| 38 | +GEN_TEMP_DIR := gen |
| 39 | +SUBMODULE_PROTO_FILES := $(wildcard $(OTEL_PROTO_SUBMODULE)/opentelemetry/proto/*/$(PROTOBUF_VERSION)/*.proto) $(wildcard $(OTEL_PROTO_SUBMODULE)/opentelemetry/proto/collector/*/$(PROTOBUF_VERSION)/*.proto) |
36 | 40 |
|
37 |
| -.PHONY: protobuf protobuf-source gen-protobuf copy-protobufs |
38 |
| -protobuf: protobuf-source gen-protobuf copy-protobufs |
| 41 | +ifeq ($(strip $(SUBMODULE_PROTO_FILES)),) |
| 42 | +$(error Submodule at $(OTEL_PROTO_SUBMODULE) is not checked out, use "git submodule update --init") |
| 43 | +endif |
39 | 44 |
|
40 |
| -protobuf-source: $(SOURCE_PROTO_FILES) | $(PROTO_SOURCE_DIR)/ |
| 45 | +PROTOBUF_GEN_DIR := exporters/otlp/internal/opentelemetry-proto-gen |
| 46 | +PROTOBUF_TEMP_DIR := $(GEN_TEMP_DIR)/pb-go |
| 47 | +PROTO_SOURCE_DIR := $(GEN_TEMP_DIR)/proto |
| 48 | +SOURCE_PROTO_FILES := $(subst $(OTEL_PROTO_SUBMODULE),$(PROTO_SOURCE_DIR),$(SUBMODULE_PROTO_FILES)) |
41 | 49 |
|
42 |
| -# Changes go_package in .proto file to point to repo-local location |
43 |
| -define exec-replace-pkgname |
44 |
| -sed 's,go_package = "github.com/open-telemetry/opentelemetry-proto/gen/go,go_package = "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen,' < $(1) > $(2) |
| 50 | +.DEFAULT_GOAL := protobuf |
45 | 51 |
|
46 |
| -endef |
| 52 | +UNAME_S := $(shell uname -s) |
| 53 | +UNAME_M := $(shell uname -m) |
| 54 | + |
| 55 | +ifeq ($(UNAME_S),Linux) |
| 56 | + |
| 57 | +PROTOC_OS := linux |
| 58 | +PROTOC_ARCH := $(UNAME_M) |
| 59 | + |
| 60 | +else ifeq ($(UNAME_S),Darwin) |
| 61 | + |
| 62 | +PROTOC_OS := osx |
| 63 | +PROTOC_ARCH := x86_64 |
| 64 | + |
| 65 | +endif |
47 | 66 |
|
48 |
| -# replace opentelemetry-proto package name by go.opentelemetry.io/otel specific version |
49 |
| -$(SOURCE_PROTO_FILES): $(PROTO_SOURCE_DIR)/%.proto: $(OTEL_PROTO_SUBMODULE)/%.proto |
50 |
| - @mkdir -p $(@D) |
51 |
| - $(call exec-replace-pkgname,$<,$@) |
| 67 | +PROTOC_ZIP_URL := https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-$(PROTOC_OS)-$(PROTOC_ARCH).zip |
52 | 68 |
|
53 |
| -# Command to run protoc using docker image |
54 |
| -define exec-protoc-all |
55 |
| -docker run -v `pwd`:/defs $(PROTOC_IMAGE) $(1) |
| 69 | +$(TOOLS_DIR)/PROTOC_$(PROTOC_VERSION): |
| 70 | + @rm -f "$(TOOLS_DIR)"/PROTOC_* && \ |
| 71 | + touch "$@" |
56 | 72 |
|
| 73 | +# Depend on a versioned file (like PROTOC_3.14.0), so when version |
| 74 | +# gets bumped, we will depend on a nonexistent file and thus download |
| 75 | +# a newer version. |
| 76 | +$(TOOLS_DIR)/protoc/bin/protoc: $(TOOLS_DIR)/PROTOC_$(PROTOC_VERSION) |
| 77 | + echo "Fetching protoc $(PROTOC_VERSION)" && \ |
| 78 | + rm -rf $(TOOLS_DIR)/protoc && \ |
| 79 | + wget -O $(TOOLS_DIR)/protoc.zip $(PROTOC_ZIP_URL) && \ |
| 80 | + unzip $(TOOLS_DIR)/protoc.zip -d $(TOOLS_DIR)/protoc-tmp && \ |
| 81 | + rm $(TOOLS_DIR)/protoc.zip && \ |
| 82 | + touch $(TOOLS_DIR)/protoc-tmp/bin/protoc && \ |
| 83 | + mv $(TOOLS_DIR)/protoc-tmp $(TOOLS_DIR)/protoc |
| 84 | + |
| 85 | +$(TOOLS_DIR)/protoc-gen-gogofast: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go |
| 86 | + cd $(TOOLS_MOD_DIR) && \ |
| 87 | + go build -o $(TOOLS_DIR)/protoc-gen-gogofast github.com/gogo/protobuf/protoc-gen-gogofast && \ |
| 88 | + go mod tidy |
| 89 | + |
| 90 | +# Return a sed expression for replacing the go_package option in proto |
| 91 | +# file with a one that's valid for us. |
| 92 | +# |
| 93 | +# Example: $(call get-sed-expr,$(PROTOBUF_GEN_DIR)) |
| 94 | +define get-sed-expr |
| 95 | +'s,go_package = "github.com/open-telemetry/opentelemetry-proto/gen/go,go_package = "go.opentelemetry.io/otel/$(1),' |
57 | 96 | endef
|
58 | 97 |
|
59 |
| -gen-protobuf: $(SOURCE_PROTO_FILES) | $(PROTOBUF_GEN_DIR)/ |
60 |
| - $(foreach file,$(subst ${PROTO_SOURCE_DIR}/,,$(SOURCE_PROTO_FILES)),$(call exec-protoc-all, -i $(PROTO_SOURCE_DIR) -f ${file} -l gogo -o ${PROTOBUF_TEMP_DIR})) |
| 98 | +.PHONY: protobuf |
| 99 | +protobuf: protobuf-source gen-protobuf copy-protobufs |
| 100 | + |
| 101 | +.PHONY: protobuf-source |
| 102 | +protobuf-source: $(SOURCE_PROTO_FILES) |
| 103 | + |
| 104 | +# This copies proto files from submodule into $(PROTO_SOURCE_DIR), |
| 105 | +# thus satisfying the $(SOURCE_PROTO_FILES) prerequisite. The copies |
| 106 | +# have their package name replaced by go.opentelemetry.io/otel. |
| 107 | +$(PROTO_SOURCE_DIR)/%.proto: $(OTEL_PROTO_SUBMODULE)/%.proto |
| 108 | + @ \ |
| 109 | + mkdir -p $(@D); \ |
| 110 | + sed -e $(call get-sed-expr,$(PROTOBUF_GEN_DIR)) "$<" >"$@.tmp"; \ |
| 111 | + mv "$@.tmp" "$@" |
61 | 112 |
|
62 |
| -# requires `pax` to be installed, as it has consistent options for both BSD (Darwin) and Linux |
63 |
| -copy-protobufs: | $(PROTOBUF_GEN_DIR)/ |
64 |
| - find ./$(PROTOBUF_TEMP_DIR)/go.opentelemetry.io/otel/$(PROTOBUF_GEN_DIR) -type f -print0 | \ |
65 |
| - pax -0 -s ',^./$(PROTOBUF_TEMP_DIR)/go.opentelemetry.io/otel/$(PROTOBUF_GEN_DIR),,' -rw ./$(PROTOBUF_GEN_DIR) |
| 113 | +.PHONY: gen-protobuf |
| 114 | +gen-protobuf: $(SOURCE_PROTO_FILES) $(TOOLS_DIR)/protoc-gen-gogofast $(TOOLS_DIR)/protoc/bin/protoc |
| 115 | + @ \ |
| 116 | + mkdir -p "$(PROTOBUF_TEMP_DIR)"; \ |
| 117 | + set -e; for f in $^; do \ |
| 118 | + if [[ "$${f}" == $(TOOLS_DIR)/* ]]; then continue; fi; \ |
| 119 | + echo "protoc $${f#"$(PROTO_SOURCE_DIR)/"}"; \ |
| 120 | + PATH="$(TOOLS_DIR):$${PATH}" $(TOOLS_DIR)/protoc/bin/protoc --proto_path="$(PROTO_SOURCE_DIR)" --gogofast_out="plugins=grpc:$(PROTOBUF_TEMP_DIR)" "$${f}"; \ |
| 121 | + done |
66 | 122 |
|
67 |
| -$(PROTO_SOURCE_DIR)/ $(PROTOBUF_GEN_DIR)/: |
68 |
| - mkdir -p $@ |
| 123 | +.PHONY: copy-protobufs |
| 124 | +copy-protobufs: |
| 125 | + @rsync -a $(PROTOBUF_TEMP_DIR)/go.opentelemetry.io/otel/exporters . |
69 | 126 |
|
70 | 127 | .PHONY: clean
|
71 | 128 | clean:
|
72 |
| - rm -rf ./gen |
| 129 | + rm -rf $(GEN_TEMP_DIR) |
0 commit comments