download.mk 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #
  2. # Copyright (C) 2006-2012 OpenWrt.org
  3. #
  4. # This is free software, licensed under the GNU General Public License v2.
  5. # See /LICENSE for more information.
  6. #
  7. OPENWRT_GIT = http://git.openwrt.org
  8. ifdef PKG_SOURCE_VERSION
  9. PKG_VERSION ?= $(if $(PKG_SOURCE_DATE),$(PKG_SOURCE_DATE)-)$(call version_abbrev,$(PKG_SOURCE_VERSION))
  10. PKG_SOURCE_SUBDIR ?= $(PKG_NAME)-$(PKG_VERSION)
  11. PKG_SOURCE ?= $(PKG_SOURCE_SUBDIR).tar.xz
  12. endif
  13. DOWNLOAD_RDEP=$(STAMP_PREPARED) $(HOST_STAMP_PREPARED)
  14. # Try to guess the download method from the URL
  15. define dl_method
  16. $(strip \
  17. $(if $(2),$(2), \
  18. $(if $(filter @APACHE/% @GITHUB/% @GNOME/% @GNU/% @KERNEL/% @SF/% @SAVANNAH/% ftp://% http://% https://% file://%,$(1)),default, \
  19. $(if $(filter git://%,$(1)),git, \
  20. $(if $(filter svn://%,$(1)),svn, \
  21. $(if $(filter cvs://%,$(1)),cvs, \
  22. $(if $(filter hg://%,$(1)),hg, \
  23. $(if $(filter sftp://%,$(1)),bzr, \
  24. unknown \
  25. ) \
  26. ) \
  27. ) \
  28. ) \
  29. ) \
  30. ) \
  31. ) \
  32. )
  33. endef
  34. # code for creating tarballs from cvs/svn/git/bzr/hg/darcs checkouts - useful for mirror support
  35. dl_pack/bz2=$(TAR) cjf $(1) $(2)
  36. dl_pack/gz=$(TAR) czf $(1) $(2)
  37. dl_pack/xz=$(TAR) c $(2) | xz -zc > $(1)
  38. dl_pack/unknown=echo "ERROR: Unknown pack format for file $(1)"; false
  39. define dl_pack
  40. $(if $(dl_pack/$(call ext,$(1))),$(dl_pack/$(call ext,$(1))),$(dl_pack/unknown))
  41. endef
  42. define DownloadMethod/unknown
  43. @echo "ERROR: No download method available"; false
  44. endef
  45. define DownloadMethod/default
  46. $(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "$(HASH)" "$(URL_FILE)" $(foreach url,$(URL),"$(url)")
  47. endef
  48. define wrap_mirror
  49. $(if $(if $(MIRROR),$(filter-out x,$(MIRROR_HASH))),$(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "$(MIRROR_HASH)" "" || ( $(1) ),$(1))
  50. endef
  51. define DownloadMethod/cvs
  52. $(call wrap_mirror, \
  53. echo "Checking out files from the cvs repository..."; \
  54. mkdir -p $(TMP_DIR)/dl && \
  55. cd $(TMP_DIR)/dl && \
  56. rm -rf $(SUBDIR) && \
  57. [ \! -d $(SUBDIR) ] && \
  58. cvs -d $(URL) export $(VERSION) $(SUBDIR) && \
  59. echo "Packing checkout..." && \
  60. $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  61. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  62. rm -rf $(SUBDIR); \
  63. )
  64. endef
  65. define DownloadMethod/svn
  66. $(call wrap_mirror, \
  67. echo "Checking out files from the svn repository..."; \
  68. mkdir -p $(TMP_DIR)/dl && \
  69. cd $(TMP_DIR)/dl && \
  70. rm -rf $(SUBDIR) && \
  71. [ \! -d $(SUBDIR) ] && \
  72. ( svn help export | grep -q trust-server-cert && \
  73. svn export --non-interactive --trust-server-cert -r$(VERSION) $(URL) $(SUBDIR) || \
  74. svn export --non-interactive -r$(VERSION) $(URL) $(SUBDIR) ) && \
  75. echo "Packing checkout..." && \
  76. $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  77. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  78. rm -rf $(SUBDIR); \
  79. )
  80. endef
  81. define DownloadMethod/git
  82. $(call wrap_mirror, \
  83. echo "Checking out files from the git repository..."; \
  84. mkdir -p $(TMP_DIR)/dl && \
  85. cd $(TMP_DIR)/dl && \
  86. rm -rf $(SUBDIR) && \
  87. [ \! -d $(SUBDIR) ] && \
  88. git clone $(URL) $(SUBDIR) && \
  89. (cd $(SUBDIR) && git checkout $(VERSION) && \
  90. git submodule update --init --recursive) && \
  91. echo "Packing checkout..." && \
  92. rm -rf $(SUBDIR)/.git && \
  93. $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  94. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  95. rm -rf $(SUBDIR); \
  96. )
  97. endef
  98. define DownloadMethod/bzr
  99. $(call wrap_mirror, \
  100. echo "Checking out files from the bzr repository..."; \
  101. mkdir -p $(TMP_DIR)/dl && \
  102. cd $(TMP_DIR)/dl && \
  103. rm -rf $(SUBDIR) && \
  104. [ \! -d $(SUBDIR) ] && \
  105. bzr export -r$(VERSION) $(SUBDIR) $(URL) && \
  106. echo "Packing checkout..." && \
  107. $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  108. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  109. rm -rf $(SUBDIR); \
  110. )
  111. endef
  112. define DownloadMethod/hg
  113. $(call wrap_mirror, \
  114. echo "Checking out files from the hg repository..."; \
  115. mkdir -p $(TMP_DIR)/dl && \
  116. cd $(TMP_DIR)/dl && \
  117. rm -rf $(SUBDIR) && \
  118. [ \! -d $(SUBDIR) ] && \
  119. hg clone -r $(VERSION) $(URL) $(SUBDIR) && \
  120. find $(SUBDIR) -name .hg | xargs rm -rf && \
  121. echo "Packing checkout..." && \
  122. $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  123. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  124. rm -rf $(SUBDIR); \
  125. )
  126. endef
  127. define DownloadMethod/darcs
  128. $(call wrap_mirror, \
  129. echo "Checking out files from the darcs repository..."; \
  130. mkdir -p $(TMP_DIR)/dl && \
  131. cd $(TMP_DIR)/dl && \
  132. rm -rf $(SUBDIR) && \
  133. [ \! -d $(SUBDIR) ] && \
  134. darcs get -t $(VERSION) $(URL) $(SUBDIR) && \
  135. find $(SUBDIR) -name _darcs | xargs rm -rf && \
  136. echo "Packing checkout..." && \
  137. $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  138. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  139. rm -rf $(SUBDIR); \
  140. )
  141. endef
  142. Validate/cvs=VERSION SUBDIR
  143. Validate/svn=VERSION SUBDIR
  144. Validate/git=VERSION SUBDIR
  145. Validate/bzr=VERSION SUBDIR
  146. Validate/hg=VERSION SUBDIR
  147. Validate/darcs=VERSION SUBDIR
  148. define Download/Defaults
  149. URL:=
  150. FILE:=
  151. URL_FILE:=
  152. PROTO:=
  153. HASH=$$(MD5SUM)
  154. MD5SUM:=x
  155. SUBDIR:=
  156. MIRROR:=1
  157. MIRROR_HASH=$$(MIRROR_MD5SUM)
  158. MIRROR_MD5SUM:=x
  159. VERSION:=
  160. endef
  161. define Download/default
  162. FILE:=$(PKG_SOURCE)
  163. URL:=$(PKG_SOURCE_URL)
  164. SUBDIR:=$(PKG_SOURCE_SUBDIR)
  165. PROTO:=$(PKG_SOURCE_PROTO)
  166. $(if $(PKG_SOURCE_MIRROR),MIRROR:=$(filter 1,$(PKG_MIRROR)))
  167. $(if $(PKG_MIRROR_MD5SUM),MIRROR_MD5SUM:=$(PKG_MIRROR_MD5SUM))
  168. $(if $(PKG_MIRROR_HASH),MIRROR_HASH:=$(PKG_MIRROR_HASH))
  169. VERSION:=$(PKG_SOURCE_VERSION)
  170. $(if $(PKG_MD5SUM),MD5SUM:=$(PKG_MD5SUM))
  171. $(if $(PKG_HASH),HASH:=$(PKG_HASH))
  172. endef
  173. define Download
  174. $(eval $(Download/Defaults))
  175. $(eval $(Download/$(1)))
  176. $(foreach FIELD,URL FILE $(Validate/$(call dl_method,$(URL),$(PROTO))),
  177. ifeq ($($(FIELD)),)
  178. $$(error Download/$(1) is missing the $(FIELD) field.)
  179. endif
  180. )
  181. $(foreach dep,$(DOWNLOAD_RDEP),
  182. $(dep): $(DL_DIR)/$(FILE)
  183. )
  184. download: $(DL_DIR)/$(FILE)
  185. $(DL_DIR)/$(FILE):
  186. mkdir -p $(DL_DIR)
  187. $(call locked,$(if $(DownloadMethod/$(call dl_method,$(URL),$(PROTO))),$(DownloadMethod/$(call dl_method,$(URL),$(PROTO))),$(DownloadMethod/unknown)),$(FILE))
  188. endef