deps.mk 20 KB

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