deps.mk 5.0 KB

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