deps.mk 21 KB

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