erlang.mk 7.2 KB

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