deps.mk 20 KB

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