erlc.mk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
  16. appsrc_verbose = $(appsrc_verbose_$(V))
  17. erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
  18. $(filter %.erl %.core,$(?F)));
  19. erlc_verbose = $(erlc_verbose_$(V))
  20. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  21. xyrl_verbose = $(xyrl_verbose_$(V))
  22. asn1_verbose_0 = @echo " ASN1 " $(filter %.asn1,$(?F));
  23. asn1_verbose = $(asn1_verbose_$(V))
  24. mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
  25. mib_verbose = $(mib_verbose_$(V))
  26. # Targets.
  27. ifeq ($(wildcard ebin/test),)
  28. app:: app-build
  29. else
  30. app:: clean app-build
  31. endif
  32. app-build: erlc-include ebin/$(PROJECT).app
  33. $(eval MODULES := $(shell find ebin -type f -name \*.beam \
  34. | sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
  35. @if [ -z "$$(grep -E '^[^%]*{modules,' src/$(PROJECT).app.src)" ]; then \
  36. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
  37. exit 1; \
  38. fi
  39. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  40. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  41. | sed "s/{modules,[[:space:]]*\[\]}/{modules, \[$(MODULES)\]}/" \
  42. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(GITDESCRIBE)\"}/" \
  43. > ebin/$(PROJECT).app
  44. erlc-include:
  45. -@if [ -d ebin/ ]; then \
  46. find include/ src/ -type f -name \*.hrl -newer ebin -exec touch $(shell find src/ -type f -name "*.erl") \; 2>/dev/null || printf ''; \
  47. fi
  48. define compile_erl
  49. $(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
  50. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),\
  51. $(COMPILE_FIRST_PATHS) $(1))
  52. endef
  53. define compile_xyrl
  54. $(xyrl_verbose) erlc -v -o ebin/ $(1)
  55. $(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
  56. @rm ebin/*.erl
  57. endef
  58. define compile_asn1
  59. $(asn1_verbose) erlc -v -I include/ -o ebin/ $(1)
  60. @mv ebin/*.hrl include/
  61. @mv ebin/*.asn1db include/
  62. @rm ebin/*.erl
  63. endef
  64. define compile_mib
  65. $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ \
  66. -I priv/mibs/ $(COMPILE_MIB_FIRST_PATHS) $(1)
  67. $(mib_verbose) erlc -o include/ -- priv/mibs/*.bin
  68. endef
  69. ifneq ($(wildcard src/),)
  70. ebin/$(PROJECT).app::
  71. @mkdir -p ebin/
  72. ifneq ($(wildcard asn1/),)
  73. ebin/$(PROJECT).app:: $(shell find asn1 -type f -name \*.asn1)
  74. @mkdir -p include
  75. $(if $(strip $?),$(call compile_asn1,$?))
  76. endif
  77. ifneq ($(wildcard mibs/),)
  78. ebin/$(PROJECT).app:: $(shell find mibs -type f -name \*.mib)
  79. @mkdir -p priv/mibs/ include
  80. $(if $(strip $?),$(call compile_mib,$?))
  81. endif
  82. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.erl) \
  83. $(shell find src -type f -name \*.core)
  84. $(if $(strip $?),$(call compile_erl,$?))
  85. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.xrl) \
  86. $(shell find src -type f -name \*.yrl)
  87. $(if $(strip $?),$(call compile_xyrl,$?))
  88. endif
  89. clean:: clean-app
  90. clean-app:
  91. $(gen_verbose) rm -rf ebin/ priv/mibs/ \
  92. $(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(wildcard mibs/*.mib)))))