deps.mk 22 KB

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