erlang.mk 4.9 KB

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