erlang.mk 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. # Packages database file.
  17. PKG_FILE ?= $(CURDIR)/.erlang.mk.packages.v1
  18. PKG_FILE_URL ?= https://raw.github.com/extend/erlang.mk/master/packages.v1.txt
  19. define get_pkg_file
  20. wget -O $(PKG_FILE) $(PKG_FILE_URL)
  21. endef
  22. # Verbosity and tweaks.
  23. V ?= 0
  24. appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
  25. appsrc_verbose = $(appsrc_verbose_$(V))
  26. erlc_verbose_0 = @echo " ERLC " $(filter %.erl %.core,$(?F));
  27. erlc_verbose = $(erlc_verbose_$(V))
  28. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  29. xyrl_verbose = $(xyrl_verbose_$(V))
  30. dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
  31. dtl_verbose = $(dtl_verbose_$(V))
  32. gen_verbose_0 = @echo " GEN " $@;
  33. gen_verbose = $(gen_verbose_$(V))
  34. .PHONY: all clean-all app clean deps clean-deps docs clean-docs \
  35. build-tests tests 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)
  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. all: deps app
  51. clean-all: clean clean-deps clean-docs
  52. $(gen_verbose) rm -rf .$(PROJECT).plt $(DEPS_DIR) logs
  53. app: ebin/$(PROJECT).app
  54. $(eval MODULES := $(shell find ebin -name \*.beam \
  55. | sed 's/ebin\///;s/\.beam/,/' | sed '$$s/.$$//'))
  56. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  57. | sed 's/{modules, \[\]}/{modules, \[$(MODULES)\]}/' \
  58. > ebin/$(PROJECT).app
  59. define compile_erl
  60. $(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
  61. -pa ebin/ -I include/ $(COMPILE_FIRST_PATHS) $(1)
  62. endef
  63. define compile_xyrl
  64. $(xyrl_verbose) erlc -v -o ebin/ $(1)
  65. $(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
  66. @rm ebin/*.erl
  67. endef
  68. define compile_dtl
  69. $(dtl_verbose) erl -noshell -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
  70. Compile = fun(F) -> \
  71. Module = list_to_atom( \
  72. string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
  73. erlydtl_compiler:compile(F, Module, [{out_dir, "ebin/"}]) \
  74. end, \
  75. _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
  76. init:stop()'
  77. endef
  78. ebin/$(PROJECT).app: src/*.erl $(wildcard src/*.core) \
  79. $(wildcard src/*.xrl) $(wildcard src/*.yrl) \
  80. $(wildcard templates/*.dtl)
  81. @mkdir -p ebin/
  82. $(if $(strip $(filter %.erl %.core,$?)), \
  83. $(call compile_erl,$(filter %.erl %.core,$?)))
  84. $(if $(strip $(filter %.xrl %.yrl,$?)), \
  85. $(call compile_xyrl,$(filter %.xrl %.yrl,$?)))
  86. $(if $(strip $(filter %.dtl,$?)), \
  87. $(call compile_dtl,$(filter %.dtl,$?)))
  88. clean:
  89. $(gen_verbose) rm -rf ebin/ test/*.beam erl_crash.dump
  90. # Dependencies.
  91. define get_dep
  92. @mkdir -p $(DEPS_DIR)
  93. ifeq (,$(findstring pkg://,$(word 1,$(dep_$(1)))))
  94. git clone -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1)
  95. else
  96. @if [ ! -f $(PKG_FILE) ]; then $(call get_pkg_file); fi
  97. git clone -n -- `awk 'BEGIN { FS = "\t" }; \
  98. $$$$1 == "$(subst pkg://,,$(word 1,$(dep_$(1))))" { print $$$$2 }' \
  99. $(PKG_FILE)` $(DEPS_DIR)/$(1)
  100. endif
  101. cd $(DEPS_DIR)/$(1) ; git checkout -q $(word 2,$(dep_$(1)))
  102. endef
  103. define dep_target
  104. $(DEPS_DIR)/$(1):
  105. $(call get_dep,$(1))
  106. endef
  107. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  108. deps: $(ALL_DEPS_DIRS)
  109. @for dep in $(ALL_DEPS_DIRS) ; do \
  110. if [ -f $$dep/Makefile ] ; then \
  111. $(MAKE) -C $$dep ; \
  112. else \
  113. echo "include $(CURDIR)/erlang.mk" | $(MAKE) -f - -C $$dep ; \
  114. fi ; \
  115. done
  116. clean-deps:
  117. @for dep in $(ALL_DEPS_DIRS) ; do $(MAKE) -C $$dep clean; done
  118. # Documentation.
  119. docs: clean-docs
  120. $(gen_verbose) erl -noshell \
  121. -eval 'edoc:application($(PROJECT), ".", []), init:stop().'
  122. clean-docs:
  123. $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
  124. # Tests.
  125. $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
  126. build-test-deps: $(ALL_TEST_DEPS_DIRS)
  127. @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  128. build-tests: build-test-deps
  129. $(gen_verbose) erlc -v $(ERLC_OPTS) -o test/ \
  130. $(wildcard test/*.erl test/*/*.erl) -pa ebin/
  131. CT_RUN = ct_run \
  132. -no_auto_compile \
  133. -noshell \
  134. -pa $(realpath ebin) $(DEPS_DIR)/*/ebin \
  135. -dir test \
  136. -logdir logs
  137. # -cover test/cover.spec
  138. CT_SUITES ?=
  139. CT_SUITES_FULL = $(addsuffix _SUITE,$(CT_SUITES))
  140. tests: ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
  141. tests: clean deps app build-tests
  142. @mkdir -p logs/
  143. @$(CT_RUN) -suite $(CT_SUITES_FULL)
  144. $(gen_verbose) rm -f test/*.beam
  145. # Dialyzer.
  146. PLT_APPS ?=
  147. DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
  148. -Wunmatched_returns # -Wunderspecs
  149. build-plt: deps app
  150. @dialyzer --build_plt --output_plt .$(PROJECT).plt \
  151. --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
  152. dialyze:
  153. @dialyzer --src src --plt .$(PROJECT).plt --no_native $(DIALYZER_OPTS)
  154. # Packages.
  155. $(PKG_FILE):
  156. @$(call get_pkg_file)
  157. pkg-list: $(PKG_FILE)
  158. @cat $(PKG_FILE) | awk 'BEGIN { FS = "\t" }; { print \
  159. "Name:\t\t" $$1 "\n" \
  160. "Repository:\t" $$2 "\n" \
  161. "Website:\t" $$3 "\n" \
  162. "Description:\t" $$4 "\n" }'
  163. ifdef q
  164. pkg-search: $(PKG_FILE)
  165. @cat $(PKG_FILE) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  166. "Name:\t\t" $$1 "\n" \
  167. "Repository:\t" $$2 "\n" \
  168. "Website:\t" $$3 "\n" \
  169. "Description:\t" $$4 "\n" }'
  170. else
  171. pkg-search:
  172. @echo "Usage: make pkg-search q=STRING"
  173. endif