deps.mk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. # Copyright (c) 2013-2015, 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 distclean-pkg pkg-list pkg-search
  4. # Configuration.
  5. IGNORE_DEPS ?=
  6. DEPS_DIR ?= $(CURDIR)/deps
  7. export DEPS_DIR
  8. REBAR_DEPS_DIR = $(DEPS_DIR)
  9. export REBAR_DEPS_DIR
  10. ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(filter-out $(IGNORE_DEPS),$(DEPS)))
  11. ifeq ($(filter $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),)
  12. ifeq ($(ERL_LIBS),)
  13. ERL_LIBS = $(DEPS_DIR)
  14. else
  15. ERL_LIBS := $(ERL_LIBS):$(DEPS_DIR)
  16. endif
  17. endif
  18. export ERL_LIBS
  19. PKG_FILE2 ?= $(CURDIR)/.erlang.mk.packages.v2
  20. export PKG_FILE2
  21. PKG_FILE_URL ?= https://raw.githubusercontent.com/ninenines/erlang.mk/master/packages.v2.tsv
  22. # Verbosity.
  23. dep_verbose_0 = @echo " DEP " $(1);
  24. dep_verbose = $(dep_verbose_$(V))
  25. # Core targets.
  26. ifneq ($(SKIP_DEPS),)
  27. deps::
  28. else
  29. deps:: $(ALL_DEPS_DIRS)
  30. @for dep in $(ALL_DEPS_DIRS) ; do \
  31. if [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ] ; then \
  32. $(MAKE) -C $$dep IS_DEP=1 || exit $$? ; \
  33. else \
  34. echo "ERROR: No Makefile to build dependency $$dep." ; \
  35. exit 1 ; \
  36. fi ; \
  37. done
  38. endif
  39. distclean:: distclean-deps distclean-pkg
  40. # Deps related targets.
  41. # @todo rename GNUmakefile and makefile into Makefile first, if they exist
  42. # While Makefile file could be GNUmakefile or makefile,
  43. # in practice only Makefile is needed so far.
  44. define dep_autopatch
  45. if [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
  46. if [ 0 != `grep -c "include ../\w*\.mk" $(DEPS_DIR)/$(1)/Makefile` ]; then \
  47. $(call dep_autopatch2,$(1)); \
  48. elif [ 0 != `grep -ci rebar $(DEPS_DIR)/$(1)/Makefile` ]; then \
  49. $(call dep_autopatch2,$(1)); \
  50. elif [ 0 != `find $(DEPS_DIR)/$(1)/ -type f -name \*.mk -not -name erlang.mk | xargs grep -ci rebar` ]; then \
  51. $(call dep_autopatch2,$(1)); \
  52. else \
  53. $(call dep_autopatch_erlang_mk,$(1)); \
  54. fi \
  55. else \
  56. if [ ! -d $(DEPS_DIR)/$(1)/src/ ]; then \
  57. $(call dep_autopatch_noop,$(1)); \
  58. else \
  59. $(call dep_autopatch2,$(1)); \
  60. fi \
  61. fi
  62. endef
  63. define dep_autopatch2
  64. if [ -f $(DEPS_DIR)/$(1)/rebar.config.script ]; then \
  65. $(call dep_autopatch_rebar_script,$(1)); \
  66. $(call dep_autopatch_rebar,$(1)); \
  67. elif [ -f $(DEPS_DIR)/$(1)/rebar.config ]; then \
  68. $(call dep_autopatch_rebar,$(1)); \
  69. else \
  70. $(call dep_autopatch_gen,$(1)); \
  71. fi
  72. endef
  73. define dep_autopatch_noop
  74. printf "noop:\n" > $(DEPS_DIR)/$(1)/Makefile
  75. endef
  76. # Overwrite erlang.mk with the current file by default.
  77. ifeq ($(NO_AUTOPATCH_ERLANG_MK),)
  78. define dep_autopatch_erlang_mk
  79. rm -f $(DEPS_DIR)/$(1)/erlang.mk; \
  80. cd $(DEPS_DIR)/$(1)/ && ln -s ../../erlang.mk; \
  81. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  82. endef
  83. else
  84. define dep_autopatch_erlang_mk
  85. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  86. endef
  87. endif
  88. define dep_autopatch_gen
  89. printf "%s\n" \
  90. "ERLC_OPTS = +debug_info" \
  91. "include ../../erlang.mk" > $(DEPS_DIR)/$(1)/Makefile; \
  92. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  93. endef
  94. define dep_autopatch_rebar_script
  95. mv $(DEPS_DIR)/$(1)/rebar.config.script $(DEPS_DIR)/$(1)/rebar.config.script.orig; \
  96. sed -r 's/rebar_utils:is_arch\((.*)\)/\1 =:= "$(PLATFORM)"/g' $(DEPS_DIR)/$(1)/rebar.config.script.orig \
  97. > $(DEPS_DIR)/$(1)/rebar.config.script
  98. endef
  99. define dep_autopatch_rebar
  100. if [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
  101. mv $(DEPS_DIR)/$(1)/Makefile $(DEPS_DIR)/$(1)/Makefile.orig.mk; \
  102. fi; \
  103. $(call erlang,$(call dep_autopatch_rebar.erl,$(1))); \
  104. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
  105. endef
  106. define dep_autopatch_rebar.erl
  107. Conf1 = case file:consult("$(DEPS_DIR)/$(1)/rebar.config") of
  108. {ok, Conf0} -> Conf0;
  109. _ -> []
  110. end,
  111. Conf = case filelib:is_file("$(DEPS_DIR)/$(1)/rebar.config.script") of
  112. false -> Conf1;
  113. true ->
  114. Bindings0 = erl_eval:new_bindings(),
  115. Bindings1 = erl_eval:add_binding('CONFIG', Conf1, Bindings0),
  116. Bindings = erl_eval:add_binding('SCRIPT', "$(DEPS_DIR)/$(1)/rebar.config.script", Bindings1),
  117. {ok, Conf2} = file:script("$(DEPS_DIR)/$(1)/rebar.config.script", Bindings),
  118. Conf2
  119. end,
  120. Write = fun (Text) ->
  121. file:write_file("$(DEPS_DIR)/$(1)/Makefile", Text, [append])
  122. end,
  123. Escape = fun (Text) ->
  124. re:replace(Text, "\\\\$$$$", "\$$$$$$$$", [global, {return, list}])
  125. end,
  126. Write("IGNORE_DEPS = edown eper eunit_formatters meck "
  127. "rebar_lock_deps_plugin rebar_vsn_plugin reltool_util\n\n"),
  128. fun() ->
  129. Write("ERLC_OPTS = +debug_info\n"),
  130. case lists:keyfind(erl_opts, 1, Conf) of
  131. false -> ok;
  132. {_, ErlOpts} ->
  133. lists:foreach(fun
  134. ({d, D}) ->
  135. Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
  136. ({parse_transform, PT}) ->
  137. Write("ERLC_OPTS += +'{parse_transform, " ++ atom_to_list(PT) ++ "}'\n");
  138. (_) -> ok
  139. end, ErlOpts)
  140. end,
  141. Write("\n")
  142. end(),
  143. fun() ->
  144. File = case lists:keyfind(deps, 1, Conf) of
  145. false -> [];
  146. {_, Deps} ->
  147. [begin
  148. Name = element(1, Dep),
  149. {Method, Repo, Commit} = case element(3, Dep) of
  150. {git, R} -> {git, R, master};
  151. {M, R, {branch, C}} -> {M, R, C};
  152. {M, R, {tag, C}} -> {M, R, C};
  153. {M, R, C} -> {M, R, C}
  154. end,
  155. Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
  156. end || Dep <- Deps, tuple_size(Dep) > 2]
  157. end
  158. end(),
  159. fun() ->
  160. First = case lists:keyfind(erl_first_files, 1, Conf) of false -> []; {_, Files} ->
  161. Names = [[" ", begin "lre." ++ Elif = lists:reverse(F), lists:reverse(Elif) end]
  162. || "src/" ++ F <- Files],
  163. Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
  164. end
  165. end(),
  166. FindFirst = fun(F, Fd) ->
  167. case io:parse_erl_form(Fd, undefined) of
  168. {ok, {attribute, _,compile, {parse_transform, PT}}, _} ->
  169. [PT, F(F, Fd)];
  170. {ok, {attribute, _, include, Hrl}, _} ->
  171. case file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]) of
  172. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  173. _ ->
  174. case file:open("$(DEPS_DIR)/$(1)/src/" ++ Hrl, [read]) of
  175. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  176. _ -> [F(F, Fd)]
  177. end
  178. end;
  179. {ok, {attribute, _, include_lib, "$(1)/include/" ++ Hrl}, _} ->
  180. {ok, HrlFd} = file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]),
  181. [F(F, HrlFd), F(F, Fd)];
  182. {ok, {attribute, _, import, {Imp, _}}, _} ->
  183. case file:open("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(Imp) ++ ".erl", [read]) of
  184. {ok, ImpFd} -> [Imp, F(F, ImpFd), F(F, Fd)];
  185. _ -> [F(F, Fd)]
  186. end;
  187. {eof, _} ->
  188. file:close(Fd),
  189. [];
  190. _ ->
  191. F(F, Fd)
  192. end
  193. end,
  194. fun() ->
  195. ErlFiles = filelib:wildcard("$(DEPS_DIR)/$(1)/src/*.erl"),
  196. First0 = lists:usort(lists:flatten([begin
  197. {ok, Fd} = file:open(F, [read]),
  198. FindFirst(FindFirst, Fd)
  199. end || F <- ErlFiles])),
  200. First = lists:flatten([begin
  201. {ok, Fd} = file:open("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(F) ++ ".erl", [read]),
  202. FindFirst(FindFirst, Fd)
  203. end || F <- First0]) ++ First0,
  204. Write(["COMPILE_FIRST +=", [[" ", atom_to_list(M)] || M <- First,
  205. lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)], "\n"])
  206. end(),
  207. PortSpec = fun(Name, {_, Output, Input, [{env, Env}]}) ->
  208. filelib:ensure_dir("$(DEPS_DIR)/$(1)/" ++ Output),
  209. file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile." ++ Name, [
  210. [["override ", K, " = $$$$\(shell echo ", Escape(V), "\)\n"]
  211. || {_, K, V} <- Env],
  212. "\nall:\n\t$$$$\(CC\) $$$$\(CFLAGS\) $$$$\(LDLIBS\) $$$$\(LDFLAGS\) ",
  213. "-o $(DEPS_DIR)/$(1)/", Output,
  214. [[" ../", F] || F <- Input]
  215. ])
  216. end,
  217. fun() ->
  218. case lists:keyfind(port_specs, 1, Conf) of
  219. {_, [{Output, _}]} ->
  220. filelib:ensure_dir("$(DEPS_DIR)/$(1)/" ++ Output),
  221. Write("C_SRC_OUTPUT = " ++ Escape(Output) ++ "\n");
  222. {_, [First, Second]} ->
  223. PortSpec("1", First),
  224. PortSpec("2", Second),
  225. file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile",
  226. "all:\n\t$$$$\(MAKE\) -f Makefile.1\n\t$$$$\(MAKE\) -f Makefile.2\n");
  227. _ -> ok
  228. end
  229. end(),
  230. fun() ->
  231. case lists:keyfind(port_env, 1, Conf) of
  232. {_, Vars} ->
  233. lists:foldl(fun
  234. ({K, V}, Acc) ->
  235. case lists:member(K, Acc) of
  236. true -> Acc;
  237. false ->
  238. Write(K ++ " = $$$$\(shell echo " ++ Escape(V) ++ "\)\n"),
  239. [K|Acc]
  240. end;
  241. ({Regex, K, V}, Acc) ->
  242. case lists:member(K, Acc) of
  243. true -> Acc;
  244. false ->
  245. case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
  246. nomatch -> Acc;
  247. match ->
  248. Write(K ++ " = $$$$\(shell echo " ++ Escape(V) ++ "\)\n"),
  249. [K|Acc]
  250. end
  251. end
  252. end, [], Vars),
  253. Write("CFLAGS += $$$$\(DRV_CFLAGS\)\n"),
  254. Write("CXXFLAGS += $$$$\(DRV_CFLAGS\)\n"),
  255. Write("LDFLAGS += $$$$\(DRV_LDFLAGS\)\n");
  256. _ -> ok
  257. end
  258. end(),
  259. fun() ->
  260. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  261. false -> ok;
  262. true ->
  263. Sources = [[" ./c_src/", S] || S <- filelib:wildcard("*.{c,C,cc,cpp}", "$(DEPS_DIR)/$(1)/c_src")],
  264. Write(io_lib:format("SOURCES := ~s\n", [Sources]))
  265. end
  266. end(),
  267. Write("\n\nrebar_dep: pre-deps deps pre-app app\n"),
  268. Write("\npre-deps::\n"),
  269. Write("\npre-app::\n"),
  270. fun() ->
  271. case lists:keyfind(pre_hooks, 1, Conf) of
  272. false -> ok;
  273. {_, Hooks} ->
  274. [case H of
  275. {'get-deps', Command} ->
  276. Write("\npre-deps::\n\t" ++ Escape(Command) ++ "\n");
  277. {compile, Command} ->
  278. Write("\npre-app::\n\t" ++ Escape(Command) ++ "\n");
  279. {Regex, compile, Command0} ->
  280. case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
  281. match ->
  282. Command = case Command0 of
  283. "make -C" ++ _ -> Escape(Command0);
  284. "gmake -C" ++ _ -> Escape(Command0);
  285. "make " ++ Command1 -> "make -f Makefile.orig.mk " ++ Escape(Command1);
  286. "gmake " ++ Command1 -> "gmake -f Makefile.orig.mk " ++ Escape(Command1);
  287. _ -> Command0
  288. end,
  289. Write("\npre-app::\n\t" ++ Command ++ "\n");
  290. nomatch ->
  291. ok
  292. end;
  293. _ -> ok
  294. end || H <- Hooks]
  295. end
  296. end(),
  297. Write("\ninclude ../../erlang.mk"),
  298. fun() ->
  299. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  300. false -> ok;
  301. true ->
  302. Write("\n\nCFLAGS := $$$$\(filter-out -std=c99 -Wmissing-prototypes,$$$$\(CFLAGS\)\)\n")
  303. end
  304. end(),
  305. fun() ->
  306. case lists:keyfind(plugins, 1, Conf) of
  307. {_, [Plugin]} when is_atom(Plugin) ->
  308. ErlFile = "$(DEPS_DIR)/$(1)/plugins/" ++ atom_to_list(Plugin) ++ ".erl",
  309. try
  310. {ok, PF} = file:read_file(ErlFile),
  311. PF2 = re:replace(PF, "rebar_utils:find_files", "find_files", [global, {return, list}]),
  312. PF3 = PF2 ++ "find_files(Dir, Regex) ->
  313. filelib:fold_files(Dir, Regex, true, fun(F, Acc) -> [F|Acc] end, []).",
  314. ok = file:write_file(ErlFile, PF3),
  315. {ok, Mod, Bin} = compile:file(ErlFile, [binary]),
  316. {module, Mod} = code:load_binary(Mod, ErlFile, Bin),
  317. c:cd("$(DEPS_DIR)/$(1)/"),
  318. ok = Mod:pre_compile(Conf, undefined)
  319. catch _:_ ->
  320. ok
  321. end;
  322. _ -> ok
  323. end
  324. end(),
  325. halt()
  326. endef
  327. define dep_autopatch_appsrc.erl
  328. AppSrcOut = "$(DEPS_DIR)/$(1)/src/$(1).app.src",
  329. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(DEPS_DIR)/$(1)/ebin/$(1).app"; true -> AppSrcOut end,
  330. case filelib:is_regular(AppSrcIn) of
  331. false -> ok;
  332. true ->
  333. fun() ->
  334. {ok, [{application, $(1), L}]} = file:consult(AppSrcIn),
  335. L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end,
  336. L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, "git"}); _ -> L2 end,
  337. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}]))
  338. end(),
  339. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  340. end,
  341. halt()
  342. endef
  343. define dep_fetch
  344. if [ "$$$$VS" = "git" ]; then \
  345. git clone -q -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  346. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  347. elif [ "$$$$VS" = "hg" ]; then \
  348. hg clone -q -U $$$$REPO $(DEPS_DIR)/$(1); \
  349. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  350. elif [ "$$$$VS" = "svn" ]; then \
  351. svn checkout -q $$$$REPO $(DEPS_DIR)/$(1); \
  352. elif [ "$$$$VS" = "cp" ]; then \
  353. cp -R $$$$REPO $(DEPS_DIR)/$(1); \
  354. else \
  355. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  356. exit 78; \
  357. fi
  358. endef
  359. define dep_target
  360. $(DEPS_DIR)/$(1):
  361. @mkdir -p $(DEPS_DIR)
  362. ifeq (,$(dep_$(1)))
  363. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  364. $(dep_verbose) DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  365. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  366. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  367. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  368. $(call dep_fetch,$(1))
  369. else
  370. ifeq (1,$(words $(dep_$(1))))
  371. $(dep_verbose) VS=git; \
  372. REPO=$(dep_$(1)); \
  373. COMMIT=master; \
  374. $(call dep_fetch,$(1))
  375. else
  376. ifeq (2,$(words $(dep_$(1))))
  377. $(dep_verbose) VS=git; \
  378. REPO=$(word 1,$(dep_$(1))); \
  379. COMMIT=$(word 2,$(dep_$(1))); \
  380. $(call dep_fetch,$(1))
  381. else
  382. $(dep_verbose) VS=$(word 1,$(dep_$(1))); \
  383. REPO=$(word 2,$(dep_$(1))); \
  384. COMMIT=$(word 3,$(dep_$(1))); \
  385. $(call dep_fetch,$(1))
  386. endif
  387. endif
  388. endif
  389. @if [ -f $(DEPS_DIR)/$(1)/configure.ac ]; then \
  390. echo " AUTO " $(1); \
  391. cd $(DEPS_DIR)/$(1) && autoreconf -vif; \
  392. fi
  393. -@if [ -f $(DEPS_DIR)/$(1)/configure ]; then \
  394. echo " CONF " $(1); \
  395. cd $(DEPS_DIR)/$(1) && ./configure; \
  396. fi
  397. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  398. @$(call dep_autopatch,$(1))
  399. endif
  400. endef
  401. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  402. distclean-deps:
  403. $(gen_verbose) rm -rf $(DEPS_DIR)
  404. # Packages related targets.
  405. $(PKG_FILE2):
  406. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  407. pkg-list: $(PKG_FILE2)
  408. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  409. "Name:\t\t" $$1 "\n" \
  410. "Repository:\t" $$3 "\n" \
  411. "Website:\t" $$5 "\n" \
  412. "Description:\t" $$6 "\n" }'
  413. ifdef q
  414. pkg-search: $(PKG_FILE2)
  415. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  416. "Name:\t\t" $$1 "\n" \
  417. "Repository:\t" $$3 "\n" \
  418. "Website:\t" $$5 "\n" \
  419. "Description:\t" $$6 "\n" }'
  420. else
  421. pkg-search:
  422. $(error Usage: $(MAKE) pkg-search q=STRING)
  423. endif
  424. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  425. distclean-pkg:
  426. $(gen_verbose) rm -f $(PKG_FILE2)
  427. endif
  428. help::
  429. @printf "%s\n" "" \
  430. "Package-related targets:" \
  431. " pkg-list List all known packages" \
  432. " pkg-search q=STRING Search for STRING in the package index"