erlang.mk 4.5 KB

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