deps.mk 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: distclean-deps distclean-pkg pkg-list pkg-search
  4. # Configuration.
  5. AUTOPATCH ?= edown gen_leader gproc
  6. export AUTOPATCH
  7. DEPS_DIR ?= $(CURDIR)/deps
  8. export DEPS_DIR
  9. REBAR_DEPS_DIR = $(DEPS_DIR)
  10. export REBAR_DEPS_DIR
  11. ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DEPS))
  12. ifeq ($(filter $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),)
  13. ifeq ($(ERL_LIBS),)
  14. ERL_LIBS = $(DEPS_DIR)
  15. else
  16. ERL_LIBS := $(ERL_LIBS):$(DEPS_DIR)
  17. endif
  18. endif
  19. export ERL_LIBS
  20. PKG_FILE2 ?= $(CURDIR)/.erlang.mk.packages.v2
  21. export PKG_FILE2
  22. PKG_FILE_URL ?= https://raw.githubusercontent.com/ninenines/erlang.mk/master/packages.v2.tsv
  23. # Verbosity.
  24. dep_verbose_0 = @echo " DEP " $(1);
  25. dep_verbose = $(dep_verbose_$(V))
  26. # Core targets.
  27. deps:: $(ALL_DEPS_DIRS)
  28. @for dep in $(ALL_DEPS_DIRS) ; do \
  29. if [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ] ; then \
  30. $(MAKE) -C $$dep || exit $$? ; \
  31. else \
  32. echo "ERROR: No makefile to build dependency $$dep. Consider adding it to AUTOPATCH." ; \
  33. exit 1 ; \
  34. fi ; \
  35. done
  36. distclean:: distclean-deps distclean-pkg
  37. # Deps related targets.
  38. define dep_autopatch
  39. $(ERL) -eval " \
  40. DepDir = \"$(DEPS_DIR)/$(1)/\", \
  41. fun() -> \
  42. {ok, Conf} = case file:consult(DepDir ++ \"rebar.config\") of \
  43. {error, enoent} -> {ok, []}; Res -> Res end, \
  44. File = case lists:keyfind(deps, 1, Conf) of false -> []; {_, Deps} -> \
  45. [begin {Method, Repo, Commit} = case Repos of \
  46. {git, R} -> {git, R, master}; \
  47. {M, R, {branch, C}} -> {M, R, C}; \
  48. {M, R, {tag, C}} -> {M, R, C}; \
  49. {M, R, C} -> {M, R, C} \
  50. end, \
  51. io_lib:format(\"DEPS += ~s\ndep_~s = ~s ~s ~s~n\", [Name, Name, Method, Repo, Commit]) \
  52. end || {Name, _, Repos} <- Deps] \
  53. end, \
  54. First = case lists:keyfind(erl_first_files, 1, Conf) of false -> []; {_, Files} -> \
  55. Names = [[\" \", begin \"lre.\" ++ R = lists:reverse(F), lists:reverse(R) end] \
  56. || \"src/\" ++ F <- Files], \
  57. io_lib:format(\"COMPILE_FIRST +=~s\n\", [Names]) \
  58. end, \
  59. ok = file:write_file(\"$(DEPS_DIR)/$(1)/Makefile\", [\"ERLC_OPTS = +debug_info\n\n\", File, First, \"\ninclude erlang.mk\"]) \
  60. end(), \
  61. AppSrcOut = \"$(DEPS_DIR)/$(1)/src/$(1).app.src\", \
  62. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> \"$(DEPS_DIR)/$(1)/ebin/$(1).app\"; true -> AppSrcOut end, \
  63. fun() -> \
  64. {ok, [{application, $(1), L}]} = file:consult(AppSrcIn), \
  65. L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end, \
  66. L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, \"git\"}); _ -> L2 end, \
  67. ok = file:write_file(AppSrcOut, io_lib:format(\"~p.~n\", [{application, $(1), L3}])) \
  68. end(), \
  69. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end, \
  70. halt()."
  71. endef
  72. ifeq ($(V),0)
  73. define dep_autopatch_verbose
  74. @echo " PATCH " $(1);
  75. endef
  76. endif
  77. define dep_fetch
  78. if [ "$$$$VS" = "git" ]; then \
  79. git clone -q -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  80. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  81. elif [ "$$$$VS" = "hg" ]; then \
  82. hg clone -q -U $$$$REPO $(DEPS_DIR)/$(1); \
  83. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  84. elif [ "$$$$VS" = "svn" ]; then \
  85. svn checkout -q $$$$REPO $(DEPS_DIR)/$(1); \
  86. elif [ "$$$$VS" = "cp" ]; then \
  87. cp -R $$$$REPO $(DEPS_DIR)/$(1); \
  88. else \
  89. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  90. exit 78; \
  91. fi
  92. endef
  93. define dep_target
  94. $(DEPS_DIR)/$(1):
  95. @mkdir -p $(DEPS_DIR)
  96. ifeq (,$(dep_$(1)))
  97. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  98. $(dep_verbose) DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  99. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  100. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  101. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  102. $(call dep_fetch,$(1))
  103. else
  104. $(dep_verbose) VS=$(word 1,$(dep_$(1))); \
  105. REPO=$(word 2,$(dep_$(1))); \
  106. COMMIT=$(word 3,$(dep_$(1))); \
  107. $(call dep_fetch,$(1))
  108. endif
  109. ifneq ($(filter $(1),$(AUTOPATCH)),)
  110. $(call dep_autopatch_verbose,$(1)) \
  111. $(call dep_autopatch,$(1)); \
  112. cd $(DEPS_DIR)/$(1)/ && ln -s ../../erlang.mk
  113. endif
  114. endef
  115. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  116. distclean-deps:
  117. $(gen_verbose) rm -rf $(DEPS_DIR)
  118. # Packages related targets.
  119. $(PKG_FILE2):
  120. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  121. pkg-list: $(PKG_FILE2)
  122. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  123. "Name:\t\t" $$1 "\n" \
  124. "Repository:\t" $$3 "\n" \
  125. "Website:\t" $$5 "\n" \
  126. "Description:\t" $$6 "\n" }'
  127. ifdef q
  128. pkg-search: $(PKG_FILE2)
  129. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  130. "Name:\t\t" $$1 "\n" \
  131. "Repository:\t" $$3 "\n" \
  132. "Website:\t" $$5 "\n" \
  133. "Description:\t" $$6 "\n" }'
  134. else
  135. pkg-search:
  136. $(error Usage: make pkg-search q=STRING)
  137. endif
  138. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  139. distclean-pkg:
  140. $(gen_verbose) rm -f $(PKG_FILE2)
  141. endif
  142. help::
  143. @printf "%s\n" "" \
  144. "Package-related targets:" \
  145. " pkg-list List all known packages" \
  146. " pkg-search q=STRING Search for STRING in the package index"