deps.mk 21 KB

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