1
0

download.mk 10 KB

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