deps.mk 20 KB

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