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_rebar_utils); \
  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_rebar_utils
  97. mkdir -p $(ERLANG_MK_TMP)/ebin; \
  98. if [ ! -f $(ERLANG_MK_TMP)/rebar.hrl ]; then \
  99. $(call core_http_get,$(ERLANG_MK_TMP)/rebar.hrl,https://raw.githubusercontent.com/rebar/rebar/791db716b5a3a7671e0b351f95ddf24b848ee173/include/rebar.hrl); \
  100. fi; \
  101. if [ ! -f $(ERLANG_MK_TMP)/rebar_utils.erl ]; then \
  102. $(call core_http_get,$(ERLANG_MK_TMP)/rebar_utils.erl,https://raw.githubusercontent.com/rebar/rebar/791db716b5a3a7671e0b351f95ddf24b848ee173/src/rebar_utils.erl); \
  103. fi; \
  104. if [ ! -f $(ERLANG_MK_TMP)/ebin/rebar_utils.beam ]; then \
  105. erlc -o $(ERLANG_MK_TMP)/ebin $(ERLANG_MK_TMP)/rebar_utils.erl; \
  106. fi; \
  107. if [ ! -f $(ERLANG_MK_TMP)/rebar_log.erl ]; then \
  108. $(call core_http_get,$(ERLANG_MK_TMP)/rebar_log.erl,https://raw.githubusercontent.com/rebar/rebar/791db716b5a3a7671e0b351f95ddf24b848ee173/src/rebar_log.erl); \
  109. fi; \
  110. if [ ! -f $(ERLANG_MK_TMP)/ebin/rebar_log.beam ]; then \
  111. erlc -o $(ERLANG_MK_TMP)/ebin $(ERLANG_MK_TMP)/rebar_log.erl; \
  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. endef
  120. define dep_autopatch_rebar.erl
  121. application:set_env(rebar, log_level, debug),
  122. Conf1 = case file:consult("$(DEPS_DIR)/$(1)/rebar.config") of
  123. {ok, Conf0} -> Conf0;
  124. _ -> []
  125. end,
  126. {Conf, OsEnv} = fun() ->
  127. case filelib:is_file("$(DEPS_DIR)/$(1)/rebar.config.script") of
  128. false -> {Conf1, []};
  129. true ->
  130. Bindings0 = erl_eval:new_bindings(),
  131. Bindings1 = erl_eval:add_binding('CONFIG', Conf1, Bindings0),
  132. Bindings = erl_eval:add_binding('SCRIPT', "$(DEPS_DIR)/$(1)/rebar.config.script", Bindings1),
  133. Before = os:getenv(),
  134. {ok, Conf2} = file:script("$(DEPS_DIR)/$(1)/rebar.config.script", Bindings),
  135. {Conf2, lists:foldl(fun(E, Acc) -> lists:delete(E, Acc) end, os:getenv(), Before)}
  136. end
  137. end(),
  138. Write = fun (Text) ->
  139. file:write_file("$(DEPS_DIR)/$(1)/Makefile", Text, [append])
  140. end,
  141. Escape = fun (Text) ->
  142. re:replace(Text, "\\\\$$$$", "\$$$$$$$$", [global, {return, list}])
  143. end,
  144. Write("IGNORE_DEPS = edown eper eunit_formatters meck node_package "
  145. "rebar_lock_deps_plugin rebar_vsn_plugin reltool_util\n"),
  146. Write("C_SRC_DIR = /path/do/not/exist\n"),
  147. Write("DRV_CFLAGS = -fPIC\nexport DRV_CFLAGS\n"),
  148. Write(["ERLANG_ARCH = ", rebar_utils:wordsize(), "\nexport ERLANG_ARCH\n"]),
  149. fun() ->
  150. Write("ERLC_OPTS = +debug_info\n"),
  151. case lists:keyfind(erl_opts, 1, Conf) of
  152. false -> ok;
  153. {_, ErlOpts} ->
  154. lists:foreach(fun
  155. ({d, D}) ->
  156. Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
  157. ({i, I}) ->
  158. Write(["ERLC_OPTS += -I ", I, "\n"]);
  159. ({platform_define, Regex, D}) ->
  160. case rebar_utils:is_arch(Regex) of
  161. true -> Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
  162. false -> ok
  163. end;
  164. ({parse_transform, PT}) ->
  165. Write("ERLC_OPTS += +'{parse_transform, " ++ atom_to_list(PT) ++ "}'\n");
  166. (_) -> ok
  167. end, ErlOpts)
  168. end,
  169. Write("\n")
  170. end(),
  171. fun() ->
  172. File = case lists:keyfind(deps, 1, Conf) of
  173. false -> [];
  174. {_, Deps} ->
  175. [begin case case Dep of
  176. {N, S} when is_tuple(S) -> {N, S};
  177. {N, _, S} -> {N, S};
  178. {N, _, S, _} -> {N, S};
  179. _ -> false
  180. end of
  181. false -> ok;
  182. {Name, Source} ->
  183. {Method, Repo, Commit} = case Source of
  184. {git, R} -> {git, R, master};
  185. {M, R, {branch, C}} -> {M, R, C};
  186. {M, R, {tag, C}} -> {M, R, C};
  187. {M, R, C} -> {M, R, C}
  188. end,
  189. Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
  190. end end || Dep <- Deps]
  191. end
  192. end(),
  193. fun() ->
  194. First = case lists:keyfind(erl_first_files, 1, Conf) of false -> []; {_, Files} ->
  195. Names = [[" ", begin "lre." ++ Elif = lists:reverse(F), lists:reverse(Elif) end]
  196. || "src/" ++ F <- Files],
  197. Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
  198. end
  199. end(),
  200. FindFirst = fun(F, Fd) ->
  201. case io:parse_erl_form(Fd, undefined) of
  202. {ok, {attribute, _,compile, {parse_transform, PT}}, _} ->
  203. [PT, F(F, Fd)];
  204. {ok, {attribute, _, include, Hrl}, _} ->
  205. case file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]) of
  206. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  207. _ ->
  208. case file:open("$(DEPS_DIR)/$(1)/src/" ++ Hrl, [read]) of
  209. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  210. _ -> [F(F, Fd)]
  211. end
  212. end;
  213. {ok, {attribute, _, include_lib, "$(1)/include/" ++ Hrl}, _} ->
  214. {ok, HrlFd} = file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]),
  215. [F(F, HrlFd), F(F, Fd)];
  216. {ok, {attribute, _, import, {Imp, _}}, _} ->
  217. case file:open("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(Imp) ++ ".erl", [read]) of
  218. {ok, ImpFd} -> [Imp, F(F, ImpFd), F(F, Fd)];
  219. _ -> [F(F, Fd)]
  220. end;
  221. {eof, _} ->
  222. file:close(Fd),
  223. [];
  224. _ ->
  225. F(F, Fd)
  226. end
  227. end,
  228. fun() ->
  229. ErlFiles = filelib:wildcard("$(DEPS_DIR)/$(1)/src/*.erl"),
  230. First0 = lists:usort(lists:flatten([begin
  231. {ok, Fd} = file:open(F, [read]),
  232. FindFirst(FindFirst, Fd)
  233. end || F <- ErlFiles])),
  234. First = lists:flatten([begin
  235. {ok, Fd} = file:open("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", [read]),
  236. FindFirst(FindFirst, Fd)
  237. end || M <- First0, lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)]) ++ First0,
  238. Write(["COMPILE_FIRST +=", [[" ", atom_to_list(M)] || M <- First,
  239. lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)], "\n"])
  240. end(),
  241. Write("\n\nrebar_dep: preprocess pre-deps deps pre-app app\n"),
  242. Write("\npreprocess::\n"),
  243. Write("\npre-deps::\n"),
  244. Write("\npre-app::\n"),
  245. PatchHook = fun(Cmd) ->
  246. case Cmd of
  247. "make -C" ++ _ -> Escape(Cmd);
  248. "gmake -C" ++ _ -> Escape(Cmd);
  249. "make " ++ Cmd1 -> "make -f Makefile.orig.mk " ++ Escape(Cmd1);
  250. "gmake " ++ Cmd1 -> "gmake -f Makefile.orig.mk " ++ Escape(Cmd1);
  251. _ -> Escape(Cmd)
  252. end
  253. end,
  254. fun() ->
  255. case lists:keyfind(pre_hooks, 1, Conf) of
  256. false -> ok;
  257. {_, Hooks} ->
  258. [case H of
  259. {'get-deps', Cmd} ->
  260. Write("\npre-deps::\n\t" ++ PatchHook(Cmd) ++ "\n");
  261. {compile, Cmd} ->
  262. Write("\npre-app::\n\t" ++ PatchHook(Cmd) ++ "\n");
  263. {Regex, compile, Cmd} ->
  264. case rebar_utils:is_arch(Regex) of
  265. true -> Write("\npre-app::\n\t" ++ PatchHook(Cmd) ++ "\n");
  266. false -> ok
  267. end;
  268. _ -> ok
  269. end || H <- Hooks]
  270. end
  271. end(),
  272. ShellToMk = fun(V) ->
  273. re:replace(re:replace(V, "(\\\\$$$$)(\\\\w*)", "\\\\1(\\\\2)", [global]),
  274. "-Werror\\\\b", "", [{return, list}, global])
  275. end,
  276. PortSpecs = fun() ->
  277. case lists:keyfind(port_specs, 1, Conf) of
  278. false ->
  279. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  280. false -> [];
  281. true ->
  282. [{"priv/" ++ proplists:get_value(so_name, Conf, "$(1)_drv.so"),
  283. proplists:get_value(port_sources, Conf, ["c_src/*.c"]), []}]
  284. end;
  285. {_, Specs} ->
  286. lists:flatten([case S of
  287. {Output, Input} -> {ShellToMk(Output), Input, []};
  288. {Regex, Output, Input} ->
  289. case rebar_utils:is_arch(Regex) of
  290. true -> {ShellToMk(Output), Input, []};
  291. false -> []
  292. end;
  293. {Regex, Output, Input, [{env, Env}]} ->
  294. case rebar_utils:is_arch(Regex) of
  295. true -> {ShellToMk(Output), Input, Env};
  296. false -> []
  297. end
  298. end || S <- Specs])
  299. end
  300. end(),
  301. PortSpecWrite = fun (Text) ->
  302. file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile.erlang.mk", Text, [append])
  303. end,
  304. case PortSpecs of
  305. [] -> ok;
  306. _ ->
  307. Write("\npre-app::\n\t$$$$\(MAKE) -f c_src/Makefile.erlang.mk\n"),
  308. PortSpecWrite(io_lib:format("ERL_CFLAGS = -finline-functions -Wall -fPIC -I ~s/erts-~s/include -I ~s\n",
  309. [code:root_dir(), erlang:system_info(version), code:lib_dir(erl_interface, include)])),
  310. PortSpecWrite(io_lib:format("ERL_LDFLAGS = -L ~s -lerl_interface -lei\n",
  311. [code:lib_dir(erl_interface, lib)])),
  312. [PortSpecWrite(["\n", E, "\n"]) || E <- OsEnv],
  313. FilterEnv = fun(Env) ->
  314. lists:flatten([case E of
  315. {_, _} -> E;
  316. {Regex, K, V} ->
  317. case rebar_utils:is_arch(Regex) of
  318. true -> {K, V};
  319. false -> []
  320. end
  321. end || E <- Env])
  322. end,
  323. MergeEnv = fun(Env) ->
  324. lists:foldl(fun ({K, V}, Acc) ->
  325. case lists:keyfind(K, 1, Acc) of
  326. false -> [{K, rebar_utils:expand_env_variable(V, K, "")}|Acc];
  327. {_, V0} -> [{K, rebar_utils:expand_env_variable(V, K, V0)}|Acc]
  328. end
  329. end, [], Env)
  330. end,
  331. PortEnv = case lists:keyfind(port_env, 1, Conf) of
  332. false -> [];
  333. {_, PortEnv0} -> FilterEnv(PortEnv0)
  334. end,
  335. PortSpec = fun ({Output, Input0, Env}) ->
  336. filelib:ensure_dir("$(DEPS_DIR)/$(1)/" ++ Output),
  337. Input = [[" ", I] || I <- Input0],
  338. PortSpecWrite([
  339. [["\n", K, " = ", ShellToMk(V)] || {K, V} <- lists:reverse(MergeEnv(PortEnv))],
  340. "\n\nall:: ", Output, "\n\n",
  341. "%.o: %.c\n\t$$$$\(CC) -c -o $$$$\@ $$$$\< $$$$\(CFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  342. "%.o: %.C\n\t$$$$\(CXX) -c -o $$$$\@ $$$$\< $$$$\(CXXFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  343. "%.o: %.cc\n\t$$$$\(CXX) -c -o $$$$\@ $$$$\< $$$$\(CXXFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  344. "%.o: %.cpp\n\t$$$$\(CXX) -c -o $$$$\@ $$$$\< $$$$\(CXXFLAGS) $$$$\(ERL_CFLAGS) $$$$\(DRV_CFLAGS) $$$$\(EXE_CFLAGS)\n\n",
  345. [[Output, ": ", K, " = ", ShellToMk(V), "\n"] || {K, V} <- lists:reverse(MergeEnv(FilterEnv(Env)))],
  346. Output, ": $$$$\(foreach ext,.c .C .cc .cpp,",
  347. "$$$$\(patsubst %$$$$\(ext),%.o,$$$$\(filter %$$$$\(ext),$$$$\(wildcard", Input, "))))\n",
  348. "\t$$$$\(CC) -o $$$$\@ $$$$\? $$$$\(LDFLAGS) $$$$\(ERL_LDFLAGS) $$$$\(DRV_LDFLAGS) $$$$\(EXE_LDFLAGS)",
  349. case filename:extension(Output) of
  350. [] -> "\n";
  351. _ -> " -shared\n"
  352. end])
  353. end,
  354. [PortSpec(S) || S <- PortSpecs]
  355. end,
  356. Write("\ninclude ../../erlang.mk"),
  357. PatchPlugin = fun(ErlFile) ->
  358. {ok, F0} = file:read_file(ErlFile),
  359. case re:replace(F0, "rebar_config:", "rebar_config_", [global]) of
  360. F0 -> ok;
  361. F ->
  362. ok = file:write_file(ErlFile, [F,
  363. "\nrebar_config_get(_, current_command, _) -> compile.\n"
  364. ])
  365. end
  366. end,
  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(Conf, undefined),
  373. io:format("rebar plugin ~p step ~p ret ~p~n", [Plugin, Step, Ret])
  374. end
  375. end,
  376. fun() ->
  377. case lists:keyfind(plugins, 1, Conf) of
  378. false -> ok;
  379. {_, Plugins} ->
  380. [begin
  381. case lists:keyfind(deps, 1, Conf) of
  382. false -> ok;
  383. {_, Deps} ->
  384. case lists:keyfind(P, 1, Deps) of
  385. false -> ok;
  386. _ ->
  387. Path = "$(DEPS_DIR)/" ++ atom_to_list(P),
  388. io:format("~s", [os:cmd("$(MAKE) -C $(DEPS_DIR)/$(1) " ++ Path)]),
  389. io:format("~s", [os:cmd("$(MAKE) -C " ++ Path ++ " IS_DEP=1")]),
  390. code:add_patha(Path ++ "/ebin")
  391. end
  392. end
  393. end || P <- Plugins],
  394. [case code:load_file(P) of
  395. {module, P} -> ok;
  396. _ ->
  397. case lists:keyfind(plugin_dir, 1, Conf) of
  398. false -> ok;
  399. {_, PluginsDir} ->
  400. ErlFile = "$(DEPS_DIR)/$(1)/" ++ PluginsDir ++ "/" ++ atom_to_list(P) ++ ".erl",
  401. PatchPlugin(ErlFile),
  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 ]; then \
  488. echo " AUTO " $(1); \
  489. cd $(DEPS_DIR)/$(1) && autoreconf -vif; \
  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"