deps.mk 19 KB

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