deps.mk 5.0 KB

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