b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0-only |
| 2 | # |
| 3 | # Copyright (C) 2006-2012 OpenWrt.org |
| 4 | # Copyright (C) 2016 LEDE project |
| 5 | |
| 6 | PROJECT_GIT = https://git.openwrt.org |
| 7 | |
| 8 | OPENWRT_GIT = $(PROJECT_GIT) |
| 9 | LEDE_GIT = $(PROJECT_GIT) |
| 10 | |
| 11 | ifdef 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 |
| 17 | endif |
| 18 | |
| 19 | DOWNLOAD_RDEP=$(STAMP_PREPARED) $(HOST_STAMP_PREPARED) |
| 20 | |
| 21 | # Export options for download.pl |
| 22 | export DOWNLOAD_CHECK_CERTIFICATE:=$(CONFIG_DOWNLOAD_CHECK_CERTIFICATE) |
| 23 | export DOWNLOAD_TOOL_CUSTOM:=$(CONFIG_DOWNLOAD_TOOL_CUSTOM) |
| 24 | |
| 25 | define dl_method_git |
| 26 | $(if $(filter https://github.com/% git://github.com/%,$(1)),github_archive,git) |
| 27 | endef |
| 28 | |
| 29 | # Try to guess the download method from the URL |
| 30 | define 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 | ) |
| 50 | endef |
| 51 | |
| 52 | # code for creating tarballs from cvs/svn/git/bzr/hg/darcs checkouts - useful for mirror support |
| 53 | dl_pack/bz2=bzip2 -c > $(1) |
| 54 | dl_pack/gz=gzip -nc > $(1) |
| 55 | dl_pack/xz=xz -zc -7e > $(1) |
| 56 | dl_pack/zst=zstd -T0 --ultra -20 -c > $(1) |
| 57 | dl_pack/unknown=$(error ERROR: Unknown pack format for file $(1)) |
| 58 | define dl_pack |
| 59 | $(if $(dl_pack/$(call ext,$(1))),$(dl_pack/$(call ext,$(1))),$(dl_pack/unknown)) |
| 60 | endef |
| 61 | define 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)) |
| 64 | endef |
| 65 | |
| 66 | gen_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. |
| 71 | define 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 | ) |
| 79 | endef |
| 80 | |
| 81 | ifdef CHECK |
| 82 | check_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} |
| 89 | check_warn_nofix = $(info $(shell printf "$(_R)WARNING: %s$(_N)" '$(call check_escape,$(call C_$(1),$(2),$(3),$(4)))')) |
| 90 | ifndef FIXUP |
| 91 | check_warn = $(check_warn_nofix) |
| 92 | else |
| 93 | check_warn = $(if $(filter-out undefined,$(origin F_$(1))),$(filter ,$(shell $(call F_$(1),$(2),$(3),$(4)) >&2)),$(check_warn_nofix)) |
| 94 | endif |
| 95 | |
| 96 | ifdef FIXUP |
| 97 | F_hash_deprecated = $(SCRIPT_DIR)/fixup-makefile.pl $(CURDIR)/Makefile fix-hash $(3) $(call gen_sha256sum,$(1)) $(2) |
| 98 | F_hash_mismatch = $(F_hash_deprecated) |
| 99 | F_hash_missing = $(SCRIPT_DIR)/fixup-makefile.pl $(CURDIR)/Makefile add-hash $(3) $(call gen_sha256sum,$(1)) |
| 100 | endif |
| 101 | |
| 102 | # $(1): filename |
| 103 | # $(2): expected hash value |
| 104 | # $(3): var name of the the form: {PKG_,Download/<name>:}{,MIRROR_}{HASH,MIRROR_HASH} |
| 105 | C_download_missing = $(1) is missing, please run make download before re-running this check |
| 106 | C_hash_mismatch = $(3) does not match $(1) hash $(call gen_sha256sum,$(1)) |
| 107 | C_hash_deprecated = $(3) uses deprecated hash, set to $(call gen_sha256sum,$(1)) |
| 108 | C_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} |
| 113 | check_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 | |
| 127 | ifdef FIXUP |
| 128 | F_md5_deprecated = $(SCRIPT_DIR)/fixup-makefile.pl $(CURDIR)/Makefile rename-var $(2) $(3) |
| 129 | endif |
| 130 | |
| 131 | C_md5_deprecated = Use of $(2) is deprecated, switch to $(3) |
| 132 | |
| 133 | check_md5 = \ |
| 134 | $(if $(filter-out x,$(1)), \ |
| 135 | $(call check_warn,md5_deprecated,$(1),$(2),$(3)) \ |
| 136 | ) |
| 137 | |
| 138 | hash_var = $(if $(filter-out x,$(1)),MD5SUM,HASH) |
| 139 | endif |
| 140 | |
| 141 | define DownloadMethod/unknown |
| 142 | echo "ERROR: No download method available"; false |
| 143 | endef |
| 144 | |
| 145 | define 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 | ) |
| 151 | endef |
| 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 |
| 156 | define 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 | ) |
| 162 | endef |
| 163 | |
| 164 | define 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 | ) |
| 177 | endef |
| 178 | |
| 179 | define 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 | ) |
| 195 | endef |
| 196 | |
| 197 | define DownloadMethod/git |
| 198 | $(call wrap_mirror,$(1),$(2), \ |
| 199 | $(call DownloadMethod/rawgit) \ |
| 200 | ) |
| 201 | endef |
| 202 | |
| 203 | define 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 | ) |
| 215 | endef |
| 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) |
| 223 | define 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); |
| 244 | endef |
| 245 | |
| 246 | define 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 | ) |
| 260 | endef |
| 261 | |
| 262 | define 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 | ) |
| 277 | endef |
| 278 | |
| 279 | define 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 | ) |
| 294 | endef |
| 295 | |
| 296 | Validate/cvs=SOURCE_VERSION SUBDIR |
| 297 | Validate/svn=SOURCE_VERSION SUBDIR |
| 298 | Validate/git=SOURCE_VERSION SUBDIR |
| 299 | Validate/bzr=SOURCE_VERSION SUBDIR |
| 300 | Validate/hg=SOURCE_VERSION SUBDIR |
| 301 | Validate/darcs=SOURCE_VERSION SUBDIR |
| 302 | |
| 303 | define 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:= |
| 317 | endef |
| 318 | |
| 319 | define 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)) |
| 332 | endef |
| 333 | |
| 334 | define 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 | |
| 357 | endef |