deps.mk 11 KB

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