erlc.mk 7.5 KB

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