deps.mk 19 KB

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