deps.mk 4.9 KB

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