erlc.mk 12 KB

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