erlang.mk 6.1 KB

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