deps.mk 26 KB

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