deps.mk 20 KB

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