erlc.mk 3.2 KB

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