deps.mk 21 KB

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