erlang.mk 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. # Project.
  15. PROJECT ?= $(notdir $(CURDIR))
  16. # Verbosity and tweaks.
  17. V ?= 0
  18. appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
  19. appsrc_verbose = $(appsrc_verbose_$(V))
  20. erlc_verbose_0 = @echo " ERLC " $(filter %.erl %.core,$(?F));
  21. erlc_verbose = $(erlc_verbose_$(V))
  22. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  23. xyrl_verbose = $(xyrl_verbose_$(V))
  24. dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
  25. dtl_verbose = $(dtl_verbose_$(V))
  26. gen_verbose_0 = @echo " GEN " $@;
  27. gen_verbose = $(gen_verbose_$(V))
  28. .PHONY: all clean-all app clean deps clean-deps docs clean-docs \
  29. build-tests tests build-plt dialyze
  30. # Deps directory.
  31. DEPS_DIR ?= $(CURDIR)/deps
  32. export DEPS_DIR
  33. REBAR_DEPS_DIR = $(DEPS_DIR)
  34. export REBAR_DEPS_DIR
  35. ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DEPS))
  36. ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
  37. # Application.
  38. ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \
  39. +warn_shadow_vars +warn_obsolete_guard # +bin_opt_info +warn_missing_spec
  40. COMPILE_FIRST ?=
  41. COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
  42. all: deps app
  43. clean-all: clean clean-deps clean-docs
  44. $(gen_verbose) rm -rf .$(PROJECT).plt $(DEPS_DIR) logs
  45. app: ebin/$(PROJECT).app
  46. $(eval MODULES := $(shell find ebin -name \*.beam \
  47. | sed 's/ebin\///;s/\.beam/,/' | sed '$$s/.$$//'))
  48. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  49. | sed 's/{modules, \[\]}/{modules, \[$(MODULES)\]}/' \
  50. > ebin/$(PROJECT).app
  51. define compile_erl
  52. $(erlc_verbose) ERL_LIBS=$(DEPS_DIR) erlc -v $(ERLC_OPTS) -o ebin/ \
  53. -pa ebin/ -I include/ $(COMPILE_FIRST_PATHS) $(1)
  54. endef
  55. define compile_xyrl
  56. $(xyrl_verbose) erlc -v -o ebin/ $(1)
  57. $(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
  58. @rm ebin/*.erl
  59. endef
  60. define compile_dtl
  61. $(dtl_verbose) erl -noshell -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
  62. Compile = fun(F) -> \
  63. Module = list_to_atom( \
  64. string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
  65. erlydtl_compiler:compile(F, Module, [{out_dir, "ebin/"}]) \
  66. end, \
  67. _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
  68. init:stop()'
  69. endef
  70. ebin/$(PROJECT).app: src/*.erl $(wildcard src/*.core) \
  71. $(wildcard src/*.xrl) $(wildcard src/*.yrl) \
  72. $(wildcard templates/*.dtl)
  73. @mkdir -p ebin/
  74. $(if $(strip $(filter %.erl %.core,$?)), \
  75. $(call compile_erl,$(filter %.erl %.core,$?)))
  76. $(if $(strip $(filter %.xrl %.yrl,$?)), \
  77. $(call compile_xyrl,$(filter %.xrl %.yrl,$?)))
  78. $(if $(strip $(filter %.dtl,$?)), \
  79. $(call compile_dtl,$(filter %.dtl,$?)))
  80. clean:
  81. $(gen_verbose) rm -rf ebin/ test/*.beam erl_crash.dump
  82. # Dependencies.
  83. define get_dep
  84. @mkdir -p $(DEPS_DIR)
  85. git clone -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1)
  86. cd $(DEPS_DIR)/$(1) ; git checkout -q $(word 2,$(dep_$(1)))
  87. endef
  88. define dep_target
  89. $(DEPS_DIR)/$(1):
  90. $(call get_dep,$(1))
  91. endef
  92. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  93. deps: $(ALL_DEPS_DIRS)
  94. @for dep in $(ALL_DEPS_DIRS) ; do \
  95. if [ -f $$dep/Makefile ] ; then \
  96. $(MAKE) -C $$dep ; \
  97. else \
  98. echo "include $(CURDIR)/erlang.mk" | $(MAKE) -f - -C $$dep ; \
  99. fi ; \
  100. done
  101. clean-deps:
  102. @for dep in $(ALL_DEPS_DIRS) ; do $(MAKE) -C $$dep clean; done
  103. # Documentation.
  104. docs: clean-docs
  105. $(gen_verbose) erl -noshell \
  106. -eval 'edoc:application($(PROJECT), ".", []), init:stop().'
  107. clean-docs:
  108. $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
  109. # Tests.
  110. $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
  111. build-test-deps: $(ALL_TEST_DEPS_DIRS)
  112. @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  113. build-tests: build-test-deps
  114. $(gen_verbose) ERL_LIBS=$(DEPS_DIR) erlc -v $(ERLC_OPTS) -o test/ \
  115. $(wildcard test/*.erl test/*/*.erl) -pa ebin/
  116. CT_RUN = ct_run \
  117. -no_auto_compile \
  118. -noshell \
  119. -pa ebin $(DEPS_DIR)/*/ebin \
  120. -dir test \
  121. -logdir logs
  122. # -cover test/cover.spec
  123. CT_SUITES ?=
  124. CT_SUITES_FULL = $(addsuffix _SUITE,$(CT_SUITES))
  125. tests: ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
  126. tests: clean deps app build-tests
  127. @mkdir -p logs/
  128. @$(CT_RUN) -suite $(CT_SUITES_FULL)
  129. $(gen_verbose) rm -f test/*.beam
  130. # Dialyzer.
  131. PLT_APPS ?=
  132. DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
  133. -Wunmatched_returns # -Wunderspecs
  134. build-plt: deps app
  135. @dialyzer --build_plt --output_plt .$(PROJECT).plt \
  136. --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
  137. dialyze:
  138. @dialyzer --src src --plt .$(PROJECT).plt --no_native $(DIALYZER_OPTS)