|
1 |
| -# Hacks for Make |
2 |
| -EMPTY := |
3 |
| -SPACE := $(EMPTY) $(EMPTY) |
4 |
| -COMMA := , |
5 |
| -SED = $(shell command -v gsed || command -v sed) |
| 1 | +empty := |
| 2 | +space := $(empty) $(empty) |
| 3 | +comma := , |
6 | 4 |
|
7 |
| -ALL_FILES ?= (git ls-files . && git ls-files . --exclude-standard --others) | grep -v node_modules | sed 's:^:./:' |
8 |
| -PROTO_FILES ?= $(ALL_FILES) | grep "\.proto$$" |
| 5 | +languages := go java js php ruby python c swift |
9 | 6 |
|
10 |
| -GO := $(shell command -v go 2> /dev/null) |
11 |
| -# Assuming this Makefile is located at `$GOPATH/src/TheThingsNetwork/api/Makefile` |
12 |
| -GOPATH = $(shell dirname $(shell dirname $(shell dirname $(shell dirname $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))))))) |
| 7 | +.PHONY: all |
13 | 8 |
|
14 |
| -GOGO_REPO=github.com/gogo/protobuf |
15 |
| -GOGO_PKG=$(GOPATH)/src/$(GOGO_REPO) |
| 9 | +all: docker $(patsubst %,protos/%,$(languages)) mocks |
16 | 10 |
|
17 |
| -GRPC_GATEWAY_REPO=github.com/grpc-ecosystem/grpc-gateway |
18 |
| -GRPC_GATEWAY_PKG=$(GOPATH)/src/$(GRPC_GATEWAY_REPO) |
| 11 | +clean: $(patsubst %,clean/%,$(languages)) clean/mocks |
19 | 12 |
|
20 |
| -DOCKER ?= docker |
21 |
| -DOCKER_ARGS = run --user `id -u` --rm -v$(GOPATH):$(GOPATH) -w`pwd` |
22 |
| -DOCKER_IMAGE ?= thethingsindustries/protoc:2 |
23 |
| -PROTOC ?= $(DOCKER) $(DOCKER_ARGS) $(DOCKER_IMAGE) -I/usr/include |
24 |
| -PROTOC += -I$(GOPATH)/src -I$(GRPC_GATEWAY_PKG)/third_party/googleapis |
| 13 | +proto_files := $(shell find . -name '*.proto') |
25 | 14 |
|
26 |
| -.PHONY: all |
| 15 | +.PHONY: docker |
27 | 16 |
|
28 |
| -all: deps protos mocks |
| 17 | +DOCKER_IMAGE ?= thethingsnetwork/api-protoc |
29 | 18 |
|
30 |
| -.PHONY: deps |
| 19 | +docker: |
| 20 | + docker build -t $(DOCKER_IMAGE):latest . |
31 | 21 |
|
32 |
| -deps: protoc mockgen |
| 22 | +docker_run = docker run --user `id -u` --rm -v $(PWD):$(PWD) |
33 | 23 |
|
34 |
| -.PHONY: protoc |
| 24 | +protoc = $(docker_run) -w $(PWD) $(DOCKER_IMAGE) -I $(GOPATH)/src |
| 25 | +sed = $(docker_run) -w $(PWD) --entrypoint sed $(DOCKER_IMAGE) |
35 | 26 |
|
36 |
| -protoc: |
37 |
| - docker pull $(DOCKER_IMAGE) |
| 27 | +define language_template |
| 28 | +.PHONY: protos/$(1) |
38 | 29 |
|
39 |
| -.PHONY: protos |
| 30 | +protos/$(1): |
| 31 | + @echo Generating $(1) protos... |
| 32 | + @$$(MAKE) $(patsubst %.proto,%.pb.$(1),$(proto_files)) |
40 | 33 |
|
41 |
| -protos: protos.go protos.js protos.java protos.swift protos.php protos.ruby protos.c protos.python |
| 34 | +.PHONY: clean/$(1) |
| 35 | +endef |
42 | 36 |
|
43 |
| -.PHONY: protos.go |
| 37 | +$(foreach lang,$(languages),$(eval $(call language_template,$(lang)))) |
44 | 38 |
|
45 |
| -# Go |
46 |
| -GO_PROTO_TARGETS ?= $(patsubst %.proto,%.pb.go,$(shell $(PROTO_FILES))) |
47 |
| -GO_PROTO_TYPES = any duration empty struct timestamp |
48 |
| -GO_PROTO_TYPE_CONVERSIONS = $(subst $(SPACE),$(COMMA),$(foreach type,$(GO_PROTO_TYPES),Mgoogle/protobuf/$(type).proto=$(GOGO_REPO)/types)) |
49 |
| -GO_PROTOC_FLAGS ?= \ |
50 |
| - --gogottn_out=plugins=grpc,$(GO_PROTO_TYPE_CONVERSIONS):$(GOPATH)/src \ |
51 |
| - --grpc-gateway_out=:$(GOPATH)/src |
52 |
| -GO_GW_SED ?= -e 's/\.AppId/\.AppID/g' -e 's/\.DevId/\.DevID/g' -e 's/\.AppEui/\.AppEUI/g' -e 's/\.DevEui/\.DevEUI/g' -e 's/\.Id/\.ID/g' |
| 39 | +go_proto_types := any duration empty field_mask struct timestamp wrappers |
| 40 | +go_proto_conversions := $(subst $(space),$(comma),$(foreach type,$(go_proto_types),Mgoogle/protobuf/$(type).proto=github.com/gogo/protobuf/types)) |
53 | 41 |
|
54 |
| -protos.go: $(GO_PROTO_TARGETS) |
55 |
| - $(SED) -i'' $(GO_GW_SED) $(shell $(ALL_FILES) | grep "\.pb\.gw\.go$$") |
| 42 | +go_subst = -e 's/\.$(1)/.$(subst Eui,EUI,$(subst Id,ID,$(1)))/g' |
| 43 | +go_gw_replace := $(foreach id,AppId AppEui DevId DevEui Id,$(call go_subst,$(id))) |
56 | 44 |
|
57 | 45 | %.pb.go: %.proto
|
58 |
| - $(PROTOC) $(GO_PROTOC_FLAGS) $(PWD)/$< |
59 |
| - |
60 |
| -# Java |
61 |
| -JAVA_PROTO_TARGETS ?= $(patsubst %.proto,%.pb.java,$(shell $(PROTO_FILES))) |
62 |
| -JAVA_PROTOC_FLAGS ?= \ |
63 |
| - --grpc-java_out=:$(PWD)/java/src \ |
64 |
| - --java_out=:$(PWD)/java/src |
| 46 | + @$(protoc) --gogottn_out=plugins=grpc,$(go_proto_conversions):$(GOPATH)/src --grpc-gateway_out=$(GOPATH)/src $(PWD)/$< |
| 47 | + @file=$(patsubst %.proto,%.pb.gw.go,$<); if [ -f $$file ]; then \ |
| 48 | + $(sed) -i'' $(go_gw_replace) $$file; \ |
| 49 | + fi |
65 | 50 |
|
66 |
| -.PHONY: protos.java |
| 51 | +clean/go: |
| 52 | + find . -name '*pb.go' -delete |
| 53 | + find . -name '*pb.gw.go' -delete |
67 | 54 |
|
68 |
| -protos.java: $(JAVA_PROTO_TARGETS) |
| 55 | +.PHONY: %.pb.java |
69 | 56 |
|
70 | 57 | %.pb.java: %.proto
|
71 |
| - $(PROTOC) $(JAVA_PROTOC_FLAGS) $(PWD)/$< |
72 |
| - |
73 |
| -# JS |
74 |
| -GRPC_NODE_PLUGIN ?= /usr/bin/grpc_node_plugin |
75 |
| -JS_PROTO_TARGETS ?= $(patsubst %.proto,%_pb.js,$(shell $(PROTO_FILES))) |
76 |
| -JS_PROTOC_FLAGS ?= \ |
77 |
| - --plugin=protoc-gen-grpc-js=$(GRPC_NODE_PLUGIN) \ |
78 |
| - --grpc-js_out=$(GOPATH)/src \ |
79 |
| - --js_out=import_style=commonjs,binary:$(GOPATH)/src |
80 |
| -JS_SED ?= -e 's/github.com_/github_com_/g' \ |
| 58 | + @$(protoc) --java_out=$(PWD)/java/src --grpc-java_out=$(PWD)/java/src $(PWD)/$< |
| 59 | + |
| 60 | +clean/java: |
| 61 | + rm -rf java/src/org/thethingsnetwork/api |
| 62 | + |
| 63 | +js_replace := -e 's/github.com_/github_com_/g' \ |
81 | 64 | -e 's|../../../github.com/TheThingsNetwork/api/||g' \
|
82 | 65 | -e 's/github_com_TheThingsNetwork_api/ttn/g' \
|
83 | 66 | -e '/github_com_gogo_protobuf_gogoproto_gogo_pb/d' \
|
84 | 67 | -e '/google_api_annotations_pb/d'
|
85 | 68 |
|
86 |
| -.PHONY: protos.js |
87 |
| - |
88 |
| -protos.js: $(JS_PROTO_TARGETS) |
89 |
| - $(SED) -i'' $(JS_SED) $(shell $(ALL_FILES) | grep "\_pb.js$$") |
90 |
| - |
91 |
| -%_pb.js: %.proto |
92 |
| - $(PROTOC) $(JS_PROTOC_FLAGS) $(PWD)/$< |
93 |
| - |
94 |
| -# Swift |
95 |
| - |
96 |
| -SWIFT_PROTO_TARGETS ?= $(patsubst %.proto,%.pb.swift,$(shell $(PROTO_FILES))) |
97 |
| -SWIFT_PROTOC_FLAGS ?= \ |
98 |
| - --swiftgrpc_out=$(PWD) \ |
99 |
| - --swift_out=$(GOPATH)/src |
100 |
| - |
101 |
| -.PHONY: protos.swift |
102 |
| - |
103 |
| -protos.swift: $(SWIFT_PROTO_TARGETS) |
104 |
| - -mv broker.client.pb.swift broker |
105 |
| - -mv broker.server.pb.swift broker |
106 |
| - -mv discovery.client.pb.swift discovery |
107 |
| - -mv discovery.server.pb.swift discovery |
108 |
| - -mv handler.client.pb.swift handler |
109 |
| - -mv handler.server.pb.swift handler |
110 |
| - -mv lorawan.client.pb.swift protocol/lorawan |
111 |
| - -mv lorawan.server.pb.swift protocol/lorawan |
112 |
| - -mv monitor.client.pb.swift monitor |
113 |
| - -mv monitor.server.pb.swift monitor |
114 |
| - -mv networkserver.client.pb.swift networkserver |
115 |
| - -mv networkserver.server.pb.swift networkserver |
116 |
| - -mv router.client.pb.swift router |
117 |
| - -mv router.server.pb.swift router |
118 |
| - |
119 |
| -%.pb.swift: %.proto |
120 |
| - $(PROTOC) $(SWIFT_PROTOC_FLAGS) $(PWD)/$< |
121 |
| - |
122 |
| -# PHP |
| 69 | +.PHONY: %.pb.js |
123 | 70 |
|
124 |
| -PHP_GRPC_PLUGIN ?= /usr/bin/grpc_php_plugin |
125 |
| -PHP_PROTO_TARGETS ?= $(patsubst %.proto,%.pb.php,$(shell $(PROTO_FILES))) |
126 |
| -PHP_PROTOC_FLAGS ?= \ |
127 |
| - --plugin=protoc-gen-grpc-php=$(PHP_GRPC_PLUGIN) \ |
128 |
| - --grpc-php_out=$(PWD)/php \ |
129 |
| - --php_out=:$(PWD)/php |
| 71 | +%.pb.js: %.proto |
| 72 | + @$(protoc) --js_out=import_style=commonjs,binary:$(GOPATH)/src --grpc-js_out=$(GOPATH)/src $(PWD)/$< |
| 73 | + @file=$(patsubst %.proto,%_pb.js,$<); if [ -f $$file ]; then \ |
| 74 | + $(sed) -i'' $(js_replace) $$file; \ |
| 75 | + fi |
| 76 | + @file=$(patsubst %.proto,%_grpc_pb.js,$<); if [ -f $$file ]; then \ |
| 77 | + $(sed) -i'' $(js_replace) $$file; \ |
| 78 | + fi |
130 | 79 |
|
131 |
| -.PHONY: protos.php |
| 80 | +clean/js: |
| 81 | + find . -name '*_pb.js' -delete |
132 | 82 |
|
133 |
| -protos.php: $(PHP_PROTO_TARGETS) |
| 83 | +.PHONY: %.pb.php |
134 | 84 |
|
135 | 85 | %.pb.php: %.proto
|
136 |
| - $(PROTOC) $(PHP_PROTOC_FLAGS) $(PWD)/$< |
137 |
| - |
138 |
| -# Ruby |
139 |
| - |
140 |
| -RUBY_GPRC_PLUGIN ?= /usr/bin/grpc_ruby_plugin |
141 |
| -RUBY_PROTO_TARGETS ?= $(patsubst %.proto,%_pb.rb,$(shell $(PROTO_FILES))) |
142 |
| -RUBY_PROTOC_FLAGS ?= \ |
143 |
| - --plugin=protoc-gen-grpc-ruby=$(RUBY_GPRC_PLUGIN) \ |
144 |
| - --grpc-ruby_out=$(GOPATH)/src \ |
145 |
| - --ruby_out=:$(GOPATH)/src |
146 |
| -RUBY_SED ?= -e "s|github.com/TheThingsNetwork/api/||g" |
147 |
| - |
148 |
| -.PHONY: protos.ruby |
149 |
| - |
150 |
| -protos.ruby: $(RUBY_PROTO_TARGETS) |
151 |
| - $(SED) -i'' $(RUBY_SED) $(shell $(ALL_FILES) | grep "\_pb.rb$$") |
| 86 | + @$(protoc) --php_out=$(PWD)/php --grpc-php_out=$(PWD)/php $(PWD)/$< |
152 | 87 |
|
153 |
| -%_pb.rb: %.proto |
154 |
| - $(PROTOC) $(RUBY_PROTOC_FLAGS) $(PWD)/$< |
| 88 | +clean/php: |
| 89 | + rm -rf php/* |
155 | 90 |
|
156 |
| -# C |
| 91 | +ruby_replace := -e "s|require 'github.com/TheThingsNetwork/api/|require '|g" |
157 | 92 |
|
158 |
| -C_PROTO_TARGETS ?= $(patsubst %.proto,%.pb-c.c,$(shell $(PROTO_FILES))) |
159 |
| -C_PROTOC_FLAGS ?= \ |
160 |
| - --c_out=$(PWD)/c |
| 93 | +.PHONY: %.pb.ruby |
161 | 94 |
|
162 |
| -.PHONY: protos.c |
| 95 | +%.pb.ruby: %.proto |
| 96 | + @$(protoc) --ruby_out=$(GOPATH)/src --grpc-ruby_out=$(GOPATH)/src $(PWD)/$< |
| 97 | + @file=$(patsubst %.proto,%_pb.rb,$<); if [ -f $$file ]; then \ |
| 98 | + $(sed) -i'' $(ruby_replace) $$file; \ |
| 99 | + fi |
| 100 | + @file=$(patsubst %.proto,%_services_pb.rb,$<); if [ -f $$file ]; then \ |
| 101 | + $(sed) -i'' $(ruby_replace) $$file; \ |
| 102 | + fi |
163 | 103 |
|
164 |
| -protos.c: $(C_PROTO_TARGETS) |
165 |
| - $(PROTOC) $(C_PROTOC_FLAGS) $(GOPATH)/src/github.com/gogo/protobuf/protobuf/google/protobuf/*.proto |
166 |
| - mkdir -p c/google/protobuf |
167 |
| - mv $(PWD)/c/github.com/gogo/protobuf/protobuf/google/protobuf/*.pb-c.* c/google/protobuf |
168 |
| - rm -rf $(PWD)/c/github.com/gogo/protobuf/protobuf |
| 104 | +clean/ruby: |
| 105 | + find . -name '*_pb.rb' -delete |
169 | 106 |
|
170 |
| - $(PROTOC) $(C_PROTOC_FLAGS) $(GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api/annotations.proto |
171 |
| - mkdir -p c/google/api |
172 |
| - mv $(PWD)/c/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api/*.pb-c.* c/google/api |
173 |
| - rm -rf $(PWD)/c/github.com/grpc-ecosystem |
| 107 | +.PHONY: %.pb.python |
174 | 108 |
|
175 |
| - $(PROTOC) $(C_PROTOC_FLAGS) $(GOPATH)/src/github.com/gogo/protobuf/gogoproto/gogo.proto |
| 109 | +python_replace := -e 's/from github\.com/from github_com/g' \ |
| 110 | + -e '/from github_com\.gogo\.protobuf/d' \ |
| 111 | + -e 's/github_dot_com_dot_gogo_dot_protobuf_dot_gogoproto_dot_gogo__pb2\.DESCRIPTOR,//g' \ |
| 112 | + -e '/from google\.api/d' \ |
| 113 | + -e 's/google_dot_api_dot_annotations__pb2\.DESCRIPTOR,//g' |
176 | 114 |
|
177 |
| -%.pb-c.c: %.proto |
178 |
| - $(PROTOC) $(C_PROTOC_FLAGS) $(PWD)/$< |
| 115 | +%.pb.python: %.proto |
| 116 | + @$(protoc) --python_out=$(PWD)/python --grpc-python_out=$(PWD)/python $(PWD)/$< |
| 117 | + @file=$(patsubst %.proto,python/github/com/TheThingsNetwork/api/%_pb2.py,$<); if [ -f $$file ]; then \ |
| 118 | + target=$(patsubst %.proto,python/github_com/TheThingsNetwork/api/%_pb2.py,$<); \ |
| 119 | + mkdir -p `dirname $$target`; \ |
| 120 | + mv $$file $$target; \ |
| 121 | + $(sed) -i'' $(python_replace) $$target; \ |
| 122 | + fi |
| 123 | + @file=$(patsubst %.proto,python/github.com/TheThingsNetwork/api/%_pb2_grpc.py,$<); if [ -f $$file ]; then \ |
| 124 | + target=$(patsubst %.proto,python/github_com/TheThingsNetwork/api/%_pb2_grpc.py,$<); \ |
| 125 | + mkdir -p `dirname $$target`; \ |
| 126 | + mv $$file $$target; \ |
| 127 | + $(sed) -i'' $(python_replace) $$target; \ |
| 128 | + fi |
179 | 129 |
|
180 |
| -# Python |
181 |
| -.PHONY: protos.python |
| 130 | +clean/python: |
| 131 | + find . -name '*_pb2.py' -delete |
| 132 | + find . -name '*_pb2_grpc.py' -delete |
| 133 | + rm -rf python/github python/github.com |
182 | 134 |
|
183 |
| -protos.python: |
184 |
| - ./gen-python.sh |
| 135 | +.PHONY: %.pb.c |
185 | 136 |
|
186 |
| -# Dependencies |
| 137 | +%.pb.c: %.proto |
| 138 | + @$(protoc) --c_out=$(PWD)/c $(PWD)/$< |
187 | 139 |
|
188 |
| -$(GOPATH)/src/github.com/%: |
189 |
| -ifeq ($(shell command -v $(GO) 2> /dev/null),) |
190 |
| - git clone https://github.com/$* $@ |
191 |
| -else |
192 |
| - $(GO) get -d github.com/$*/... |
193 |
| -endif |
| 140 | +clean/c: |
| 141 | + rm -rf c/github.com/TheThingsNetwork |
194 | 142 |
|
195 |
| -PROTO_TARGETS = $(GO_PROTO_TARGETS) $(JS_PROTO_TARGETS) $(JAVA_PROTO_TARGETS) $(SWIFT_PROTO_TARGETS) $(PHP_PROTO_TARGETS) $(RUBY_PROTO_TARGETS) $(C_PROTO_TARGETS) |
196 |
| -$(PROTO_TARGETS): $(GOGO_PKG) $(GRPC_GATEWAY_PKG) |
| 143 | +.PHONY: %.pb.swift |
197 | 144 |
|
198 |
| -# Mocks |
| 145 | +%.pb.swift: %.proto |
| 146 | + @$(protoc) --swift_out=$(GOPATH)/src --grpc-swift_out=$(GOPATH)/src $(PWD)/$< |
199 | 147 |
|
200 |
| -MOCKGEN ?= mockgen |
| 148 | +clean/swift: |
| 149 | + find . -name '*.swift' -delete |
201 | 150 |
|
202 | 151 | .PHONY: mockgen
|
203 | 152 |
|
204 | 153 | mockgen:
|
205 |
| -ifeq ($(shell command -v $(MOCKGEN) 2> /dev/null),) |
206 |
| -ifeq ($(shell command -v $(GO) 2> /dev/null),) |
207 |
| - $(error go is not installed) |
208 |
| -else |
209 |
| - $(GO) get github.com/golang/mock/mockgen |
210 |
| -endif |
211 |
| -endif |
212 |
| - |
| 154 | + @command -v mockgen > /dev/null || go get github.com/golang/mock/mockgen |
213 | 155 |
|
214 | 156 | .PHONY: mocks
|
215 | 157 |
|
216 | 158 | mocks: mockgen
|
217 |
| - $(MOCKGEN) -source=./protocol/lorawan/device.pb.go -package lorawan DeviceManagerClient > protocol/lorawan/device_mock.go |
218 |
| - $(MOCKGEN) -source=./discovery/discoveryclient/client.go -package discoveryclient Client > discovery/discoveryclient/client_mock.go |
219 |
| - $(MOCKGEN) -source=./networkserver/networkserver.pb.go -package networkserver NetworkServerClient > networkserver/networkserver_mock.go |
220 |
| - |
221 |
| -# Clean |
222 |
| - |
223 |
| -.PHONY: clean |
224 |
| - |
225 |
| -clean: clean-protos clean-mocks |
226 |
| - |
227 |
| -clean-protos: |
228 |
| - find . -name '*pb.*' -delete -or -name '*pb_test.go' -delete -or -name '*.pb-c.*' -delete -or -name '*_pb2*.py' -delete -or -name '__init__.py' -delete -or -wholename './php/*' -delete -or -wholename './java/src/*' -delete |
| 159 | + mockgen -source=./protocol/lorawan/device.pb.go -package lorawan DeviceManagerClient > protocol/lorawan/device_mock.go |
| 160 | + mockgen -source=./discovery/discoveryclient/client.go -package discoveryclient Client > discovery/discoveryclient/client_mock.go |
| 161 | + mockgen -source=./networkserver/networkserver.pb.go -package networkserver NetworkServerClient > networkserver/networkserver_mock.go |
229 | 162 |
|
230 |
| -clean-mocks: |
| 163 | +clean/mocks: |
231 | 164 | find . -name '*_mock.go' -delete
|
0 commit comments