deps.mk 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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), $(PLATFORM)} of
  392. {[], _} -> "\n";
  393. {_, darwin} -> "\n";
  394. _ -> " -shared\n"
  395. end])
  396. end,
  397. [PortSpec(S) || S <- PortSpecs]
  398. end,
  399. Write("\ninclude $(call core_relpath,$(dir $(ERLANG_MK_FILENAME)),$(DEPS_DIR)/app)/erlang.mk"),
  400. RunPlugin = fun(Plugin, Step) ->
  401. case erlang:function_exported(Plugin, Step, 2) of
  402. false -> ok;
  403. true ->
  404. c:cd("$(call core_native_path,$(DEPS_DIR)/$1/)"),
  405. Ret = Plugin:Step({config, "", Conf, dict:new(), dict:new(), dict:new(),
  406. dict:store(base_dir, "", dict:new())}, undefined),
  407. io:format("rebar plugin ~p step ~p ret ~p~n", [Plugin, Step, Ret])
  408. end
  409. end,
  410. fun() ->
  411. case lists:keyfind(plugins, 1, Conf) of
  412. false -> ok;
  413. {_, Plugins} ->
  414. [begin
  415. case lists:keyfind(deps, 1, Conf) of
  416. false -> ok;
  417. {_, Deps} ->
  418. case lists:keyfind(P, 1, Deps) of
  419. false -> ok;
  420. _ ->
  421. Path = "$(call core_native_path,$(DEPS_DIR)/)" ++ atom_to_list(P),
  422. io:format("~s", [os:cmd("$(MAKE) -C $(call core_native_path,$(DEPS_DIR)/$1) " ++ Path)]),
  423. io:format("~s", [os:cmd("$(MAKE) -C " ++ Path ++ " IS_DEP=1")]),
  424. code:add_patha(Path ++ "/ebin")
  425. end
  426. end
  427. end || P <- Plugins],
  428. [case code:load_file(P) of
  429. {module, P} -> ok;
  430. _ ->
  431. case lists:keyfind(plugin_dir, 1, Conf) of
  432. false -> ok;
  433. {_, PluginsDir} ->
  434. ErlFile = "$(call core_native_path,$(DEPS_DIR)/$1/)" ++ PluginsDir ++ "/" ++ atom_to_list(P) ++ ".erl",
  435. {ok, P, Bin} = compile:file(ErlFile, [binary]),
  436. {module, P} = code:load_binary(P, ErlFile, Bin)
  437. end
  438. end || P <- Plugins],
  439. [RunPlugin(P, preprocess) || P <- Plugins],
  440. [RunPlugin(P, pre_compile) || P <- Plugins],
  441. [RunPlugin(P, compile) || P <- Plugins]
  442. end
  443. end(),
  444. halt()
  445. endef
  446. define dep_autopatch_app.erl
  447. UpdateModules = fun(App) ->
  448. case filelib:is_regular(App) of
  449. false -> ok;
  450. true ->
  451. {ok, [{application, '$(1)', L0}]} = file:consult(App),
  452. Mods = filelib:fold_files("$(call core_native_path,$(DEPS_DIR)/$1/src)", "\\\\.erl$$", true,
  453. fun (F, Acc) -> [list_to_atom(filename:rootname(filename:basename(F)))|Acc] end, []),
  454. L = lists:keystore(modules, 1, L0, {modules, Mods}),
  455. ok = file:write_file(App, io_lib:format("~p.~n", [{application, '$(1)', L}]))
  456. end
  457. end,
  458. UpdateModules("$(call core_native_path,$(DEPS_DIR)/$1/ebin/$1.app)"),
  459. halt()
  460. endef
  461. define dep_autopatch_appsrc_script.erl
  462. AppSrc = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)",
  463. AppSrcScript = AppSrc ++ ".script",
  464. Bindings = erl_eval:new_bindings(),
  465. {ok, Conf} = file:script(AppSrcScript, Bindings),
  466. ok = file:write_file(AppSrc, io_lib:format("~p.~n", [Conf])),
  467. halt()
  468. endef
  469. define dep_autopatch_appsrc.erl
  470. AppSrcOut = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)",
  471. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(call core_native_path,$(DEPS_DIR)/$1/ebin/$1.app)"; true -> AppSrcOut end,
  472. case filelib:is_regular(AppSrcIn) of
  473. false -> ok;
  474. true ->
  475. {ok, [{application, $(1), L0}]} = file:consult(AppSrcIn),
  476. L1 = lists:keystore(modules, 1, L0, {modules, []}),
  477. L2 = case lists:keyfind(vsn, 1, L1) of {_, git} -> lists:keyreplace(vsn, 1, L1, {vsn, "git"}); _ -> L1 end,
  478. L3 = case lists:keyfind(registered, 1, L2) of false -> [{registered, []}|L2]; _ -> L2 end,
  479. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}])),
  480. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  481. end,
  482. halt()
  483. endef
  484. define dep_fetch_git
  485. git clone -q -n -- $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
  486. cd $(DEPS_DIR)/$(call dep_name,$(1)) && git checkout -q $(call dep_commit,$(1));
  487. endef
  488. define dep_fetch_git-submodule
  489. git submodule update --init -- $(DEPS_DIR)/$1;
  490. endef
  491. define dep_fetch_hg
  492. hg clone -q -U $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
  493. cd $(DEPS_DIR)/$(call dep_name,$(1)) && hg update -q $(call dep_commit,$(1));
  494. endef
  495. define dep_fetch_svn
  496. svn checkout -q $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
  497. endef
  498. define dep_fetch_cp
  499. cp -R $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
  500. endef
  501. define dep_fetch_hex.erl
  502. ssl:start(),
  503. inets:start(),
  504. {ok, {{_, 200, _}, _, Body}} = httpc:request(get,
  505. {"https://s3.amazonaws.com/s3.hex.pm/tarballs/$(1)-$(2).tar", []},
  506. [], [{body_format, binary}]),
  507. {ok, Files} = erl_tar:extract({binary, Body}, [memory]),
  508. {_, Source} = lists:keyfind("contents.tar.gz", 1, Files),
  509. ok = erl_tar:extract({binary, Source}, [{cwd, "$(call core_native_path,$(DEPS_DIR)/$1)"}, compressed]),
  510. halt()
  511. endef
  512. # Hex only has a package version. No need to look in the Erlang.mk packages.
  513. define dep_fetch_hex
  514. $(call erlang,$(call dep_fetch_hex.erl,$(1),$(strip $(word 2,$(dep_$(1))))));
  515. endef
  516. define dep_fetch_fail
  517. echo "Error: Unknown or invalid dependency: $(1)." >&2; \
  518. exit 78;
  519. endef
  520. # Kept for compatibility purposes with older Erlang.mk configuration.
  521. define dep_fetch_legacy
  522. $(warning WARNING: '$(1)' dependency configuration uses deprecated format.) \
  523. git clone -q -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1); \
  524. cd $(DEPS_DIR)/$(1) && git checkout -q $(if $(word 2,$(dep_$(1))),$(word 2,$(dep_$(1))),master);
  525. endef
  526. define dep_fetch
  527. $(if $(dep_$(1)), \
  528. $(if $(dep_fetch_$(word 1,$(dep_$(1)))), \
  529. $(word 1,$(dep_$(1))), \
  530. $(if $(IS_DEP),legacy,fail)), \
  531. $(if $(filter $(1),$(PACKAGES)), \
  532. $(pkg_$(1)_fetch), \
  533. fail))
  534. endef
  535. define dep_target
  536. $(DEPS_DIR)/$(call dep_name,$1):
  537. $(eval DEP_NAME := $(call dep_name,$1))
  538. $(eval DEP_STR := $(if $(filter-out $1,$(DEP_NAME)),$1,"$1 ($(DEP_NAME))"))
  539. $(verbose) if test -d $(APPS_DIR)/$(DEP_NAME); then \
  540. echo "Error: Dependency" $(DEP_STR) "conflicts with application found in $(APPS_DIR)/$(DEP_NAME)."; \
  541. exit 17; \
  542. fi
  543. $(verbose) mkdir -p $(DEPS_DIR)
  544. $(dep_verbose) $(call dep_fetch_$(strip $(call dep_fetch,$(1))),$(1))
  545. $(verbose) if [ -f $(DEPS_DIR)/$(1)/configure.ac -o -f $(DEPS_DIR)/$(1)/configure.in ] \
  546. && [ ! -f $(DEPS_DIR)/$(1)/configure ]; then \
  547. echo " AUTO " $(1); \
  548. cd $(DEPS_DIR)/$(1) && autoreconf -Wall -vif -I m4; \
  549. fi
  550. - $(verbose) if [ -f $(DEPS_DIR)/$(DEP_NAME)/configure ]; then \
  551. echo " CONF " $(DEP_STR); \
  552. cd $(DEPS_DIR)/$(DEP_NAME) && ./configure; \
  553. fi
  554. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  555. $(verbose) if [ "$(1)" = "amqp_client" -a "$(RABBITMQ_CLIENT_PATCH)" ]; then \
  556. if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
  557. echo " PATCH Downloading rabbitmq-codegen"; \
  558. git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
  559. fi; \
  560. if [ ! -d $(DEPS_DIR)/rabbitmq-server ]; then \
  561. echo " PATCH Downloading rabbitmq-server"; \
  562. git clone https://github.com/rabbitmq/rabbitmq-server.git $(DEPS_DIR)/rabbitmq-server; \
  563. fi; \
  564. ln -s $(DEPS_DIR)/amqp_client/deps/rabbit_common-0.0.0 $(DEPS_DIR)/rabbit_common; \
  565. elif [ "$(1)" = "rabbit" -a "$(RABBITMQ_SERVER_PATCH)" ]; then \
  566. if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
  567. echo " PATCH Downloading rabbitmq-codegen"; \
  568. git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
  569. fi \
  570. else \
  571. $$(call dep_autopatch,$(DEP_NAME)) \
  572. fi
  573. endif
  574. endef
  575. $(foreach dep,$(BUILD_DEPS) $(DEPS),$(eval $(call dep_target,$(dep))))
  576. ifndef IS_APP
  577. clean:: clean-apps
  578. clean-apps:
  579. $(verbose) for dep in $(ALL_APPS_DIRS) ; do \
  580. $(MAKE) -C $$dep clean IS_APP=1 || exit $$?; \
  581. done
  582. distclean:: distclean-apps
  583. distclean-apps:
  584. $(verbose) for dep in $(ALL_APPS_DIRS) ; do \
  585. $(MAKE) -C $$dep distclean IS_APP=1 || exit $$?; \
  586. done
  587. endif
  588. ifndef SKIP_DEPS
  589. distclean:: distclean-deps
  590. distclean-deps:
  591. $(gen_verbose) rm -rf $(DEPS_DIR)
  592. endif
  593. # External plugins.
  594. DEP_PLUGINS ?=
  595. define core_dep_plugin
  596. -include $(DEPS_DIR)/$(1)
  597. $(DEPS_DIR)/$(1): $(DEPS_DIR)/$(2) ;
  598. endef
  599. $(foreach p,$(DEP_PLUGINS),\
  600. $(eval $(if $(findstring /,$p),\
  601. $(call core_dep_plugin,$p,$(firstword $(subst /, ,$p))),\
  602. $(call core_dep_plugin,$p/plugins.mk,$p))))