deps.mk 24 KB

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