deps.mk 21 KB

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