erlc.mk 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. DepsDirs = lists:usort(string:tokens("$(wildcard $(DEPS_DIR)/*/src) $(wildcard $(DEPS_DIR)/*/include)", ", ")),
  116. AppsDirs = lists:usort(string:tokens("$(wildcard $(APPS_DIR)/*/src) $(wildcard $(APPS_DIR)/*/include)", ", ")),
  117. Modules = [{list_to_atom(filename:basename(F, ".erl")), F} || F <- ErlFiles],
  118. Add = fun (Mod, Dep) ->
  119. case lists:keyfind(Dep, 1, Modules) of
  120. false -> ok;
  121. {_, DepFile} ->
  122. {_, ModFile} = lists:keyfind(Mod, 1, Modules),
  123. ets:insert(E, {ModFile, DepFile}),
  124. digraph:add_vertex(G, Mod),
  125. digraph:add_vertex(G, Dep),
  126. digraph:add_edge(G, Mod, Dep)
  127. end
  128. end,
  129. AddHd = fun (F, Mod, DepFile) ->
  130. case file:open(DepFile, [read]) of
  131. {error, enoent} ->
  132. ok;
  133. {ok, Fd} ->
  134. F(F, Fd, Mod),
  135. {_, ModFile} = lists:keyfind(Mod, 1, Modules),
  136. ets:insert(E, {ModFile, DepFile})
  137. end
  138. end,
  139. SearchHrl = fun
  140. F(_Hrl, []) -> {error,enoent};
  141. F(Hrl, [Dir|Dirs]) ->
  142. HrlF = filename:join([Dir,Hrl]),
  143. case filelib:is_file(HrlF) of
  144. true -> {ok, HrlF};
  145. false -> F(Hrl,Dirs)
  146. end
  147. end,
  148. Attr = fun
  149. (_F, Mod, behavior, Dep) ->
  150. Add(Mod, Dep);
  151. (_F, Mod, behaviour, Dep) ->
  152. Add(Mod, Dep);
  153. (_F, Mod, compile, {parse_transform, Dep}) ->
  154. Add(Mod, Dep);
  155. (_F, Mod, compile, Opts) when is_list(Opts) ->
  156. case proplists:get_value(parse_transform, Opts) of
  157. undefined -> ok;
  158. Dep -> Add(Mod, Dep)
  159. end;
  160. (F, Mod, include, Hrl) ->
  161. case SearchHrl(Hrl, ["src", "include","$(APPS_DIR)","$(DEPS_DIR)"]++AppsDirs++DepsDirs) of
  162. {ok, FoundHrl} -> AddHd(F, Mod, FoundHrl);
  163. {error, _} -> false
  164. end;
  165. (F, Mod, include_lib, "$1/include/" ++ Hrl) ->
  166. AddHd(F, Mod, "include/" ++ Hrl);
  167. (F, Mod, include_lib, Hrl) ->
  168. case SearchHrl(Hrl, ["src", "include","$(APPS_DIR)","$(DEPS_DIR)"]++AppsDirs++DepsDirs) of
  169. {ok, FoundHrl} -> AddHd(F, Mod, FoundHrl);
  170. {error, _} -> false
  171. end;
  172. (F, Mod, import, {Imp, _}) ->
  173. IsFile =
  174. case lists:keyfind(Imp, 1, Modules) of
  175. false -> false;
  176. {_, FilePath} -> filelib:is_file(FilePath)
  177. end,
  178. case IsFile of
  179. false -> ok;
  180. true -> Add(Mod, Imp)
  181. end;
  182. (_, _, _, _) -> ok
  183. end,
  184. MakeDepend = fun(F, Fd, Mod) ->
  185. case io:parse_erl_form(Fd, undefined) of
  186. {ok, {attribute, _, Key, Value}, _} ->
  187. Attr(F, Mod, Key, Value),
  188. F(F, Fd, Mod);
  189. {eof, _} ->
  190. file:close(Fd);
  191. _ ->
  192. F(F, Fd, Mod)
  193. end
  194. end,
  195. [begin
  196. Mod = list_to_atom(filename:basename(F, ".erl")),
  197. {ok, Fd} = file:open(F, [read]),
  198. MakeDepend(MakeDepend, Fd, Mod)
  199. end || F <- ErlFiles],
  200. Depend = sofs:to_external(sofs:relation_to_family(sofs:relation(ets:tab2list(E)))),
  201. CompileFirst = [X || X <- lists:reverse(digraph_utils:topsort(G)), [] =/= digraph:in_neighbours(G, X)],
  202. TargetPath = fun(Target) ->
  203. case lists:keyfind(Target, 1, Modules) of
  204. false -> "";
  205. {_, DepFile} ->
  206. DirSubname = tl(string:tokens(filename:dirname(DepFile), "/")),
  207. string:join(DirSubname ++ [atom_to_list(Target)], "/")
  208. end
  209. end,
  210. ok = file:write_file("$(1)", [
  211. [[F, "::", [[" ", D] || D <- Deps], "; @touch \$$@\n"] || {F, Deps} <- Depend],
  212. "\nCOMPILE_FIRST +=", [[" ", TargetPath(CF)] || CF <- CompileFirst], "\n"
  213. ]),
  214. halt()
  215. endef
  216. ifeq ($(if $(NO_MAKEDEP),$(wildcard $(PROJECT).d),),)
  217. $(PROJECT).d:: $(ERL_FILES) $(call core_find,include/,*.hrl) $(MAKEFILE_LIST)
  218. ## TODO: Analyze if it is possible to use erlc -M instead of makedep.erl
  219. ## e.g.: erlc -M -MG -MF $@ -I $(APPS_DIR) -I $(DEPS_DIR) -I include/ src/*
  220. $(makedep_verbose) $(call erlang,$(call makedep.erl,$@))
  221. endif
  222. ifneq ($(words $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES)),0)
  223. # Rebuild everything when the Makefile changes.
  224. $(ERLANG_MK_TMP)/last-makefile-change: $(MAKEFILE_LIST)
  225. $(verbose) mkdir -p $(ERLANG_MK_TMP)
  226. $(verbose) if test -f $@; then \
  227. touch $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES); \
  228. touch -c $(PROJECT).d; \
  229. fi
  230. $(verbose) touch $@
  231. $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES):: $(ERLANG_MK_TMP)/last-makefile-change
  232. ebin/$(PROJECT).app:: $(ERLANG_MK_TMP)/last-makefile-change
  233. endif
  234. include $(wildcard $(PROJECT).d)
  235. ebin/$(PROJECT).app:: ebin/
  236. ebin/:
  237. $(verbose) mkdir -p ebin/
  238. define compile_erl
  239. $(erlc_verbose) erlc -v $(if $(IS_DEP),$(filter-out -Werror,$(ERLC_OPTS)),$(ERLC_OPTS)) -o ebin/ \
  240. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),$(COMPILE_FIRST_PATHS) $(1))
  241. endef
  242. ebin/$(PROJECT).app:: $(ERL_FILES) $(CORE_FILES) $(wildcard src/$(PROJECT).app.src)
  243. $(eval FILES_TO_COMPILE := $(filter-out src/$(PROJECT).app.src,$?))
  244. $(if $(strip $(FILES_TO_COMPILE)),$(call compile_erl,$(FILES_TO_COMPILE)))
  245. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  246. $(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename \
  247. $(filter-out $(ERLC_EXCLUDE_PATHS),$(ERL_FILES) $(CORE_FILES) $(BEAM_FILES)))))))
  248. ifeq ($(wildcard src/$(PROJECT).app.src),)
  249. $(app_verbose) printf '$(subst %,%%,$(subst $(newline),\n,$(subst ','\'',$(call app_file,$(GITDESCRIBE),$(MODULES)))))' \
  250. > ebin/$(PROJECT).app
  251. else
  252. $(verbose) if [ -z "$$(grep -e '^[^%]*{\s*modules\s*,' src/$(PROJECT).app.src)" ]; then \
  253. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
  254. exit 1; \
  255. fi
  256. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  257. | sed "s/{[[:space:]]*modules[[:space:]]*,[[:space:]]*\[\]}/{modules, \[$(call comma_list,$(MODULES))\]}/" \
  258. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(subst /,\/,$(GITDESCRIBE))\"}/" \
  259. > ebin/$(PROJECT).app
  260. endif
  261. clean:: clean-app
  262. clean-app:
  263. $(gen_verbose) rm -rf $(PROJECT).d ebin/ priv/mibs/ $(XRL_ERL_FILES) $(YRL_ERL_FILES) \
  264. $(addprefix include/,$(patsubst %.mib,%.hrl,$(notdir $(MIB_FILES)))) \
  265. $(addprefix include/,$(patsubst %.asn1,%.hrl,$(notdir $(ASN1_FILES)))) \
  266. $(addprefix include/,$(patsubst %.asn1,%.asn1db,$(notdir $(ASN1_FILES)))) \
  267. $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))
  268. endif