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