Makefile 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. PROJECT = mad
  2. CT_SUITES = mad_utils mad_deps mad_compile
  3. .PHONY: all build
  4. all: app build
  5. build:
  6. escript build
  7. # github.com/extend/erlang.mk
  8. # Copyright (c) 2013, Loïc Hoguin <essen@ninenines.eu>
  9. #
  10. # Permission to use, copy, modify, and/or distribute this software for any
  11. # purpose with or without fee is hereby granted, provided that the above
  12. # copyright notice and this permission notice appear in all copies.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  15. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  16. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  17. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  19. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  20. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21. # Project.
  22. PROJECT ?= $(notdir $(CURDIR))
  23. # Verbosity and tweaks.
  24. V ?= 0
  25. appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
  26. appsrc_verbose = $(appsrc_verbose_$(V))
  27. erlc_verbose_0 = @echo " ERLC " $(filter %.erl %.core,$(?F));
  28. erlc_verbose = $(erlc_verbose_$(V))
  29. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  30. xyrl_verbose = $(xyrl_verbose_$(V))
  31. dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
  32. dtl_verbose = $(dtl_verbose_$(V))
  33. gen_verbose_0 = @echo " GEN " $@;
  34. gen_verbose = $(gen_verbose_$(V))
  35. .PHONY: app clean clean-all clean-docs docs build-tests test build-plt dialyze
  36. # Deps directory.
  37. DEPS_DIR ?= $(CURDIR)/deps
  38. export DEPS_DIR
  39. REBAR_DEPS_DIR = $(DEPS_DIR)
  40. export REBAR_DEPS_DIR
  41. ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DEPS))
  42. ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
  43. # Application.
  44. ERL_LIBS ?= $(DEPS_DIR)
  45. export ERL_LIBS
  46. ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \
  47. +warn_shadow_vars +warn_obsolete_guard # +bin_opt_info +warn_missing_spec
  48. COMPILE_FIRST ?=
  49. COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
  50. clean-all: clean clean-docs
  51. $(gen_verbose) rm -rf .$(PROJECT).plt $(DEPS_DIR) logs
  52. app: ebin/$(PROJECT).app
  53. $(eval MODULES := $(shell find ebin -type f -name \*.beam \
  54. | sed 's/ebin\///;s/\.beam/,/' | sed '$$s/.$$//'))
  55. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  56. | sed 's/{modules, \[\]}/{modules, \[$(MODULES)\]}/' \
  57. > ebin/$(PROJECT).app
  58. define compile_erl
  59. $(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
  60. -pa ebin/ -I include/ $(COMPILE_FIRST_PATHS) $(1)
  61. endef
  62. define compile_xyrl
  63. $(xyrl_verbose) erlc -v -o ebin/ $(1)
  64. $(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
  65. @rm ebin/*.erl
  66. endef
  67. define compile_dtl
  68. $(dtl_verbose) erl -noshell -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
  69. Compile = fun(F) -> \
  70. Module = list_to_atom( \
  71. string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
  72. erlydtl_compiler:compile(F, Module, [{out_dir, "ebin/"}]) \
  73. end, \
  74. _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
  75. init:stop()'
  76. endef
  77. ebin/$(PROJECT).app: $(shell find src -type f -name \*.erl) \
  78. $(shell find src -type f -name \*.core) \
  79. $(shell find src -type f -name \*.xrl) \
  80. $(shell find src -type f -name \*.yrl) \
  81. $(shell find templates -type f -name \*.dtl 2>/dev/null)
  82. @mkdir -p ebin/
  83. $(if $(strip $(filter %.erl %.core,$?)), \
  84. $(call compile_erl,$(filter %.erl %.core,$?)))
  85. $(if $(strip $(filter %.xrl %.yrl,$?)), \
  86. $(call compile_xyrl,$(filter %.xrl %.yrl,$?)))
  87. $(if $(strip $(filter %.dtl,$?)), \
  88. $(call compile_dtl,$(filter %.dtl,$?)))
  89. clean:
  90. $(gen_verbose) rm -rf ebin/ test/*.beam erl_crash.dump
  91. # Documentation.
  92. docs: clean-docs
  93. $(gen_verbose) erl -noshell \
  94. -eval 'edoc:application($(PROJECT), ".", []), init:stop().'
  95. clean-docs:
  96. $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
  97. # Tests.
  98. $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
  99. build-test-deps: $(ALL_TEST_DEPS_DIRS)
  100. @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  101. build-tests: build-test-deps
  102. $(gen_verbose) erlc -v $(ERLC_OPTS) -o test/ \
  103. $(wildcard test/*.erl test/*/*.erl) -pa ebin/
  104. CT_RUN = ct_run \
  105. -no_auto_compile \
  106. -noshell \
  107. -pa $(realpath ebin) $(DEPS_DIR)/*/ebin \
  108. -dir test \
  109. -logdir logs
  110. # -cover test/cover.spec
  111. CT_SUITES ?=
  112. define test_target
  113. test_$(1): ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
  114. test_$(1): clean deps app build-tests
  115. @if [ -d "test" ] ; \
  116. then \
  117. mkdir -p logs/ ; \
  118. $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) ; \
  119. fi
  120. $(gen_verbose) rm -f test/*.beam
  121. endef
  122. $(foreach test,$(CT_SUITES),$(eval $(call test_target,$(test))))
  123. test: ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
  124. test: app build-tests
  125. @if [ -d "test" ] ; \
  126. then \
  127. mkdir -p logs/ ; \
  128. $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) ; \
  129. fi
  130. $(gen_verbose) rm -f test/*.beam
  131. # Dialyzer.
  132. PLT_APPS ?=
  133. DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
  134. -Wunmatched_returns # -Wunderspecs
  135. build-plt: app
  136. @dialyzer --build_plt --output_plt .$(PROJECT).plt \
  137. --apps erts kernel stdlib $(PLT_APPS)
  138. dialyze:
  139. @dialyzer --src src --plt .$(PROJECT).plt --no_native $(DIALYZER_OPTS)