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