deps.mk 17 KB

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