erlang.mk 7.9 KB

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