deps.mk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. fun() ->
  127. Write("ERLC_OPTS = +debug_info\n"),
  128. case lists:keyfind(erl_opts, 1, Conf) of
  129. false -> ok;
  130. {_, ErlOpts} ->
  131. lists:foreach(fun
  132. ({d, D}) ->
  133. Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
  134. ({parse_transform, PT}) ->
  135. Write("ERLC_OPTS += +'{parse_transform, " ++ atom_to_list(PT) ++ "}'\n");
  136. (_) -> ok
  137. end, ErlOpts)
  138. end,
  139. Write("\n")
  140. end(),
  141. fun() ->
  142. File = case lists:keyfind(deps, 1, Conf) of
  143. false -> [];
  144. {_, Deps} ->
  145. [begin
  146. Name = element(1, Dep),
  147. {Method, Repo, Commit} = case element(3, Dep) of
  148. {git, R} -> {git, R, master};
  149. {M, R, {branch, C}} -> {M, R, C};
  150. {M, R, {tag, C}} -> {M, R, C};
  151. {M, R, C} -> {M, R, C}
  152. end,
  153. Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
  154. end || Dep <- Deps, tuple_size(Dep) > 2]
  155. end
  156. end(),
  157. fun() ->
  158. First = case lists:keyfind(erl_first_files, 1, Conf) of false -> []; {_, Files} ->
  159. Names = [[" ", begin "lre." ++ Elif = lists:reverse(F), lists:reverse(Elif) end]
  160. || "src/" ++ F <- Files],
  161. Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
  162. end
  163. end(),
  164. FindFirst = fun(F, Fd) ->
  165. case io:parse_erl_form(Fd, undefined) of
  166. {ok, {attribute, _,compile, {parse_transform, PT}}, _} ->
  167. [PT, F(F, Fd)];
  168. {ok, {attribute, _, include, Hrl}, _} ->
  169. case file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]) of
  170. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  171. _ ->
  172. case file:open("$(DEPS_DIR)/$(1)/src/" ++ Hrl, [read]) of
  173. {ok, HrlFd} -> [F(F, HrlFd), F(F, Fd)];
  174. _ -> [F(F, Fd)]
  175. end
  176. end;
  177. {ok, {attribute, _, include_lib, "$(1)/include/" ++ Hrl}, _} ->
  178. {ok, HrlFd} = file:open("$(DEPS_DIR)/$(1)/include/" ++ Hrl, [read]),
  179. [F(F, HrlFd), F(F, Fd)];
  180. {eof, _} ->
  181. file:close(Fd),
  182. [];
  183. _ ->
  184. F(F, Fd)
  185. end
  186. end,
  187. fun() ->
  188. ErlFiles = filelib:wildcard("$(DEPS_DIR)/$(1)/src/*.erl"),
  189. First = lists:usort(lists:flatten([begin
  190. {ok, Fd} = file:open(F, [read]),
  191. FindFirst(FindFirst, Fd)
  192. end || F <- ErlFiles])),
  193. Write(["COMPILE_FIRST +=", [[" ", atom_to_list(M)] || M <- First,
  194. lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)], "\n"])
  195. end(),
  196. PortSpec = fun(Name, {_, Output, Input, [{env, Env}]}) ->
  197. filelib:ensure_dir("$(DEPS_DIR)/$(1)/" ++ Output),
  198. file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile." ++ Name, [
  199. [["override ", K, " = $$$$\(shell echo ", Escape(V), "\)\n"]
  200. || {_, K, V} <- Env],
  201. "\nall:\n\t$$$$\(CC\) $$$$\(CFLAGS\) $$$$\(LDLIBS\) $$$$\(LDFLAGS\) ",
  202. "-o $(DEPS_DIR)/$(1)/", Output,
  203. [[" ../", F] || F <- Input]
  204. ])
  205. end,
  206. fun() ->
  207. case lists:keyfind(port_specs, 1, Conf) of
  208. {_, [{Output, _}]} ->
  209. filelib:ensure_dir("$(DEPS_DIR)/$(1)/" ++ Output),
  210. Write("C_SRC_OUTPUT = " ++ Escape(Output) ++ "\n");
  211. {_, [First, Second]} ->
  212. PortSpec("1", First),
  213. PortSpec("2", Second),
  214. file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile",
  215. "all:\n\t$$$$\(MAKE\) -f Makefile.1\n\t$$$$\(MAKE\) -f Makefile.2\n");
  216. _ -> ok
  217. end
  218. end(),
  219. fun() ->
  220. case lists:keyfind(port_env, 1, Conf) of
  221. {_, Vars} ->
  222. lists:foldl(fun
  223. ({K, V}, Acc) ->
  224. case lists:member(K, Acc) of
  225. true -> Acc;
  226. false ->
  227. Write(K ++ " = $$$$\(shell echo " ++ Escape(V) ++ "\)\n"),
  228. [K|Acc]
  229. end;
  230. ({Regex, K, V}, Acc) ->
  231. case lists:member(K, Acc) of
  232. true -> Acc;
  233. false ->
  234. case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
  235. nomatch -> Acc;
  236. match ->
  237. Write(K ++ " = $$$$\(shell echo " ++ Escape(V) ++ "\)\n"),
  238. [K|Acc]
  239. end
  240. end
  241. end, [], Vars),
  242. Write("CFLAGS += $$$$\(DRV_CFLAGS\)\n"),
  243. Write("CXXFLAGS += $$$$\(DRV_CFLAGS\)\n"),
  244. Write("LDFLAGS += $$$$\(DRV_LDFLAGS\)\n");
  245. _ -> ok
  246. end
  247. end(),
  248. fun() ->
  249. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  250. false -> ok;
  251. true ->
  252. Sources = [[" ./c_src/", S] || S <- filelib:wildcard("*.{c,C,cc,cpp}", "$(DEPS_DIR)/$(1)/c_src")],
  253. Write(io_lib:format("SOURCES := ~s\n", [Sources]))
  254. end
  255. end(),
  256. Write("\n\nrebar_dep: pre-deps deps pre-app app\n"),
  257. Write("\npre-deps::\n"),
  258. Write("\npre-app::\n"),
  259. fun() ->
  260. case lists:keyfind(pre_hooks, 1, Conf) of
  261. false -> ok;
  262. {_, Hooks} ->
  263. [case H of
  264. {'get-deps', Command} ->
  265. Write("\npre-deps::\n\t" ++ Escape(Command) ++ "\n");
  266. {compile, Command} ->
  267. Write("\npre-app::\n\t" ++ Escape(Command) ++ "\n");
  268. {Regex, compile, Command0} ->
  269. case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
  270. match ->
  271. Command = case Command0 of
  272. "make -C" ++ _ -> Escape(Command0);
  273. "gmake -C" ++ _ -> Escape(Command0);
  274. "make " ++ Command1 -> "make -f Makefile.orig.mk " ++ Escape(Command1);
  275. "gmake " ++ Command1 -> "gmake -f Makefile.orig.mk " ++ Escape(Command1);
  276. _ -> Command0
  277. end,
  278. Write("\npre-app::\n\t" ++ Command ++ "\n");
  279. nomatch ->
  280. ok
  281. end;
  282. _ -> ok
  283. end || H <- Hooks]
  284. end
  285. end(),
  286. Write("\ninclude ../../erlang.mk"),
  287. fun() ->
  288. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  289. false -> ok;
  290. true ->
  291. Write("\n\nCFLAGS := $$$$\(filter-out -std=c99 -Wmissing-prototypes,$$$$\(CFLAGS\)\)\n")
  292. end
  293. end(),
  294. fun() ->
  295. case lists:keyfind(plugins, 1, Conf) of
  296. {_, [Plugin]} when is_atom(Plugin) ->
  297. ErlFile = "$(DEPS_DIR)/$(1)/plugins/" ++ atom_to_list(Plugin) ++ ".erl",
  298. try
  299. {ok, PF} = file:read_file(ErlFile),
  300. PF2 = re:replace(PF, "rebar_utils:find_files", "find_files", [global, {return, list}]),
  301. PF3 = PF2 ++ "find_files(Dir, Regex) ->
  302. filelib:fold_files(Dir, Regex, true, fun(F, Acc) -> [F|Acc] end, []).",
  303. ok = file:write_file(ErlFile, PF3),
  304. {ok, Mod, Bin} = compile:file(ErlFile, [binary]),
  305. {module, Mod} = code:load_binary(Mod, ErlFile, Bin),
  306. c:cd("$(DEPS_DIR)/$(1)/"),
  307. ok = Mod:pre_compile(Conf, undefined)
  308. catch _:_ ->
  309. ok
  310. end;
  311. _ -> ok
  312. end
  313. end(),
  314. halt()
  315. endef
  316. define dep_autopatch_appsrc.erl
  317. AppSrcOut = "$(DEPS_DIR)/$(1)/src/$(1).app.src",
  318. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(DEPS_DIR)/$(1)/ebin/$(1).app"; true -> AppSrcOut end,
  319. case filelib:is_regular(AppSrcIn) of
  320. false -> ok;
  321. true ->
  322. fun() ->
  323. {ok, [{application, $(1), L}]} = file:consult(AppSrcIn),
  324. L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end,
  325. L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, "git"}); _ -> L2 end,
  326. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}]))
  327. end(),
  328. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  329. end,
  330. halt()
  331. endef
  332. define dep_fetch
  333. if [ "$$$$VS" = "git" ]; then \
  334. git clone -q -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  335. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  336. elif [ "$$$$VS" = "hg" ]; then \
  337. hg clone -q -U $$$$REPO $(DEPS_DIR)/$(1); \
  338. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  339. elif [ "$$$$VS" = "svn" ]; then \
  340. svn checkout -q $$$$REPO $(DEPS_DIR)/$(1); \
  341. elif [ "$$$$VS" = "cp" ]; then \
  342. cp -R $$$$REPO $(DEPS_DIR)/$(1); \
  343. else \
  344. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  345. exit 78; \
  346. fi
  347. endef
  348. define dep_target
  349. $(DEPS_DIR)/$(1):
  350. @mkdir -p $(DEPS_DIR)
  351. ifeq (,$(dep_$(1)))
  352. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  353. $(dep_verbose) DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  354. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  355. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  356. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  357. $(call dep_fetch,$(1))
  358. else
  359. ifeq (1,$(words $(dep_$(1))))
  360. $(dep_verbose) VS=git; \
  361. REPO=$(dep_$(1)); \
  362. COMMIT=master; \
  363. $(call dep_fetch,$(1))
  364. else
  365. ifeq (2,$(words $(dep_$(1))))
  366. $(dep_verbose) VS=git; \
  367. REPO=$(word 1,$(dep_$(1))); \
  368. COMMIT=$(word 2,$(dep_$(1))); \
  369. $(call dep_fetch,$(1))
  370. else
  371. $(dep_verbose) VS=$(word 1,$(dep_$(1))); \
  372. REPO=$(word 2,$(dep_$(1))); \
  373. COMMIT=$(word 3,$(dep_$(1))); \
  374. $(call dep_fetch,$(1))
  375. endif
  376. endif
  377. endif
  378. @if [ -f $(DEPS_DIR)/$(1)/configure.ac ]; then \
  379. echo " AUTO " $(1); \
  380. cd $(DEPS_DIR)/$(1) && autoreconf -vif; \
  381. fi
  382. -@if [ -f $(DEPS_DIR)/$(1)/configure ]; then \
  383. echo " CONF " $(1); \
  384. cd $(DEPS_DIR)/$(1) && ./configure; \
  385. fi
  386. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  387. @$(call dep_autopatch,$(1))
  388. endif
  389. endef
  390. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  391. distclean-deps:
  392. $(gen_verbose) rm -rf $(DEPS_DIR)
  393. # Packages related targets.
  394. $(PKG_FILE2):
  395. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  396. pkg-list: $(PKG_FILE2)
  397. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  398. "Name:\t\t" $$1 "\n" \
  399. "Repository:\t" $$3 "\n" \
  400. "Website:\t" $$5 "\n" \
  401. "Description:\t" $$6 "\n" }'
  402. ifdef q
  403. pkg-search: $(PKG_FILE2)
  404. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  405. "Name:\t\t" $$1 "\n" \
  406. "Repository:\t" $$3 "\n" \
  407. "Website:\t" $$5 "\n" \
  408. "Description:\t" $$6 "\n" }'
  409. else
  410. pkg-search:
  411. $(error Usage: $(MAKE) pkg-search q=STRING)
  412. endif
  413. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  414. distclean-pkg:
  415. $(gen_verbose) rm -f $(PKG_FILE2)
  416. endif
  417. help::
  418. @printf "%s\n" "" \
  419. "Package-related targets:" \
  420. " pkg-list List all known packages" \
  421. " pkg-search q=STRING Search for STRING in the package index"