forked from evanmcc/gpb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
335 lines (279 loc) · 10.2 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#
# Makefile for gpb -- for projects that use (GNU) make to build
#
# Copyright (C) 2013 Tomas Abrahamsson
#
# Author: Tomas Abrahamsson <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
# Parameters:
# ----------
# GPB_PREFIX -- if this makefile is to be included in some larger project
# NB: must end with a slash!
# VERBOSE -- set (to any value) to make the following steps verbose:
# * eunit testing
# * generation of the include/gpb_version.hrl and
# ebin/gpb.app files
# * edoc generation command
# * xref checking command
# ERL -- path to erl
# ERLC -- path to erlc
# ERLC_FLAGS -- any additional flags to erlc
# EDOC_DEST_DIR -- destination directory for documentation
# EDOC_OPTS -- any options to edoc (NB: _not_ including list brackets!)
# ERL_BATCH_FLAGS -- Flags to start the Erlang VM in batch mode
.SUFFIXES += .erl .xrl .yrl .hrl .beam .proto .plt
.PHONY: all clean test doc xref dialyze build_plt clean_plt
SHELL = /bin/sh
ERL ?= erl
ERLC ?= erlc
GPB_PREFIX =
src = $(GPB_PREFIX)src
ebin = $(GPB_PREFIX)ebin
incdir = $(GPB_PREFIX)include
descr_src = $(GPB_PREFIX)descr_src
test = $(GPB_PREFIX)test
priv = $(GPB_PREFIX)priv
doc = $(GPB_PREFIX)doc
build = $(GPB_PREFIX)build
empty_str :=
space := $(empty_str) $(empty_str)
comma := ,
##
ifndef EDOC_DEST_DIR
EDOC_DEST_DIR=.
endif
## Check verbosity
ifdef VERBOSE
verbose_opt := verbose
silencer :=
else
verbose :=
silencer := @
endif
# When making 'clean', don't run in parallel.
# Otherwise "make -j clean all" in an already-built dir will fail:
# it will clean while rebuilding, or fail to detect what needs to
# be rebuilt since it runs in parallel.
#
# If anyone tries that, revert to non-parallel execution, which
# is safe, but slower. Issue a warning about that.
ifneq ($(filter clean,$(MAKECMDGOALS)),)
.NOTPARALLEL:
ifneq ($(filter-out clean,$(MAKECMDGOALS)),)
# User is not _only_ making clean...
$(warning "WARNING: cannot make in parallel when _also_ making clean")
endif
endif
ERLC_FLAGS += -Wall +debug_info -I$(incdir)
ERL_BATCH_FLAGS = +B -noshell -noinput
OTP_MAJOR_MINOR = $(shell $(ERL) $(ERL_BATCH_FLAGS) -eval ' \
GetVsnFromFile = \
fun(V) -> case lists:reverse(string:strip(V,right,$$\n)) of \
"**"++Rest -> lists:reverse(Rest); \
Rest -> lists:reverse(Rest) \
end \
end, \
RootDir = code:root_dir(), \
io:format( \
"~s~n", \
[case erlang:system_info(otp_release) of \
"R"++_=Rel -> Rel; \
RStr -> \
lists:foldl( \
fun(_,R) when R /= RStr -> R; \
(F,R) -> case file:read_file(F) of \
{ok,B} -> GetVsnFromFile(binary_to_list(B));\
_ -> R \
end \
end, \
RStr, \
[filename:join([RootDir,"releases",RStr,"OTP_VERSION"]), \
filename:join([RootDir,"releases","OTP_VERSION"])]) \
end]), \
halt(0).')
# Use the same plt file as rebar would use, for compatibility
plt = $(GPB_PREFIX).gpb-$(OTP_MAJOR_MINOR).plt
ifdef NO_HAVE_MAPS
ERLC_FLAGS += -DNO_HAVE_MAPS=true
else
## attempt to auto-detect
ERLVM_SUPPORTS_MAPS := $(shell $(ERL) $(ERL_BATCH_FLAGS) -eval ' \
try maps:size(maps:new()) of \
0 -> io:format("true~n") \
catch error:undef -> io:format("false~n") \
end, \
receive after 10 -> ok end.' \
-s erlang halt)
ifeq ($(ERLVM_SUPPORTS_MAPS),false)
ERLC_FLAGS += -DNO_HAVE_MAPS=true
endif
endif
ifdef NO_HAVE_RAND
ERLC_FLAGS += -DNO_HAVE_RAND=true
else
## attempt to auto-detect
ERL_HAS_RAND := $(shell $(ERL) $(ERL_BATCH_FLAGS) -eval ' \
try rand:uniform() of \
F when is_float(F) -> io:format("true~n") \
catch error:undef -> io:format("false~n") \
end, \
receive after 10 -> ok end.' \
-s erlang halt)
ifeq ($(ERL_HAS_RAND),false)
ERLC_FLAGS += -DNO_HAVE_RAND=true
endif
endif
# Sorting it also eliminates duplicates
# (eg: gpb_parse due to both .yrl and .erl on rebuild, ditto for gpb_scan)
MODULES := \
$(sort \
$(patsubst $(src)/%.erl,%,$(wildcard $(src)/*.erl)) \
$(patsubst $(src)/%.yrl,%,$(wildcard $(src)/*.yrl)) \
$(patsubst $(src)/%.xrl,%,$(wildcard $(src)/*.xrl)))
DESCR_PROTO := $(priv)/proto3/google/protobuf/descriptor.proto
DESCR_PROTO_ERL := $(descr_src)/gpb_descriptor.erl
DESCR_PROTO_HRL := $(descr_src)/gpb_descriptor.hrl
DESCR_MODULES := \
gpb_compile_descr \
$(patsubst $(priv)/proto3/google/protobuf/%.proto,gpb_%,$(DESCR_PROTO))
TEST_MODULES := \
$(patsubst $(test)/%.erl,%,$(wildcard $(test)/*.erl))
# Run eunit on these modules:
# - If module M and M_tests exist, only include M (M_tests is then implicit)
# - If M_tests exists, but no M, include M_tests (eg gpb_compile_maps_tests)
# sorting it also removes duplicates (gpb_parse)
EUNIT_MODULES := \
$(MODULES) \
$(filter-out $(patsubst %,%_tests,$(MODULES)),$(TEST_MODULES))
BEAMS := $(patsubst %,$(ebin)/%.beam,$(MODULES))
DESCR_BEAMS := $(patsubst %,$(ebin)/%.beam,$(DESCR_MODULES))
TEST_BEAMS := $(patsubst %,$(test)/%.beam,$(TEST_MODULES)) \
$(test)/gpb_compile_maps_tests.beam
TARGETS = \
$(incdir)/gpb_version.hrl \
$(BEAMS) \
$(DESCR_BEAMS) \
$(ebin)/gpb.app
all: $(TARGETS)
clean:
$(RM) $(TARGETS)
$(RM) $(src)/gpb_parse.erl
$(RM) $(src)/gpb_scan.erl
$(RM) $(DESCR_PROTO_ERL)
$(RM) $(DESCR_PROTO_HRL)
$(RM) $(TEST_BEAMS)
$(RM) -r doc
test: all $(TEST_BEAMS) FORCE
@echo Testing...
$(silencer)$(ERL) $(ERL_BATCH_FLAGS) -pa $(test) -pa $(ebin) -eval " \
case eunit:test([$(subst $(space),$(comma),$(EUNIT_MODULES))], \
[$(verbose_opt)]) of \
ok -> halt(0); \
_ -> halt(1) \
end."
doc: | $(src)/gpb_parse.erl $(src)/gpb_scan.erl
@echo Generating documentation...
$(silencer)$(ERL) $(ERL_BATCH_FLAGS) -pa $(ebin) -eval " \
case edoc:application(gpb,\"$(EDOC_DEST_DIR)\",[$(EDOC_OPTS)]) of \
ok -> halt(0); \
_ -> halt(1) \
end."
xref: all
@echo Checking for calls to undefined functions...
$(silencer)$(ERL) $(ERL_BATCH_FLAGS) -eval " \
Res = xref:d(\"$(ebin)\"), \
case lists:keyfind(undefined,1,Res) of \
{undefined,[]} -> halt(0); \
{undefined,Undefs} -> io:format(\"~p~n\",[Undefs]), halt(1) \
end."
dialyze: all $(plt)
dialyzer -q --plt $(plt) -r $(ebin)
build_plt: $(plt)
clean_plt:
$(RM) -f $(plt)
$(plt):
dialyzer -q --build_plt --output_plt $@ --apps erts kernel stdlib
FORCE:
##
## General default rules for how to compile some files
##
$(ebin)/%.beam: $(src)/%.erl | $(ebin)
$(ERLC) $(ERLC_FLAGS) -pa $(ebin) -o $(ebin) $<
$(ebin)/%.beam: $(descr_src)/%.erl | $(ebin)
$(ERLC) $(ERLC_FLAGS) -pa $(ebin) -o $(ebin) $<
$(test)/%.beam: $(test)/%.erl | $(ebin)
$(ERLC) $(ERLC_FLAGS) $(EUNIT_ERLC_FLAGS) -pa $(ebin) -o $(test) $<
$(src)/%.erl: $(src)/%.yrl
$(ERLC) -o $(src) $<
$(src)/%.erl: $(src)/%.xrl
$(ERLC) -o $(src) $<
$(ebin):
mkdir -pv $(ebin)
##
## Some extra dependencies, not covered by default rules above
##
# To compile gpb_compile, we first need the parse transform in gpb_codegen
$(ebin)/gpb_compile.beam: $(ebin)/gpb_codegen.beam
# To compile gpb_codegen_tests, we first need the parse transform in gpb_codegen
$(test)/gpb_codegen_tests.beam: $(ebin)/gpb_codegen.beam
# To compile gpb.erl, we need gpb_include.hrl
$(ebin)/gpb.beam: $(src)/gpb.erl $(incdir)/gpb_version.hrl
# gpb_compile_tests.erl includes gpb_tests.erl (see the files for details
# on this unorthodox setup), this dependency needs to be recorded
$(test)/gpb_compile_tests.beam: $(test)/gpb_compile_tests.erl \
$(test)/gpb_tests.erl
# To compile the description generator, we
# must first have compiled the proto file for the gpb_description.proto
$(ebin)/gpb_compile_descr.beam: $(descr_src)/gpb_compile_descr.erl \
$(DESCR_PROTO_ERL)
$(descr_src)/gpb_%.erl: $(priv)/proto3/google/protobuf/%.proto $(BEAMS)
@echo Proto-compiling the description definition...
$(silencer)$(ERL) $(ERL_BATCH_FLAGS) -pa $(ebin) \
-I $(abspath $(priv)/proto3/google/protobuf) \
-modprefix gpb_ \
-o $(descr_src) \
-s gpb_compile c $(abspath $(<))
# To generate the ebin/gpb.app file, process the src/gpb.app.src file
$(ebin)/gpb.app: $(src)/gpb.app.src | $(ebin)
@echo Generating $@...
$(silencer)$(ERL) +B -noshell -noinput -eval " \
try \
{ok, [{application,App,KVs1}]} = file:consult(\"$<\"), \
Vsn2 = case lists:keyfind(vsn,1,KVs1) of \
{vsn,{cmd,Cmd}} -> \
string:strip(os:cmd(Cmd),right,$$\n); \
{vsn,Vsn1} -> \
Vsn1 \
end, \
KVs2 = lists:keystore(vsn,1,KVs1,{vsn,Vsn2}), \
AppTerm = {application,App,KVs2}, \
ok = file:write_file( \
\"$@\", \
iolist_to_binary( \
io_lib:format(\"~p.~n\", [AppTerm]))), \
halt(0) \
catch Class:Reason -> \
ST = erlang:get_stacktrace(), \
io:format(\"ERROR: {~p,~p~n\" \
\" ~p}~n\", [Class,Reason,ST]), \
halt(1) \
end."
$(incdir)/gpb_version.hrl: $(incdir)/gpb_version.hrl.in
@echo Generating $@...
$(silencer)$(build)/mk_version_hrl \
< include/gpb_version.hrl.in \
> include/gpb_version.hrl