deps.mk 21 KB

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