deps.mk 21 KB

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