deps.mk 28 KB

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