deps.mk 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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
  4. # Configuration.
  5. IGNORE_DEPS ?=
  6. DEPS_DIR ?= $(CURDIR)/deps
  7. export DEPS_DIR
  8. REBAR_DEPS_DIR = $(DEPS_DIR)
  9. export REBAR_DEPS_DIR
  10. ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(filter-out $(IGNORE_DEPS),$(DEPS)))
  11. ifeq ($(filter $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),)
  12. ifeq ($(ERL_LIBS),)
  13. ERL_LIBS = $(DEPS_DIR)
  14. else
  15. ERL_LIBS := $(ERL_LIBS):$(DEPS_DIR)
  16. endif
  17. endif
  18. export ERL_LIBS
  19. # Verbosity.
  20. dep_verbose_0 = @echo " DEP " $(1);
  21. dep_verbose = $(dep_verbose_$(V))
  22. # Core targets.
  23. ifneq ($(SKIP_DEPS),)
  24. deps::
  25. else
  26. deps:: $(ALL_DEPS_DIRS)
  27. ifneq ($(IS_DEP),1)
  28. $(verbose) rm -f $(ERLANG_MK_TMP)/deps.log
  29. endif
  30. $(verbose) mkdir -p $(ERLANG_MK_TMP)
  31. $(verbose) for dep in $(ALL_DEPS_DIRS) ; do \
  32. if grep -qs ^$$dep$$ $(ERLANG_MK_TMP)/deps.log; then \
  33. echo -n; \
  34. else \
  35. echo $$dep >> $(ERLANG_MK_TMP)/deps.log; \
  36. if [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ]; then \
  37. $(MAKE) -C $$dep IS_DEP=1 || exit $$?; \
  38. else \
  39. echo "ERROR: No Makefile to build dependency $$dep."; \
  40. exit 1; \
  41. fi \
  42. fi \
  43. done
  44. endif
  45. distclean:: distclean-deps distclean-pkg
  46. # Deps related targets.
  47. # @todo rename GNUmakefile and makefile into Makefile first, if they exist
  48. # While Makefile file could be GNUmakefile or makefile,
  49. # in practice only Makefile is needed so far.
  50. define dep_autopatch
  51. if [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
  52. if [ 0 != `grep -c "include ../\w*\.mk" $(DEPS_DIR)/$(1)/Makefile` ]; then \
  53. $(call dep_autopatch2,$(1)); \
  54. elif [ 0 != `grep -ci rebar $(DEPS_DIR)/$(1)/Makefile` ]; then \
  55. $(call dep_autopatch2,$(1)); \
  56. elif [ -n "`find $(DEPS_DIR)/$(1)/ -type f -name \*.mk -not -name erlang.mk -exec grep -i rebar '{}' \;`" ]; then \
  57. $(call dep_autopatch2,$(1)); \
  58. else \
  59. if [ -f $(DEPS_DIR)/$(1)/erlang.mk ]; then \
  60. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1))); \
  61. $(call dep_autopatch_erlang_mk,$(1)); \
  62. else \
  63. $(call erlang,$(call dep_autopatch_app.erl,$(1))); \
  64. fi \
  65. fi \
  66. else \
  67. if [ ! -d $(DEPS_DIR)/$(1)/src/ ]; then \
  68. $(call dep_autopatch_noop,$(1)); \
  69. else \
  70. $(call dep_autopatch2,$(1)); \
  71. fi \
  72. fi
  73. endef
  74. define dep_autopatch2
  75. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1))); \
  76. if [ -f $(DEPS_DIR)/$(1)/rebar.config -o -f $(DEPS_DIR)/$(1)/rebar.config.script ]; then \
  77. $(call dep_autopatch_fetch_rebar); \
  78. $(call dep_autopatch_rebar,$(1)); \
  79. else \
  80. $(call dep_autopatch_gen,$(1)); \
  81. fi
  82. endef
  83. define dep_autopatch_noop
  84. printf "noop:\n" > $(DEPS_DIR)/$(1)/Makefile
  85. endef
  86. # Overwrite erlang.mk with the current file by default.
  87. ifeq ($(NO_AUTOPATCH_ERLANG_MK),)
  88. define dep_autopatch_erlang_mk
  89. echo "include $(ERLANG_MK_FILENAME)" > $(DEPS_DIR)/$(1)/erlang.mk
  90. endef
  91. else
  92. define dep_autopatch_erlang_mk
  93. echo -n
  94. endef
  95. endif
  96. define dep_autopatch_gen
  97. printf "%s\n" \
  98. "ERLC_OPTS = +debug_info" \
  99. "include ../../erlang.mk" > $(DEPS_DIR)/$(1)/Makefile
  100. endef
  101. define dep_autopatch_fetch_rebar
  102. mkdir -p $(ERLANG_MK_TMP); \
  103. if [ ! -d $(ERLANG_MK_TMP)/rebar ]; then \
  104. git clone -q -n -- https://github.com/rebar/rebar $(ERLANG_MK_TMP)/rebar; \
  105. cd $(ERLANG_MK_TMP)/rebar; \
  106. git checkout -q 791db716b5a3a7671e0b351f95ddf24b848ee173; \
  107. $(MAKE); \
  108. cd -; \
  109. fi
  110. endef
  111. define dep_autopatch_rebar
  112. if [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
  113. mv $(DEPS_DIR)/$(1)/Makefile $(DEPS_DIR)/$(1)/Makefile.orig.mk; \
  114. fi; \
  115. $(call erlang,$(call dep_autopatch_rebar.erl,$(1))); \
  116. rm -f $(DEPS_DIR)/$(1)/ebin/$(1).app
  117. endef
  118. define dep_autopatch_rebar.erl
  119. application:set_env(rebar, log_level, debug),
  120. Conf1 = case file:consult("$(DEPS_DIR)/$(1)/rebar.config") of
  121. {ok, Conf0} -> Conf0;
  122. _ -> []
  123. end,
  124. {Conf, OsEnv} = fun() ->
  125. case filelib:is_file("$(DEPS_DIR)/$(1)/rebar.config.script") of
  126. false -> {Conf1, []};
  127. true ->
  128. Bindings0 = erl_eval:new_bindings(),
  129. Bindings1 = erl_eval:add_binding('CONFIG', Conf1, Bindings0),
  130. Bindings = erl_eval:add_binding('SCRIPT', "$(DEPS_DIR)/$(1)/rebar.config.script", Bindings1),
  131. Before = os:getenv(),
  132. {ok, Conf2} = file:script("$(DEPS_DIR)/$(1)/rebar.config.script", Bindings),
  133. {Conf2, lists:foldl(fun(E, Acc) -> lists:delete(E, Acc) end, os:getenv(), Before)}
  134. end
  135. end(),
  136. Write = fun (Text) ->
  137. file:write_file("$(DEPS_DIR)/$(1)/Makefile", Text, [append])
  138. end,
  139. Escape = fun (Text) ->
  140. re:replace(Text, "\\\\$$$$", "\$$$$$$$$", [global, {return, list}])
  141. end,
  142. Write("IGNORE_DEPS = edown eper eunit_formatters meck node_package "
  143. "rebar_lock_deps_plugin rebar_vsn_plugin reltool_util\n"),
  144. Write("C_SRC_DIR = /path/do/not/exist\n"),
  145. Write("DRV_CFLAGS = -fPIC\nexport DRV_CFLAGS\n"),
  146. Write(["ERLANG_ARCH = ", rebar_utils:wordsize(), "\nexport ERLANG_ARCH\n"]),
  147. fun() ->
  148. Write("ERLC_OPTS = +debug_info\nexport ERLC_OPTS\n"),
  149. case lists:keyfind(erl_opts, 1, Conf) of
  150. false -> ok;
  151. {_, ErlOpts} ->
  152. lists:foreach(fun
  153. ({d, D}) ->
  154. Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
  155. ({i, I}) ->
  156. Write(["ERLC_OPTS += -I ", I, "\n"]);
  157. ({platform_define, Regex, D}) ->
  158. case rebar_utils:is_arch(Regex) of
  159. true -> Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
  160. false -> ok
  161. end;
  162. ({parse_transform, PT}) ->
  163. Write("ERLC_OPTS += +'{parse_transform, " ++ atom_to_list(PT) ++ "}'\n");
  164. (_) -> ok
  165. end, ErlOpts)
  166. end,
  167. Write("\n")
  168. end(),
  169. fun() ->
  170. File = case lists:keyfind(deps, 1, Conf) of
  171. false -> [];
  172. {_, Deps} ->
  173. [begin case case Dep of
  174. {N, S} when is_atom(N), is_list(S) -> {N, {hex, S}};
  175. {N, S} when is_tuple(S) -> {N, S};
  176. {N, _, S} -> {N, S};
  177. {N, _, S, _} -> {N, S};
  178. _ -> false
  179. end of
  180. false -> ok;
  181. {Name, Source} ->
  182. {Method, Repo, Commit} = case Source of
  183. {hex, V} -> {hex, V, undefined};
  184. {git, R} -> {git, R, master};
  185. {M, R, {branch, C}} -> {M, R, C};
  186. {M, R, {ref, C}} -> {M, R, C};
  187. {M, R, {tag, C}} -> {M, R, C};
  188. {M, R, C} -> {M, R, C}
  189. end,
  190. Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
  191. end end || Dep <- Deps]
  192. end
  193. end(),
  194. fun() ->
  195. case lists:keyfind(erl_first_files, 1, Conf) of
  196. false -> ok;
  197. {_, Files} ->
  198. Names = [[" ", case lists:reverse(F) of
  199. "lre." ++ Elif -> lists:reverse(Elif);
  200. Elif -> lists:reverse(Elif)
  201. end] || "src/" ++ F <- Files],
  202. Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
  203. end
  204. end(),
  205. FindFirst = fun(F, Fd) ->
  206. case io:parse_erl_form(Fd, undefined) of
  207. {ok, {attribute, _, compile, {parse_transform, PT}}, _} ->
  208. [PT, F(F, Fd)];
  209. {ok, {attribute, _, compile, CompileOpts}, _} when is_list(CompileOpts) ->
  210. case proplists:get_value(parse_transform, CompileOpts) of
  211. undefined -> [F(F, Fd)];
  212. PT -> [PT, F(F, Fd)]
  213. end;
  214. {ok, {attribute, _, include, Hrl}, _} ->
  215. case file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]) of
  216. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  217. _ ->
  218. case file:open("$(DEPS_DIR)/$(1)/src/" ++ Hrl, [read]) of
  219. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  220. _ -> [F(F, Fd)]
  221. end
  222. end;
  223. {ok, {attribute, _, include_lib, "$(1)/include/" ++ Hrl}, _} ->
  224. {ok, HrlFd} = file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]),
  225. [F(F, HrlFd), F(F, Fd)];
  226. {ok, {attribute, _, include_lib, Hrl}, _} ->
  227. case file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]) of
  228. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  229. _ -> [F(F, Fd)]
  230. end;
  231. {ok, {attribute, _, import, {Imp, _}}, _} ->
  232. case file:open("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(Imp) ++ ".erl", [read]) of
  233. {ok, ImpFd} -> [Imp, F(F, ImpFd), F(F, Fd)];
  234. _ -> [F(F, Fd)]
  235. end;
  236. {eof, _} ->
  237. file:close(Fd),
  238. [];
  239. _ ->
  240. F(F, Fd)
  241. end
  242. end,
  243. fun() ->
  244. ErlFiles = filelib:wildcard("$(DEPS_DIR)/$(1)/src/*.erl"),
  245. First0 = lists:usort(lists:flatten([begin
  246. {ok, Fd} = file:open(F, [read]),
  247. FindFirst(FindFirst, Fd)
  248. end || F <- ErlFiles])),
  249. First = lists:flatten([begin
  250. {ok, Fd} = file:open("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", [read]),
  251. FindFirst(FindFirst, Fd)
  252. end || M <- First0, lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)]) ++ First0,
  253. Write(["COMPILE_FIRST +=", [[" ", atom_to_list(M)] || M <- First,
  254. lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)], "\n"])
  255. end(),
  256. Write("\n\nrebar_dep: preprocess pre-deps deps pre-app app\n"),
  257. Write("\npreprocess::\n"),
  258. Write("\npre-deps::\n"),
  259. Write("\npre-app::\n"),
  260. PatchHook = fun(Cmd) ->
  261. case Cmd of
  262. "make -C" ++ Cmd1 -> "$$$$\(MAKE) -C" ++ Escape(Cmd1);
  263. "gmake -C" ++ Cmd1 -> "$$$$\(MAKE) -C" ++ Escape(Cmd1);
  264. "make " ++ Cmd1 -> "$$$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
  265. "gmake " ++ Cmd1 -> "$$$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
  266. _ -> Escape(Cmd)
  267. end
  268. end,
  269. fun() ->
  270. case lists:keyfind(pre_hooks, 1, Conf) of
  271. false -> ok;
  272. {_, Hooks} ->
  273. [case H of
  274. {'get-deps', Cmd} ->
  275. Write("\npre-deps::\n\t" ++ PatchHook(Cmd) ++ "\n");
  276. {compile, Cmd} ->
  277. Write("\npre-app::\n\tCC=$$$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
  278. {Regex, compile, Cmd} ->
  279. case rebar_utils:is_arch(Regex) of
  280. true -> Write("\npre-app::\n\tCC=$$$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
  281. false -> ok
  282. end;
  283. _ -> ok
  284. end || H <- Hooks]
  285. end
  286. end(),
  287. ShellToMk = fun(V) ->
  288. re:replace(re:replace(V, "(\\\\$$$$)(\\\\w*)", "\\\\1(\\\\2)", [global]),
  289. "-Werror\\\\b", "", [{return, list}, global])
  290. end,
  291. PortSpecs = fun() ->
  292. case lists:keyfind(port_specs, 1, Conf) of
  293. false ->
  294. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  295. false -> [];
  296. true ->
  297. [{"priv/" ++ proplists:get_value(so_name, Conf, "$(1)_drv.so"),
  298. proplists:get_value(port_sources, Conf, ["c_src/*.c"]), []}]
  299. end;
  300. {_, Specs} ->
  301. lists:flatten([case S of
  302. {Output, Input} -> {ShellToMk(Output), Input, []};
  303. {Regex, Output, Input} ->
  304. case rebar_utils:is_arch(Regex) of
  305. true -> {ShellToMk(Output), Input, []};
  306. false -> []
  307. end;
  308. {Regex, Output, Input, [{env, Env}]} ->
  309. case rebar_utils:is_arch(Regex) of
  310. true -> {ShellToMk(Output), Input, Env};
  311. false -> []
  312. end
  313. end || S <- Specs])
  314. end
  315. end(),
  316. PortSpecWrite = fun (Text) ->
  317. file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile.erlang.mk", Text, [append])
  318. end,
  319. case PortSpecs of
  320. [] -> ok;
  321. _ ->
  322. Write("\npre-app::\n\t$$$$\(MAKE) -f c_src/Makefile.erlang.mk\n"),
  323. PortSpecWrite(io_lib:format("ERL_CFLAGS = -finline-functions -Wall -fPIC -I ~s/erts-~s/include -I ~s\n",
  324. [code:root_dir(), erlang:system_info(version), code:lib_dir(erl_interface, include)])),
  325. PortSpecWrite(io_lib:format("ERL_LDFLAGS = -L ~s -lerl_interface -lei\n",
  326. [code:lib_dir(erl_interface, lib)])),
  327. [PortSpecWrite(["\n", E, "\n"]) || E <- OsEnv],
  328. FilterEnv = fun(Env) ->
  329. lists:flatten([case E of
  330. {_, _} -> E;
  331. {Regex, K, V} ->
  332. case rebar_utils:is_arch(Regex) of
  333. true -> {K, V};
  334. false -> []
  335. end
  336. end || E <- Env])
  337. end,
  338. MergeEnv = fun(Env) ->
  339. lists:foldl(fun ({K, V}, Acc) ->
  340. case lists:keyfind(K, 1, Acc) of
  341. false -> [{K, rebar_utils:expand_env_variable(V, K, "")}|Acc];
  342. {_, V0} -> [{K, rebar_utils:expand_env_variable(V, K, V0)}|Acc]
  343. end
  344. end, [], Env)
  345. end,
  346. PortEnv = case lists:keyfind(port_env, 1, Conf) of
  347. false -> [];
  348. {_, PortEnv0} -> FilterEnv(PortEnv0)
  349. end,
  350. PortSpec = fun ({Output, Input0, Env}) ->
  351. filelib:ensure_dir("$(DEPS_DIR)/$(1)/" ++ Output),
  352. Input = [[" ", I] || I <- Input0],
  353. PortSpecWrite([
  354. [["\n", K, " = ", ShellToMk(V)] || {K, V} <- lists:reverse(MergeEnv(PortEnv))],
  355. case $(PLATFORM) of
  356. darwin -> "\n\nLDFLAGS += -flat_namespace -undefined suppress";
  357. _ -> ""
  358. end,
  359. "\n\nall:: ", Output, "\n\n",
  360. "%.o: %.c\n\t$$$$\(CC) -c -o $$$$\@ $$$$\< $$$$\(CFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  361. "%.o: %.C\n\t$$$$\(CXX) -c -o $$$$\@ $$$$\< $$$$\(CXXFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  362. "%.o: %.cc\n\t$$$$\(CXX) -c -o $$$$\@ $$$$\< $$$$\(CXXFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  363. "%.o: %.cpp\n\t$$$$\(CXX) -c -o $$$$\@ $$$$\< $$$$\(CXXFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  364. [[Output, ": ", K, " = ", ShellToMk(V), "\n"] || {K, V} <- lists:reverse(MergeEnv(FilterEnv(Env)))],
  365. Output, ": $$$$\(foreach ext,.c .C .cc .cpp,",
  366. "$$$$\(patsubst %$$$$\(ext),%.o,$$$$\(filter %$$$$\(ext),$$$$\(wildcard", Input, "))))\n",
  367. "\t$$$$\(CC) -o $$$$\@ $$$$\? $$$$\(LDFLAGS) $$$$\(ERL_LDFLAGS) $$$$\(DRV_LDFLAGS) $$$$\(EXE_LDFLAGS)",
  368. case filename:extension(Output) of
  369. [] -> "\n";
  370. _ -> " -shared\n"
  371. end])
  372. end,
  373. [PortSpec(S) || S <- PortSpecs]
  374. end,
  375. Write("\ninclude $(ERLANG_MK_FILENAME)"),
  376. RunPlugin = fun(Plugin, Step) ->
  377. case erlang:function_exported(Plugin, Step, 2) of
  378. false -> ok;
  379. true ->
  380. c:cd("$(DEPS_DIR)/$(1)/"),
  381. Ret = Plugin:Step({config, "", Conf, dict:new(), dict:new(), dict:new(),
  382. dict:store(base_dir, "", dict:new())}, undefined),
  383. io:format("rebar plugin ~p step ~p ret ~p~n", [Plugin, Step, Ret])
  384. end
  385. end,
  386. fun() ->
  387. case lists:keyfind(plugins, 1, Conf) of
  388. false -> ok;
  389. {_, Plugins} ->
  390. [begin
  391. case lists:keyfind(deps, 1, Conf) of
  392. false -> ok;
  393. {_, Deps} ->
  394. case lists:keyfind(P, 1, Deps) of
  395. false -> ok;
  396. _ ->
  397. Path = "$(DEPS_DIR)/" ++ atom_to_list(P),
  398. io:format("~s", [os:cmd("$(MAKE) -C $(DEPS_DIR)/$(1) " ++ Path)]),
  399. io:format("~s", [os:cmd("$(MAKE) -C " ++ Path ++ " IS_DEP=1")]),
  400. code:add_patha(Path ++ "/ebin")
  401. end
  402. end
  403. end || P <- Plugins],
  404. [case code:load_file(P) of
  405. {module, P} -> ok;
  406. _ ->
  407. case lists:keyfind(plugin_dir, 1, Conf) of
  408. false -> ok;
  409. {_, PluginsDir} ->
  410. ErlFile = "$(DEPS_DIR)/$(1)/" ++ PluginsDir ++ "/" ++ atom_to_list(P) ++ ".erl",
  411. {ok, P, Bin} = compile:file(ErlFile, [binary]),
  412. {module, P} = code:load_binary(P, ErlFile, Bin)
  413. end
  414. end || P <- Plugins],
  415. [RunPlugin(P, preprocess) || P <- Plugins],
  416. [RunPlugin(P, pre_compile) || P <- Plugins],
  417. [RunPlugin(P, compile) || P <- Plugins]
  418. end
  419. end(),
  420. halt()
  421. endef
  422. define dep_autopatch_app.erl
  423. UpdateModules = fun(App) ->
  424. case filelib:is_regular(App) of
  425. false -> ok;
  426. true ->
  427. {ok, [{application, $(1), L0}]} = file:consult(App),
  428. Mods = filelib:fold_files("$(DEPS_DIR)/$(1)/src", "\\\\.erl$$$$", true,
  429. fun (F, Acc) -> [list_to_atom(filename:rootname(filename:basename(F)))|Acc] end, []),
  430. L = lists:keystore(modules, 1, L0, {modules, Mods}),
  431. ok = file:write_file(App, io_lib:format("~p.~n", [{application, $(1), L}]))
  432. end
  433. end,
  434. UpdateModules("$(DEPS_DIR)/$(1)/ebin/$(1).app"),
  435. halt()
  436. endef
  437. define dep_autopatch_appsrc.erl
  438. AppSrcOut = "$(DEPS_DIR)/$(1)/src/$(1).app.src",
  439. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(DEPS_DIR)/$(1)/ebin/$(1).app"; true -> AppSrcOut end,
  440. case filelib:is_regular(AppSrcIn) of
  441. false -> ok;
  442. true ->
  443. {ok, [{application, $(1), L0}]} = file:consult(AppSrcIn),
  444. L1 = lists:keystore(modules, 1, L0, {modules, []}),
  445. L2 = case lists:keyfind(vsn, 1, L1) of {_, git} -> lists:keyreplace(vsn, 1, L1, {vsn, "git"}); _ -> L1 end,
  446. L3 = case lists:keyfind(registered, 1, L2) of false -> [{registered, []}|L2]; _ -> L2 end,
  447. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}])),
  448. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  449. end,
  450. halt()
  451. endef
  452. define dep_fetch_git
  453. git clone -q -n -- $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
  454. cd $(DEPS_DIR)/$(call dep_name,$(1)) && git checkout -q $(call dep_commit,$(1));
  455. endef
  456. define dep_fetch_hg
  457. hg clone -q -U $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
  458. cd $(DEPS_DIR)/$(call dep_name,$(1)) && hg update -q $(call dep_commit,$(1));
  459. endef
  460. define dep_fetch_svn
  461. svn checkout -q $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
  462. endef
  463. define dep_fetch_cp
  464. cp -R $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
  465. endef
  466. define dep_fetch_hex.erl
  467. ssl:start(),
  468. inets:start(),
  469. {ok, {{_, 200, _}, _, Body}} = httpc:request(get,
  470. {"https://s3.amazonaws.com/s3.hex.pm/tarballs/$(1)-$(2).tar", []},
  471. [], [{body_format, binary}]),
  472. {ok, Files} = erl_tar:extract({binary, Body}, [memory]),
  473. {_, Source} = lists:keyfind("contents.tar.gz", 1, Files),
  474. ok = erl_tar:extract({binary, Source}, [{cwd, "$(DEPS_DIR)/$(1)"}, compressed]),
  475. halt()
  476. endef
  477. # Hex only has a package version. No need to look in the Erlang.mk packages.
  478. define dep_fetch_hex
  479. $(call erlang,$(call dep_fetch_hex.erl,$(1),$(strip $(word 2,$(dep_$(1))))));
  480. endef
  481. define dep_fetch_fail
  482. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  483. exit 78;
  484. endef
  485. # Kept for compatibility purposes with older Erlang.mk configuration.
  486. define dep_fetch_legacy
  487. $(warning WARNING: '$(1)' dependency configuration uses deprecated format.) \
  488. git clone -q -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1); \
  489. cd $(DEPS_DIR)/$(1) && git checkout -q $(if $(word 2,$(dep_$(1))),$(word 2,$(dep_$(1))),master);
  490. endef
  491. define dep_fetch
  492. $(if $(dep_$(1)), \
  493. $(if $(dep_fetch_$(word 1,$(dep_$(1)))), \
  494. $(word 1,$(dep_$(1))), \
  495. legacy), \
  496. $(if $(filter $(1),$(PACKAGES)), \
  497. $(pkg_$(1)_fetch), \
  498. fail))
  499. endef
  500. dep_name = $(if $(dep_$(1)),$(1),$(pkg_$(1)_name))
  501. dep_repo = $(patsubst git://github.com/%,https://github.com/%, \
  502. $(if $(dep_$(1)),$(word 2,$(dep_$(1))),$(pkg_$(1)_repo)))
  503. dep_commit = $(if $(dep_$(1)),$(word 3,$(dep_$(1))),$(pkg_$(1)_commit))
  504. define dep_target
  505. $(DEPS_DIR)/$(1):
  506. $(verbose) mkdir -p $(DEPS_DIR)
  507. $(dep_verbose) $(call dep_fetch_$(strip $(call dep_fetch,$(1))),$(1))
  508. $(verbose) if [ -f $(DEPS_DIR)/$(1)/configure.ac -o -f $(DEPS_DIR)/$(1)/configure.in ]; then \
  509. echo " AUTO " $(1); \
  510. cd $(DEPS_DIR)/$(1) && autoreconf -Wall -vif -I m4; \
  511. fi
  512. - $(verbose) if [ -f $(DEPS_DIR)/$(1)/configure ]; then \
  513. echo " CONF " $(1); \
  514. cd $(DEPS_DIR)/$(1) && ./configure; \
  515. fi
  516. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  517. $(verbose) if [ "$(1)" = "amqp_client" -a "$(RABBITMQ_CLIENT_PATCH)" ]; then \
  518. if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
  519. echo " PATCH Downloading rabbitmq-codegen"; \
  520. git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
  521. fi; \
  522. if [ ! -d $(DEPS_DIR)/rabbitmq-server ]; then \
  523. echo " PATCH Downloading rabbitmq-server"; \
  524. git clone https://github.com/rabbitmq/rabbitmq-server.git $(DEPS_DIR)/rabbitmq-server; \
  525. fi; \
  526. ln -s $(DEPS_DIR)/amqp_client/deps/rabbit_common-0.0.0 $(DEPS_DIR)/rabbit_common; \
  527. elif [ "$(1)" = "rabbit" -a "$(RABBITMQ_SERVER_PATCH)" ]; then \
  528. if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
  529. echo " PATCH Downloading rabbitmq-codegen"; \
  530. git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
  531. fi \
  532. else \
  533. $(call dep_autopatch,$(1)) \
  534. fi
  535. endif
  536. endef
  537. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  538. distclean-deps:
  539. $(gen_verbose) rm -rf $(DEPS_DIR)
  540. # External plugins.
  541. DEP_PLUGINS ?=
  542. define core_dep_plugin
  543. -include $(DEPS_DIR)/$(1)
  544. $(DEPS_DIR)/$(1): $(DEPS_DIR)/$(2) ;
  545. endef
  546. $(foreach p,$(DEP_PLUGINS),\
  547. $(eval $(if $(findstring /,$p),\
  548. $(call core_dep_plugin,$p,$(firstword $(subst /, ,$p))),\
  549. $(call core_dep_plugin,$p/plugins.mk,$p))))