deps.mk 5.2 KB

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