erlc.mk 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
  20. $(filter %.erl %.core,$(?F)));
  21. erlc_verbose = $(erlc_verbose_$(V))
  22. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  23. xyrl_verbose = $(xyrl_verbose_$(V))
  24. asn1_verbose_0 = @echo " ASN1 " $(filter %.asn1,$(?F));
  25. asn1_verbose = $(asn1_verbose_$(V))
  26. mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
  27. mib_verbose = $(mib_verbose_$(V))
  28. # Targets.
  29. ifeq ($(wildcard ebin/test),)
  30. app:: app-build
  31. else
  32. app:: clean app-build
  33. endif
  34. ifeq ($(wildcard src/$(PROJECT)_app.erl),)
  35. define app_file
  36. {application, $(PROJECT), [
  37. {description, "$(PROJECT_DESCRIPTION)"},
  38. {vsn, "$(PROJECT_VERSION)"},
  39. {id, "$(1)"},
  40. {modules, [$(call comma_list,$(2))]},
  41. {registered, []},
  42. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(DEPS))]}
  43. ]}.
  44. endef
  45. else
  46. define app_file
  47. {application, $(PROJECT), [
  48. {description, "$(PROJECT_DESCRIPTION)"},
  49. {vsn, "$(PROJECT_VERSION)"},
  50. {id, "$(1)"},
  51. {modules, [$(call comma_list,$(2))]},
  52. {registered, [$(call comma_list,$(PROJECT)_sup $(PROJECT_REGISTERED))]},
  53. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(DEPS))]},
  54. {mod, {$(PROJECT)_app, []}}
  55. ]}.
  56. endef
  57. endif
  58. app-build: erlc-include ebin/$(PROJECT).app
  59. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  60. $(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename $(wildcard ebin/*.beam))))))
  61. ifeq ($(wildcard src/$(PROJECT).app.src),)
  62. $(app_verbose) echo $(subst $(newline),,$(subst ",\",$(call app_file,$(GITDESCRIBE),$(MODULES)))) \
  63. > ebin/$(PROJECT).app
  64. else
  65. @if [ -z "$$(grep -E '^[^%]*{\s*modules\s*,' src/$(PROJECT).app.src)" ]; then \
  66. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
  67. exit 1; \
  68. fi
  69. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  70. | sed "s/{[[:space:]]*modules[[:space:]]*,[[:space:]]*\[\]}/{modules, \[$(call comma_list,$(MODULES))\]}/" \
  71. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(GITDESCRIBE)\"}/" \
  72. > ebin/$(PROJECT).app
  73. endif
  74. erlc-include:
  75. -@if [ -d ebin/ ]; then \
  76. find include/ src/ -type f -name \*.hrl -newer ebin -exec touch $(shell find src/ -type f -name "*.erl") \; 2>/dev/null || printf ''; \
  77. fi
  78. define compile_erl
  79. $(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
  80. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),\
  81. $(COMPILE_FIRST_PATHS) $(1))
  82. endef
  83. define compile_xyrl
  84. $(xyrl_verbose) erlc -v -o ebin/ $(1)
  85. $(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
  86. @rm ebin/*.erl
  87. endef
  88. define compile_asn1
  89. $(asn1_verbose) erlc -v -I include/ -o ebin/ $(1)
  90. @mv ebin/*.hrl include/
  91. @mv ebin/*.asn1db include/
  92. @rm ebin/*.erl
  93. endef
  94. define compile_mib
  95. $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ \
  96. -I priv/mibs/ $(COMPILE_MIB_FIRST_PATHS) $(1)
  97. $(mib_verbose) erlc -o include/ -- priv/mibs/*.bin
  98. endef
  99. ifneq ($(wildcard src/),)
  100. ebin/$(PROJECT).app::
  101. @mkdir -p ebin/
  102. ifneq ($(wildcard asn1/),)
  103. ebin/$(PROJECT).app:: $(sort $(call core_find,asn1/,*.asn1))
  104. @mkdir -p include
  105. $(if $(strip $?),$(call compile_asn1,$?))
  106. endif
  107. ifneq ($(wildcard mibs/),)
  108. ebin/$(PROJECT).app:: $(sort $(call core_find,mibs/,*.mib))
  109. @mkdir -p priv/mibs/ include
  110. $(if $(strip $?),$(call compile_mib,$?))
  111. endif
  112. ebin/$(PROJECT).app:: $(sort $(call core_find,src/,*.erl *.core))
  113. $(if $(strip $?),$(call compile_erl,$?))
  114. ebin/$(PROJECT).app:: $(sort $(call core_find,src/,*.xrl *.yrl))
  115. $(if $(strip $?),$(call compile_xyrl,$?))
  116. endif
  117. clean:: clean-app
  118. clean-app:
  119. $(gen_verbose) rm -rf ebin/ priv/mibs/ \
  120. $(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(call core_find,mibs/,*.mib)))))