deps.mk 21 KB

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