You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Makefile.protos 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.protos 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.
  24. #
  25. # Currently, all the generated code is in
  26. # `exporters/otlp/internal/opentelemetry-proto-gen`.
  27. #
  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).
  31. PROTOC_VERSION := 3.14.0
  32. TOOLS_DIR := $(abspath ./.tools)
  33. TOOLS_MOD_DIR := ./internal/tools
  34. PROTOBUF_VERSION := v1
  35. OTEL_PROTO_SUBMODULE := exporters/otlp/internal/opentelemetry-proto
  36. GEN_TEMP_DIR := gen
  37. SUBMODULE_PROTO_FILES := $(wildcard $(OTEL_PROTO_SUBMODULE)/opentelemetry/proto/*/$(PROTOBUF_VERSION)/*.proto) $(wildcard $(OTEL_PROTO_SUBMODULE)/opentelemetry/proto/collector/*/$(PROTOBUF_VERSION)/*.proto)
  38. ifeq ($(strip $(SUBMODULE_PROTO_FILES)),)
  39. $(error Submodule at $(OTEL_PROTO_SUBMODULE) is not checked out, use "git submodule update --init")
  40. endif
  41. PROTOBUF_GEN_DIR := exporters/otlp/internal/opentelemetry-proto-gen
  42. PROTOBUF_TEMP_DIR := $(GEN_TEMP_DIR)/pb-go
  43. PROTO_SOURCE_DIR := $(GEN_TEMP_DIR)/proto
  44. SOURCE_PROTO_FILES := $(subst $(OTEL_PROTO_SUBMODULE),$(PROTO_SOURCE_DIR),$(SUBMODULE_PROTO_FILES))
  45. .DEFAULT_GOAL := protobuf
  46. UNAME_S := $(shell uname -s)
  47. UNAME_M := $(shell uname -m)
  48. ifeq ($(UNAME_S),Linux)
  49. PROTOC_OS := linux
  50. PROTOC_ARCH := $(UNAME_M)
  51. else ifeq ($(UNAME_S),Darwin)
  52. PROTOC_OS := osx
  53. PROTOC_ARCH := x86_64
  54. endif
  55. PROTOC_ZIP_URL := https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-$(PROTOC_OS)-$(PROTOC_ARCH).zip
  56. $(TOOLS_DIR)/PROTOC_$(PROTOC_VERSION):
  57. @rm -f "$(TOOLS_DIR)"/PROTOC_* && \
  58. touch "$@"
  59. # Depend on a versioned file (like PROTOC_3.14.0), so when version
  60. # gets bumped, we will depend on a nonexistent file and thus download
  61. # a newer version.
  62. $(TOOLS_DIR)/protoc/bin/protoc: $(TOOLS_DIR)/PROTOC_$(PROTOC_VERSION)
  63. echo "Fetching protoc $(PROTOC_VERSION)" && \
  64. rm -rf $(TOOLS_DIR)/protoc && \
  65. wget -O $(TOOLS_DIR)/protoc.zip $(PROTOC_ZIP_URL) && \
  66. unzip $(TOOLS_DIR)/protoc.zip -d $(TOOLS_DIR)/protoc-tmp && \
  67. rm $(TOOLS_DIR)/protoc.zip && \
  68. touch $(TOOLS_DIR)/protoc-tmp/bin/protoc && \
  69. mv $(TOOLS_DIR)/protoc-tmp $(TOOLS_DIR)/protoc
  70. $(TOOLS_DIR)/protoc-gen-gogofast: $(TOOLS_MOD_DIR)/go.mod $(TOOLS_MOD_DIR)/go.sum $(TOOLS_MOD_DIR)/tools.go
  71. cd $(TOOLS_MOD_DIR) && \
  72. go build -o $(TOOLS_DIR)/protoc-gen-gogofast github.com/gogo/protobuf/protoc-gen-gogofast && \
  73. go mod tidy
  74. # Return a sed expression for replacing the go_package option in proto
  75. # file with a one that's valid for us.
  76. #
  77. # Example: $(call get-sed-expr,$(PROTOBUF_GEN_DIR))
  78. define get-sed-expr
  79. 's,go_package = "github.com/open-telemetry/opentelemetry-proto/gen/go,go_package = "go.opentelemetry.io/otel/$(1),'
  80. endef
  81. .PHONY: protobuf
  82. protobuf: protobuf-source gen-protobuf copy-protobufs
  83. .PHONY: protobuf-source
  84. protobuf-source: $(SOURCE_PROTO_FILES)
  85. # This copies proto files from submodule into $(PROTO_SOURCE_DIR),
  86. # thus satisfying the $(SOURCE_PROTO_FILES) prerequisite. The copies
  87. # have their package name replaced by go.opentelemetry.io/otel.
  88. $(PROTO_SOURCE_DIR)/%.proto: $(OTEL_PROTO_SUBMODULE)/%.proto
  89. @ \
  90. mkdir -p $(@D); \
  91. sed -e $(call get-sed-expr,$(PROTOBUF_GEN_DIR)) "$<" >"$@.tmp"; \
  92. mv "$@.tmp" "$@"
  93. .PHONY: gen-protobuf
  94. gen-protobuf: $(SOURCE_PROTO_FILES) $(TOOLS_DIR)/protoc-gen-gogofast $(TOOLS_DIR)/protoc/bin/protoc
  95. @ \
  96. mkdir -p "$(PROTOBUF_TEMP_DIR)"; \
  97. set -e; for f in $^; do \
  98. if [[ "$${f}" == $(TOOLS_DIR)/* ]]; then continue; fi; \
  99. echo "protoc $${f#"$(PROTO_SOURCE_DIR)/"}"; \
  100. PATH="$(TOOLS_DIR):$${PATH}" $(TOOLS_DIR)/protoc/bin/protoc --proto_path="$(PROTO_SOURCE_DIR)" --gogofast_out="plugins=grpc:$(PROTOBUF_TEMP_DIR)" "$${f}"; \
  101. done
  102. .PHONY: copy-protobufs
  103. copy-protobufs:
  104. @rsync -a $(PROTOBUF_TEMP_DIR)/go.opentelemetry.io/otel/exporters .
  105. .PHONY: clean
  106. clean:
  107. rm -rf $(GEN_TEMP_DIR)