erlang.mk 6.3 KB

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