deps.mk 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. GetHexVsn3 = fun
  367. (N, NP, "~>" ++ S0) ->
  368. case GetHexVsn2(N, NP) of
  369. false ->
  370. S2 = case S0 of
  371. " " ++ S1 -> S1;
  372. _ -> S0
  373. end,
  374. S = case length([ok || $$. <- S2]) of
  375. 0 -> S2 ++ ".0.0";
  376. 1 -> S2 ++ ".0";
  377. _ -> S2
  378. end,
  379. {N, {hex, NP, S}};
  380. NameSource ->
  381. NameSource
  382. end;
  383. (N, NP, S) -> {N, {hex, NP, S}}
  384. end,
  385. fun() ->
  386. File = case lists:keyfind(deps, 1, Conf) of
  387. false -> [];
  388. {_, Deps} ->
  389. [begin case case Dep of
  390. N when is_atom(N) -> GetHexVsn2(N, N);
  391. {N, S} when is_atom(N), is_list(S) -> GetHexVsn3(N, N, S);
  392. {N, {pkg, NP}} when is_atom(N) -> GetHexVsn2(N, NP);
  393. {N, S, {pkg, NP}} -> GetHexVsn3(N, NP, S);
  394. {N, S} when is_tuple(S) -> {N, S};
  395. {N, _, S} -> {N, S};
  396. {N, _, S, _} -> {N, S};
  397. _ -> false
  398. end of
  399. false -> ok;
  400. {Name, Source} ->
  401. {Method, Repo, Commit} = case Source of
  402. {hex, NPV, V} -> {hex, V, NPV};
  403. {git, R} -> {git, R, master};
  404. {M, R, {branch, C}} -> {M, R, C};
  405. {M, R, {ref, C}} -> {M, R, C};
  406. {M, R, {tag, C}} -> {M, R, C};
  407. {M, R, C} -> {M, R, C}
  408. end,
  409. Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
  410. end end || Dep <- Deps]
  411. end
  412. end(),
  413. fun() ->
  414. case lists:keyfind(erl_first_files, 1, Conf) of
  415. false -> ok;
  416. {_, Files} ->
  417. Names = [[" ", case lists:reverse(F) of
  418. "lre." ++ Elif -> lists:reverse(Elif);
  419. "lrx." ++ Elif -> lists:reverse(Elif);
  420. "lry." ++ Elif -> lists:reverse(Elif);
  421. Elif -> lists:reverse(Elif)
  422. end] || "src/" ++ F <- Files],
  423. Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
  424. end
  425. end(),
  426. Write("\n\nrebar_dep: preprocess pre-deps deps pre-app app\n"),
  427. Write("\npreprocess::\n"),
  428. Write("\npre-deps::\n"),
  429. Write("\npre-app::\n"),
  430. PatchHook = fun(Cmd) ->
  431. Cmd2 = re:replace(Cmd, "^([g]?make)(.*)( -C.*)", "\\\\1\\\\3\\\\2", [{return, list}]),
  432. case Cmd2 of
  433. "make -C" ++ Cmd1 -> "$$\(MAKE) -C" ++ Escape(Cmd1);
  434. "gmake -C" ++ Cmd1 -> "$$\(MAKE) -C" ++ Escape(Cmd1);
  435. "make " ++ Cmd1 -> "$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
  436. "gmake " ++ Cmd1 -> "$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
  437. _ -> Escape(Cmd)
  438. end
  439. end,
  440. fun() ->
  441. case lists:keyfind(pre_hooks, 1, Conf) of
  442. false -> ok;
  443. {_, Hooks} ->
  444. [case H of
  445. {'get-deps', Cmd} ->
  446. Write("\npre-deps::\n\t" ++ PatchHook(Cmd) ++ "\n");
  447. {compile, Cmd} ->
  448. Write("\npre-app::\n\tCC=$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
  449. {{pc, compile}, Cmd} ->
  450. Write("\npre-app::\n\tCC=$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
  451. {Regex, compile, Cmd} ->
  452. case rebar_utils:is_arch(Regex) of
  453. true -> Write("\npre-app::\n\tCC=$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
  454. false -> ok
  455. end;
  456. _ -> ok
  457. end || H <- Hooks]
  458. end
  459. end(),
  460. ShellToMk = fun(V0) ->
  461. V1 = re:replace(V0, "[$$][(]", "$$\(shell ", [global]),
  462. V = re:replace(V1, "([$$])(?![(])(\\\\w*)", "\\\\1(\\\\2)", [global]),
  463. re:replace(V, "-Werror\\\\b", "", [{return, list}, global])
  464. end,
  465. PortSpecs = fun() ->
  466. case lists:keyfind(port_specs, 1, Conf) of
  467. false ->
  468. case filelib:is_dir("$(call core_native_path,$(DEPS_DIR)/$1/c_src)") of
  469. false -> [];
  470. true ->
  471. [{"priv/" ++ proplists:get_value(so_name, Conf, "$(1)_drv.so"),
  472. proplists:get_value(port_sources, Conf, ["c_src/*.c"]), []}]
  473. end;
  474. {_, Specs} ->
  475. lists:flatten([case S of
  476. {Output, Input} -> {ShellToMk(Output), Input, []};
  477. {Regex, Output, Input} ->
  478. case rebar_utils:is_arch(Regex) of
  479. true -> {ShellToMk(Output), Input, []};
  480. false -> []
  481. end;
  482. {Regex, Output, Input, [{env, Env}]} ->
  483. case rebar_utils:is_arch(Regex) of
  484. true -> {ShellToMk(Output), Input, Env};
  485. false -> []
  486. end
  487. end || S <- Specs])
  488. end
  489. end(),
  490. PortSpecWrite = fun (Text) ->
  491. file:write_file("$(call core_native_path,$(DEPS_DIR)/$1/c_src/Makefile.erlang.mk)", Text, [append])
  492. end,
  493. case PortSpecs of
  494. [] -> ok;
  495. _ ->
  496. Write("\npre-app::\n\t@$$\(MAKE) --no-print-directory -f c_src/Makefile.erlang.mk\n"),
  497. PortSpecWrite(io_lib:format("ERL_CFLAGS ?= -finline-functions -Wall -fPIC -I \\"~s/erts-~s/include\\" -I \\"~s\\"\n",
  498. [code:root_dir(), erlang:system_info(version), code:lib_dir(erl_interface, include)])),
  499. PortSpecWrite(io_lib:format("ERL_LDFLAGS ?= -L \\"~s\\" -lei\n",
  500. [code:lib_dir(erl_interface, lib)])),
  501. [PortSpecWrite(["\n", E, "\n"]) || E <- OsEnv],
  502. FilterEnv = fun(Env) ->
  503. lists:flatten([case E of
  504. {_, _} -> E;
  505. {Regex, K, V} ->
  506. case rebar_utils:is_arch(Regex) of
  507. true -> {K, V};
  508. false -> []
  509. end
  510. end || E <- Env])
  511. end,
  512. MergeEnv = fun(Env) ->
  513. lists:foldl(fun ({K, V}, Acc) ->
  514. case lists:keyfind(K, 1, Acc) of
  515. false -> [{K, rebar_utils:expand_env_variable(V, K, "")}|Acc];
  516. {_, V0} -> [{K, rebar_utils:expand_env_variable(V, K, V0)}|Acc]
  517. end
  518. end, [], Env)
  519. end,
  520. PortEnv = case lists:keyfind(port_env, 1, Conf) of
  521. false -> [];
  522. {_, PortEnv0} -> FilterEnv(PortEnv0)
  523. end,
  524. PortSpec = fun ({Output, Input0, Env}) ->
  525. filelib:ensure_dir("$(call core_native_path,$(DEPS_DIR)/$1/)" ++ Output),
  526. Input = [[" ", I] || I <- Input0],
  527. PortSpecWrite([
  528. [["\n", K, " = ", ShellToMk(V)] || {K, V} <- lists:reverse(MergeEnv(PortEnv))],
  529. case $(PLATFORM) of
  530. darwin -> "\n\nLDFLAGS += -flat_namespace -undefined suppress";
  531. _ -> ""
  532. end,
  533. "\n\nall:: ", Output, "\n\t@:\n\n",
  534. "%.o: %.c\n\t$$\(CC) -c -o $$\@ $$\< $$\(CFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
  535. "%.o: %.C\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
  536. "%.o: %.cc\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
  537. "%.o: %.cpp\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
  538. [[Output, ": ", K, " += ", ShellToMk(V), "\n"] || {K, V} <- lists:reverse(MergeEnv(FilterEnv(Env)))],
  539. Output, ": $$\(foreach ext,.c .C .cc .cpp,",
  540. "$$\(patsubst %$$\(ext),%.o,$$\(filter %$$\(ext),$$\(wildcard", Input, "))))\n",
  541. "\t$$\(CC) -o $$\@ $$\? $$\(LDFLAGS) $$\(ERL_LDFLAGS) $$\(DRV_LDFLAGS) $$\(EXE_LDFLAGS)",
  542. case {filename:extension(Output), $(PLATFORM)} of
  543. {[], _} -> "\n";
  544. {_, darwin} -> "\n";
  545. _ -> " -shared\n"
  546. end])
  547. end,
  548. [PortSpec(S) || S <- PortSpecs]
  549. end,
  550. fun() ->
  551. case lists:keyfind(plugins, 1, Conf) of
  552. false -> ok;
  553. {_, Plugins0} ->
  554. Plugins = [P || P <- Plugins0, is_tuple(P)],
  555. case lists:keyfind('lfe-compile', 1, Plugins) of
  556. false -> ok;
  557. _ -> Write("\nBUILD_DEPS = lfe lfe.mk\ndep_lfe.mk = git https://github.com/ninenines/lfe.mk master\nDEP_PLUGINS = lfe.mk\n")
  558. end
  559. end
  560. end(),
  561. Write("\ninclude $$\(if $$\(ERLANG_MK_FILENAME),$$\(ERLANG_MK_FILENAME),erlang.mk)"),
  562. RunPlugin = fun(Plugin, Step) ->
  563. case erlang:function_exported(Plugin, Step, 2) of
  564. false -> ok;
  565. true ->
  566. c:cd("$(call core_native_path,$(DEPS_DIR)/$1/)"),
  567. Ret = Plugin:Step({config, "", Conf, dict:new(), dict:new(), dict:new(),
  568. dict:store(base_dir, "", dict:new())}, undefined),
  569. io:format("rebar plugin ~p step ~p ret ~p~n", [Plugin, Step, Ret])
  570. end
  571. end,
  572. fun() ->
  573. case lists:keyfind(plugins, 1, Conf) of
  574. false -> ok;
  575. {_, Plugins0} ->
  576. Plugins = [P || P <- Plugins0, is_atom(P)],
  577. [begin
  578. case lists:keyfind(deps, 1, Conf) of
  579. false -> ok;
  580. {_, Deps} ->
  581. case lists:keyfind(P, 1, Deps) of
  582. false -> ok;
  583. _ ->
  584. Path = "$(call core_native_path,$(DEPS_DIR)/)" ++ atom_to_list(P),
  585. io:format("~s", [os:cmd("$(MAKE) -C $(call core_native_path,$(DEPS_DIR)/$1) " ++ Path)]),
  586. io:format("~s", [os:cmd("$(MAKE) -C " ++ Path ++ " IS_DEP=1")]),
  587. code:add_patha(Path ++ "/ebin")
  588. end
  589. end
  590. end || P <- Plugins],
  591. [case code:load_file(P) of
  592. {module, P} -> ok;
  593. _ ->
  594. case lists:keyfind(plugin_dir, 1, Conf) of
  595. false -> ok;
  596. {_, PluginsDir} ->
  597. ErlFile = "$(call core_native_path,$(DEPS_DIR)/$1/)" ++ PluginsDir ++ "/" ++ atom_to_list(P) ++ ".erl",
  598. {ok, P, Bin} = compile:file(ErlFile, [binary]),
  599. {module, P} = code:load_binary(P, ErlFile, Bin)
  600. end
  601. end || P <- Plugins],
  602. [RunPlugin(P, preprocess) || P <- Plugins],
  603. [RunPlugin(P, pre_compile) || P <- Plugins],
  604. [RunPlugin(P, compile) || P <- Plugins]
  605. end
  606. end(),
  607. halt()
  608. endef
  609. define dep_autopatch_appsrc_script.erl
  610. AppSrc = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)",
  611. AppSrcScript = AppSrc ++ ".script",
  612. Conf1 = case file:consult(AppSrc) of
  613. {ok, Conf0} -> Conf0;
  614. {error, enoent} -> []
  615. end,
  616. Bindings0 = erl_eval:new_bindings(),
  617. Bindings1 = erl_eval:add_binding('CONFIG', Conf1, Bindings0),
  618. Bindings = erl_eval:add_binding('SCRIPT', AppSrcScript, Bindings1),
  619. Conf = case file:script(AppSrcScript, Bindings) of
  620. {ok, [C]} -> C;
  621. {ok, C} -> C
  622. end,
  623. ok = file:write_file(AppSrc, io_lib:format("~p.~n", [Conf])),
  624. halt()
  625. endef
  626. define dep_autopatch_appsrc.erl
  627. AppSrcOut = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)",
  628. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(call core_native_path,$(DEPS_DIR)/$1/ebin/$1.app)"; true -> AppSrcOut end,
  629. case filelib:is_regular(AppSrcIn) of
  630. false -> ok;
  631. true ->
  632. {ok, [{application, $(1), L0}]} = file:consult(AppSrcIn),
  633. L1 = lists:keystore(modules, 1, L0, {modules, []}),
  634. L2 = case lists:keyfind(vsn, 1, L1) of
  635. {_, git} -> lists:keyreplace(vsn, 1, L1, {vsn, lists:droplast(os:cmd("git -C $(DEPS_DIR)/$1 describe --dirty --tags --always"))});
  636. {_, {cmd, _}} -> lists:keyreplace(vsn, 1, L1, {vsn, "cmd"});
  637. _ -> L1
  638. end,
  639. L3 = case lists:keyfind(registered, 1, L2) of false -> [{registered, []}|L2]; _ -> L2 end,
  640. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}])),
  641. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  642. end,
  643. halt()
  644. endef
  645. define dep_fetch_git
  646. git clone -q -n -- $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
  647. cd $(DEPS_DIR)/$(call dep_name,$(1)) && git checkout -q $(call dep_commit,$(1));
  648. endef
  649. define dep_fetch_git-subfolder
  650. mkdir -p $(ERLANG_MK_TMP)/git-subfolder; \
  651. git clone -q -n -- $(call dep_repo,$1) \
  652. $(ERLANG_MK_TMP)/git-subfolder/$(call dep_name,$1); \
  653. cd $(ERLANG_MK_TMP)/git-subfolder/$(call dep_name,$1) \
  654. && git checkout -q $(call dep_commit,$1); \
  655. ln -s $(ERLANG_MK_TMP)/git-subfolder/$(call dep_name,$1)/$(word 4,$(dep_$(1))) \
  656. $(DEPS_DIR)/$(call dep_name,$1);
  657. endef
  658. define dep_fetch_git-submodule
  659. git submodule update --init -- $(DEPS_DIR)/$1;
  660. endef
  661. define dep_fetch_hg
  662. hg clone -q -U $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
  663. cd $(DEPS_DIR)/$(call dep_name,$(1)) && hg update -q $(call dep_commit,$(1));
  664. endef
  665. define dep_fetch_svn
  666. svn checkout -q $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
  667. endef
  668. define dep_fetch_cp
  669. cp -R $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
  670. endef
  671. define dep_fetch_ln
  672. ln -s $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
  673. endef
  674. # Hex only has a package version. No need to look in the Erlang.mk packages.
  675. define dep_fetch_hex
  676. mkdir -p $(ERLANG_MK_TMP)/hex $(DEPS_DIR)/$1; \
  677. $(call core_http_get,$(ERLANG_MK_TMP)/hex/$1.tar,\
  678. https://repo.hex.pm/tarballs/$(if $(word 3,$(dep_$1)),$(word 3,$(dep_$1)),$1)-$(strip $(word 2,$(dep_$1))).tar); \
  679. tar -xOf $(ERLANG_MK_TMP)/hex/$1.tar contents.tar.gz | tar -C $(DEPS_DIR)/$1 -xzf -;
  680. endef
  681. define dep_fetch_fail
  682. echo "Error: Unknown or invalid dependency: $(1)." >&2; \
  683. exit 78;
  684. endef
  685. # Kept for compatibility purposes with older Erlang.mk configuration.
  686. define dep_fetch_legacy
  687. $(warning WARNING: '$(1)' dependency configuration uses deprecated format.) \
  688. git clone -q -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1); \
  689. cd $(DEPS_DIR)/$(1) && git checkout -q $(if $(word 2,$(dep_$(1))),$(word 2,$(dep_$(1))),master);
  690. endef
  691. define dep_target
  692. $(DEPS_DIR)/$(call dep_name,$1): | $(ERLANG_MK_TMP)
  693. $(eval DEP_NAME := $(call dep_name,$1))
  694. $(eval DEP_STR := $(if $(filter $1,$(DEP_NAME)),$1,"$1 ($(DEP_NAME))"))
  695. $(verbose) if test -d $(APPS_DIR)/$(DEP_NAME); then \
  696. echo "Error: Dependency" $(DEP_STR) "conflicts with application found in $(APPS_DIR)/$(DEP_NAME)." >&2; \
  697. exit 17; \
  698. fi
  699. $(verbose) mkdir -p $(DEPS_DIR)
  700. $(dep_verbose) $(call dep_fetch_$(strip $(call dep_fetch,$(1))),$(1))
  701. $(verbose) if [ -f $(DEPS_DIR)/$(1)/configure.ac -o -f $(DEPS_DIR)/$(1)/configure.in ] \
  702. && [ ! -f $(DEPS_DIR)/$(1)/configure ]; then \
  703. echo " AUTO " $(DEP_STR); \
  704. cd $(DEPS_DIR)/$(1) && autoreconf -Wall -vif -I m4; \
  705. fi
  706. - $(verbose) if [ -f $(DEPS_DIR)/$(DEP_NAME)/configure ]; then \
  707. echo " CONF " $(DEP_STR); \
  708. cd $(DEPS_DIR)/$(DEP_NAME) && ./configure; \
  709. fi
  710. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  711. $(verbose) $$(MAKE) --no-print-directory autopatch-$(DEP_NAME)
  712. endif
  713. .PHONY: autopatch-$(call dep_name,$1)
  714. autopatch-$(call dep_name,$1)::
  715. if [ "$1" = "elixir" -a "$(ELIXIR_PATCH)" ]; then \
  716. ln -s lib/elixir/ebin $(DEPS_DIR)/elixir/; \
  717. else \
  718. $$(call dep_autopatch,$(call dep_name,$1)) \
  719. fi
  720. endef
  721. $(foreach dep,$(BUILD_DEPS) $(DEPS),$(eval $(call dep_target,$(dep))))
  722. ifndef IS_APP
  723. clean:: clean-apps
  724. clean-apps:
  725. $(verbose) set -e; for dep in $(ALL_APPS_DIRS) ; do \
  726. $(MAKE) -C $$dep clean IS_APP=1; \
  727. done
  728. distclean:: distclean-apps
  729. distclean-apps:
  730. $(verbose) set -e; for dep in $(ALL_APPS_DIRS) ; do \
  731. $(MAKE) -C $$dep distclean IS_APP=1; \
  732. done
  733. endif
  734. ifndef SKIP_DEPS
  735. distclean:: distclean-deps
  736. distclean-deps:
  737. $(gen_verbose) rm -rf $(DEPS_DIR)
  738. endif
  739. # Forward-declare variables used in core/deps-tools.mk. This is required
  740. # in case plugins use them.
  741. ERLANG_MK_RECURSIVE_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-deps-list.log
  742. ERLANG_MK_RECURSIVE_DOC_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-doc-deps-list.log
  743. ERLANG_MK_RECURSIVE_REL_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-rel-deps-list.log
  744. ERLANG_MK_RECURSIVE_TEST_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-test-deps-list.log
  745. ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST = $(ERLANG_MK_TMP)/recursive-shell-deps-list.log
  746. ERLANG_MK_QUERY_DEPS_FILE = $(ERLANG_MK_TMP)/query-deps.log
  747. ERLANG_MK_QUERY_DOC_DEPS_FILE = $(ERLANG_MK_TMP)/query-doc-deps.log
  748. ERLANG_MK_QUERY_REL_DEPS_FILE = $(ERLANG_MK_TMP)/query-rel-deps.log
  749. ERLANG_MK_QUERY_TEST_DEPS_FILE = $(ERLANG_MK_TMP)/query-test-deps.log
  750. ERLANG_MK_QUERY_SHELL_DEPS_FILE = $(ERLANG_MK_TMP)/query-shell-deps.log