deps.mk 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 -ci 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. if [ ! -d $(DEPS_DIR)/$(1)/src/ ]; then \
  54. $(call dep_autopatch_noop,$(1)); \
  55. else \
  56. $(call dep_autopatch2,$(1)); \
  57. fi \
  58. fi
  59. endef
  60. define dep_autopatch2
  61. if [ ! -f $(DEPS_DIR)/$(1)/rebar.config ]; then \
  62. $(call dep_autopatch_gen,$(1)); \
  63. else \
  64. $(call dep_autopatch_rebar,$(1)); \
  65. fi
  66. endef
  67. define dep_autopatch_noop
  68. printf "noop:\n" > $(DEPS_DIR)/$(1)/Makefile
  69. endef
  70. # Overwrite erlang.mk with the current file by default.
  71. ifeq ($(NO_AUTOPATCH_ERLANG_MK),)
  72. define dep_autopatch_erlang_mk
  73. rm -f $(DEPS_DIR)/$(1)/erlang.mk; \
  74. cd $(DEPS_DIR)/$(1)/ && ln -s ../../erlang.mk; \
  75. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  76. endef
  77. else
  78. define dep_autopatch_erlang_mk
  79. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  80. endef
  81. endif
  82. define dep_autopatch_gen
  83. printf "%s\n" \
  84. "ERLC_OPTS = +debug_info" \
  85. "include ../../erlang.mk" > $(DEPS_DIR)/$(1)/Makefile; \
  86. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  87. endef
  88. define dep_autopatch_rebar
  89. if [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
  90. mv $(DEPS_DIR)/$(1)/Makefile $(DEPS_DIR)/$(1)/Makefile.orig.mk; \
  91. fi; \
  92. $(call erlang,$(call dep_autopatch_rebar.erl,$(1))); \
  93. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  94. endef
  95. define dep_autopatch_rebar.erl
  96. {ok, Conf} = file:consult("$(DEPS_DIR)/$(1)/rebar.config"),
  97. Write = fun (Text) ->
  98. file:write_file("$(DEPS_DIR)/$(1)/Makefile", Text, [append])
  99. end,
  100. Escape = fun (Text) ->
  101. re:replace(Text, "\\\\$$$$", "\$$$$$$$$", [global, {return, list}])
  102. end,
  103. Write("ERLC_OPTS = +debug_info\n\n"),
  104. fun() ->
  105. File = case lists:keyfind(deps, 1, Conf) of
  106. false -> [];
  107. {_, Deps} ->
  108. [begin
  109. Name = element(1, Dep),
  110. {Method, Repo, Commit} = case element(3, Dep) of
  111. {git, R} -> {git, R, master};
  112. {M, R, {branch, C}} -> {M, R, C};
  113. {M, R, {tag, C}} -> {M, R, C};
  114. {M, R, C} -> {M, R, C}
  115. end,
  116. Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
  117. end || Dep <- Deps, tuple_size(Dep) > 2]
  118. end
  119. end(),
  120. fun() ->
  121. First = case lists:keyfind(erl_first_files, 1, Conf) of false -> []; {_, Files} ->
  122. Names = [[" ", begin "lre." ++ Elif = lists:reverse(F), lists:reverse(Elif) end]
  123. || "src/" ++ F <- Files],
  124. Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
  125. end
  126. end(),
  127. fun() ->
  128. case lists:keyfind(port_env, 1, Conf) of
  129. {_, Vars} ->
  130. [Write(K ++ " = $$$$\(shell echo " ++ Escape(V) ++ "\)\n") || {K, V} <- Vars],
  131. Write("CFLAGS += $$$$\(DRV_CFLAGS\)\n"),
  132. Write("CXXFLAGS += $$$$\(DRV_CFLAGS\)\n"),
  133. Write("LDFLAGS += $$$$\(DRV_LDFLAGS\)\n");
  134. _ -> ok
  135. end
  136. end(),
  137. fun() ->
  138. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  139. false -> ok;
  140. true ->
  141. Sources = [[" ./c_src/", S] || S <- filelib:wildcard("*.{c,C,cc,cpp}", "$(DEPS_DIR)/$(1)/c_src")],
  142. Write(io_lib:format("SOURCES := ~s\n", [Sources]))
  143. end
  144. end(),
  145. Write("\n\nrebar_dep: pre-deps deps pre-app app\n"),
  146. Write("\npre-deps::\n"),
  147. Write("\npre-app::\n"),
  148. fun() ->
  149. case lists:keyfind(pre_hooks, 1, Conf) of
  150. false -> ok;
  151. {_, Hooks} ->
  152. [case H of
  153. {'get-deps', Command} ->
  154. Write("\npre-deps::\n\t" ++ Escape(Command) ++ "\n");
  155. {compile, Command} ->
  156. Write("\npre-app::\n\t" ++ Escape(Command) ++ "\n");
  157. {Regex, compile, Command0} ->
  158. case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
  159. match ->
  160. Command = case Command0 of
  161. "make -C" ++ _ -> Escape(Command0);
  162. "gmake -C" ++ _ -> Escape(Command0);
  163. "make " ++ Command1 -> "make -f Makefile.orig.mk " ++ Escape(Command1);
  164. "gmake " ++ Command1 -> "gmake -f Makefile.orig.mk " ++ Escape(Command1);
  165. _ -> Command0
  166. end,
  167. Write("\npre-app::\n\t" ++ Command ++ "\n");
  168. nomatch ->
  169. ok
  170. end;
  171. _ -> ok
  172. end || H <- Hooks]
  173. end
  174. end(),
  175. Write("\ninclude ../../erlang.mk"),
  176. halt()
  177. endef
  178. define dep_autopatch_appsrc.erl
  179. AppSrcOut = "$(DEPS_DIR)/$(1)/src/$(1).app.src",
  180. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(DEPS_DIR)/$(1)/ebin/$(1).app"; true -> AppSrcOut end,
  181. case filelib:is_regular(AppSrcIn) of
  182. false -> ok;
  183. true ->
  184. fun() ->
  185. {ok, [{application, $(1), L}]} = file:consult(AppSrcIn),
  186. L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end,
  187. L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, "git"}); _ -> L2 end,
  188. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}]))
  189. end(),
  190. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  191. end,
  192. halt()
  193. endef
  194. define dep_fetch
  195. if [ "$$$$VS" = "git" ]; then \
  196. git clone -q -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  197. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  198. elif [ "$$$$VS" = "hg" ]; then \
  199. hg clone -q -U $$$$REPO $(DEPS_DIR)/$(1); \
  200. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  201. elif [ "$$$$VS" = "svn" ]; then \
  202. svn checkout -q $$$$REPO $(DEPS_DIR)/$(1); \
  203. elif [ "$$$$VS" = "cp" ]; then \
  204. cp -R $$$$REPO $(DEPS_DIR)/$(1); \
  205. else \
  206. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  207. exit 78; \
  208. fi
  209. endef
  210. define dep_target
  211. $(DEPS_DIR)/$(1):
  212. @mkdir -p $(DEPS_DIR)
  213. ifeq (,$(dep_$(1)))
  214. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  215. $(dep_verbose) DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  216. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  217. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  218. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  219. $(call dep_fetch,$(1))
  220. else
  221. ifeq (1,$(words $(dep_$(1))))
  222. $(dep_verbose) VS=git; \
  223. REPO=$(dep_$(1)); \
  224. COMMIT=master; \
  225. $(call dep_fetch,$(1))
  226. else
  227. ifeq (2,$(words $(dep_$(1))))
  228. $(dep_verbose) VS=git; \
  229. REPO=$(word 1,$(dep_$(1))); \
  230. COMMIT=$(word 2,$(dep_$(1))); \
  231. $(call dep_fetch,$(1))
  232. else
  233. $(dep_verbose) VS=$(word 1,$(dep_$(1))); \
  234. REPO=$(word 2,$(dep_$(1))); \
  235. COMMIT=$(word 3,$(dep_$(1))); \
  236. $(call dep_fetch,$(1))
  237. endif
  238. endif
  239. endif
  240. @if [ -f $(DEPS_DIR)/$(1)/configure.ac ]; then \
  241. echo " AUTO " $(1); \
  242. cd $(DEPS_DIR)/$(1) && autoreconf -vif; \
  243. fi
  244. -@if [ -f $(DEPS_DIR)/$(1)/configure ]; then \
  245. echo " CONF " $(1); \
  246. cd $(DEPS_DIR)/$(1) && ./configure; \
  247. fi
  248. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  249. @$(call dep_autopatch,$(1))
  250. endif
  251. endef
  252. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  253. distclean-deps:
  254. $(gen_verbose) rm -rf $(DEPS_DIR)
  255. # Packages related targets.
  256. $(PKG_FILE2):
  257. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  258. pkg-list: $(PKG_FILE2)
  259. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  260. "Name:\t\t" $$1 "\n" \
  261. "Repository:\t" $$3 "\n" \
  262. "Website:\t" $$5 "\n" \
  263. "Description:\t" $$6 "\n" }'
  264. ifdef q
  265. pkg-search: $(PKG_FILE2)
  266. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  267. "Name:\t\t" $$1 "\n" \
  268. "Repository:\t" $$3 "\n" \
  269. "Website:\t" $$5 "\n" \
  270. "Description:\t" $$6 "\n" }'
  271. else
  272. pkg-search:
  273. $(error Usage: make pkg-search q=STRING)
  274. endif
  275. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  276. distclean-pkg:
  277. $(gen_verbose) rm -f $(PKG_FILE2)
  278. endif
  279. help::
  280. @printf "%s\n" "" \
  281. "Package-related targets:" \
  282. " pkg-list List all known packages" \
  283. " pkg-search q=STRING Search for STRING in the package index"