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