deps.mk 24 KB

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