download.mk 11 KB

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