deps.mk 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 || 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. define dep_autopatch.erl
  43. DepDir = "$(DEPS_DIR)/$(1)/",
  44. fun() ->
  45. {ok, Conf} = case file:consult(DepDir ++ "rebar.config") of
  46. {error, enoent} -> {ok, []}; Res -> Res end,
  47. File = case lists:keyfind(deps, 1, Conf) of false -> []; {_, Deps} ->
  48. [begin {Method, Repo, Commit} = case Repos of
  49. {git, R} -> {git, R, master};
  50. {M, R, {branch, C}} -> {M, R, C};
  51. {M, R, {tag, C}} -> {M, R, C};
  52. {M, R, C} -> {M, R, C}
  53. end,
  54. io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit])
  55. end || {Name, _, Repos} <- Deps]
  56. end,
  57. First = case lists:keyfind(erl_first_files, 1, Conf) of false -> []; {_, Files} ->
  58. Names = [[" ", begin "lre." ++ R = lists:reverse(F), lists:reverse(R) end]
  59. || "src/" ++ F <- Files],
  60. io_lib:format("COMPILE_FIRST +=~s\n", [Names])
  61. end,
  62. ok = file:write_file("$(DEPS_DIR)/$(1)/Makefile", ["ERLC_OPTS = +debug_info\n\n", File, First, "\ninclude erlang.mk"])
  63. end(),
  64. AppSrcOut = "$(DEPS_DIR)/$(1)/src/$(1).app.src",
  65. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(DEPS_DIR)/$(1)/ebin/$(1).app"; true -> AppSrcOut end,
  66. fun() ->
  67. {ok, [{application, $(1), L}]} = file:consult(AppSrcIn),
  68. L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end,
  69. L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, "git"}); _ -> L2 end,
  70. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}]))
  71. end(),
  72. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end,
  73. halt().
  74. endef
  75. ifeq ($(V),0)
  76. define dep_autopatch_verbose
  77. @echo " PATCH " $(1);
  78. endef
  79. endif
  80. define dep_fetch
  81. if [ "$$$$VS" = "git" ]; then \
  82. git clone -q -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  83. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  84. elif [ "$$$$VS" = "hg" ]; then \
  85. hg clone -q -U $$$$REPO $(DEPS_DIR)/$(1); \
  86. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  87. elif [ "$$$$VS" = "svn" ]; then \
  88. svn checkout -q $$$$REPO $(DEPS_DIR)/$(1); \
  89. elif [ "$$$$VS" = "cp" ]; then \
  90. cp -R $$$$REPO $(DEPS_DIR)/$(1); \
  91. else \
  92. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  93. exit 78; \
  94. fi
  95. endef
  96. define dep_target
  97. $(DEPS_DIR)/$(1):
  98. @mkdir -p $(DEPS_DIR)
  99. ifeq (,$(dep_$(1)))
  100. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  101. $(dep_verbose) DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  102. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  103. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  104. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  105. $(call dep_fetch,$(1))
  106. else
  107. $(dep_verbose) VS=$(word 1,$(dep_$(1))); \
  108. REPO=$(word 2,$(dep_$(1))); \
  109. COMMIT=$(word 3,$(dep_$(1))); \
  110. $(call dep_fetch,$(1))
  111. endif
  112. ifneq ($(filter $(1),$(AUTOPATCH)),)
  113. $(call dep_autopatch_verbose,$(1)) \
  114. $(call erlang,$(call dep_autopatch.erl,$(1))); \
  115. cd $(DEPS_DIR)/$(1)/ && ln -s ../../erlang.mk
  116. endif
  117. endef
  118. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  119. distclean-deps:
  120. $(gen_verbose) rm -rf $(DEPS_DIR)
  121. # Packages related targets.
  122. $(PKG_FILE2):
  123. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  124. pkg-list: $(PKG_FILE2)
  125. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  126. "Name:\t\t" $$1 "\n" \
  127. "Repository:\t" $$3 "\n" \
  128. "Website:\t" $$5 "\n" \
  129. "Description:\t" $$6 "\n" }'
  130. ifdef q
  131. pkg-search: $(PKG_FILE2)
  132. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  133. "Name:\t\t" $$1 "\n" \
  134. "Repository:\t" $$3 "\n" \
  135. "Website:\t" $$5 "\n" \
  136. "Description:\t" $$6 "\n" }'
  137. else
  138. pkg-search:
  139. $(error Usage: make pkg-search q=STRING)
  140. endif
  141. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  142. distclean-pkg:
  143. $(gen_verbose) rm -f $(PKG_FILE2)
  144. endif
  145. help::
  146. @printf "%s\n" "" \
  147. "Package-related targets:" \
  148. " pkg-list List all known packages" \
  149. " pkg-search q=STRING Search for STRING in the package index"