build: add defaults for PKG_SOURCE, PKG_SOURCE_SUBDIR, PKG_VERSION
[openwrt/staging/lynxis/omap.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 openssl dgst -sha256 $(DL_DIR)/$(1) | awk '{print $$2}')
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 # Skip MD5SUM check in feeds until OpenWrt is updated
98 ifneq ($(filter $(foreach dir,package tools toolchain, $(TOPDIR)/$(dir)/%),$(CURDIR)),)
99 check_md5 = \
100 $(if $(filter-out x,$(1)), \
101 $(call check_warn,md5_deprecated,$(1),$(2),$(3)) \
102 )
103 endif
104
105 hash_var = $(if $(filter-out x,$(1)),MD5SUM,HASH)
106 endif
107
108 define DownloadMethod/unknown
109 @echo "ERROR: No download method available"; false
110 endef
111
112 define DownloadMethod/default
113 $(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "$(HASH)" "$(URL_FILE)" $(foreach url,$(URL),"$(url)") \
114 $(if $(filter check,$(1)), \
115 $(call check_hash,$(FILE),$(HASH),$(2)$(call hash_var,$(MD5SUM))) \
116 $(call check_md5,$(MD5SUM),$(2)MD5SUM,$(2)HASH) \
117 )
118 endef
119
120 define wrap_mirror
121 $(if $(if $(MIRROR),$(filter-out x,$(MIRROR_HASH))),$(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "$(MIRROR_HASH)" "" || ( $(3) ),$(3)) \
122 $(if $(filter check,$(1)), \
123 $(call check_hash,$(FILE),$(MIRROR_HASH),$(2)MIRROR_$(call hash_var,$(MIRROR_MD5SUM))) \
124 $(call check_md5,$(MIRROR_MD5SUM),$(2)MIRROR_MD5SUM,$(2)MIRROR_HASH) \
125 )
126 endef
127
128 define DownloadMethod/cvs
129 $(call wrap_mirror,$(1),$(2), \
130 echo "Checking out files from the cvs repository..."; \
131 mkdir -p $(TMP_DIR)/dl && \
132 cd $(TMP_DIR)/dl && \
133 rm -rf $(SUBDIR) && \
134 [ \! -d $(SUBDIR) ] && \
135 cvs -d $(URL) export $(VERSION) $(SUBDIR) && \
136 echo "Packing checkout..." && \
137 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
138 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
139 rm -rf $(SUBDIR); \
140 )
141 endef
142
143 define DownloadMethod/svn
144 $(call wrap_mirror,$(1),$(2), \
145 echo "Checking out files from the svn repository..."; \
146 mkdir -p $(TMP_DIR)/dl && \
147 cd $(TMP_DIR)/dl && \
148 rm -rf $(SUBDIR) && \
149 [ \! -d $(SUBDIR) ] && \
150 ( svn help export | grep -q trust-server-cert && \
151 svn export --non-interactive --trust-server-cert -r$(VERSION) $(URL) $(SUBDIR) || \
152 svn export --non-interactive -r$(VERSION) $(URL) $(SUBDIR) ) && \
153 echo "Packing checkout..." && \
154 export TAR_TIMESTAMP="" && \
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 define DownloadMethod/git
162 $(call wrap_mirror,$(1),$(2), \
163 echo "Checking out files from the git repository..."; \
164 mkdir -p $(TMP_DIR)/dl && \
165 cd $(TMP_DIR)/dl && \
166 rm -rf $(SUBDIR) && \
167 [ \! -d $(SUBDIR) ] && \
168 git clone $(OPTS) $(URL) $(SUBDIR) && \
169 (cd $(SUBDIR) && git checkout $(VERSION) && \
170 git submodule update --init --recursive) && \
171 echo "Packing checkout..." && \
172 export TAR_TIMESTAMP=`cd $(SUBDIR) && git log -1 --format='@%ct'` && \
173 rm -rf $(SUBDIR)/.git && \
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/bzr
181 $(call wrap_mirror,$(1),$(2), \
182 echo "Checking out files from the bzr repository..."; \
183 mkdir -p $(TMP_DIR)/dl && \
184 cd $(TMP_DIR)/dl && \
185 rm -rf $(SUBDIR) && \
186 [ \! -d $(SUBDIR) ] && \
187 bzr export --per-file-timestamps -r$(VERSION) $(SUBDIR) $(URL) && \
188 echo "Packing checkout..." && \
189 export TAR_TIMESTAMP="" && \
190 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
191 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
192 rm -rf $(SUBDIR); \
193 )
194 endef
195
196 define DownloadMethod/hg
197 $(call wrap_mirror,$(1),$(2), \
198 echo "Checking out files from the hg repository..."; \
199 mkdir -p $(TMP_DIR)/dl && \
200 cd $(TMP_DIR)/dl && \
201 rm -rf $(SUBDIR) && \
202 [ \! -d $(SUBDIR) ] && \
203 hg clone -r $(VERSION) $(URL) $(SUBDIR) && \
204 export TAR_TIMESTAMP=`cd $(SUBDIR) && hg log --template '@{date}' -l 1` && \
205 find $(SUBDIR) -name .hg | xargs rm -rf && \
206 echo "Packing checkout..." && \
207 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
208 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
209 rm -rf $(SUBDIR); \
210 )
211 endef
212
213 define DownloadMethod/darcs
214 $(call wrap_mirror, $(1), $(2), \
215 echo "Checking out files from the darcs repository..."; \
216 mkdir -p $(TMP_DIR)/dl && \
217 cd $(TMP_DIR)/dl && \
218 rm -rf $(SUBDIR) && \
219 [ \! -d $(SUBDIR) ] && \
220 darcs get -t $(VERSION) $(URL) $(SUBDIR) && \
221 export TAR_TIMESTAMP=`cd $(SUBDIR) && LC_ALL=C darcs log --last 1 | sed -ne 's!^Date: \+!!p'` && \
222 find $(SUBDIR) -name _darcs | xargs rm -rf && \
223 echo "Packing checkout..." && \
224 $(call dl_tar_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
225 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
226 rm -rf $(SUBDIR); \
227 )
228 endef
229
230 Validate/cvs=VERSION SUBDIR
231 Validate/svn=VERSION SUBDIR
232 Validate/git=VERSION SUBDIR
233 Validate/bzr=VERSION SUBDIR
234 Validate/hg=VERSION SUBDIR
235 Validate/darcs=VERSION SUBDIR
236
237 define Download/Defaults
238 URL:=
239 FILE:=
240 URL_FILE:=
241 PROTO:=
242 HASH=$$(MD5SUM)
243 MD5SUM:=x
244 SUBDIR:=
245 MIRROR:=1
246 MIRROR_HASH=$$(MIRROR_MD5SUM)
247 MIRROR_MD5SUM:=x
248 VERSION:=
249 OPTS:=
250 endef
251
252 define Download/default
253 FILE:=$(PKG_SOURCE)
254 URL:=$(PKG_SOURCE_URL)
255 SUBDIR:=$(PKG_SOURCE_SUBDIR)
256 PROTO:=$(PKG_SOURCE_PROTO)
257 $(if $(PKG_SOURCE_MIRROR),MIRROR:=$(filter 1,$(PKG_MIRROR)))
258 $(if $(PKG_MIRROR_MD5SUM),MIRROR_MD5SUM:=$(PKG_MIRROR_MD5SUM))
259 $(if $(PKG_MIRROR_HASH),MIRROR_HASH:=$(PKG_MIRROR_HASH))
260 VERSION:=$(PKG_SOURCE_VERSION)
261 $(if $(PKG_MD5SUM),MD5SUM:=$(PKG_MD5SUM))
262 $(if $(PKG_HASH),HASH:=$(PKG_HASH))
263 endef
264
265 define Download
266 $(eval $(Download/Defaults))
267 $(eval $(Download/$(1)))
268 $(foreach FIELD,URL FILE $(Validate/$(call dl_method,$(URL),$(PROTO))),
269 ifeq ($($(FIELD)),)
270 $$(error Download/$(1) is missing the $(FIELD) field.)
271 endif
272 )
273
274 $(foreach dep,$(DOWNLOAD_RDEP),
275 $(dep): $(DL_DIR)/$(FILE)
276 )
277 download: $(DL_DIR)/$(FILE)
278
279 $(DL_DIR)/$(FILE):
280 mkdir -p $(DL_DIR)
281 $(call locked, \
282 $(if $(DownloadMethod/$(call dl_method,$(URL),$(PROTO))), \
283 $(call DownloadMethod/$(call dl_method,$(URL),$(PROTO)),check,$(if $(filter default,$(1)),PKG_,Download/$(1):)), \
284 $(DownloadMethod/unknown) \
285 ),\
286 $(FILE))
287
288 endef