erlang.mk 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 --no-check-certificate -O $(PKG_FILE) $(PKG_FILE_URL) || rm $(PKG_FILE)
  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: rel clean-rel all clean-all app clean deps clean-deps \
  36. docs clean-docs build-tests tests build-plt dialyze
  37. # Release.
  38. RELX_CONFIG ?= $(CURDIR)/relx.config
  39. ifneq ($(wildcard $(RELX_CONFIG)),)
  40. RELX ?= $(CURDIR)/relx
  41. export RELX
  42. RELX_URL ?= https://github.com/erlware/relx/releases/download/v0.5.2/relx
  43. RELX_OPTS ?=
  44. define get_relx
  45. wget -O $(RELX) $(RELX_URL) || rm $(RELX)
  46. chmod +x $(RELX)
  47. endef
  48. rel: clean-rel all $(RELX)
  49. @$(RELX) -c $(RELX_CONFIG) $(RELX_OPTS)
  50. $(RELX):
  51. @$(call get_relx)
  52. clean-rel:
  53. @rm -rf _rel
  54. endif
  55. # Deps directory.
  56. DEPS_DIR ?= $(CURDIR)/deps
  57. export DEPS_DIR
  58. REBAR_DEPS_DIR = $(DEPS_DIR)
  59. export REBAR_DEPS_DIR
  60. ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DEPS))
  61. ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
  62. # Application.
  63. ifeq ($(filter $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),)
  64. ifeq ($(ERL_LIBS),)
  65. ERL_LIBS = $(DEPS_DIR)
  66. else
  67. ERL_LIBS := $(ERL_LIBS):$(DEPS_DIR)
  68. endif
  69. endif
  70. export ERL_LIBS
  71. ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \
  72. +warn_shadow_vars +warn_obsolete_guard # +bin_opt_info +warn_missing_spec
  73. COMPILE_FIRST ?=
  74. COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
  75. all: deps app
  76. clean-all: clean clean-deps clean-docs
  77. $(gen_verbose) rm -rf .$(PROJECT).plt $(DEPS_DIR) logs
  78. app: ebin/$(PROJECT).app
  79. $(eval MODULES := $(shell find ebin -type f -name \*.beam \
  80. | sed 's/ebin\///;s/\.beam/,/' | sed '$$s/.$$//'))
  81. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  82. | sed 's/{modules,[[:space:]]*\[\]}/{modules, \[$(MODULES)\]}/' \
  83. > ebin/$(PROJECT).app
  84. define compile_erl
  85. $(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
  86. -pa ebin/ -I include/ $(COMPILE_FIRST_PATHS) $(1)
  87. endef
  88. define compile_xyrl
  89. $(xyrl_verbose) erlc -v -o ebin/ $(1)
  90. $(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
  91. @rm ebin/*.erl
  92. endef
  93. define compile_dtl
  94. $(dtl_verbose) erl -noshell -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
  95. Compile = fun(F) -> \
  96. Module = list_to_atom( \
  97. string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
  98. erlydtl_compiler:compile(F, Module, [{out_dir, "ebin/"}]) \
  99. end, \
  100. _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
  101. init:stop()'
  102. endef
  103. ebin/$(PROJECT).app: $(shell find src -type f -name \*.erl) \
  104. $(shell find src -type f -name \*.core) \
  105. $(shell find src -type f -name \*.xrl) \
  106. $(shell find src -type f -name \*.yrl) \
  107. $(shell find templates -type f -name \*.dtl 2>/dev/null)
  108. @mkdir -p ebin/
  109. $(if $(strip $(filter %.erl %.core,$?)), \
  110. $(call compile_erl,$(filter %.erl %.core,$?)))
  111. $(if $(strip $(filter %.xrl %.yrl,$?)), \
  112. $(call compile_xyrl,$(filter %.xrl %.yrl,$?)))
  113. $(if $(strip $(filter %.dtl,$?)), \
  114. $(call compile_dtl,$(filter %.dtl,$?)))
  115. clean:
  116. $(gen_verbose) rm -rf ebin/ test/*.beam erl_crash.dump
  117. # Dependencies.
  118. define get_dep
  119. @mkdir -p $(DEPS_DIR)
  120. ifeq (,$(findstring pkg://,$(word 1,$(dep_$(1)))))
  121. git clone -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1)
  122. else
  123. @if [ ! -f $(PKG_FILE) ]; then $(call get_pkg_file); fi
  124. git clone -n -- `awk 'BEGIN { FS = "\t" }; \
  125. $$$$1 == "$(subst pkg://,,$(word 1,$(dep_$(1))))" { print $$$$2 }' \
  126. $(PKG_FILE)` $(DEPS_DIR)/$(1)
  127. endif
  128. cd $(DEPS_DIR)/$(1) ; git checkout -q $(word 2,$(dep_$(1)))
  129. endef
  130. define dep_target
  131. $(DEPS_DIR)/$(1):
  132. $(call get_dep,$(1))
  133. endef
  134. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  135. deps: $(ALL_DEPS_DIRS)
  136. @for dep in $(ALL_DEPS_DIRS) ; do \
  137. if [ -f $$dep/Makefile ] ; then \
  138. $(MAKE) -C $$dep ; \
  139. else \
  140. echo "include $(CURDIR)/erlang.mk" | $(MAKE) -f - -C $$dep ; \
  141. fi ; \
  142. done
  143. clean-deps:
  144. @for dep in $(ALL_DEPS_DIRS) ; do \
  145. if [ -f $$dep/Makefile ] ; then \
  146. $(MAKE) -C $$dep clean ; \
  147. else \
  148. echo "include $(CURDIR)/erlang.mk" | $(MAKE) -f - -C $$dep clean ; \
  149. fi ; \
  150. done
  151. # Documentation.
  152. EDOC_OPTS ?=
  153. docs: clean-docs
  154. $(gen_verbose) erl -noshell \
  155. -eval 'edoc:application($(PROJECT), ".", [$(EDOC_OPTS)]), init:stop().'
  156. clean-docs:
  157. $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
  158. # Tests.
  159. $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
  160. build-test-deps: $(ALL_TEST_DEPS_DIRS)
  161. @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  162. build-tests: build-test-deps
  163. $(gen_verbose) erlc -v $(ERLC_OPTS) -o test/ \
  164. $(wildcard test/*.erl test/*/*.erl) -pa ebin/
  165. CT_OPTS ?=
  166. CT_RUN = ct_run \
  167. -no_auto_compile \
  168. -noshell \
  169. -pa $(realpath ebin) $(DEPS_DIR)/*/ebin \
  170. -dir test \
  171. -logdir logs \
  172. $(CT_OPTS)
  173. CT_SUITES ?=
  174. define test_target
  175. test_$(1): ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
  176. test_$(1): clean deps app build-tests
  177. @if [ -d "test" ] ; \
  178. then \
  179. mkdir -p logs/ ; \
  180. $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) ; \
  181. fi
  182. $(gen_verbose) rm -f test/*.beam
  183. endef
  184. $(foreach test,$(CT_SUITES),$(eval $(call test_target,$(test))))
  185. tests: ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
  186. tests: clean deps app build-tests
  187. @if [ -d "test" ] ; \
  188. then \
  189. mkdir -p logs/ ; \
  190. $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) ; \
  191. fi
  192. $(gen_verbose) rm -f test/*.beam
  193. # Dialyzer.
  194. PLT_APPS ?=
  195. DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
  196. -Wunmatched_returns # -Wunderspecs
  197. build-plt: deps app
  198. @dialyzer --build_plt --output_plt .$(PROJECT).plt \
  199. --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
  200. dialyze:
  201. @dialyzer --src src --plt .$(PROJECT).plt --no_native $(DIALYZER_OPTS)
  202. # Packages.
  203. $(PKG_FILE):
  204. @$(call get_pkg_file)
  205. pkg-list: $(PKG_FILE)
  206. @cat $(PKG_FILE) | awk 'BEGIN { FS = "\t" }; { print \
  207. "Name:\t\t" $$1 "\n" \
  208. "Repository:\t" $$2 "\n" \
  209. "Website:\t" $$3 "\n" \
  210. "Description:\t" $$4 "\n" }'
  211. ifdef q
  212. pkg-search: $(PKG_FILE)
  213. @cat $(PKG_FILE) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  214. "Name:\t\t" $$1 "\n" \
  215. "Repository:\t" $$2 "\n" \
  216. "Website:\t" $$3 "\n" \
  217. "Description:\t" $$4 "\n" }'
  218. else
  219. pkg-search:
  220. @echo "Usage: make pkg-search q=STRING"
  221. endif