deps.mk 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 IS_DEP=1 || 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. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  69. endef
  70. else
  71. define dep_autopatch_erlang_mk
  72. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  73. endef
  74. endif
  75. define dep_autopatch_gen
  76. printf "%s\n" \
  77. "ERLC_OPTS = +debug_info" \
  78. "include ../../erlang.mk" > $(DEPS_DIR)/$(1)/Makefile; \
  79. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  80. endef
  81. define dep_autopatch_rebar
  82. rm -f $(DEPS_DIR)/$(1)/Makefile; \
  83. $(call erlang,$(call dep_autopatch_rebar.erl,$(1))); \
  84. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  85. endef
  86. define dep_autopatch_rebar.erl
  87. {ok, Conf} = file:consult("$(DEPS_DIR)/$(1)/rebar.config"),
  88. Write = fun (Text) ->
  89. file:write_file("$(DEPS_DIR)/$(1)/Makefile", Text, [append])
  90. end,
  91. Write("ERLC_OPTS = +debug_info\n\n"),
  92. fun() ->
  93. File = case lists:keyfind(deps, 1, Conf) of
  94. false -> [];
  95. {_, Deps} ->
  96. [begin
  97. Name = element(1, Dep),
  98. {Method, Repo, Commit} = case element(3, Dep) of
  99. {git, R} -> {git, R, master};
  100. {M, R, {branch, C}} -> {M, R, C};
  101. {M, R, {tag, C}} -> {M, R, C};
  102. {M, R, C} -> {M, R, C}
  103. end,
  104. Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
  105. end || Dep <- Deps, tuple_size(Dep) > 2]
  106. end
  107. end(),
  108. fun() ->
  109. First = case lists:keyfind(erl_first_files, 1, Conf) of false -> []; {_, Files} ->
  110. Names = [[" ", begin "lre." ++ Elif = lists:reverse(F), lists:reverse(Elif) end]
  111. || "src/" ++ F <- Files],
  112. Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
  113. end
  114. end(),
  115. Write("\ninclude ../../erlang.mk"),
  116. halt()
  117. endef
  118. define dep_autopatch_appsrc.erl
  119. AppSrcOut = "$(DEPS_DIR)/$(1)/src/$(1).app.src",
  120. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(DEPS_DIR)/$(1)/ebin/$(1).app"; true -> AppSrcOut end,
  121. case filelib:is_regular(AppSrcIn) of
  122. false -> ok;
  123. true ->
  124. fun() ->
  125. {ok, [{application, $(1), L}]} = file:consult(AppSrcIn),
  126. L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end,
  127. L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, "git"}); _ -> L2 end,
  128. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}]))
  129. end(),
  130. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  131. end,
  132. halt()
  133. endef
  134. define dep_fetch
  135. if [ "$$$$VS" = "git" ]; then \
  136. git clone -q -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  137. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  138. elif [ "$$$$VS" = "hg" ]; then \
  139. hg clone -q -U $$$$REPO $(DEPS_DIR)/$(1); \
  140. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  141. elif [ "$$$$VS" = "svn" ]; then \
  142. svn checkout -q $$$$REPO $(DEPS_DIR)/$(1); \
  143. elif [ "$$$$VS" = "cp" ]; then \
  144. cp -R $$$$REPO $(DEPS_DIR)/$(1); \
  145. else \
  146. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  147. exit 78; \
  148. fi
  149. endef
  150. define dep_target
  151. $(DEPS_DIR)/$(1):
  152. @mkdir -p $(DEPS_DIR)
  153. ifeq (,$(dep_$(1)))
  154. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  155. $(dep_verbose) DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  156. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  157. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  158. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  159. $(call dep_fetch,$(1))
  160. else
  161. ifeq (1,$(words $(dep_$(1))))
  162. $(dep_verbose) VS=git; \
  163. REPO=$(dep_$(1)); \
  164. COMMIT=master; \
  165. $(call dep_fetch,$(1))
  166. else
  167. ifeq (2,$(words $(dep_$(1))))
  168. $(dep_verbose) VS=git; \
  169. REPO=$(word 1,$(dep_$(1))); \
  170. COMMIT=$(word 2,$(dep_$(1))); \
  171. $(call dep_fetch,$(1))
  172. else
  173. $(dep_verbose) VS=$(word 1,$(dep_$(1))); \
  174. REPO=$(word 2,$(dep_$(1))); \
  175. COMMIT=$(word 3,$(dep_$(1))); \
  176. $(call dep_fetch,$(1))
  177. endif
  178. endif
  179. endif
  180. @if [ -f $(DEPS_DIR)/$(1)/configure.ac ]; then \
  181. echo " AUTO " $(1); \
  182. cd $(DEPS_DIR)/$(1) && autoreconf -vif; \
  183. fi
  184. -@if [ -f $(DEPS_DIR)/$(1)/configure ]; then \
  185. echo " CONF " $(1); \
  186. cd $(DEPS_DIR)/$(1) && ./configure; \
  187. fi
  188. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  189. @$(call dep_autopatch,$(1))
  190. endif
  191. endef
  192. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  193. distclean-deps:
  194. $(gen_verbose) rm -rf $(DEPS_DIR)
  195. # Packages related targets.
  196. $(PKG_FILE2):
  197. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  198. pkg-list: $(PKG_FILE2)
  199. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  200. "Name:\t\t" $$1 "\n" \
  201. "Repository:\t" $$3 "\n" \
  202. "Website:\t" $$5 "\n" \
  203. "Description:\t" $$6 "\n" }'
  204. ifdef q
  205. pkg-search: $(PKG_FILE2)
  206. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  207. "Name:\t\t" $$1 "\n" \
  208. "Repository:\t" $$3 "\n" \
  209. "Website:\t" $$5 "\n" \
  210. "Description:\t" $$6 "\n" }'
  211. else
  212. pkg-search:
  213. $(error Usage: make pkg-search q=STRING)
  214. endif
  215. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  216. distclean-pkg:
  217. $(gen_verbose) rm -f $(PKG_FILE2)
  218. endif
  219. help::
  220. @printf "%s\n" "" \
  221. "Package-related targets:" \
  222. " pkg-list List all known packages" \
  223. " pkg-search q=STRING Search for STRING in the package index"