deps.mk 28 KB

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