deps.mk 22 KB

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