erlc.mk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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
  42. $(verbose) $(MAKE) --no-print-directory $(PROJECT).d
  43. $(verbose) $(MAKE) --no-print-directory app-build
  44. else
  45. app:: clean deps
  46. $(verbose) $(MAKE) --no-print-directory $(PROJECT).d
  47. $(verbose) $(MAKE) --no-print-directory app-build
  48. endif
  49. ifeq ($(wildcard src/$(PROJECT_MOD).erl),)
  50. define app_file
  51. {application, '$(PROJECT)', [
  52. {description, "$(PROJECT_DESCRIPTION)"},
  53. {vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP),
  54. {id$(comma)$(space)"$(1)"}$(comma))
  55. {modules, [$(call comma_list,$(2))]},
  56. {registered, []},
  57. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]},
  58. {env, $(subst \,\\,$(PROJECT_ENV))}$(if $(findstring {,$(PROJECT_APP_EXTRA_KEYS)),$(comma)$(newline)$(tab)$(subst \,\\,$(PROJECT_APP_EXTRA_KEYS)),)
  59. ]}.
  60. endef
  61. else
  62. define app_file
  63. {application, '$(PROJECT)', [
  64. {description, "$(PROJECT_DESCRIPTION)"},
  65. {vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP),
  66. {id$(comma)$(space)"$(1)"}$(comma))
  67. {modules, [$(call comma_list,$(2))]},
  68. {registered, [$(call comma_list,$(PROJECT)_sup $(PROJECT_REGISTERED))]},
  69. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]},
  70. {mod, {$(PROJECT_MOD), []}},
  71. {env, $(subst \,\\,$(PROJECT_ENV))}$(if $(findstring {,$(PROJECT_APP_EXTRA_KEYS)),$(comma)$(newline)$(tab)$(subst \,\\,$(PROJECT_APP_EXTRA_KEYS)),)
  72. ]}.
  73. endef
  74. endif
  75. app-build: ebin/$(PROJECT).app
  76. $(verbose) :
  77. # Source files.
  78. ALL_SRC_FILES := $(sort $(call core_find,src/,*))
  79. ERL_FILES := $(filter %.erl,$(ALL_SRC_FILES))
  80. CORE_FILES := $(filter %.core,$(ALL_SRC_FILES))
  81. # ASN.1 files.
  82. ifneq ($(wildcard asn1/),)
  83. ASN1_FILES = $(sort $(call core_find,asn1/,*.asn1))
  84. ERL_FILES += $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))
  85. define compile_asn1
  86. $(verbose) mkdir -p include/
  87. $(asn1_verbose) erlc -v -I include/ -o asn1/ +noobj $(ERLC_ASN1_OPTS) $(1)
  88. $(verbose) mv asn1/*.erl src/
  89. -$(verbose) mv asn1/*.hrl include/
  90. $(verbose) mv asn1/*.asn1db include/
  91. endef
  92. $(PROJECT).d:: $(ASN1_FILES)
  93. $(if $(strip $?),$(call compile_asn1,$?))
  94. endif
  95. # SNMP MIB files.
  96. ifneq ($(wildcard mibs/),)
  97. MIB_FILES = $(sort $(call core_find,mibs/,*.mib))
  98. $(PROJECT).d:: $(COMPILE_MIB_FIRST_PATHS) $(MIB_FILES)
  99. $(verbose) mkdir -p include/ priv/mibs/
  100. $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ -I priv/mibs/ $?
  101. $(mib_verbose) erlc -o include/ -- $(addprefix priv/mibs/,$(patsubst %.mib,%.bin,$(notdir $?)))
  102. endif
  103. # Leex and Yecc files.
  104. XRL_FILES := $(filter %.xrl,$(ALL_SRC_FILES))
  105. XRL_ERL_FILES = $(addprefix src/,$(patsubst %.xrl,%.erl,$(notdir $(XRL_FILES))))
  106. ERL_FILES += $(XRL_ERL_FILES)
  107. YRL_FILES := $(filter %.yrl,$(ALL_SRC_FILES))
  108. YRL_ERL_FILES = $(addprefix src/,$(patsubst %.yrl,%.erl,$(notdir $(YRL_FILES))))
  109. ERL_FILES += $(YRL_ERL_FILES)
  110. $(PROJECT).d:: $(XRL_FILES) $(YRL_FILES)
  111. $(if $(strip $?),$(xyrl_verbose) erlc -v -o src/ $(YRL_ERLC_OPTS) $?)
  112. # Erlang and Core Erlang files.
  113. define makedep.erl
  114. E = ets:new(makedep, [bag]),
  115. G = digraph:new([acyclic]),
  116. ErlFiles = lists:usort(string:tokens("$(ERL_FILES)", " ")),
  117. DepsDir = "$(call core_native_path,$(DEPS_DIR))",
  118. AppsDir = "$(call core_native_path,$(APPS_DIR))",
  119. DepsDirsSrc = "$(if $(wildcard $(DEPS_DIR)/*/src), $(call core_native_path,$(wildcard $(DEPS_DIR)/*/src)))",
  120. DepsDirsInc = "$(if $(wildcard $(DEPS_DIR)/*/include), $(call core_native_path,$(wildcard $(DEPS_DIR)/*/include)))",
  121. AppsDirsSrc = "$(if $(wildcard $(APPS_DIR)/*/src), $(call core_native_path,$(wildcard $(APPS_DIR)/*/src)))",
  122. AppsDirsInc = "$(if $(wildcard $(APPS_DIR)/*/include), $(call core_native_path,$(wildcard $(APPS_DIR)/*/include)))",
  123. DepsDirs = lists:usort(string:tokens(DepsDirsSrc++DepsDirsInc, " ")),
  124. AppsDirs = lists:usort(string:tokens(AppsDirsSrc++AppsDirsInc, " ")),
  125. Modules = [{list_to_atom(filename:basename(F, ".erl")), F} || F <- ErlFiles],
  126. Add = fun (Mod, Dep) ->
  127. case lists:keyfind(Dep, 1, Modules) of
  128. false -> ok;
  129. {_, DepFile} ->
  130. {_, ModFile} = lists:keyfind(Mod, 1, Modules),
  131. ets:insert(E, {ModFile, DepFile}),
  132. digraph:add_vertex(G, Mod),
  133. digraph:add_vertex(G, Dep),
  134. digraph:add_edge(G, Mod, Dep)
  135. end
  136. end,
  137. AddHd = fun (F, Mod, DepFile) ->
  138. case file:open(DepFile, [read]) of
  139. {error, enoent} ->
  140. ok;
  141. {ok, Fd} ->
  142. {_, ModFile} = lists:keyfind(Mod, 1, Modules),
  143. case ets:match(E, {ModFile, DepFile}) of
  144. [] ->
  145. ets:insert(E, {ModFile, DepFile}),
  146. F(F, Fd, Mod,0);
  147. _ -> ok
  148. end
  149. end
  150. end,
  151. SearchHrl = fun
  152. F(_Hrl, []) -> {error,enoent};
  153. F(Hrl, [Dir|Dirs]) ->
  154. HrlF = filename:join([Dir,Hrl]),
  155. case filelib:is_file(HrlF) of
  156. true ->
  157. {ok, HrlF};
  158. false -> F(Hrl,Dirs)
  159. end
  160. end,
  161. Attr = fun
  162. (_F, Mod, behavior, Dep) ->
  163. Add(Mod, Dep);
  164. (_F, Mod, behaviour, Dep) ->
  165. Add(Mod, Dep);
  166. (_F, Mod, compile, {parse_transform, Dep}) ->
  167. Add(Mod, Dep);
  168. (_F, Mod, compile, Opts) when is_list(Opts) ->
  169. case proplists:get_value(parse_transform, Opts) of
  170. undefined -> ok;
  171. Dep -> Add(Mod, Dep)
  172. end;
  173. (F, Mod, include, Hrl) ->
  174. case SearchHrl(Hrl, ["src", "include",AppsDir,DepsDir]++AppsDirs++DepsDirs) of
  175. {ok, FoundHrl} -> AddHd(F, Mod, FoundHrl);
  176. {error, _} -> false
  177. end;
  178. (F, Mod, include_lib, Hrl) ->
  179. case SearchHrl(Hrl, ["src", "include",AppsDir,DepsDir]++AppsDirs++DepsDirs) of
  180. {ok, FoundHrl} -> AddHd(F, Mod, FoundHrl);
  181. {error, _} -> false
  182. end;
  183. (F, Mod, import, {Imp, _}) ->
  184. IsFile =
  185. case lists:keyfind(Imp, 1, Modules) of
  186. false -> false;
  187. {_, FilePath} -> filelib:is_file(FilePath)
  188. end,
  189. case IsFile of
  190. false -> ok;
  191. true -> Add(Mod, Imp)
  192. end;
  193. (_, _, _, _) -> ok
  194. end,
  195. MakeDepend = fun
  196. (F, Fd, Mod, StartLocation) ->
  197. {ok, Filename} = file:pid2name(Fd),
  198. case io:parse_erl_form(Fd, undefined, StartLocation) of
  199. {ok, AbsData, EndLocation} ->
  200. case AbsData of
  201. {attribute, _, Key, Value} ->
  202. Attr(F, Mod, Key, Value),
  203. F(F, Fd, Mod, EndLocation);
  204. _ -> F(F, Fd, Mod, EndLocation)
  205. end;
  206. {eof, _ } -> file:close(Fd);
  207. {error, ErrorDescription } ->
  208. file:close(Fd);
  209. {error, ErrorInfo, ErrorLocation} ->
  210. F(F, Fd, Mod, ErrorLocation)
  211. end,
  212. ok
  213. end,
  214. [begin
  215. Mod = list_to_atom(filename:basename(F, ".erl")),
  216. case file:open(F, [read]) of
  217. {ok, Fd} -> MakeDepend(MakeDepend, Fd, Mod,0);
  218. {error, enoent} -> ok
  219. end
  220. end || F <- ErlFiles],
  221. Depend = sofs:to_external(sofs:relation_to_family(sofs:relation(ets:tab2list(E)))),
  222. CompileFirst = [X || X <- lists:reverse(digraph_utils:topsort(G)), [] =/= digraph:in_neighbours(G, X)],
  223. TargetPath = fun(Target) ->
  224. case lists:keyfind(Target, 1, Modules) of
  225. false -> "";
  226. {_, DepFile} ->
  227. DirSubname = tl(string:tokens(filename:dirname(DepFile), "/")),
  228. string:join(DirSubname ++ [atom_to_list(Target)], "/")
  229. end
  230. end,
  231. ok = file:write_file("$(1)", [
  232. "# Generated by Erlang.mk. Edit at your own risk!\n\n",
  233. [[F, "::", [[" ", D] || D <- Deps], "; @touch \$$@\n"] || {F, Deps} <- Depend],
  234. "\nCOMPILE_FIRST +=", [[" ", TargetPath(CF)] || CF <- CompileFirst], "\n"
  235. ]),
  236. halt()
  237. endef
  238. ifeq ($(if $(NO_MAKEDEP),$(wildcard $(PROJECT).d),),)
  239. $(PROJECT).d:: $(ERL_FILES) $(call core_find,include/,*.hrl) $(MAKEFILE_LIST)
  240. $(makedep_verbose) $(call erlang,$(call makedep.erl,$@))
  241. endif
  242. ifneq ($(words $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES)),0)
  243. # Rebuild everything when the Makefile changes.
  244. $(ERLANG_MK_TMP)/last-makefile-change: $(MAKEFILE_LIST)
  245. $(verbose) mkdir -p $(ERLANG_MK_TMP)
  246. $(verbose) if test -f $@; then \
  247. touch $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES); \
  248. touch -c $(PROJECT).d; \
  249. fi
  250. $(verbose) touch $@
  251. $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES):: $(ERLANG_MK_TMP)/last-makefile-change
  252. ebin/$(PROJECT).app:: $(ERLANG_MK_TMP)/last-makefile-change
  253. endif
  254. include $(wildcard $(PROJECT).d)
  255. ebin/$(PROJECT).app:: ebin/
  256. ebin/:
  257. $(verbose) mkdir -p ebin/
  258. define compile_erl
  259. $(erlc_verbose) erlc -v $(if $(IS_DEP),$(filter-out -Werror,$(ERLC_OPTS)),$(ERLC_OPTS)) -o ebin/ \
  260. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),$(COMPILE_FIRST_PATHS) $(1))
  261. endef
  262. define validate_app_file
  263. case file:consult("ebin/$(PROJECT).app") of
  264. {ok, _} -> halt();
  265. _ -> halt(1)
  266. end
  267. endef
  268. ebin/$(PROJECT).app:: $(ERL_FILES) $(CORE_FILES) $(wildcard src/$(PROJECT).app.src)
  269. $(eval FILES_TO_COMPILE := $(filter-out src/$(PROJECT).app.src,$?))
  270. $(if $(strip $(FILES_TO_COMPILE)),$(call compile_erl,$(FILES_TO_COMPILE)))
  271. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  272. $(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename \
  273. $(filter-out $(ERLC_EXCLUDE_PATHS),$(ERL_FILES) $(CORE_FILES) $(BEAM_FILES)))))))
  274. ifeq ($(wildcard src/$(PROJECT).app.src),)
  275. $(app_verbose) printf '$(subst %,%%,$(subst $(newline),\n,$(subst ','\'',$(call app_file,$(GITDESCRIBE),$(MODULES)))))' \
  276. > ebin/$(PROJECT).app
  277. $(verbose) if ! $(call erlang,$(call validate_app_file)); then \
  278. echo "The .app file produced is invalid. Please verify the value of PROJECT_ENV." >&2; \
  279. exit 1; \
  280. fi
  281. else
  282. $(verbose) if [ -z "$$(grep -e '^[^%]*{\s*modules\s*,' src/$(PROJECT).app.src)" ]; then \
  283. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk documentation for instructions." >&2; \
  284. exit 1; \
  285. fi
  286. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  287. | sed "s/{[[:space:]]*modules[[:space:]]*,[[:space:]]*\[\]}/{modules, \[$(call comma_list,$(MODULES))\]}/" \
  288. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(subst /,\/,$(GITDESCRIBE))\"}/" \
  289. > ebin/$(PROJECT).app
  290. endif
  291. clean:: clean-app
  292. clean-app:
  293. $(gen_verbose) rm -rf $(PROJECT).d ebin/ priv/mibs/ $(XRL_ERL_FILES) $(YRL_ERL_FILES) \
  294. $(addprefix include/,$(patsubst %.mib,%.hrl,$(notdir $(MIB_FILES)))) \
  295. $(addprefix include/,$(patsubst %.asn1,%.hrl,$(notdir $(ASN1_FILES)))) \
  296. $(addprefix include/,$(patsubst %.asn1,%.asn1db,$(notdir $(ASN1_FILES)))) \
  297. $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))
  298. endif