erlc.mk 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, [$(MODULES)]},
  41. {registered, []},
  42. {applications, $(call erlang_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, [$(MODULES)]},
  52. {registered, $(call erlang_list,$(PROJECT)_sup $(PROJECT_REGISTERED))},
  53. {applications, $(call erlang_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 MODULES := $(shell find ebin -type f -name \*.beam \
  60. | sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
  61. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  62. ifeq ($(wildcard src/$(PROJECT).app.src),)
  63. $(app_verbose) echo $(subst $(newline),,$(subst ",\",$(call app_file,$(GITDESCRIBE),$(MODULES)))) \
  64. > ebin/$(PROJECT).app
  65. else
  66. @if [ -z "$$(grep -E '^[^%]*{\s*modules\s*,' src/$(PROJECT).app.src)" ]; then \
  67. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
  68. exit 1; \
  69. fi
  70. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  71. | sed "s/{modules,[[:space:]]*\[\]}/{modules, \[$(MODULES)\]}/" \
  72. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(GITDESCRIBE)\"}/" \
  73. > ebin/$(PROJECT).app
  74. endif
  75. erlc-include:
  76. -@if [ -d ebin/ ]; then \
  77. find include/ src/ -type f -name \*.hrl -newer ebin -exec touch $(shell find src/ -type f -name "*.erl") \; 2>/dev/null || printf ''; \
  78. fi
  79. define compile_erl
  80. $(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
  81. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),\
  82. $(COMPILE_FIRST_PATHS) $(1))
  83. endef
  84. define compile_xyrl
  85. $(xyrl_verbose) erlc -v -o ebin/ $(1)
  86. $(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
  87. @rm ebin/*.erl
  88. endef
  89. define compile_asn1
  90. $(asn1_verbose) erlc -v -I include/ -o ebin/ $(1)
  91. @mv ebin/*.hrl include/
  92. @mv ebin/*.asn1db include/
  93. @rm ebin/*.erl
  94. endef
  95. define compile_mib
  96. $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ \
  97. -I priv/mibs/ $(COMPILE_MIB_FIRST_PATHS) $(1)
  98. $(mib_verbose) erlc -o include/ -- priv/mibs/*.bin
  99. endef
  100. ifneq ($(wildcard src/),)
  101. ebin/$(PROJECT).app::
  102. @mkdir -p ebin/
  103. ifneq ($(wildcard asn1/),)
  104. ebin/$(PROJECT).app:: $(shell find asn1 -type f -name \*.asn1)
  105. @mkdir -p include
  106. $(if $(strip $?),$(call compile_asn1,$?))
  107. endif
  108. ifneq ($(wildcard mibs/),)
  109. ebin/$(PROJECT).app:: $(shell find mibs -type f -name \*.mib)
  110. @mkdir -p priv/mibs/ include
  111. $(if $(strip $?),$(call compile_mib,$?))
  112. endif
  113. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.erl -o -name \*.core)
  114. $(if $(strip $?),$(call compile_erl,$?))
  115. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.xrl -o -name \*.yrl)
  116. $(if $(strip $?),$(call compile_xyrl,$?))
  117. endif
  118. clean:: clean-app
  119. clean-app:
  120. $(gen_verbose) rm -rf ebin/ priv/mibs/ \
  121. $(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(wildcard mibs/*.mib)))))