deps.mk 18 KB

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