deps.mk 18 KB

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