1
0

download.mk 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #
  2. # Copyright (C) 2006-2012 OpenWrt.org
  3. # Copyright (C) 2016 LEDE project
  4. #
  5. # This is free software, licensed under the GNU General Public License v2.
  6. # See /LICENSE for more information.
  7. #
  8. OPENWRT_GIT = http://git.openwrt.org
  9. LEDE_GIT = https://git.lede-project.org
  10. ifdef PKG_SOURCE_VERSION
  11. PKG_VERSION ?= $(if $(PKG_SOURCE_DATE),$(PKG_SOURCE_DATE)-)$(call version_abbrev,$(PKG_SOURCE_VERSION))
  12. PKG_SOURCE_SUBDIR ?= $(PKG_NAME)-$(PKG_VERSION)
  13. PKG_SOURCE ?= $(PKG_SOURCE_SUBDIR).tar.xz
  14. endif
  15. DOWNLOAD_RDEP=$(STAMP_PREPARED) $(HOST_STAMP_PREPARED)
  16. # Try to guess the download method from the URL
  17. define dl_method
  18. $(strip \
  19. $(if $(2),$(2), \
  20. $(if $(filter @APACHE/% @GITHUB/% @GNOME/% @GNU/% @KERNEL_LIBRE/% @KERNEL/% @SF/% @SAVANNAH/% ftp://% http://% https://% file://%,$(1)),default, \
  21. $(if $(filter git://%,$(1)),git, \
  22. $(if $(filter svn://%,$(1)),svn, \
  23. $(if $(filter cvs://%,$(1)),cvs, \
  24. $(if $(filter hg://%,$(1)),hg, \
  25. $(if $(filter sftp://%,$(1)),bzr, \
  26. unknown \
  27. ) \
  28. ) \
  29. ) \
  30. ) \
  31. ) \
  32. ) \
  33. ) \
  34. )
  35. endef
  36. # code for creating tarballs from cvs/svn/git/bzr/hg/darcs checkouts - useful for mirror support
  37. dl_pack/bz2=bzip2 -c > $(1)
  38. dl_pack/gz=gzip -nc > $(1)
  39. dl_pack/xz=xz -zc -7e > $(1)
  40. dl_pack/unknown=$(error ERROR: Unknown pack format for file $(1))
  41. define dl_pack
  42. $(if $(dl_pack/$(call ext,$(1))),$(dl_pack/$(call ext,$(1))),$(dl_pack/unknown))
  43. endef
  44. define dl_tar_pack
  45. $(TAR) --numeric-owner --owner=0 --group=0 --sort=name $$$${TAR_TIMESTAMP:+--mtime="$$$$TAR_TIMESTAMP"} -c $(2) | $(call dl_pack,$(1))
  46. endef
  47. ifdef CHECK
  48. check_escape=$(subst ','\'',$(1))
  49. #')
  50. check_warn_nofix = $(info $(shell printf "$(_R)WARNING: %s$(_N)" '$(call check_escape,$(call C_$(1),$(2),$(3),$(4)))'))
  51. ifndef FIXUP
  52. check_warn = $(check_warn_nofix)
  53. else
  54. check_warn = $(if $(filter-out undefined,$(origin F_$(1))),$(filter ,$(shell $(call F_$(1),$(2),$(3),$(4)) >&2)),$(check_warn_nofix))
  55. endif
  56. gen_sha256sum = $(shell mkhash sha256 $(DL_DIR)/$(1))
  57. ifdef FIXUP
  58. F_hash_deprecated = $(SCRIPT_DIR)/fixup-makefile.pl $(CURDIR)/Makefile fix-hash $(3) $(call gen_sha256sum,$(1)) $(2)
  59. F_hash_mismatch = $(F_hash_deprecated)
  60. F_hash_missing = $(SCRIPT_DIR)/fixup-makefile.pl $(CURDIR)/Makefile add-hash $(3) $(call gen_sha256sum,$(1))
  61. endif
  62. C_download_missing = $(1) is missing, please run make download before re-running this check
  63. C_hash_mismatch = $(3) does not match $(1) hash $(call gen_sha256sum,$(1))
  64. C_hash_deprecated = $(3) uses deprecated hash, set to $(call gen_sha256sum,$(1))
  65. C_hash_missing = $(3) is missing, set to $(call gen_sha256sum,$(1))
  66. check_hash = \
  67. $(if $(wildcard $(DL_DIR)/$(1)), \
  68. $(if $(filter-out x,$(2)), \
  69. $(if $(filter 64,$(shell printf '%s' '$(2)' | wc -c)), \
  70. $(if $(filter $(2),$(call gen_sha256sum,$(1))),, \
  71. $(call check_warn,hash_mismatch,$(1),$(2),$(3)) \
  72. ), \
  73. $(call check_warn,hash_deprecated,$(1),$(2),$(3)), \
  74. ), \
  75. $(call check_warn,hash_missing,$(1),$(2),$(3)) \
  76. ), \
  77. $(call check_warn,download_missing,$(1),$(2),$(3)) \
  78. )
  79. ifdef FIXUP
  80. F_md5_deprecated = $(SCRIPT_DIR)/fixup-makefile.pl $(CURDIR)/Makefile rename-var $(2) $(3)
  81. endif
  82. C_md5_deprecated = Use of $(2) is deprecated, switch to $(3)
  83. check_md5 = \
  84. $(if $(filter-out x,$(1)), \
  85. $(call check_warn,md5_deprecated,$(1),$(2),$(3)) \
  86. )
  87. hash_var = $(if $(filter-out x,$(1)),MD5SUM,HASH)
  88. endif
  89. define DownloadMethod/unknown
  90. @echo "ERROR: No download method available"; false
  91. endef
  92. define DownloadMethod/default
  93. $(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "$(HASH)" "$(URL_FILE)" $(foreach url,$(URL),"$(url)") \
  94. $(if $(filter check,$(1)), \
  95. $(call check_hash,$(FILE),$(HASH),$(2)$(call hash_var,$(MD5SUM))) \
  96. $(call check_md5,$(MD5SUM),$(2)MD5SUM,$(2)HASH) \
  97. )
  98. endef
  99. define wrap_mirror
  100. $(if $(if $(MIRROR),$(filter-out x,$(MIRROR_HASH))),$(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "$(MIRROR_HASH)" "" || ( $(3) ),$(3)) \
  101. $(if $(filter check,$(1)), \
  102. $(call check_hash,$(FILE),$(MIRROR_HASH),$(2)MIRROR_$(call hash_var,$(MIRROR_MD5SUM))) \
  103. $(call check_md5,$(MIRROR_MD5SUM),$(2)MIRROR_MD5SUM,$(2)MIRROR_HASH) \
  104. )
  105. endef
  106. define DownloadMethod/cvs
  107. $(call wrap_mirror,$(1),$(2), \
  108. echo "Checking out files from the cvs repository..."; \
  109. mkdir -p $(TMP_DIR)/dl && \
  110. cd $(TMP_DIR)/dl && \
  111. rm -rf $(SUBDIR) && \
  112. [ \! -d $(SUBDIR) ] && \
  113. cvs -d $(URL) export $(VERSION) $(SUBDIR) && \
  114. echo "Packing checkout..." && \
  115. $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  116. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  117. rm -rf $(SUBDIR); \
  118. )
  119. endef
  120. define DownloadMethod/svn
  121. $(call wrap_mirror,$(1),$(2), \
  122. echo "Checking out files from the svn repository..."; \
  123. mkdir -p $(TMP_DIR)/dl && \
  124. cd $(TMP_DIR)/dl && \
  125. rm -rf $(SUBDIR) && \
  126. [ \! -d $(SUBDIR) ] && \
  127. ( svn help export | grep -q trust-server-cert && \
  128. svn export --non-interactive --trust-server-cert -r$(VERSION) $(URL) $(SUBDIR) || \
  129. svn export --non-interactive -r$(VERSION) $(URL) $(SUBDIR) ) && \
  130. echo "Packing checkout..." && \
  131. export TAR_TIMESTAMP="" && \
  132. $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  133. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  134. rm -rf $(SUBDIR); \
  135. )
  136. endef
  137. define DownloadMethod/git
  138. $(call wrap_mirror,$(1),$(2), \
  139. echo "Checking out files from the git repository..."; \
  140. mkdir -p $(TMP_DIR)/dl && \
  141. cd $(TMP_DIR)/dl && \
  142. rm -rf $(SUBDIR) && \
  143. [ \! -d $(SUBDIR) ] && \
  144. git clone $(OPTS) $(URL) $(SUBDIR) && \
  145. (cd $(SUBDIR) && git checkout $(VERSION) && \
  146. git submodule update --init --recursive) && \
  147. echo "Packing checkout..." && \
  148. export TAR_TIMESTAMP=`cd $(SUBDIR) && git log -1 --format='@%ct'` && \
  149. rm -rf $(SUBDIR)/.git && \
  150. $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  151. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  152. rm -rf $(SUBDIR); \
  153. )
  154. endef
  155. define DownloadMethod/bzr
  156. $(call wrap_mirror,$(1),$(2), \
  157. echo "Checking out files from the bzr repository..."; \
  158. mkdir -p $(TMP_DIR)/dl && \
  159. cd $(TMP_DIR)/dl && \
  160. rm -rf $(SUBDIR) && \
  161. [ \! -d $(SUBDIR) ] && \
  162. bzr export --per-file-timestamps -r$(VERSION) $(SUBDIR) $(URL) && \
  163. echo "Packing checkout..." && \
  164. export TAR_TIMESTAMP="" && \
  165. $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  166. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  167. rm -rf $(SUBDIR); \
  168. )
  169. endef
  170. define DownloadMethod/hg
  171. $(call wrap_mirror,$(1),$(2), \
  172. echo "Checking out files from the hg repository..."; \
  173. mkdir -p $(TMP_DIR)/dl && \
  174. cd $(TMP_DIR)/dl && \
  175. rm -rf $(SUBDIR) && \
  176. [ \! -d $(SUBDIR) ] && \
  177. hg clone -r $(VERSION) $(URL) $(SUBDIR) && \
  178. export TAR_TIMESTAMP=`cd $(SUBDIR) && hg log --template '@{date}' -l 1` && \
  179. find $(SUBDIR) -name .hg | xargs rm -rf && \
  180. echo "Packing checkout..." && \
  181. $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  182. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  183. rm -rf $(SUBDIR); \
  184. )
  185. endef
  186. define DownloadMethod/darcs
  187. $(call wrap_mirror, $(1), $(2), \
  188. echo "Checking out files from the darcs repository..."; \
  189. mkdir -p $(TMP_DIR)/dl && \
  190. cd $(TMP_DIR)/dl && \
  191. rm -rf $(SUBDIR) && \
  192. [ \! -d $(SUBDIR) ] && \
  193. darcs get -t $(VERSION) $(URL) $(SUBDIR) && \
  194. export TAR_TIMESTAMP=`cd $(SUBDIR) && LC_ALL=C darcs log --last 1 | sed -ne 's!^Date: \+!!p'` && \
  195. find $(SUBDIR) -name _darcs | xargs rm -rf && \
  196. echo "Packing checkout..." && \
  197. $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
  198. mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
  199. rm -rf $(SUBDIR); \
  200. )
  201. endef
  202. Validate/cvs=VERSION SUBDIR
  203. Validate/svn=VERSION SUBDIR
  204. Validate/git=VERSION SUBDIR
  205. Validate/bzr=VERSION SUBDIR
  206. Validate/hg=VERSION SUBDIR
  207. Validate/darcs=VERSION SUBDIR
  208. define Download/Defaults
  209. URL:=
  210. FILE:=
  211. URL_FILE:=
  212. PROTO:=
  213. HASH=$$(MD5SUM)
  214. MD5SUM:=x
  215. SUBDIR:=
  216. MIRROR:=1
  217. MIRROR_HASH=$$(MIRROR_MD5SUM)
  218. MIRROR_MD5SUM:=x
  219. VERSION:=
  220. OPTS:=
  221. endef
  222. define Download/default
  223. FILE:=$(PKG_SOURCE)
  224. URL:=$(PKG_SOURCE_URL)
  225. SUBDIR:=$(PKG_SOURCE_SUBDIR)
  226. PROTO:=$(PKG_SOURCE_PROTO)
  227. $(if $(PKG_SOURCE_MIRROR),MIRROR:=$(filter 1,$(PKG_MIRROR)))
  228. $(if $(PKG_MIRROR_MD5SUM),MIRROR_MD5SUM:=$(PKG_MIRROR_MD5SUM))
  229. $(if $(PKG_MIRROR_HASH),MIRROR_HASH:=$(PKG_MIRROR_HASH))
  230. VERSION:=$(PKG_SOURCE_VERSION)
  231. $(if $(PKG_MD5SUM),MD5SUM:=$(PKG_MD5SUM))
  232. $(if $(PKG_HASH),HASH:=$(PKG_HASH))
  233. endef
  234. define Download
  235. $(eval $(Download/Defaults))
  236. $(eval $(Download/$(1)))
  237. $(foreach FIELD,URL FILE $(Validate/$(call dl_method,$(URL),$(PROTO))),
  238. ifeq ($($(FIELD)),)
  239. $$(error Download/$(1) is missing the $(FIELD) field.)
  240. endif
  241. )
  242. $(foreach dep,$(DOWNLOAD_RDEP),
  243. $(dep): $(DL_DIR)/$(FILE)
  244. )
  245. download: $(DL_DIR)/$(FILE)
  246. $(DL_DIR)/$(FILE):
  247. mkdir -p $(DL_DIR)
  248. $(call locked, \
  249. $(if $(DownloadMethod/$(call dl_method,$(URL),$(PROTO))), \
  250. $(call DownloadMethod/$(call dl_method,$(URL),$(PROTO)),check,$(if $(filter default,$(1)),PKG_,Download/$(1):)), \
  251. $(DownloadMethod/unknown) \
  252. ),\
  253. $(FILE))
  254. endef