forked from 2600hz/kazoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kz.mk
270 lines (203 loc) · 8.97 KB
/
kz.mk
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
## Kazoo Makefile targets
## Targets are run from the application's root directory (not KAZOO root).
.PHONY: compile compile-lean compile-test compile-test-direct compile-test-kz-deps \
clean clean-test \
json \
eunit proper test \
dialyze xref fixture_shell app_src depend splchk \
code_checks apps_of_app
## Platform detection.
ifeq ($(PLATFORM),)
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
PLATFORM = linux
else ifeq ($(UNAME_S),Darwin)
PLATFORM = darwin
else ifeq ($(UNAME_S),SunOS)
PLATFORM = solaris
else ifeq ($(UNAME_S),GNU)
PLATFORM = gnu
else ifeq ($(UNAME_S),FreeBSD)
PLATFORM = freebsd
else ifeq ($(UNAME_S),NetBSD)
PLATFORM = netbsd
else ifeq ($(UNAME_S),OpenBSD)
PLATFORM = openbsd
else ifeq ($(UNAME_S),DragonFly)
PLATFORM = dragonfly
else
$(error Unable to detect platform.)
endif
export PLATFORM
endif
## pipefail enforces that the command fails even when run through a pipe
SHELL := /bin/bash -o pipefail
BASE_BRANCH := $(shell cat $(ROOT)/.base_branch)
ifndef ERLC_OPTS_SUPERSECRET
ERLC_OPTS += +debug_info
else
ERLC_OPTS += $(ERLC_OPTS_SUPERSECRET)
endif
ERLC_OPTS += -Iinclude -Isrc -I../ +'{parse_transform, lager_transform}'
## Use pedantic flags when compiling apps from applications/ & core/
ERLC_OPTS += +warn_export_all +warn_unused_import +warn_unused_vars +warn_missing_spec -Werror
ELIBS ?= $(if $(ERL_LIBS),$(ERL_LIBS):)$(ROOT)/deps:$(ROOT)/core:$(ROOT)/applications
EBINS += $(ROOT)/deps/lager/ebin
TEST_EBINS += $(EBINS) $(ROOT)/deps/proper/ebin
PA = -pa ebin/ $(foreach EBIN,$(EBINS),-pa $(EBIN))
TEST_PA = -pa ebin/ $(foreach EBIN,$(TEST_EBINS),-pa $(EBIN))
DEPS_RULES = .deps.rules
TEST_DEPS = .test.deps
DEPS_MK = $(CURDIR)/deps.mk
DOT_ERLANG_MK = $(ROOT)/.erlang.mk
.PHONY: deps
ifneq (,$(wildcard $(DEPS_MK)))
# Track app's dependencies, if any
DEPS_HASH := $(shell md5sum $(DEPS_MK) | cut -d' ' -f1)
DEPS_HASH_FILE := .deps.mk.$(DEPS_HASH)
deps: $(DOT_ERLANG_MK) $(DEPS_MK) $(DEPS_HASH_FILE)
$(DEPS_HASH_FILE):
@[[ -s $(DEPS_MK) ]] && DEPS_MK='$(DEPS_MK)' $(MAKE) -C $(ROOT)/deps/ all || true
else
deps: $(DEPS_MK)
$(DEPS_MK):
@touch $(DEPS_MK)
@touch .deps.mk.$(shell md5sum $(DEPS_MK) | cut -d' ' -f1)
endif
$(DOT_ERLANG_MK):
@$(MAKE) -C $(ROOT) dot_erlang_mk
clean-deps: clean-deps-hash
clean-deps-hash:
$(if $(wildcard .deps.mk.*), rm .deps.mk.*)
comma := ,
empty :=
space := $(empty) $(empty)
KZ_VERSION ?= $(shell $(ROOT)/scripts/next_version)
## SOURCES provides a way to specify compilation order (left to right)
SOURCES ?= $(wildcard src/*.erl) $(wildcard src/*/*.erl)
MODULE_NAMES := $(sort $(foreach module,$(SOURCES),$(shell basename $(module) .erl)))
MODULES := $(shell echo $(MODULE_NAMES) | sed 's/ /,/g')
BEAMS := $(sort $(foreach module,$(SOURCES),ebin/$(shell basename $(module) .erl).beam))
JSON := $(find . -name "*.json")
TEST_SOURCES := $(SOURCES) $(wildcard test/*.erl)
TEST_MODULE_NAMES := $(sort $(foreach module,$(TEST_SOURCES),$(shell basename $(module) .erl)))
TEST_MODULES := $(shell echo $(TEST_MODULE_NAMES) | sed 's/ /,/g')
ifneq ($(wildcard $(DEPS_RULES)),)
include $(DEPS_RULES)
endif
## COMPILE_MOAR can contain Makefile-specific targets (see CLEAN_MOAR, compile-test)
compile: deps $(TEST_DEPS) $(COMPILE_MOAR) ebin/$(PROJECT).app json depend $(BEAMS)
compile-lean: ERLC_OPTS := $(filter-out +debug_info,$(ERLC_OPTS)) +deterministic
compile-lean: compile
ebin/$(PROJECT).app:
@mkdir -p ebin/
ERL_LIBS=$(ELIBS) erlc -v $(ERLC_OPTS) $(PA) -o ebin/ $(SOURCES)
@sed "s/{modules,[[:space:]]*\[\]}/{modules, \[$(MODULES)\]}/" src/$(PROJECT).app.src \
| sed -e "s!{vsn,\([^}]*\)}!\{vsn,\"$(KZ_VERSION)\"}!" > $@
ebin/%.beam: src/%.erl
ERL_LIBS=$(ELIBS) erlc -v $(ERLC_OPTS) $(PA) -o ebin/ $<
ebin/%.beam: src/*/%.erl
ERL_LIBS=$(ELIBS) erlc -v $(ERLC_OPTS) $(PA) -o ebin/ $<
depend: $(DEPS_RULES) $(TEST_DEPS)
$(DEPS_RULES):
@rm -f $(DEPS_RULES)
ERL_LIBS=$(ELIBS) erlc -v +makedep +'{makedep_output, standard_io}' $(PA) -o ebin/ $(SOURCES) > $(DEPS_RULES)
app_src:
@ERL_LIBS=$(ROOT)/deps:$(ROOT)/core:$(ROOT)/applications $(ROOT)/scripts/apps_of_app.escript -a $(shell find $(ROOT) -name $(PROJECT).app.src)
json: JSON = $(shell find . -name '*.json')
json:
@$(ROOT)/scripts/format-json.py $(JSON)
compile-test: $(TEST_DEPS) compile-test-kz-deps compile-test-direct json
compile-test-direct: deps $(COMPILE_MOAR) test/$(PROJECT).app
$(TEST_DEPS):
ERL_LIBS=$(ROOT)/deps:$(ROOT)/core:$(ROOT)/applications $(ROOT)/scripts/calculate-dep-targets.escript $(ROOT) $(PROJECT) > $(TEST_DEPS)
ifeq (,$(wildcard $(TEST_DEPS)))
KZ_DEPS_TARGETS =
else
KZ_DEPS = $(filter kazoo%,$(shell cat $(TEST_DEPS)))
KZ_DEPS_TARGETS = $(strip $(subst kazoo,compile-test-core-kazoo,$(KZ_DEPS)))
endif
ifeq ($(KZ_DEPS_TARGETS),)
compile-test-kz-deps: test/$(PROJECT).app
else
compile-test-kz-deps: $(KZ_DEPS_TARGETS)
compile-test-core-%:
$(MAKE) compile-test-direct -C $(ROOT)/core/$*
endif
test/$(PROJECT).app: ERLC_OPTS += -DTEST
test/$(PROJECT).app: $(TEST_SOURCES)
@mkdir -p test/
@mkdir -p ebin/
ERL_LIBS=$(ELIBS) erlc -v +nowarn_missing_spec $(ERLC_OPTS) $(TEST_PA) -o ebin/ $?
@sed "s/{modules,\s*\[\]}/{modules, \[$(TEST_MODULES)\]}/" src/$(PROJECT).app.src > $@
@sed "s/{modules,\s*\[\]}/{modules, \[$(TEST_MODULES)\]}/" src/$(PROJECT).app.src > ebin/$(PROJECT).app
clean: clean-test
@$(if $(wildcard cover/*), rm -r cover)
@$(if $(wildcard ebin/*), rm ebin/*)
@$(if $(wildcard *crash.dump), rm *crash.dump)
@$(if $(wildcard $(DEPS_RULES)), rm $(DEPS_RULES))
clean-test: $(CLEAN_MOAR)
@$(if $(wildcard $(TEST_DEPS)), rm $(TEST_DEPS))
$(if $(wildcard test/$(PROJECT).app), rm test/$(PROJECT).app)
TEST_CONFIG=$(ROOT)/rel/config-test.ini
## Use this one when debugging
test: compile-test
KAZOO_CONFIG=$(TEST_CONFIG) ERL_LIBS=$(ELIBS) $(ROOT)/scripts/eunit_run.escript $(TEST_MODULE_NAMES)
test.%: check-compile-test
KAZOO_CONFIG=$(TEST_CONFIG) ERL_LIBS=$(ELIBS) $(ROOT)/scripts/eunit_run.escript $*
check-compile-test: $(COMPILE_MOAR) test/$(PROJECT).app
COVER_REPORT_DIR=cover
## Use this one when CI
eunit: test/$(PROJECT).app eunit-run
eunit-run:
KAZOO_CONFIG=$(TEST_CONFIG) ERL_LIBS=$(ELIBS) $(ROOT)/scripts/eunit_run.escript --with-cover \
--cover-project-name $(PROJECT) --cover-report-dir $(COVER_REPORT_DIR) \
$(TEST_MODULE_NAMES)
cover: $(ROOT)/make/cover.mk
COVER=1 $(MAKE) eunit
cover-report: $(ROOT)/make/core.mk $(ROOT)/make/cover.mk eunit
COVER=1 CT_RUN=1 $(MAKE) -f $(ROOT)/make/core.mk -f $(ROOT)/make/cover.mk cover-report
$(ROOT)/make/cover.mk: $(ROOT)/make/core.mk
wget 'https://raw.githubusercontent.com/ninenines/erlang.mk/master/plugins/cover.mk' -O $(ROOT)/make/cover.mk
$(ROOT)/make/core.mk:
wget 'https://raw.githubusercontent.com/ninenines/erlang.mk/master/core/core.mk' -O $(ROOT)/make/core.mk
proper: compile-proper eunit-run
compile-proper: ERLC_OPTS += -DPROPER
compile-proper: clean-test compile-test
PLT ?= $(ROOT)/.kazoo.plt
$(PLT):
$(MAKE) -C $(ROOT) '.kazoo.plt'
dialyze: TO_DIALYZE ?= $(abspath ebin)
dialyze: $(PLT) compile
@ERL_LIBS=$(ROOT)/deps:$(ROOT)/core:$(ROOT)/applications $(ROOT)/scripts/check-dialyzer.escript $(ROOT)/.kazoo.plt $(TO_DIALYZE)
dialyze-hard: TO_DIALYZE ?= $(abspath ebin)
dialyze-hard: $(PLT) compile
@ERL_LIBS=$(ROOT)/deps:$(ROOT)/core:$(ROOT)/applications $(ROOT)/scripts/check-dialyzer.escript $(ROOT)/.kazoo.plt --hard $(TO_DIALYZE)
REBAR=$(ROOT)/deps/.erlang.mk/rebar/rebar
xref: TO_XREF = ebin/ #FIXME: set TO_XREF to an app's dependencies' ebin/ directories
xref: compile
@ERL_LIBS=$(ELIBS) $(REBAR) xref skip_deps=true -C $(ROOT)/make/xref.local.config
fmt: TO_FMT ?= $(shell find src include test -iname '*.erl' -or -iname '*.hrl' -or -iname '*.escript')
perf: ERLC_OPTS += -pa $(ROOT)/deps/horse/ebin -DPERF +'{parse_transform, horse_autoexport}'
perf: compile-test
$(gen_verbose) @ERL_LIBS=$(ELIBS) erl -noshell -pa $(ROOT)/deps/horse/ebin -pa $(TEST_PA) \
-eval 'horse:app_perf($(PROJECT)), init:stop().'
fixture_shell: ERL_CRASH_DUMP = "$(ROOT)/$(shell date +%s)_ecallmgr_erl_crash.dump"
fixture_shell: ERL_LIBS = "$(ROOT)/deps:$(ROOT)/core:$(ROOT)/applications:$(shell echo $(ROOT)/deps/rabbitmq_erlang_client-*/deps)"
fixture_shell: NODE_NAME ?= fixturedb
fixture_shell:
@ERL_CRASH_DUMP="$(ERL_CRASH_DUMP)" ERL_LIBS="$(ERL_LIBS)" KAZOO_CONFIG=$(ROOT)/rel/config-test.ini \
erl -name '$(NODE_NAME)' -s reloader "$$@"
code_checks:
@printf ":: Check for copyright year\n\n"
@$(ROOT)/scripts/bump-copyright-year.py $(SOURCES)
@printf "\n:: Check code\n\n"
@$(ROOT)/scripts/code_checks.bash $(SOURCES)
@printf "\n:: Check for raw JSON usage\n\n"
@ERL_LIBS=$(ROOT)/deps:$(ROOT)/core:$(ROOT)/applications $(ROOT)/scripts/no_raw_json.escript $(SOURCES)
@printf "\n:: Check for Erlang 21 new stacktrace syntax\n\n"
@$(ROOT)/scripts/check-stacktrace.py $(SOURCES)
apps_of_app:
ERL_LIBS=$(ROOT)/deps:$(ROOT)/core:$(ROOT)/applications $(ROOT)/scripts/apps_of_app.escript -a $(ROOT)/applications/$(PROJECT)/src/$(PROJECT).app.src
include $(ROOT)/make/splchk.mk
include $(ROOT)/make/fmt.mk