deps.mk 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. ifneq ($(SKIP_DEPS),)
  28. deps::
  29. else
  30. deps:: $(ALL_DEPS_DIRS)
  31. @for dep in $(ALL_DEPS_DIRS) ; do \
  32. if [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ] ; then \
  33. $(MAKE) -C $$dep || exit $$? ; \
  34. else \
  35. echo "ERROR: No makefile to build dependency $$dep. Consider adding it to AUTOPATCH." ; \
  36. exit 1 ; \
  37. fi ; \
  38. done
  39. endif
  40. distclean:: distclean-deps distclean-pkg
  41. # Deps related targets.
  42. # @todo rename GNUmakefile and makefile into Makefile first, if they exist
  43. # While Makefile file could be GNUmakefile or makefile,
  44. # in practice only Makefile is needed so far.
  45. define dep_autopatch
  46. if [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
  47. if [ 0 != `grep -c rebar $(DEPS_DIR)/$(1)/Makefile` ]; then \
  48. $(call dep_autopatch2,$(1)); \
  49. else \
  50. $(call dep_autopatch_erlang_mk,$(1)); \
  51. fi \
  52. else \
  53. $(call dep_autopatch2,$(1)); \
  54. fi
  55. endef
  56. define dep_autopatch2
  57. if [ ! -f $(DEPS_DIR)/$(1)/rebar.config ]; then \
  58. $(call dep_autopatch_gen,$(1)); \
  59. else \
  60. $(call dep_autopatch_rebar,$(1)); \
  61. fi
  62. endef
  63. # Overwrite erlang.mk with the current file by default.
  64. ifeq ($(NO_AUTOPATCH_ERLANG_MK),)
  65. define dep_autopatch_erlang_mk
  66. rm -f $(DEPS_DIR)/$(1)/erlang.mk; \
  67. cd $(DEPS_DIR)/$(1)/ && ln -s ../../erlang.mk
  68. endef
  69. else
  70. define dep_autopatch_erlang_mk
  71. echo -n
  72. endef
  73. endif
  74. define dep_autopatch_gen
  75. printf "%s\n" \
  76. "ERLC_OPTS = +debug_info" \
  77. "include ../../erlang.mk" > $(DEPS_DIR)/$(1)/Makefile
  78. endef
  79. define dep_autopatch_rebar
  80. $(call erlang,$(call dep_autopatch_rebar.erl,$(1)))
  81. endef
  82. define dep_autopatch_rebar.erl
  83. DepDir = "$(DEPS_DIR)/$(1)/",
  84. fun() ->
  85. {ok, Conf} = file:consult(DepDir ++ "rebar.config"),
  86. File = case lists:keyfind(deps, 1, Conf) of false -> []; {_, Deps} ->
  87. [begin {Method, Repo, Commit} = case Repos of
  88. {git, R} -> {git, R, master};
  89. {M, R, {branch, C}} -> {M, R, C};
  90. {M, R, {tag, C}} -> {M, R, C};
  91. {M, R, C} -> {M, R, C}
  92. end,
  93. io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit])
  94. end || {Name, _, Repos} <- Deps, {Name, _, Repos, [raw]} <- Deps]
  95. end,
  96. First = case lists:keyfind(erl_first_files, 1, Conf) of false -> []; {_, Files} ->
  97. Names = [[" ", begin "lre." ++ R = lists:reverse(F), lists:reverse(R) end]
  98. || "src/" ++ F <- Files],
  99. io_lib:format("COMPILE_FIRST +=~s\n", [Names])
  100. end,
  101. ok = file:write_file("$(DEPS_DIR)/$(1)/Makefile", ["ERLC_OPTS = +debug_info\n\n", File, First, "\ninclude ../../erlang.mk"])
  102. end(),
  103. AppSrcOut = "$(DEPS_DIR)/$(1)/src/$(1).app.src",
  104. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(DEPS_DIR)/$(1)/ebin/$(1).app"; true -> AppSrcOut end,
  105. fun() ->
  106. {ok, [{application, $(1), L}]} = file:consult(AppSrcIn),
  107. L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end,
  108. L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, "git"}); _ -> L2 end,
  109. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}]))
  110. end(),
  111. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end,
  112. halt().
  113. endef
  114. define dep_fetch
  115. if [ "$$$$VS" = "git" ]; then \
  116. git clone -q -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  117. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  118. elif [ "$$$$VS" = "hg" ]; then \
  119. hg clone -q -U $$$$REPO $(DEPS_DIR)/$(1); \
  120. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  121. elif [ "$$$$VS" = "svn" ]; then \
  122. svn checkout -q $$$$REPO $(DEPS_DIR)/$(1); \
  123. elif [ "$$$$VS" = "cp" ]; then \
  124. cp -R $$$$REPO $(DEPS_DIR)/$(1); \
  125. else \
  126. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  127. exit 78; \
  128. fi
  129. endef
  130. define dep_target
  131. $(DEPS_DIR)/$(1):
  132. @mkdir -p $(DEPS_DIR)
  133. ifeq (,$(dep_$(1)))
  134. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  135. $(dep_verbose) DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  136. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  137. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  138. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  139. $(call dep_fetch,$(1))
  140. else
  141. ifeq (1,$(words $(dep_$(1))))
  142. $(dep_verbose) VS=git; \
  143. REPO=$(dep_$(1)); \
  144. COMMIT=master; \
  145. $(call dep_fetch,$(1))
  146. else
  147. ifeq (2,$(words $(dep_$(1))))
  148. $(dep_verbose) VS=git; \
  149. REPO=$(word 1,$(dep_$(1))); \
  150. COMMIT=$(word 2,$(dep_$(1))); \
  151. $(call dep_fetch,$(1))
  152. else
  153. $(dep_verbose) VS=$(word 1,$(dep_$(1))); \
  154. REPO=$(word 2,$(dep_$(1))); \
  155. COMMIT=$(word 3,$(dep_$(1))); \
  156. $(call dep_fetch,$(1))
  157. endif
  158. endif
  159. endif
  160. -@if [ -f $(DEPS_DIR)/$(1)/configure ]; then \
  161. echo " CONF " $(1); \
  162. cd $(DEPS_DIR)/$(1) && ./configure; \
  163. fi
  164. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  165. @$(call dep_autopatch,$(1))
  166. endif
  167. endef
  168. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  169. distclean-deps:
  170. $(gen_verbose) rm -rf $(DEPS_DIR)
  171. # Packages related targets.
  172. $(PKG_FILE2):
  173. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  174. pkg-list: $(PKG_FILE2)
  175. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  176. "Name:\t\t" $$1 "\n" \
  177. "Repository:\t" $$3 "\n" \
  178. "Website:\t" $$5 "\n" \
  179. "Description:\t" $$6 "\n" }'
  180. ifdef q
  181. pkg-search: $(PKG_FILE2)
  182. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  183. "Name:\t\t" $$1 "\n" \
  184. "Repository:\t" $$3 "\n" \
  185. "Website:\t" $$5 "\n" \
  186. "Description:\t" $$6 "\n" }'
  187. else
  188. pkg-search:
  189. $(error Usage: make pkg-search q=STRING)
  190. endif
  191. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  192. distclean-pkg:
  193. $(gen_verbose) rm -f $(PKG_FILE2)
  194. endif
  195. help::
  196. @printf "%s\n" "" \
  197. "Package-related targets:" \
  198. " pkg-list List all known packages" \
  199. " pkg-search q=STRING Search for STRING in the package index"