deps.mk 19 KB

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