deps.mk 28 KB

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