deps.mk 25 KB

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