Joey Armstrong | 903c69d | 2024-02-01 19:46:39 -0500 | [diff] [blame] | 1 | # -*- mode: makefile; -*- |
| 2 | # Copyright The OpenTelemetry Authors |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 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. |
| 19 | # |
| 20 | # The protoc binary and other tools are sourced from a docker image |
| 21 | # `PROTOC_IMAGE`. |
| 22 | # |
| 23 | # Prereqs: The archiving utility `pax` is installed. |
| 24 | |
| 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)) |
| 34 | |
| 35 | default: protobuf |
| 36 | |
| 37 | .PHONY: protobuf protobuf-source gen-protobuf copy-protobufs |
| 38 | protobuf: protobuf-source gen-protobuf copy-protobufs |
| 39 | |
| 40 | protobuf-source: $(SOURCE_PROTO_FILES) | $(PROTO_SOURCE_DIR)/ |
| 41 | |
| 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) |
| 45 | |
| 46 | endef |
| 47 | |
| 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,$<,$@) |
| 52 | |
| 53 | # Command to run protoc using docker image |
| 54 | define exec-protoc-all |
| 55 | docker run -v `pwd`:/defs $(PROTOC_IMAGE) $(1) |
| 56 | |
| 57 | endef |
| 58 | |
| 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})) |
| 61 | |
| 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) |
| 66 | |
| 67 | $(PROTO_SOURCE_DIR)/ $(PROTOBUF_GEN_DIR)/: |
| 68 | mkdir -p $@ |
| 69 | |
| 70 | .PHONY: clean |
| 71 | clean: |
| 72 | rm -rf ./gen |