erlang.mk 7.0 KB

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