deps.mk 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. @rm -f $(ERLANG_MK_TMP)/deps.log
  29. endif
  30. @mkdir -p $(ERLANG_MK_TMP)
  31. @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 | xargs -r 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_tuple(S) -> {N, S};
  175. {N, _, S} -> {N, S};
  176. {N, _, S, _} -> {N, S};
  177. _ -> false
  178. end of
  179. false -> ok;
  180. {Name, Source} ->
  181. {Method, Repo, Commit} = case Source of
  182. {git, R} -> {git, R, master};
  183. {M, R, {branch, C}} -> {M, R, C};
  184. {M, R, {tag, C}} -> {M, R, C};
  185. {M, R, C} -> {M, R, C}
  186. end,
  187. Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
  188. end end || Dep <- Deps]
  189. end
  190. end(),
  191. fun() ->
  192. case lists:keyfind(erl_first_files, 1, Conf) of
  193. false -> ok;
  194. {_, Files} ->
  195. Names = [[" ", case lists:reverse(F) of
  196. "lre." ++ Elif -> lists:reverse(Elif);
  197. Elif -> lists:reverse(Elif)
  198. end] || "src/" ++ F <- Files],
  199. Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
  200. end
  201. end(),
  202. FindFirst = fun(F, Fd) ->
  203. case io:parse_erl_form(Fd, undefined) of
  204. {ok, {attribute, _, compile, {parse_transform, PT}}, _} ->
  205. [PT, F(F, Fd)];
  206. {ok, {attribute, _, compile, CompileOpts}, _} when is_list(CompileOpts) ->
  207. case proplists:get_value(parse_transform, CompileOpts) of
  208. undefined -> [F(F, Fd)];
  209. PT -> [PT, F(F, Fd)]
  210. end;
  211. {ok, {attribute, _, include, Hrl}, _} ->
  212. case file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]) of
  213. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  214. _ ->
  215. case file:open("$(DEPS_DIR)/$(1)/src/" ++ Hrl, [read]) of
  216. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  217. _ -> [F(F, Fd)]
  218. end
  219. end;
  220. {ok, {attribute, _, include_lib, "$(1)/include/" ++ Hrl}, _} ->
  221. {ok, HrlFd} = file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]),
  222. [F(F, HrlFd), F(F, Fd)];
  223. {ok, {attribute, _, include_lib, Hrl}, _} ->
  224. case file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]) of
  225. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  226. _ -> [F(F, Fd)]
  227. end;
  228. {ok, {attribute, _, import, {Imp, _}}, _} ->
  229. case file:open("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(Imp) ++ ".erl", [read]) of
  230. {ok, ImpFd} -> [Imp, F(F, ImpFd), F(F, Fd)];
  231. _ -> [F(F, Fd)]
  232. end;
  233. {eof, _} ->
  234. file:close(Fd),
  235. [];
  236. _ ->
  237. F(F, Fd)
  238. end
  239. end,
  240. fun() ->
  241. ErlFiles = filelib:wildcard("$(DEPS_DIR)/$(1)/src/*.erl"),
  242. First0 = lists:usort(lists:flatten([begin
  243. {ok, Fd} = file:open(F, [read]),
  244. FindFirst(FindFirst, Fd)
  245. end || F <- ErlFiles])),
  246. First = lists:flatten([begin
  247. {ok, Fd} = file:open("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", [read]),
  248. FindFirst(FindFirst, Fd)
  249. end || M <- First0, lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)]) ++ First0,
  250. Write(["COMPILE_FIRST +=", [[" ", atom_to_list(M)] || M <- First,
  251. lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)], "\n"])
  252. end(),
  253. Write("\n\nrebar_dep: preprocess pre-deps deps pre-app app\n"),
  254. Write("\npreprocess::\n"),
  255. Write("\npre-deps::\n"),
  256. Write("\npre-app::\n"),
  257. PatchHook = fun(Cmd) ->
  258. case Cmd of
  259. "make -C" ++ Cmd1 -> "$$$$\(MAKE) -C" ++ Escape(Cmd1);
  260. "gmake -C" ++ Cmd1 -> "$$$$\(MAKE) -C" ++ Escape(Cmd1);
  261. "make " ++ Cmd1 -> "$$$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
  262. "gmake " ++ Cmd1 -> "$$$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
  263. _ -> Escape(Cmd)
  264. end
  265. end,
  266. fun() ->
  267. case lists:keyfind(pre_hooks, 1, Conf) of
  268. false -> ok;
  269. {_, Hooks} ->
  270. [case H of
  271. {'get-deps', Cmd} ->
  272. Write("\npre-deps::\n\t" ++ PatchHook(Cmd) ++ "\n");
  273. {compile, Cmd} ->
  274. Write("\npre-app::\n\tCC=$$$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
  275. {Regex, compile, Cmd} ->
  276. case rebar_utils:is_arch(Regex) of
  277. true -> Write("\npre-app::\n\tCC=$$$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
  278. false -> ok
  279. end;
  280. _ -> ok
  281. end || H <- Hooks]
  282. end
  283. end(),
  284. ShellToMk = fun(V) ->
  285. re:replace(re:replace(V, "(\\\\$$$$)(\\\\w*)", "\\\\1(\\\\2)", [global]),
  286. "-Werror\\\\b", "", [{return, list}, global])
  287. end,
  288. PortSpecs = fun() ->
  289. case lists:keyfind(port_specs, 1, Conf) of
  290. false ->
  291. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  292. false -> [];
  293. true ->
  294. [{"priv/" ++ proplists:get_value(so_name, Conf, "$(1)_drv.so"),
  295. proplists:get_value(port_sources, Conf, ["c_src/*.c"]), []}]
  296. end;
  297. {_, Specs} ->
  298. lists:flatten([case S of
  299. {Output, Input} -> {ShellToMk(Output), Input, []};
  300. {Regex, Output, Input} ->
  301. case rebar_utils:is_arch(Regex) of
  302. true -> {ShellToMk(Output), Input, []};
  303. false -> []
  304. end;
  305. {Regex, Output, Input, [{env, Env}]} ->
  306. case rebar_utils:is_arch(Regex) of
  307. true -> {ShellToMk(Output), Input, Env};
  308. false -> []
  309. end
  310. end || S <- Specs])
  311. end
  312. end(),
  313. PortSpecWrite = fun (Text) ->
  314. file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile.erlang.mk", Text, [append])
  315. end,
  316. case PortSpecs of
  317. [] -> ok;
  318. _ ->
  319. Write("\npre-app::\n\t$$$$\(MAKE) -f c_src/Makefile.erlang.mk\n"),
  320. PortSpecWrite(io_lib:format("ERL_CFLAGS = -finline-functions -Wall -fPIC -I ~s/erts-~s/include -I ~s\n",
  321. [code:root_dir(), erlang:system_info(version), code:lib_dir(erl_interface, include)])),
  322. PortSpecWrite(io_lib:format("ERL_LDFLAGS = -L ~s -lerl_interface -lei\n",
  323. [code:lib_dir(erl_interface, lib)])),
  324. [PortSpecWrite(["\n", E, "\n"]) || E <- OsEnv],
  325. FilterEnv = fun(Env) ->
  326. lists:flatten([case E of
  327. {_, _} -> E;
  328. {Regex, K, V} ->
  329. case rebar_utils:is_arch(Regex) of
  330. true -> {K, V};
  331. false -> []
  332. end
  333. end || E <- Env])
  334. end,
  335. MergeEnv = fun(Env) ->
  336. lists:foldl(fun ({K, V}, Acc) ->
  337. case lists:keyfind(K, 1, Acc) of
  338. false -> [{K, rebar_utils:expand_env_variable(V, K, "")}|Acc];
  339. {_, V0} -> [{K, rebar_utils:expand_env_variable(V, K, V0)}|Acc]
  340. end
  341. end, [], Env)
  342. end,
  343. PortEnv = case lists:keyfind(port_env, 1, Conf) of
  344. false -> [];
  345. {_, PortEnv0} -> FilterEnv(PortEnv0)
  346. end,
  347. PortSpec = fun ({Output, Input0, Env}) ->
  348. filelib:ensure_dir("$(DEPS_DIR)/$(1)/" ++ Output),
  349. Input = [[" ", I] || I <- Input0],
  350. PortSpecWrite([
  351. [["\n", K, " = ", ShellToMk(V)] || {K, V} <- lists:reverse(MergeEnv(PortEnv))],
  352. case $(PLATFORM) of
  353. darwin -> "\n\nLDFLAGS += -flat_namespace -undefined suppress";
  354. _ -> ""
  355. end,
  356. "\n\nall:: ", Output, "\n\n",
  357. "%.o: %.c\n\t$$$$\(CC) -c -o $$$$\@ $$$$\< $$$$\(CFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  358. "%.o: %.C\n\t$$$$\(CXX) -c -o $$$$\@ $$$$\< $$$$\(CXXFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  359. "%.o: %.cc\n\t$$$$\(CXX) -c -o $$$$\@ $$$$\< $$$$\(CXXFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  360. "%.o: %.cpp\n\t$$$$\(CXX) -c -o $$$$\@ $$$$\< $$$$\(CXXFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  361. [[Output, ": ", K, " = ", ShellToMk(V), "\n"] || {K, V} <- lists:reverse(MergeEnv(FilterEnv(Env)))],
  362. Output, ": $$$$\(foreach ext,.c .C .cc .cpp,",
  363. "$$$$\(patsubst %$$$$\(ext),%.o,$$$$\(filter %$$$$\(ext),$$$$\(wildcard", Input, "))))\n",
  364. "\t$$$$\(CC) -o $$$$\@ $$$$\? $$$$\(LDFLAGS) $$$$\(ERL_LDFLAGS) $$$$\(DRV_LDFLAGS) $$$$\(EXE_LDFLAGS)",
  365. case filename:extension(Output) of
  366. [] -> "\n";
  367. _ -> " -shared\n"
  368. end])
  369. end,
  370. [PortSpec(S) || S <- PortSpecs]
  371. end,
  372. Write("\ninclude $(ERLANG_MK_FILENAME)"),
  373. RunPlugin = fun(Plugin, Step) ->
  374. case erlang:function_exported(Plugin, Step, 2) of
  375. false -> ok;
  376. true ->
  377. c:cd("$(DEPS_DIR)/$(1)/"),
  378. Ret = Plugin:Step({config, "", Conf, dict:new(), dict:new(), dict:new(),
  379. dict:store(base_dir, "", dict:new())}, undefined),
  380. io:format("rebar plugin ~p step ~p ret ~p~n", [Plugin, Step, Ret])
  381. end
  382. end,
  383. fun() ->
  384. case lists:keyfind(plugins, 1, Conf) of
  385. false -> ok;
  386. {_, Plugins} ->
  387. [begin
  388. case lists:keyfind(deps, 1, Conf) of
  389. false -> ok;
  390. {_, Deps} ->
  391. case lists:keyfind(P, 1, Deps) of
  392. false -> ok;
  393. _ ->
  394. Path = "$(DEPS_DIR)/" ++ atom_to_list(P),
  395. io:format("~s", [os:cmd("$(MAKE) -C $(DEPS_DIR)/$(1) " ++ Path)]),
  396. io:format("~s", [os:cmd("$(MAKE) -C " ++ Path ++ " IS_DEP=1")]),
  397. code:add_patha(Path ++ "/ebin")
  398. end
  399. end
  400. end || P <- Plugins],
  401. [case code:load_file(P) of
  402. {module, P} -> ok;
  403. _ ->
  404. case lists:keyfind(plugin_dir, 1, Conf) of
  405. false -> ok;
  406. {_, PluginsDir} ->
  407. ErlFile = "$(DEPS_DIR)/$(1)/" ++ PluginsDir ++ "/" ++ atom_to_list(P) ++ ".erl",
  408. {ok, P, Bin} = compile:file(ErlFile, [binary]),
  409. {module, P} = code:load_binary(P, ErlFile, Bin)
  410. end
  411. end || P <- Plugins],
  412. [RunPlugin(P, preprocess) || P <- Plugins],
  413. [RunPlugin(P, pre_compile) || P <- Plugins]
  414. end
  415. end(),
  416. halt()
  417. endef
  418. define dep_autopatch_app.erl
  419. UpdateModules = fun(App) ->
  420. case filelib:is_regular(App) of
  421. false -> ok;
  422. true ->
  423. {ok, [{application, $(1), L0}]} = file:consult(App),
  424. Mods = filelib:fold_files("$(DEPS_DIR)/$(1)/src", "\\\\.erl$$$$", true,
  425. fun (F, Acc) -> [list_to_atom(filename:rootname(filename:basename(F)))|Acc] end, []),
  426. L = lists:keystore(modules, 1, L0, {modules, Mods}),
  427. ok = file:write_file(App, io_lib:format("~p.~n", [{application, $(1), L}]))
  428. end
  429. end,
  430. UpdateModules("$(DEPS_DIR)/$(1)/ebin/$(1).app"),
  431. halt()
  432. endef
  433. define dep_autopatch_appsrc.erl
  434. AppSrcOut = "$(DEPS_DIR)/$(1)/src/$(1).app.src",
  435. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(DEPS_DIR)/$(1)/ebin/$(1).app"; true -> AppSrcOut end,
  436. case filelib:is_regular(AppSrcIn) of
  437. false -> ok;
  438. true ->
  439. {ok, [{application, $(1), L0}]} = file:consult(AppSrcIn),
  440. L1 = lists:keystore(modules, 1, L0, {modules, []}),
  441. L2 = case lists:keyfind(vsn, 1, L1) of {_, git} -> lists:keyreplace(vsn, 1, L1, {vsn, "git"}); _ -> L1 end,
  442. L3 = case lists:keyfind(registered, 1, L2) of false -> [{registered, []}|L2]; _ -> L2 end,
  443. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}])),
  444. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  445. end,
  446. halt()
  447. endef
  448. define dep_fetch
  449. if [ "$(2)" = "git" ]; then \
  450. git clone -q -n -- $(3) $(DEPS_DIR)/$(1); \
  451. cd $(DEPS_DIR)/$(1) && git checkout -q $(4); \
  452. elif [ "$(2)" = "hg" ]; then \
  453. hg clone -q -U $(3) $(DEPS_DIR)/$(1); \
  454. cd $(DEPS_DIR)/$(1) && hg update -q $(4); \
  455. elif [ "$(2)" = "svn" ]; then \
  456. svn checkout -q $(3) $(DEPS_DIR)/$(1); \
  457. elif [ "$(2)" = "cp" ]; then \
  458. cp -R $(3) $(DEPS_DIR)/$(1); \
  459. else \
  460. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  461. exit 78; \
  462. fi
  463. endef
  464. define dep_target
  465. $(DEPS_DIR)/$(1):
  466. @mkdir -p $(DEPS_DIR)
  467. ifeq (,$(dep_$(1)))
  468. $(dep_verbose) $(call dep_fetch,$(pkg_$(1)_name),$(pkg_$(1)_fetch), \
  469. $(patsubst git://github.com/%,https://github.com/%,$(pkg_$(1)_repo)), \
  470. $(pkg_$(1)_commit))
  471. else
  472. ifeq (1,$(words $(dep_$(1))))
  473. $(dep_verbose) $(call dep_fetch,$(1),git, \
  474. $(patsubst git://github.com/%,https://github.com/%,$(dep_$(1))), \
  475. master)
  476. else
  477. ifeq (2,$(words $(dep_$(1))))
  478. $(dep_verbose) $(call dep_fetch,$(1),git, \
  479. $(patsubst git://github.com/%,https://github.com/%,$(word 1,$(dep_$(1)))), \
  480. $(word 2,$(dep_$(1))))
  481. else
  482. $(dep_verbose) $(call dep_fetch,$(1),$(word 1,$(dep_$(1))), \
  483. $(patsubst git://github.com/%,https://github.com/%,$(word 2,$(dep_$(1)))), \
  484. $(word 3,$(dep_$(1))))
  485. endif
  486. endif
  487. endif
  488. @if [ -f $(DEPS_DIR)/$(1)/configure.ac -o -f $(DEPS_DIR)/$(1)/configure.in ]; then \
  489. echo " AUTO " $(1); \
  490. cd $(DEPS_DIR)/$(1) && autoreconf -Wall -vif -I m4; \
  491. fi
  492. -@if [ -f $(DEPS_DIR)/$(1)/configure ]; then \
  493. echo " CONF " $(1); \
  494. cd $(DEPS_DIR)/$(1) && ./configure; \
  495. fi
  496. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  497. @if [ "$(1)" = "amqp_client" -a "$(RABBITMQ_CLIENT_PATCH)" ]; then \
  498. if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
  499. echo " PATCH Downloading rabbitmq-codegen"; \
  500. git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
  501. fi; \
  502. if [ ! -d $(DEPS_DIR)/rabbitmq-server ]; then \
  503. echo " PATCH Downloading rabbitmq-server"; \
  504. git clone https://github.com/rabbitmq/rabbitmq-server.git $(DEPS_DIR)/rabbitmq-server; \
  505. fi; \
  506. ln -s $(DEPS_DIR)/amqp_client/deps/rabbit_common-0.0.0 $(DEPS_DIR)/rabbit_common; \
  507. elif [ "$(1)" = "rabbit" -a "$(RABBITMQ_SERVER_PATCH)" ]; then \
  508. if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
  509. echo " PATCH Downloading rabbitmq-codegen"; \
  510. git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
  511. fi \
  512. else \
  513. $(call dep_autopatch,$(1)) \
  514. fi
  515. endif
  516. endef
  517. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  518. distclean-deps:
  519. $(gen_verbose) rm -rf $(DEPS_DIR)