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
32PROTOC_VERSION := 3.14.0
33
34TOOLS_DIR                       := $(abspath ./.tools)
35TOOLS_MOD_DIR                   := ./internal/tools
36PROTOBUF_VERSION                := v1
37OTEL_PROTO_SUBMODULE            := exporters/otlp/internal/opentelemetry-proto
38GEN_TEMP_DIR                    := gen
39SUBMODULE_PROTO_FILES           := $(wildcard $(OTEL_PROTO_SUBMODULE)/opentelemetry/proto/*/$(PROTOBUF_VERSION)/*.proto) $(wildcard $(OTEL_PROTO_SUBMODULE)/opentelemetry/proto/collector/*/$(PROTOBUF_VERSION)/*.proto)
40
41ifeq ($(strip $(SUBMODULE_PROTO_FILES)),)
42$(error Submodule at $(OTEL_PROTO_SUBMODULE) is not checked out, use "git submodule update --init")
43endif
44
45PROTOBUF_GEN_DIR   := exporters/otlp/internal/opentelemetry-proto-gen
46PROTOBUF_TEMP_DIR  := $(GEN_TEMP_DIR)/pb-go
47PROTO_SOURCE_DIR   := $(GEN_TEMP_DIR)/proto
48SOURCE_PROTO_FILES := $(subst $(OTEL_PROTO_SUBMODULE),$(PROTO_SOURCE_DIR),$(SUBMODULE_PROTO_FILES))
49
50.DEFAULT_GOAL := protobuf
51
52UNAME_S := $(shell uname -s)
53UNAME_M := $(shell uname -m)
54
55ifeq ($(UNAME_S),Linux)
56
57PROTOC_OS := linux
58PROTOC_ARCH := $(UNAME_M)
59
60else ifeq ($(UNAME_S),Darwin)
61
62PROTOC_OS := osx
63PROTOC_ARCH := x86_64
64
65endif
66
67PROTOC_ZIP_URL := https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-$(PROTOC_OS)-$(PROTOC_ARCH).zip
68
69$(TOOLS_DIR)/PROTOC_$(PROTOC_VERSION):
70	@rm -f "$(TOOLS_DIR)"/PROTOC_* && \
71	touch "$@"
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))
94define get-sed-expr
95's,go_package = "github.com/open-telemetry/opentelemetry-proto/gen/go,go_package = "go.opentelemetry.io/otel/$(1),'
96endef
97
98.PHONY: protobuf
99protobuf: protobuf-source gen-protobuf copy-protobufs
100
101.PHONY: protobuf-source
102protobuf-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" "$@"
112
113.PHONY: gen-protobuf
114gen-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
122
123.PHONY: copy-protobufs
124copy-protobufs:
125	@rsync -a $(PROTOBUF_TEMP_DIR)/go.opentelemetry.io/otel/exporters .
126
127.PHONY: clean
128clean:
129	rm -rf $(GEN_TEMP_DIR)
130