deps.mk 22 KB

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