erlang.mk 6.1 KB

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