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