deps.mk 21 KB

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