include/package.mk: sync default value for hash fallback with mirror hash
[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 define DownloadMethod/unknown
48 @echo "ERROR: No download method available"; false
49 endef
50
51 define DownloadMethod/default
52 $(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "$(HASH)" "$(URL_FILE)" $(foreach url,$(URL),"$(url)")
53 endef
54
55 define wrap_mirror
56 $(if $(if $(MIRROR),$(filter-out x,$(MIRROR_HASH))),$(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "$(MIRROR_HASH)" "" || ( $(1) ),$(1))
57 endef
58
59 define DownloadMethod/cvs
60 $(call wrap_mirror, \
61 echo "Checking out files from the cvs repository..."; \
62 mkdir -p $(TMP_DIR)/dl && \
63 cd $(TMP_DIR)/dl && \
64 rm -rf $(SUBDIR) && \
65 [ \! -d $(SUBDIR) ] && \
66 cvs -d $(URL) export $(VERSION) $(SUBDIR) && \
67 echo "Packing checkout..." && \
68 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
69 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
70 rm -rf $(SUBDIR); \
71 )
72 endef
73
74 define DownloadMethod/svn
75 $(call wrap_mirror, \
76 echo "Checking out files from the svn repository..."; \
77 mkdir -p $(TMP_DIR)/dl && \
78 cd $(TMP_DIR)/dl && \
79 rm -rf $(SUBDIR) && \
80 [ \! -d $(SUBDIR) ] && \
81 ( svn help export | grep -q trust-server-cert && \
82 svn export --non-interactive --trust-server-cert -r$(VERSION) $(URL) $(SUBDIR) || \
83 svn export --non-interactive -r$(VERSION) $(URL) $(SUBDIR) ) && \
84 echo "Packing checkout..." && \
85 export TAR_TIMESTAMP="" && \
86 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
87 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
88 rm -rf $(SUBDIR); \
89 )
90 endef
91
92 define DownloadMethod/git
93 $(call wrap_mirror, \
94 echo "Checking out files from the git repository..."; \
95 mkdir -p $(TMP_DIR)/dl && \
96 cd $(TMP_DIR)/dl && \
97 rm -rf $(SUBDIR) && \
98 [ \! -d $(SUBDIR) ] && \
99 git clone $(OPTS) $(URL) $(SUBDIR) && \
100 (cd $(SUBDIR) && git checkout $(VERSION) && \
101 git submodule update --init --recursive) && \
102 echo "Packing checkout..." && \
103 export TAR_TIMESTAMP=`cd $(SUBDIR) && git log -1 --format='@%ct'` && \
104 rm -rf $(SUBDIR)/.git && \
105 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
106 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
107 rm -rf $(SUBDIR); \
108 )
109 endef
110
111 define DownloadMethod/bzr
112 $(call wrap_mirror, \
113 echo "Checking out files from the bzr repository..."; \
114 mkdir -p $(TMP_DIR)/dl && \
115 cd $(TMP_DIR)/dl && \
116 rm -rf $(SUBDIR) && \
117 [ \! -d $(SUBDIR) ] && \
118 bzr export --per-file-timestamps -r$(VERSION) $(SUBDIR) $(URL) && \
119 echo "Packing checkout..." && \
120 export TAR_TIMESTAMP="" && \
121 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
122 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
123 rm -rf $(SUBDIR); \
124 )
125 endef
126
127 define DownloadMethod/hg
128 $(call wrap_mirror, \
129 echo "Checking out files from the hg repository..."; \
130 mkdir -p $(TMP_DIR)/dl && \
131 cd $(TMP_DIR)/dl && \
132 rm -rf $(SUBDIR) && \
133 [ \! -d $(SUBDIR) ] && \
134 hg clone -r $(VERSION) $(URL) $(SUBDIR) && \
135 export TAR_TIMESTAMP=`cd $(SUBDIR) && hg log --template '@{date}' -l 1` && \
136 find $(SUBDIR) -name .hg | xargs rm -rf && \
137 echo "Packing checkout..." && \
138 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
139 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
140 rm -rf $(SUBDIR); \
141 )
142 endef
143
144 define DownloadMethod/darcs
145 $(call wrap_mirror, \
146 echo "Checking out files from the darcs repository..."; \
147 mkdir -p $(TMP_DIR)/dl && \
148 cd $(TMP_DIR)/dl && \
149 rm -rf $(SUBDIR) && \
150 [ \! -d $(SUBDIR) ] && \
151 darcs get -t $(VERSION) $(URL) $(SUBDIR) && \
152 export TAR_TIMESTAMP=`cd $(SUBDIR) && LC_ALL=C darcs log --last 1 | sed -ne 's!^Date: \+!!p'` && \
153 find $(SUBDIR) -name _darcs | xargs rm -rf && \
154 echo "Packing checkout..." && \
155 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
156 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
157 rm -rf $(SUBDIR); \
158 )
159 endef
160
161 Validate/cvs=VERSION SUBDIR
162 Validate/svn=VERSION SUBDIR
163 Validate/git=VERSION SUBDIR
164 Validate/bzr=VERSION SUBDIR
165 Validate/hg=VERSION SUBDIR
166 Validate/darcs=VERSION SUBDIR
167
168 define Download/Defaults
169 URL:=
170 FILE:=
171 URL_FILE:=
172 PROTO:=
173 HASH=$$(MD5SUM)
174 MD5SUM:=x
175 SUBDIR:=
176 MIRROR:=1
177 MIRROR_HASH=$$(MIRROR_MD5SUM)
178 MIRROR_MD5SUM:=x
179 VERSION:=
180 OPTS:=
181 endef
182
183 define Download/default
184 FILE:=$(PKG_SOURCE)
185 URL:=$(PKG_SOURCE_URL)
186 SUBDIR:=$(PKG_SOURCE_SUBDIR)
187 PROTO:=$(PKG_SOURCE_PROTO)
188 $(if $(PKG_SOURCE_MIRROR),MIRROR:=$(filter 1,$(PKG_MIRROR)))
189 $(if $(PKG_MIRROR_MD5SUM),MIRROR_MD5SUM:=$(PKG_MIRROR_MD5SUM))
190 $(if $(PKG_MIRROR_HASH),MIRROR_HASH:=$(PKG_MIRROR_HASH))
191 VERSION:=$(PKG_SOURCE_VERSION)
192 $(if $(PKG_MD5SUM),MD5SUM:=$(PKG_MD5SUM))
193 $(if $(PKG_HASH),HASH:=$(PKG_HASH))
194 endef
195
196 define Download
197 $(eval $(Download/Defaults))
198 $(eval $(Download/$(1)))
199 $(foreach FIELD,URL FILE $(Validate/$(call dl_method,$(URL),$(PROTO))),
200 ifeq ($($(FIELD)),)
201 $$(error Download/$(1) is missing the $(FIELD) field.)
202 endif
203 )
204
205 $(foreach dep,$(DOWNLOAD_RDEP),
206 $(dep): $(DL_DIR)/$(FILE)
207 )
208 download: $(DL_DIR)/$(FILE)
209
210 $(DL_DIR)/$(FILE):
211 mkdir -p $(DL_DIR)
212 $(call locked,$(if $(DownloadMethod/$(call dl_method,$(URL),$(PROTO))),$(DownloadMethod/$(call dl_method,$(URL),$(PROTO))),$(DownloadMethod/unknown)),$(FILE))
213
214 endef