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