pkg-download.mk 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. ################################################################################
  2. #
  3. # This file contains the download helpers for the various package
  4. # infrastructures. It is used to handle downloads from HTTP servers,
  5. # FTP servers, Git repositories, Subversion repositories, Mercurial
  6. # repositories, Bazaar repositories, and SCP servers.
  7. #
  8. ################################################################################
  9. # Download method commands
  10. WGET := $(call qstrip,$(BR2_WGET)) $(QUIET)
  11. SVN := $(call qstrip,$(BR2_SVN))
  12. CVS := $(call qstrip,$(BR2_CVS))
  13. BZR := $(call qstrip,$(BR2_BZR))
  14. GIT := $(call qstrip,$(BR2_GIT))
  15. HG := $(call qstrip,$(BR2_HG)) $(QUIET)
  16. SCP := $(call qstrip,$(BR2_SCP)) $(QUIET)
  17. SSH := $(call qstrip,$(BR2_SSH)) $(QUIET)
  18. LOCALFILES := $(call qstrip,$(BR2_LOCALFILES))
  19. # Default spider mode is 'DOWNLOAD'. Other possible values are 'SOURCE_CHECK'
  20. # used by the _source-check target and 'SHOW_EXTERNAL_DEPS', used by the
  21. # external-deps target.
  22. DL_MODE=DOWNLOAD
  23. # DL_DIR may have been set already from the environment
  24. ifeq ($(origin DL_DIR),undefined)
  25. DL_DIR ?= $(call qstrip,$(BR2_DL_DIR))
  26. ifeq ($(DL_DIR),)
  27. DL_DIR := $(TOPDIR)/dl
  28. endif
  29. else
  30. # Restore the BR2_DL_DIR that was overridden by the .config file
  31. BR2_DL_DIR = $(DL_DIR)
  32. endif
  33. # ensure it exists and a absolute path
  34. DL_DIR := $(shell mkdir -p $(DL_DIR) && cd $(DL_DIR) >/dev/null && pwd)
  35. #
  36. # URI scheme helper functions
  37. # Example URIs:
  38. # * http://www.example.com/dir/file
  39. # * scp://www.example.com:dir/file (with domainseparator :)
  40. #
  41. # geturischeme: http
  42. geturischeme=$(firstword $(subst ://, ,$(call qstrip,$(1))))
  43. # stripurischeme: www.example.com/dir/file
  44. stripurischeme=$(lastword $(subst ://, ,$(call qstrip,$(1))))
  45. # domain: www.example.com
  46. domain=$(firstword $(subst $(call domainseparator,$(2)), ,$(call stripurischeme,$(1))))
  47. # notdomain: dir/file
  48. notdomain=$(patsubst $(call domain,$(1),$(2))$(call domainseparator,$(2))%,%,$(call stripurischeme,$(1)))
  49. #
  50. # default domainseparator is /, specify alternative value as first argument
  51. domainseparator=$(if $(1),$(1),/)
  52. # github(user,package,version): returns site of github repository
  53. github = https://github.com/$(1)/$(2)/tarball/$(3)
  54. ################################################################################
  55. # The DOWNLOAD_* helpers are in charge of getting a working copy
  56. # of the source repository for their corresponding SCM,
  57. # checking out the requested version / commit / tag, and create an
  58. # archive out of it. DOWNLOAD_SCP uses scp to obtain a remote file with
  59. # ssh authentication. DOWNLOAD_WGET is the normal wget-based download
  60. # mechanism.
  61. #
  62. # The SOURCE_CHECK_* helpers are in charge of simply checking that the source
  63. # is available for download. This can be used to make sure one will be able
  64. # to get all the sources needed for one's build configuration.
  65. #
  66. # The SHOW_EXTERNAL_DEPS_* helpers simply output to the console the names
  67. # of the files that will be downloaded, or path and revision of the
  68. # source repositories, producing a list of all the "external dependencies"
  69. # of a given build configuration.
  70. ################################################################################
  71. # Try a shallow clone - but that only works if the version is a ref (tag or
  72. # branch). Before trying to do a shallow clone we check if $($(PKG)_DL_VERSION)
  73. # is in the list provided by git ls-remote. If not we fall back on a full clone.
  74. #
  75. # Messages for the type of clone used are provided to ease debugging in case of
  76. # problems
  77. define DOWNLOAD_GIT
  78. test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
  79. (pushd $(DL_DIR) > /dev/null && \
  80. ((test "`git ls-remote $($(PKG)_SITE) $($(PKG)_DL_VERSION)`" && \
  81. echo "Doing shallow clone" && \
  82. $(GIT) clone --depth 1 -b $($(PKG)_DL_VERSION) --bare $($(PKG)_SITE) $($(PKG)_BASE_NAME)) || \
  83. (echo "Doing full clone" && \
  84. $(GIT) clone --bare $($(PKG)_SITE) $($(PKG)_BASE_NAME))) && \
  85. pushd $($(PKG)_BASE_NAME) > /dev/null && \
  86. $(GIT) archive --format=tar --prefix=$($(PKG)_BASE_NAME)/ -o $(DL_DIR)/.$($(PKG)_SOURCE).tmp $($(PKG)_DL_VERSION) && \
  87. gzip -c $(DL_DIR)/.$($(PKG)_SOURCE).tmp > $(DL_DIR)/$($(PKG)_SOURCE) && \
  88. rm -f $(DL_DIR)/.$($(PKG)_SOURCE).tmp && \
  89. popd > /dev/null && \
  90. rm -rf $($(PKG)_DL_DIR) && \
  91. popd > /dev/null)
  92. endef
  93. # TODO: improve to check that the given PKG_DL_VERSION exists on the remote
  94. # repository
  95. define SOURCE_CHECK_GIT
  96. $(GIT) ls-remote --heads $($(PKG)_SITE) > /dev/null
  97. endef
  98. define SHOW_EXTERNAL_DEPS_GIT
  99. echo $($(PKG)_SOURCE)
  100. endef
  101. define DOWNLOAD_BZR
  102. test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
  103. $(BZR) export $(DL_DIR)/$($(PKG)_SOURCE) $($(PKG)_SITE) -r $($(PKG)_DL_VERSION)
  104. endef
  105. define SOURCE_CHECK_BZR
  106. $(BZR) ls --quiet $($(PKG)_SITE) > /dev/null
  107. endef
  108. define SHOW_EXTERNAL_DEPS_BZR
  109. echo $($(PKG)_SOURCE)
  110. endef
  111. define DOWNLOAD_CVS
  112. test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
  113. (pushd $(DL_DIR) > /dev/null && \
  114. $(CVS) -z3 -d:pserver:anonymous@$(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) \
  115. co -d $($(PKG)_BASE_NAME) -r :$($(PKG)_DL_VERSION) -P $($(PKG)_RAWNAME) && \
  116. $(TAR) czf $($(PKG)_SOURCE) $($(PKG)_BASE_NAME)/ && \
  117. rm -rf $($(PKG)_DL_DIR) && \
  118. popd > /dev/null)
  119. endef
  120. # Not all CVS servers support ls/rls, use login to see if we can connect
  121. define SOURCE_CHECK_CVS
  122. $(CVS) -d:pserver:anonymous:@$(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) login
  123. endef
  124. define SHOW_EXTERNAL_DEPS_CVS
  125. echo $($(PKG)_SOURCE)
  126. endef
  127. define DOWNLOAD_SVN
  128. test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
  129. (pushd $(DL_DIR) > /dev/null && \
  130. $(SVN) export -r $($(PKG)_DL_VERSION) $($(PKG)_SITE) $($(PKG)_DL_DIR) && \
  131. $(TAR) czf $($(PKG)_SOURCE) $($(PKG)_BASE_NAME)/ && \
  132. rm -rf $($(PKG)_DL_DIR) && \
  133. popd > /dev/null)
  134. endef
  135. define SOURCE_CHECK_SVN
  136. $(SVN) ls $($(PKG)_SITE) > /dev/null
  137. endef
  138. define SHOW_EXTERNAL_DEPS_SVN
  139. echo $($(PKG)_SOURCE)
  140. endef
  141. # SCP URIs should be of the form scp://[user@]host:filepath
  142. # Note that filepath is relative to the user's home directory, so you may want
  143. # to prepend the path with a slash: scp://[user@]host:/absolutepath
  144. define DOWNLOAD_SCP
  145. test -e $(DL_DIR)/$(2) || \
  146. $(SCP) '$(call stripurischeme,$(call qstrip,$(1)))' $(DL_DIR)/$(2)
  147. endef
  148. define SOURCE_CHECK_SCP
  149. $(SSH) $(call domain,$(1),:) ls '$(call notdomain,$(1),:)' > /dev/null
  150. endef
  151. define SHOW_EXTERNAL_DEPS_SCP
  152. echo $(2)
  153. endef
  154. define DOWNLOAD_HG
  155. test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
  156. (pushd $(DL_DIR) > /dev/null && \
  157. rm -rf $($(PKG)_BASE_NAME) && \
  158. $(HG) clone --noupdate --rev $($(PKG)_DL_VERSION) $($(PKG)_SITE) $($(PKG)_BASE_NAME) && \
  159. $(HG) archive --repository $($(PKG)_BASE_NAME) --type tgz --prefix $($(PKG)_BASE_NAME)/ \
  160. --rev $($(PKG)_DL_VERSION) $(DL_DIR)/$($(PKG)_SOURCE) && \
  161. rm -rf $($(PKG)_DL_DIR) && \
  162. popd > /dev/null)
  163. endef
  164. # TODO: improve to check that the given PKG_DL_VERSION exists on the remote
  165. # repository
  166. define SOURCE_CHECK_HG
  167. $(HG) incoming --force -l1 $($(PKG)_SITE) > /dev/null
  168. endef
  169. define SHOW_EXTERNAL_DEPS_HG
  170. echo $($(PKG)_SOURCE)
  171. endef
  172. # Download a file using wget. Only download the file if it doesn't
  173. # already exist in the download directory. If the download fails,
  174. # remove the file (because wget -O creates a 0-byte file even if the
  175. # download fails). To handle an interrupted download as well, download
  176. # to a temporary file first. The temporary file will be overwritten
  177. # the next time the download is tried.
  178. define DOWNLOAD_WGET
  179. test -e $(DL_DIR)/$(2) || \
  180. ($(WGET) -O $(DL_DIR)/$(2).tmp '$(call qstrip,$(1))' && \
  181. mv $(DL_DIR)/$(2).tmp $(DL_DIR)/$(2)) || \
  182. (rm -f $(DL_DIR)/$(2).tmp ; exit 1)
  183. endef
  184. define SOURCE_CHECK_WGET
  185. $(WGET) --spider '$(call qstrip,$(1))'
  186. endef
  187. define SHOW_EXTERNAL_DEPS_WGET
  188. echo $(2)
  189. endef
  190. define DOWNLOAD_LOCALFILES
  191. test -e $(DL_DIR)/$(2) || \
  192. $(LOCALFILES) $(call stripurischeme,$(call qstrip,$(1))) $(DL_DIR)
  193. endef
  194. define SOURCE_CHECK_LOCALFILES
  195. test -e $(call stripurischeme,$(call qstrip,$(1)))
  196. endef
  197. define SHOW_EXTERNAL_DEPS_LOCALFILES
  198. echo $(2)
  199. endef
  200. ################################################################################
  201. # DOWNLOAD -- Download helper. Will try to download source from:
  202. # 1) BR2_PRIMARY_SITE if enabled
  203. # 2) Download site, unless BR2_PRIMARY_SITE_ONLY is set
  204. # 3) BR2_BACKUP_SITE if enabled, unless BR2_PRIMARY_SITE_ONLY is set
  205. #
  206. # Argument 1 is the source location
  207. # Argument 2 is the source filename
  208. #
  209. # E.G. use like this:
  210. # $(call DOWNLOAD,$(FOO_SITE),$(FOO_SOURCE))
  211. ################################################################################
  212. define DOWNLOAD
  213. $(call DOWNLOAD_INNER,$(1),$(if $(2),$(2),$(notdir $(1))))
  214. endef
  215. define DOWNLOAD_INNER
  216. $(Q)if test -n "$(call qstrip,$(BR2_PRIMARY_SITE))" ; then \
  217. case "$(call geturischeme,$(BR2_PRIMARY_SITE))" in \
  218. scp) $(call $(DL_MODE)_SCP,$(BR2_PRIMARY_SITE)/$(2),$(2)) && exit ;; \
  219. *) $(call $(DL_MODE)_WGET,$(BR2_PRIMARY_SITE)/$(2),$(2)) && exit ;; \
  220. esac ; \
  221. fi ; \
  222. if test "$(BR2_PRIMARY_SITE_ONLY)" = "y" ; then \
  223. exit 1 ; \
  224. fi ; \
  225. if test -n "$(1)" ; then \
  226. if test -z "$($(PKG)_SITE_METHOD)" ; then \
  227. scheme="$(call geturischeme,$(1))" ; \
  228. else \
  229. scheme="$($(PKG)_SITE_METHOD)" ; \
  230. fi ; \
  231. case "$$scheme" in \
  232. git) $($(DL_MODE)_GIT) && exit ;; \
  233. svn) $($(DL_MODE)_SVN) && exit ;; \
  234. cvs) $($(DL_MODE)_CVS) && exit ;; \
  235. bzr) $($(DL_MODE)_BZR) && exit ;; \
  236. file) $($(DL_MODE)_LOCALFILES) && exit ;; \
  237. scp) $($(DL_MODE)_SCP) && exit ;; \
  238. hg) $($(DL_MODE)_HG) && exit ;; \
  239. *) $(call $(DL_MODE)_WGET,$(1),$(2)) && exit ;; \
  240. esac ; \
  241. fi ; \
  242. if test -n "$(call qstrip,$(BR2_BACKUP_SITE))" ; then \
  243. $(call $(DL_MODE)_WGET,$(BR2_BACKUP_SITE)/$(2),$(2)) && exit ; \
  244. fi ; \
  245. exit 1
  246. endef