deps.mk 21 KB

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