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