erlc.mk 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # Copyright (c) 2013-2015, 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_MIB_OPTS ?=
  12. COMPILE_MIB_FIRST ?=
  13. COMPILE_MIB_FIRST_PATHS = $(addprefix mibs/,$(addsuffix .mib,$(COMPILE_MIB_FIRST)))
  14. # Verbosity.
  15. app_verbose_0 = @echo " APP " $(PROJECT);
  16. app_verbose = $(app_verbose_$(V))
  17. appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
  18. appsrc_verbose = $(appsrc_verbose_$(V))
  19. makedep_verbose_0 = @echo " DEPEND" $(PROJECT).d;
  20. makedep_verbose = $(makedep_verbose_$(V))
  21. erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
  22. $(filter %.erl %.core,$(?F)));
  23. erlc_verbose = $(erlc_verbose_$(V))
  24. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  25. xyrl_verbose = $(xyrl_verbose_$(V))
  26. asn1_verbose_0 = @echo " ASN1 " $(filter %.asn1,$(?F));
  27. asn1_verbose = $(asn1_verbose_$(V))
  28. mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
  29. mib_verbose = $(mib_verbose_$(V))
  30. # Targets.
  31. ifeq ($(wildcard ebin/test),)
  32. app:: $(PROJECT).d
  33. $(verbose) $(MAKE) --no-print-directory app-build
  34. else
  35. app:: clean $(PROJECT).d
  36. $(verbose) $(MAKE) --no-print-directory app-build
  37. endif
  38. ifeq ($(wildcard src/$(PROJECT)_app.erl),)
  39. define app_file
  40. {application, $(PROJECT), [
  41. {description, "$(PROJECT_DESCRIPTION)"},
  42. {vsn, "$(PROJECT_VERSION)"},
  43. {id, "$(1)"},
  44. {modules, [$(call comma_list,$(2))]},
  45. {registered, []},
  46. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(DEPS))]}
  47. ]}.
  48. endef
  49. else
  50. define app_file
  51. {application, $(PROJECT), [
  52. {description, "$(PROJECT_DESCRIPTION)"},
  53. {vsn, "$(PROJECT_VERSION)"},
  54. {id, "$(1)"},
  55. {modules, [$(call comma_list,$(2))]},
  56. {registered, [$(call comma_list,$(PROJECT)_sup $(PROJECT_REGISTERED))]},
  57. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(DEPS))]},
  58. {mod, {$(PROJECT)_app, []}}
  59. ]}.
  60. endef
  61. endif
  62. app-build: ebin/$(PROJECT).app ; @echo -n
  63. # Source files.
  64. ifneq ($(wildcard src/),)
  65. ERL_FILES = $(sort $(call core_find,src/,*.erl))
  66. CORE_FILES = $(sort $(call core_find,src/,*.core))
  67. # ASN.1 files.
  68. ifneq ($(wildcard asn1/),)
  69. ASN1_FILES = $(sort $(call core_find,asn1/,*.asn1))
  70. ERL_FILES += $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))
  71. define compile_asn1
  72. $(verbose) mkdir -p include/
  73. $(asn1_verbose) erlc -v -I include/ -o asn1/ +noobj $(1)
  74. $(verbose) mv asn1/*.erl src/
  75. $(verbose) mv asn1/*.hrl include/
  76. $(verbose) mv asn1/*.asn1db include/
  77. endef
  78. $(PROJECT).d:: $(ASN1_FILES)
  79. $(if $(strip $?),$(call compile_asn1,$?))
  80. endif
  81. # SNMP MIB files.
  82. ifneq ($(wildcard mibs/),)
  83. MIB_FILES = $(sort $(call core_find,mibs/,*.mib))
  84. $(PROJECT).d:: $(COMPILE_MIB_FIRST_PATHS) $(MIB_FILES)
  85. $(verbose) mkdir -p include/ priv/mibs/
  86. $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ -I priv/mibs/ $?
  87. $(mib_verbose) erlc -o include/ -- $(addprefix priv/mibs/,$(patsubst %.mib,%.bin,$(notdir $?)))
  88. endif
  89. # Leex and Yecc files.
  90. XRL_FILES = $(sort $(call core_find,src/,*.xrl))
  91. XRL_ERL_FILES = $(addprefix src/,$(patsubst %.xrl,%.erl,$(notdir $(XRL_FILES))))
  92. ERL_FILES += $(XRL_ERL_FILES)
  93. YRL_FILES = $(sort $(call core_find,src/,*.yrl))
  94. YRL_ERL_FILES = $(addprefix src/,$(patsubst %.yrl,%.erl,$(notdir $(YRL_FILES))))
  95. ERL_FILES += $(YRL_ERL_FILES)
  96. $(PROJECT).d:: $(XRL_FILES) $(YRL_FILES)
  97. $(if $(strip $?),$(xyrl_verbose) erlc -v -o src/ $?)
  98. # Erlang and Core Erlang files.
  99. define makedep.erl
  100. ErlFiles = lists:usort(string:tokens("$(ERL_FILES)", " ")),
  101. Modules = [{filename:basename(F, ".erl"), F} || F <- ErlFiles],
  102. Add = fun (Dep, Acc) ->
  103. case lists:keyfind(atom_to_list(Dep), 1, Modules) of
  104. {_, DepFile} -> [DepFile|Acc];
  105. false -> Acc
  106. end
  107. end,
  108. AddHd = fun (Dep, Acc) ->
  109. case {Dep, lists:keymember(Dep, 2, Modules)} of
  110. {"src/" ++ _, false} -> [Dep|Acc];
  111. {"include/" ++ _, false} -> [Dep|Acc];
  112. _ -> Acc
  113. end
  114. end,
  115. CompileFirst = fun (Deps) ->
  116. First0 = [case filename:extension(D) of
  117. ".erl" -> filename:basename(D, ".erl");
  118. _ -> []
  119. end || D <- Deps],
  120. case lists:usort(First0) of
  121. [] -> [];
  122. [[]] -> [];
  123. First -> ["COMPILE_FIRST +=", [[" ", F] || F <- First], "\n"]
  124. end
  125. end,
  126. Depend = [begin
  127. case epp:parse_file(F, [{includes, ["include/"]}]) of
  128. {ok, Forms} ->
  129. Deps = lists:usort(lists:foldl(fun
  130. ({attribute, _, behavior, Dep}, Acc) -> Add(Dep, Acc);
  131. ({attribute, _, behaviour, Dep}, Acc) -> Add(Dep, Acc);
  132. ({attribute, _, compile, {parse_transform, Dep}}, Acc) -> Add(Dep, Acc);
  133. ({attribute, _, file, {Dep, _}}, Acc) -> AddHd(Dep, Acc);
  134. (_, Acc) -> Acc
  135. end, [], Forms)),
  136. case Deps of
  137. [] -> "";
  138. _ -> [F, "::", [[" ", D] || D <- Deps], "; @touch \$$@\n", CompileFirst(Deps)]
  139. end;
  140. {error, enoent} ->
  141. []
  142. end
  143. end || F <- ErlFiles],
  144. ok = file:write_file("$(1)", Depend),
  145. halt()
  146. endef
  147. $(PROJECT).d:: $(ERL_FILES) $(call core_find,include/,*.hrl)
  148. $(makedep_verbose) $(call erlang,$(call makedep.erl,$@))
  149. -include $(PROJECT).d
  150. ebin/$(PROJECT).app:: ebin/
  151. ebin/:
  152. $(verbose) mkdir -p ebin/
  153. define compile_erl
  154. $(erlc_verbose) erlc -v $(if $(IS_DEP),$(filter-out -Werror,$(ERLC_OPTS)),$(ERLC_OPTS)) -o ebin/ \
  155. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),$(COMPILE_FIRST_PATHS) $(1))
  156. endef
  157. ebin/$(PROJECT).app:: $(ERL_FILES) $(CORE_FILES)
  158. $(if $(strip $?),$(call compile_erl,$?))
  159. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  160. $(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename $(ERL_FILES) $(CORE_FILES))))))
  161. ifeq ($(wildcard src/$(PROJECT).app.src),)
  162. $(app_verbose) echo $(subst $(newline),,$(subst ",\",$(call app_file,$(GITDESCRIBE),$(MODULES)))) \
  163. > ebin/$(PROJECT).app
  164. else
  165. $(verbose) if [ -z "$$(grep -E '^[^%]*{\s*modules\s*,' src/$(PROJECT).app.src)" ]; then \
  166. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
  167. exit 1; \
  168. fi
  169. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  170. | sed "s/{[[:space:]]*modules[[:space:]]*,[[:space:]]*\[\]}/{modules, \[$(call comma_list,$(MODULES))\]}/" \
  171. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(GITDESCRIBE)\"}/" \
  172. > ebin/$(PROJECT).app
  173. endif
  174. endif
  175. clean:: clean-app
  176. clean-app:
  177. $(gen_verbose) rm -rf $(PROJECT).d ebin/ priv/mibs/ $(XRL_ERL_FILES) $(YRL_ERL_FILES) \
  178. $(addprefix include/,$(patsubst %.mib,%.hrl,$(notdir $(MIB_FILES)))) \
  179. $(addprefix include/,$(patsubst %.asn1,%.hrl,$(notdir $(ASN1_FILES)))) \
  180. $(addprefix include/,$(patsubst %.asn1,%.asn1db,$(notdir $(ASN1_FILES)))) \
  181. $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))