deps.mk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. fun() ->
  165. case lists:keyfind(port_env, 1, Conf) of
  166. {_, Vars} ->
  167. lists:foldl(fun
  168. ({K, V}, Acc) ->
  169. case lists:member(K, Acc) of
  170. true -> Acc;
  171. false ->
  172. Write(K ++ " = $$$$\(shell echo " ++ Escape(V) ++ "\)\n"),
  173. [K|Acc]
  174. end;
  175. ({Regex, K, V}, Acc) ->
  176. case lists:member(K, Acc) of
  177. true -> Acc;
  178. false ->
  179. case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
  180. nomatch -> Acc;
  181. match ->
  182. Write(K ++ " = $$$$\(shell echo " ++ Escape(V) ++ "\)\n"),
  183. [K|Acc]
  184. end
  185. end
  186. end, [], Vars),
  187. Write("CFLAGS += $$$$\(DRV_CFLAGS\)\n"),
  188. Write("CXXFLAGS += $$$$\(DRV_CFLAGS\)\n"),
  189. Write("LDFLAGS += $$$$\(DRV_LDFLAGS\)\n");
  190. _ -> ok
  191. end
  192. end(),
  193. fun() ->
  194. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  195. false -> ok;
  196. true ->
  197. Sources = [[" ./c_src/", S] || S <- filelib:wildcard("*.{c,C,cc,cpp}", "$(DEPS_DIR)/$(1)/c_src")],
  198. Write(io_lib:format("SOURCES := ~s\n", [Sources]))
  199. end
  200. end(),
  201. Write("\n\nrebar_dep: pre-deps deps pre-app app\n"),
  202. Write("\npre-deps::\n"),
  203. Write("\npre-app::\n"),
  204. fun() ->
  205. case lists:keyfind(pre_hooks, 1, Conf) of
  206. false -> ok;
  207. {_, Hooks} ->
  208. [case H of
  209. {'get-deps', Command} ->
  210. Write("\npre-deps::\n\t" ++ Escape(Command) ++ "\n");
  211. {compile, Command} ->
  212. Write("\npre-app::\n\t" ++ Escape(Command) ++ "\n");
  213. {Regex, compile, Command0} ->
  214. case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
  215. match ->
  216. Command = case Command0 of
  217. "make -C" ++ _ -> Escape(Command0);
  218. "gmake -C" ++ _ -> Escape(Command0);
  219. "make " ++ Command1 -> "make -f Makefile.orig.mk " ++ Escape(Command1);
  220. "gmake " ++ Command1 -> "gmake -f Makefile.orig.mk " ++ Escape(Command1);
  221. _ -> Command0
  222. end,
  223. Write("\npre-app::\n\t" ++ Command ++ "\n");
  224. nomatch ->
  225. ok
  226. end;
  227. _ -> ok
  228. end || H <- Hooks]
  229. end
  230. end(),
  231. Write("\ninclude ../../erlang.mk"),
  232. fun() ->
  233. case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
  234. false -> ok;
  235. true ->
  236. Write("\n\nCFLAGS := $$$$\(filter-out -std=c99 -Wmissing-prototypes,$$$$\(CFLAGS\)\)\n")
  237. end
  238. end(),
  239. fun() ->
  240. case lists:keyfind(plugins, 1, Conf) of
  241. {_, [Plugin]} when is_atom(Plugin) ->
  242. ErlFile = "$(DEPS_DIR)/$(1)/plugins/" ++ atom_to_list(Plugin) ++ ".erl",
  243. try
  244. {ok, PF} = file:read_file(ErlFile),
  245. PF2 = re:replace(PF, "rebar_utils:find_files", "find_files", [global, {return, list}]),
  246. PF3 = PF2 ++ "find_files(Dir, Regex) ->
  247. filelib:fold_files(Dir, Regex, true, fun(F, Acc) -> [F|Acc] end, []).",
  248. ok = file:write_file(ErlFile, PF3),
  249. {ok, Mod, Bin} = compile:file(ErlFile, [binary]),
  250. {module, Mod} = code:load_binary(Mod, ErlFile, Bin),
  251. c:cd("$(DEPS_DIR)/$(1)/"),
  252. ok = Mod:pre_compile(Conf, undefined)
  253. catch _:_ ->
  254. ok
  255. end;
  256. _ -> ok
  257. end
  258. end(),
  259. halt()
  260. endef
  261. define dep_autopatch_appsrc.erl
  262. AppSrcOut = "$(DEPS_DIR)/$(1)/src/$(1).app.src",
  263. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(DEPS_DIR)/$(1)/ebin/$(1).app"; true -> AppSrcOut end,
  264. case filelib:is_regular(AppSrcIn) of
  265. false -> ok;
  266. true ->
  267. fun() ->
  268. {ok, [{application, $(1), L}]} = file:consult(AppSrcIn),
  269. L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end,
  270. L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, "git"}); _ -> L2 end,
  271. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}]))
  272. end(),
  273. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  274. end,
  275. halt()
  276. endef
  277. define dep_fetch
  278. if [ "$$$$VS" = "git" ]; then \
  279. git clone -q -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  280. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  281. elif [ "$$$$VS" = "hg" ]; then \
  282. hg clone -q -U $$$$REPO $(DEPS_DIR)/$(1); \
  283. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  284. elif [ "$$$$VS" = "svn" ]; then \
  285. svn checkout -q $$$$REPO $(DEPS_DIR)/$(1); \
  286. elif [ "$$$$VS" = "cp" ]; then \
  287. cp -R $$$$REPO $(DEPS_DIR)/$(1); \
  288. else \
  289. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  290. exit 78; \
  291. fi
  292. endef
  293. define dep_target
  294. $(DEPS_DIR)/$(1):
  295. @mkdir -p $(DEPS_DIR)
  296. ifeq (,$(dep_$(1)))
  297. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  298. $(dep_verbose) DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  299. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  300. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  301. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  302. $(call dep_fetch,$(1))
  303. else
  304. ifeq (1,$(words $(dep_$(1))))
  305. $(dep_verbose) VS=git; \
  306. REPO=$(dep_$(1)); \
  307. COMMIT=master; \
  308. $(call dep_fetch,$(1))
  309. else
  310. ifeq (2,$(words $(dep_$(1))))
  311. $(dep_verbose) VS=git; \
  312. REPO=$(word 1,$(dep_$(1))); \
  313. COMMIT=$(word 2,$(dep_$(1))); \
  314. $(call dep_fetch,$(1))
  315. else
  316. $(dep_verbose) VS=$(word 1,$(dep_$(1))); \
  317. REPO=$(word 2,$(dep_$(1))); \
  318. COMMIT=$(word 3,$(dep_$(1))); \
  319. $(call dep_fetch,$(1))
  320. endif
  321. endif
  322. endif
  323. @if [ -f $(DEPS_DIR)/$(1)/configure.ac ]; then \
  324. echo " AUTO " $(1); \
  325. cd $(DEPS_DIR)/$(1) && autoreconf -vif; \
  326. fi
  327. -@if [ -f $(DEPS_DIR)/$(1)/configure ]; then \
  328. echo " CONF " $(1); \
  329. cd $(DEPS_DIR)/$(1) && ./configure; \
  330. fi
  331. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  332. @$(call dep_autopatch,$(1))
  333. endif
  334. endef
  335. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  336. distclean-deps:
  337. $(gen_verbose) rm -rf $(DEPS_DIR)
  338. # Packages related targets.
  339. $(PKG_FILE2):
  340. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  341. pkg-list: $(PKG_FILE2)
  342. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  343. "Name:\t\t" $$1 "\n" \
  344. "Repository:\t" $$3 "\n" \
  345. "Website:\t" $$5 "\n" \
  346. "Description:\t" $$6 "\n" }'
  347. ifdef q
  348. pkg-search: $(PKG_FILE2)
  349. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  350. "Name:\t\t" $$1 "\n" \
  351. "Repository:\t" $$3 "\n" \
  352. "Website:\t" $$5 "\n" \
  353. "Description:\t" $$6 "\n" }'
  354. else
  355. pkg-search:
  356. $(error Usage: $(MAKE) pkg-search q=STRING)
  357. endif
  358. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  359. distclean-pkg:
  360. $(gen_verbose) rm -f $(PKG_FILE2)
  361. endif
  362. help::
  363. @printf "%s\n" "" \
  364. "Package-related targets:" \
  365. " pkg-list List all known packages" \
  366. " pkg-search q=STRING Search for STRING in the package index"