erlang.mk 4.5 KB

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