erlydtl.mk 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (c) 2013-2016, Loïc Hoguin <essen@ninenines.eu>
  2. # This file is part of erlang.mk and subject to the terms of the ISC License.
  3. # Configuration.
  4. DTL_FULL_PATH ?=
  5. DTL_PATH ?= templates/
  6. DTL_PREFIX ?=
  7. DTL_SUFFIX ?= _dtl
  8. DTL_OPTS ?=
  9. # Verbosity.
  10. dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
  11. dtl_verbose = $(dtl_verbose_$(V))
  12. # Core targets.
  13. DTL_PATH := $(abspath $(DTL_PATH))
  14. DTL_FILES := $(sort $(call core_find,$(DTL_PATH),*.dtl))
  15. ifneq ($(DTL_FILES),)
  16. DTL_NAMES = $(addprefix $(DTL_PREFIX),$(addsuffix $(DTL_SUFFIX),$(DTL_FILES:$(DTL_PATH)/%.dtl=%)))
  17. DTL_MODULES = $(if $(DTL_FULL_PATH),$(subst /,_,$(DTL_NAMES)),$(notdir $(DTL_NAMES)))
  18. BEAM_FILES += $(addsuffix .beam,$(addprefix ebin/,$(DTL_MODULES)))
  19. ifneq ($(words $(DTL_FILES)),0)
  20. # Rebuild templates when the Makefile changes.
  21. $(ERLANG_MK_TMP)/last-makefile-change-erlydtl: $(MAKEFILE_LIST)
  22. $(verbose) mkdir -p $(ERLANG_MK_TMP)
  23. $(verbose) if test -f $@; then \
  24. touch $(DTL_FILES); \
  25. fi
  26. $(verbose) touch $@
  27. ebin/$(PROJECT).app:: $(ERLANG_MK_TMP)/last-makefile-change-erlydtl
  28. endif
  29. define erlydtl_compile.erl
  30. [begin
  31. Module0 = case "$(strip $(DTL_FULL_PATH))" of
  32. "" ->
  33. filename:basename(F, ".dtl");
  34. _ ->
  35. "$(DTL_PATH)/" ++ F2 = filename:rootname(F, ".dtl"),
  36. re:replace(F2, "/", "_", [{return, list}, global])
  37. end,
  38. Module = list_to_atom("$(DTL_PREFIX)" ++ string:to_lower(Module0) ++ "$(DTL_SUFFIX)"),
  39. case erlydtl:compile(F, Module, [$(DTL_OPTS)] ++ [{out_dir, "ebin/"}, return_errors]) of
  40. ok -> ok;
  41. {ok, _} -> ok
  42. end
  43. end || F <- string:tokens("$(1)", " ")],
  44. halt().
  45. endef
  46. ebin/$(PROJECT).app:: $(DTL_FILES) | ebin/
  47. $(if $(strip $?),\
  48. $(dtl_verbose) $(call erlang,$(call erlydtl_compile.erl,$(call core_native_path,$?)),\
  49. -pa ebin/))
  50. endif