erlc.mk 12 KB

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