erlc.mk 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # Copyright (c) 2013-2016, Loïc Hoguin <essen@ninenines.eu>
  2. # This file is part of erlang.mk and subject to the terms of the ISC License.
  3. .PHONY: clean-app
  4. # Configuration.
  5. ERLC_OPTS ?= -Werror +debug_info +warn_export_vars +warn_shadow_vars \
  6. +warn_obsolete_guard # +bin_opt_info +warn_export_all +warn_missing_spec
  7. COMPILE_FIRST ?=
  8. COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
  9. ERLC_EXCLUDE ?=
  10. ERLC_EXCLUDE_PATHS = $(addprefix src/,$(addsuffix .erl,$(ERLC_EXCLUDE)))
  11. ERLC_ASN1_OPTS ?=
  12. ERLC_MIB_OPTS ?=
  13. COMPILE_MIB_FIRST ?=
  14. COMPILE_MIB_FIRST_PATHS = $(addprefix mibs/,$(addsuffix .mib,$(COMPILE_MIB_FIRST)))
  15. # Verbosity.
  16. app_verbose_0 = @echo " APP " $(PROJECT);
  17. app_verbose_2 = set -x;
  18. app_verbose = $(app_verbose_$(V))
  19. appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
  20. appsrc_verbose_2 = set -x;
  21. appsrc_verbose = $(appsrc_verbose_$(V))
  22. makedep_verbose_0 = @echo " DEPEND" $(PROJECT).d;
  23. makedep_verbose_2 = set -x;
  24. makedep_verbose = $(makedep_verbose_$(V))
  25. erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
  26. $(filter %.erl %.core,$(?F)));
  27. erlc_verbose_2 = set -x;
  28. erlc_verbose = $(erlc_verbose_$(V))
  29. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  30. xyrl_verbose_2 = set -x;
  31. xyrl_verbose = $(xyrl_verbose_$(V))
  32. asn1_verbose_0 = @echo " ASN1 " $(filter %.asn1,$(?F));
  33. asn1_verbose_2 = set -x;
  34. asn1_verbose = $(asn1_verbose_$(V))
  35. mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
  36. mib_verbose_2 = set -x;
  37. mib_verbose = $(mib_verbose_$(V))
  38. ifneq ($(wildcard src/),)
  39. # Targets.
  40. ifeq ($(wildcard ebin/test),)
  41. app:: deps $(PROJECT).d
  42. $(verbose) $(MAKE) --no-print-directory app-build
  43. else
  44. app:: clean deps $(PROJECT).d
  45. $(verbose) $(MAKE) --no-print-directory app-build
  46. endif
  47. ifeq ($(wildcard src/$(PROJECT_MOD).erl),)
  48. define app_file
  49. {application, '$(PROJECT)', [
  50. {description, "$(PROJECT_DESCRIPTION)"},
  51. {vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP),
  52. {id$(comma)$(space)"$(1)"}$(comma))
  53. {modules, [$(call comma_list,$(2))]},
  54. {registered, []},
  55. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]},
  56. {env, $(subst \,\\,$(PROJECT_ENV))}$(if $(findstring {,$(PROJECT_APP_EXTRA_KEYS)),$(comma)$(newline)$(tab)$(subst \,\\,$(PROJECT_APP_EXTRA_KEYS)),)
  57. ]}.
  58. endef
  59. else
  60. define app_file
  61. {application, '$(PROJECT)', [
  62. {description, "$(PROJECT_DESCRIPTION)"},
  63. {vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP),
  64. {id$(comma)$(space)"$(1)"}$(comma))
  65. {modules, [$(call comma_list,$(2))]},
  66. {registered, [$(call comma_list,$(PROJECT)_sup $(PROJECT_REGISTERED))]},
  67. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]},
  68. {mod, {$(PROJECT_MOD), []}},
  69. {env, $(subst \,\\,$(PROJECT_ENV))}$(if $(findstring {,$(PROJECT_APP_EXTRA_KEYS)),$(comma)$(newline)$(tab)$(subst \,\\,$(PROJECT_APP_EXTRA_KEYS)),)
  70. ]}.
  71. endef
  72. endif
  73. app-build: ebin/$(PROJECT).app
  74. $(verbose) :
  75. # Source files.
  76. ALL_SRC_FILES := $(sort $(call core_find,src/,*))
  77. ERL_FILES := $(filter %.erl,$(ALL_SRC_FILES))
  78. CORE_FILES := $(filter %.core,$(ALL_SRC_FILES))
  79. # ASN.1 files.
  80. ifneq ($(wildcard asn1/),)
  81. ASN1_FILES = $(sort $(call core_find,asn1/,*.asn1))
  82. ERL_FILES += $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))
  83. define compile_asn1
  84. $(verbose) mkdir -p include/
  85. $(asn1_verbose) erlc -v -I include/ -o asn1/ +noobj $(ERLC_ASN1_OPTS) $(1)
  86. $(verbose) mv asn1/*.erl src/
  87. $(verbose) mv asn1/*.hrl include/
  88. $(verbose) mv asn1/*.asn1db include/
  89. endef
  90. $(PROJECT).d:: $(ASN1_FILES)
  91. $(if $(strip $?),$(call compile_asn1,$?))
  92. endif
  93. # SNMP MIB files.
  94. ifneq ($(wildcard mibs/),)
  95. MIB_FILES = $(sort $(call core_find,mibs/,*.mib))
  96. $(PROJECT).d:: $(COMPILE_MIB_FIRST_PATHS) $(MIB_FILES)
  97. $(verbose) mkdir -p include/ priv/mibs/
  98. $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ -I priv/mibs/ $?
  99. $(mib_verbose) erlc -o include/ -- $(addprefix priv/mibs/,$(patsubst %.mib,%.bin,$(notdir $?)))
  100. endif
  101. # Leex and Yecc files.
  102. XRL_FILES := $(filter %.xrl,$(ALL_SRC_FILES))
  103. XRL_ERL_FILES = $(addprefix src/,$(patsubst %.xrl,%.erl,$(notdir $(XRL_FILES))))
  104. ERL_FILES += $(XRL_ERL_FILES)
  105. YRL_FILES := $(filter %.yrl,$(ALL_SRC_FILES))
  106. YRL_ERL_FILES = $(addprefix src/,$(patsubst %.yrl,%.erl,$(notdir $(YRL_FILES))))
  107. ERL_FILES += $(YRL_ERL_FILES)
  108. $(PROJECT).d:: $(XRL_FILES) $(YRL_FILES)
  109. $(if $(strip $?),$(xyrl_verbose) erlc -v -o src/ $(YRL_ERLC_OPTS) $?)
  110. # Erlang and Core Erlang files.
  111. define makedep.erl
  112. E = ets:new(makedep, [bag]),
  113. G = digraph:new([acyclic]),
  114. ErlFiles = lists:usort(string:tokens("$(ERL_FILES)", " ")),
  115. Modules = [{list_to_atom(filename:basename(F, ".erl")), F} || F <- ErlFiles],
  116. Add = fun (Mod, Dep) ->
  117. case lists:keyfind(Dep, 1, Modules) of
  118. false -> ok;
  119. {_, DepFile} ->
  120. {_, ModFile} = lists:keyfind(Mod, 1, Modules),
  121. ets:insert(E, {ModFile, DepFile}),
  122. digraph:add_vertex(G, Mod),
  123. digraph:add_vertex(G, Dep),
  124. digraph:add_edge(G, Mod, Dep)
  125. end
  126. end,
  127. AddHd = fun (F, Mod, DepFile) ->
  128. case file:open(DepFile, [read]) of
  129. {error, enoent} -> ok;
  130. {ok, Fd} ->
  131. F(F, Fd, Mod),
  132. {_, ModFile} = lists:keyfind(Mod, 1, Modules),
  133. ets:insert(E, {ModFile, DepFile})
  134. end
  135. end,
  136. Attr = fun
  137. (F, Mod, behavior, Dep) -> Add(Mod, Dep);
  138. (F, Mod, behaviour, Dep) -> Add(Mod, Dep);
  139. (F, Mod, compile, {parse_transform, Dep}) -> Add(Mod, Dep);
  140. (F, Mod, compile, Opts) when is_list(Opts) ->
  141. case proplists:get_value(parse_transform, Opts) of
  142. undefined -> ok;
  143. Dep -> Add(Mod, Dep)
  144. end;
  145. (F, Mod, include, Hrl) ->
  146. case filelib:is_file("include/" ++ Hrl) of
  147. true -> AddHd(F, Mod, "include/" ++ Hrl);
  148. false ->
  149. case filelib:is_file("src/" ++ Hrl) of
  150. true -> AddHd(F, Mod, "src/" ++ Hrl);
  151. false -> false
  152. end
  153. end;
  154. (F, Mod, include_lib, "$1/include/" ++ Hrl) -> AddHd(F, Mod, "include/" ++ Hrl);
  155. (F, Mod, include_lib, Hrl) -> AddHd(F, Mod, "include/" ++ Hrl);
  156. (F, Mod, import, {Imp, _}) ->
  157. IsFile =
  158. case lists:keyfind(Imp, 1, Modules) of
  159. false -> false;
  160. {_, FilePath} -> filelib:is_file(FilePath)
  161. end,
  162. case IsFile of
  163. false -> ok;
  164. true -> Add(Mod, Imp)
  165. end;
  166. (_, _, _, _) -> ok
  167. end,
  168. MakeDepend = fun(F, Fd, Mod) ->
  169. case io:parse_erl_form(Fd, undefined) of
  170. {ok, {attribute, _, Key, Value}, _} ->
  171. Attr(F, Mod, Key, Value),
  172. F(F, Fd, Mod);
  173. {eof, _} ->
  174. file:close(Fd);
  175. _ ->
  176. F(F, Fd, Mod)
  177. end
  178. end,
  179. [begin
  180. Mod = list_to_atom(filename:basename(F, ".erl")),
  181. {ok, Fd} = file:open(F, [read]),
  182. MakeDepend(MakeDepend, Fd, Mod)
  183. end || F <- ErlFiles],
  184. Depend = sofs:to_external(sofs:relation_to_family(sofs:relation(ets:tab2list(E)))),
  185. CompileFirst = [X || X <- lists:reverse(digraph_utils:topsort(G)), [] =/= digraph:in_neighbours(G, X)],
  186. TargetPath = fun(Target) ->
  187. case lists:keyfind(Target, 1, Modules) of
  188. false -> "";
  189. {_, DepFile} ->
  190. DirSubname = tl(string:tokens(filename:dirname(DepFile), "/")),
  191. string:join(DirSubname ++ [atom_to_list(Target)], "/")
  192. end
  193. end,
  194. ok = file:write_file("$(1)", [
  195. [[F, "::", [[" ", D] || D <- Deps], "; @touch \$$@\n"] || {F, Deps} <- Depend],
  196. "\nCOMPILE_FIRST +=", [[" ", TargetPath(CF)] || CF <- CompileFirst], "\n"
  197. ]),
  198. halt()
  199. endef
  200. ifeq ($(if $(NO_MAKEDEP),$(wildcard $(PROJECT).d),),)
  201. $(PROJECT).d:: $(ERL_FILES) $(call core_find,include/,*.hrl) $(MAKEFILE_LIST)
  202. $(makedep_verbose) $(call erlang,$(call makedep.erl,$@))
  203. endif
  204. ifneq ($(words $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES)),0)
  205. # Rebuild everything when the Makefile changes.
  206. $(ERLANG_MK_TMP)/last-makefile-change: $(MAKEFILE_LIST)
  207. $(verbose) mkdir -p $(ERLANG_MK_TMP)
  208. $(verbose) if test -f $@; then \
  209. touch $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES); \
  210. touch -c $(PROJECT).d; \
  211. fi
  212. $(verbose) touch $@
  213. $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES):: $(ERLANG_MK_TMP)/last-makefile-change
  214. ebin/$(PROJECT).app:: $(ERLANG_MK_TMP)/last-makefile-change
  215. endif
  216. include $(wildcard $(PROJECT).d)
  217. ebin/$(PROJECT).app:: ebin/
  218. ebin/:
  219. $(verbose) mkdir -p ebin/
  220. define compile_erl
  221. $(erlc_verbose) erlc -v $(if $(IS_DEP),$(filter-out -Werror,$(ERLC_OPTS)),$(ERLC_OPTS)) -o ebin/ \
  222. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),$(COMPILE_FIRST_PATHS) $(1))
  223. endef
  224. ebin/$(PROJECT).app:: $(ERL_FILES) $(CORE_FILES) $(wildcard src/$(PROJECT).app.src)
  225. $(eval FILES_TO_COMPILE := $(filter-out src/$(PROJECT).app.src,$?))
  226. $(if $(strip $(FILES_TO_COMPILE)),$(call compile_erl,$(FILES_TO_COMPILE)))
  227. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  228. $(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename \
  229. $(filter-out $(ERLC_EXCLUDE_PATHS),$(ERL_FILES) $(CORE_FILES) $(BEAM_FILES)))))))
  230. ifeq ($(wildcard src/$(PROJECT).app.src),)
  231. $(app_verbose) printf '$(subst %,%%,$(subst $(newline),\n,$(subst ','\'',$(call app_file,$(GITDESCRIBE),$(MODULES)))))' \
  232. > ebin/$(PROJECT).app
  233. else
  234. $(verbose) if [ -z "$$(grep -e '^[^%]*{\s*modules\s*,' src/$(PROJECT).app.src)" ]; then \
  235. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
  236. exit 1; \
  237. fi
  238. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  239. | sed "s/{[[:space:]]*modules[[:space:]]*,[[:space:]]*\[\]}/{modules, \[$(call comma_list,$(MODULES))\]}/" \
  240. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(subst /,\/,$(GITDESCRIBE))\"}/" \
  241. > ebin/$(PROJECT).app
  242. endif
  243. clean:: clean-app
  244. clean-app:
  245. $(gen_verbose) rm -rf $(PROJECT).d ebin/ priv/mibs/ $(XRL_ERL_FILES) $(YRL_ERL_FILES) \
  246. $(addprefix include/,$(patsubst %.mib,%.hrl,$(notdir $(MIB_FILES)))) \
  247. $(addprefix include/,$(patsubst %.asn1,%.hrl,$(notdir $(ASN1_FILES)))) \
  248. $(addprefix include/,$(patsubst %.asn1,%.asn1db,$(notdir $(ASN1_FILES)))) \
  249. $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))
  250. endif