luaposix: bump to version 31
[buildroot-gz.git] / package / pkg-download.mk
blob8e4a1ec33ba634502797660817cee1a31279438b
1 ################################################################################
3 # This file contains the download helpers for the various package
4 # infrastructures. It is used to handle downloads from HTTP servers,
5 # FTP servers, Git repositories, Subversion repositories, Mercurial
6 # repositories, Bazaar repositories, and SCP servers.
8 ################################################################################
10 # Download method commands
11 WGET := $(call qstrip,$(BR2_WGET)) $(QUIET)
12 SVN := $(call qstrip,$(BR2_SVN))
13 CVS := $(call qstrip,$(BR2_CVS))
14 BZR := $(call qstrip,$(BR2_BZR))
15 GIT := $(call qstrip,$(BR2_GIT))
16 HG := $(call qstrip,$(BR2_HG)) $(QUIET)
17 SCP := $(call qstrip,$(BR2_SCP)) $(QUIET)
18 SSH := $(call qstrip,$(BR2_SSH)) $(QUIET)
19 LOCALFILES := $(call qstrip,$(BR2_LOCALFILES))
21 # Default spider mode is 'DOWNLOAD'. Other possible values are 'SOURCE_CHECK'
22 # used by the _source-check target and 'SHOW_EXTERNAL_DEPS', used by the
23 # external-deps target.
24 DL_MODE=DOWNLOAD
26 # Override BR2_DL_DIR if shell variable defined
27 ifneq ($(BUILDROOT_DL_DIR),)
28 DL_DIR := $(BUILDROOT_DL_DIR)
29 else
30 DL_DIR := $(call qstrip,$(BR2_DL_DIR))
31 endif
33 ifeq ($(DL_DIR),)
34 DL_DIR := $(TOPDIR)/dl
35 endif
37 # ensure it exists and a absolute path
38 DL_DIR := $(shell mkdir -p $(DL_DIR) && cd $(DL_DIR) >/dev/null && pwd)
41 # URI scheme helper functions
42 # Example URIs:
43 # * http://www.example.com/dir/file
44 # * scp://www.example.com:dir/file (with domainseparator :)
46 # geturischeme: http
47 geturischeme=$(firstword $(subst ://, ,$(call qstrip,$(1))))
48 # stripurischeme: www.example.com/dir/file
49 stripurischeme=$(lastword $(subst ://, ,$(call qstrip,$(1))))
50 # domain: www.example.com
51 domain=$(firstword $(subst $(call domainseparator,$(2)), ,$(call stripurischeme,$(1))))
52 # notdomain: dir/file
53 notdomain=$(patsubst $(call domain,$(1),$(2))$(call domainseparator,$(2))%,%,$(call stripurischeme,$(1)))
55 # default domainseparator is /, specify alternative value as first argument
56 domainseparator=$(if $(1),$(1),/)
58 ################################################################################
59 # The DOWNLOAD_* helpers are in charge of getting a working copy
60 # of the source repository for their corresponding SCM,
61 # checking out the requested version / commit / tag, and create an
62 # archive out of it. DOWNLOAD_SCP uses scp to obtain a remote file with
63 # ssh authentication. DOWNLOAD_WGET is the normal wget-based download
64 # mechanism.
66 # The SOURCE_CHECK_* helpers are in charge of simply checking that the source
67 # is available for download. This can be used to make sure one will be able
68 # to get all the sources needed for one's build configuration.
70 # The SHOW_EXTERNAL_DEPS_* helpers simply output to the console the names
71 # of the files that will be downloaded, or path and revision of the
72 # source repositories, producing a list of all the "external dependencies"
73 # of a given build configuration.
74 ################################################################################
76 # Try a shallow clone - but that only works if the version is a ref (tag or
77 # branch). Before trying to do a shallow clone we check if $($(PKG)_DL_VERSION)
78 # is in the list provided by git ls-remote. If not we fall back on a full clone.
80 # Messages for the type of clone used are provided to ease debugging in case of
81 # problems
82 define DOWNLOAD_GIT
83 test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
84 (pushd $(DL_DIR) > /dev/null && \
85 ((test "`git ls-remote $($(PKG)_SITE) $($(PKG)_DL_VERSION)`" && \
86 echo "Doing shallow clone" && \
87 $(GIT) clone --depth 1 -b $($(PKG)_DL_VERSION) --bare $($(PKG)_SITE) $($(PKG)_BASE_NAME)) || \
88 (echo "Doing full clone" && \
89 $(GIT) clone --bare $($(PKG)_SITE) $($(PKG)_BASE_NAME))) && \
90 pushd $($(PKG)_BASE_NAME) > /dev/null && \
91 $(GIT) archive --format=tar --prefix=$($(PKG)_BASE_NAME)/ $($(PKG)_DL_VERSION) | \
92 gzip -c > $(DL_DIR)/$($(PKG)_SOURCE) && \
93 popd > /dev/null && \
94 rm -rf $($(PKG)_DL_DIR) && \
95 popd > /dev/null)
96 endef
98 # TODO: improve to check that the given PKG_DL_VERSION exists on the remote
99 # repository
100 define SOURCE_CHECK_GIT
101 $(GIT) ls-remote --heads $($(PKG)_SITE) > /dev/null
102 endef
104 define SHOW_EXTERNAL_DEPS_GIT
105 echo $($(PKG)_SOURCE)
106 endef
109 define DOWNLOAD_BZR
110 test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
111 $(BZR) export $(DL_DIR)/$($(PKG)_SOURCE) $($(PKG)_SITE) -r $($(PKG)_DL_VERSION)
112 endef
114 define SOURCE_CHECK_BZR
115 $(BZR) ls --quiet $($(PKG)_SITE) > /dev/null
116 endef
118 define SHOW_EXTERNAL_DEPS_BZR
119 echo $($(PKG)_SOURCE)
120 endef
122 define DOWNLOAD_CVS
123 test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
124 (pushd $(DL_DIR) > /dev/null && \
125 $(CVS) -z3 -d:pserver:anonymous@$(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) \
126 co -d $($(PKG)_BASE_NAME) -r :$($(PKG)_DL_VERSION) -P $($(PKG)_RAWNAME) && \
127 $(TAR) czf $($(PKG)_SOURCE) $($(PKG)_BASE_NAME)/ && \
128 rm -rf $($(PKG)_DL_DIR) && \
129 popd > /dev/null)
130 endef
132 # Not all CVS servers support ls/rls, use login to see if we can connect
133 define SOURCE_CHECK_CVS
134 $(CVS) -d:pserver:anonymous:@$(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) login
135 endef
137 define SHOW_EXTERNAL_DEPS_CVS
138 echo $($(PKG)_SOURCE)
139 endef
141 define DOWNLOAD_SVN
142 test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
143 (pushd $(DL_DIR) > /dev/null && \
144 $(SVN) export -r $($(PKG)_DL_VERSION) $($(PKG)_SITE) $($(PKG)_DL_DIR) && \
145 $(TAR) czf $($(PKG)_SOURCE) $($(PKG)_BASE_NAME)/ && \
146 rm -rf $($(PKG)_DL_DIR) && \
147 popd > /dev/null)
148 endef
150 define SOURCE_CHECK_SVN
151 $(SVN) ls $($(PKG)_SITE) > /dev/null
152 endef
154 define SHOW_EXTERNAL_DEPS_SVN
155 echo $($(PKG)_SOURCE)
156 endef
158 # SCP URIs should be of the form scp://[user@]host:filepath
159 # Note that filepath is relative to the user's home directory, so you may want
160 # to prepend the path with a slash: scp://[user@]host:/absolutepath
161 define DOWNLOAD_SCP
162 test -e $(DL_DIR)/$(2) || \
163 $(SCP) '$(call stripurischeme,$(call qstrip,$(1)))' $(DL_DIR)/$(2)
164 endef
166 define SOURCE_CHECK_SCP
167 $(SSH) $(call domain,$(1),:) ls '$(call notdomain,$(1),:)' > /dev/null
168 endef
170 define SHOW_EXTERNAL_DEPS_SCP
171 echo $(2)
172 endef
175 define DOWNLOAD_HG
176 test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
177 (pushd $(DL_DIR) > /dev/null && \
178 $(HG) clone --noupdate --rev $($(PKG)_DL_VERSION) $($(PKG)_SITE) $($(PKG)_BASE_NAME) && \
179 $(HG) archive --repository $($(PKG)_BASE_NAME) --type tgz --prefix $($(PKG)_BASE_NAME)/ \
180 --rev $($(PKG)_DL_VERSION) $(DL_DIR)/$($(PKG)_SOURCE) && \
181 rm -rf $($(PKG)_DL_DIR) && \
182 popd > /dev/null)
183 endef
185 # TODO: improve to check that the given PKG_DL_VERSION exists on the remote
186 # repository
187 define SOURCE_CHECK_HG
188 $(HG) incoming --force -l1 $($(PKG)_SITE) > /dev/null
189 endef
191 define SHOW_EXTERNAL_DEPS_HG
192 echo $($(PKG)_SOURCE)
193 endef
195 # Download a file using wget. Only download the file if it doesn't
196 # already exist in the download directory. If the download fails,
197 # remove the file (because wget -O creates a 0-byte file even if the
198 # download fails). To handle an interrupted download as well, download
199 # to a temporary file first. The temporary file will be overwritten
200 # the next time the download is tried.
201 define DOWNLOAD_WGET
202 test -e $(DL_DIR)/$(2) || \
203 ($(WGET) -O $(DL_DIR)/$(2).tmp '$(call qstrip,$(1))' && \
204 mv $(DL_DIR)/$(2).tmp $(DL_DIR)/$(2)) || \
205 (rm -f $(DL_DIR)/$(2).tmp ; exit 1)
206 endef
208 define SOURCE_CHECK_WGET
209 $(WGET) --spider '$(call qstrip,$(1))'
210 endef
212 define SHOW_EXTERNAL_DEPS_WGET
213 echo $(2)
214 endef
216 define DOWNLOAD_LOCALFILES
217 test -e $(DL_DIR)/$(2) || \
218 $(LOCALFILES) $(call stripurischeme,$(call qstrip,$(1))) $(DL_DIR)
219 endef
221 define SOURCE_CHECK_LOCALFILES
222 test -e $(call stripurischeme,$(call qstrip,$(1)))
223 endef
225 define SHOW_EXTERNAL_DEPS_LOCALFILES
226 echo $(2)
227 endef
229 ################################################################################
230 # DOWNLOAD -- Download helper. Will try to download source from:
231 # 1) BR2_PRIMARY_SITE if enabled
232 # 2) Download site, unless BR2_PRIMARY_SITE_ONLY is set
233 # 3) BR2_BACKUP_SITE if enabled, unless BR2_PRIMARY_SITE_ONLY is set
235 # Argument 1 is the source location
236 # Argument 2 is the source filename
238 # E.G. use like this:
239 # $(call DOWNLOAD,$(FOO_SITE),$(FOO_SOURCE))
240 ################################################################################
242 define DOWNLOAD
243 $(call DOWNLOAD_INNER,$(1),$(if $(2),$(2),$(notdir $(1))))
244 endef
246 define DOWNLOAD_INNER
247 $(Q)if test -n "$(call qstrip,$(BR2_PRIMARY_SITE))" ; then \
248 case "$(call geturischeme,$(BR2_PRIMARY_SITE))" in \
249 scp) $(call $(DL_MODE)_SCP,$(BR2_PRIMARY_SITE)/$(2),$(2)) && exit ;; \
250 *) $(call $(DL_MODE)_WGET,$(BR2_PRIMARY_SITE)/$(2),$(2)) && exit ;; \
251 esac ; \
252 fi ; \
253 if test "$(BR2_PRIMARY_SITE_ONLY)" = "y" ; then \
254 exit 1 ; \
255 fi ; \
256 if test -n "$(1)" ; then \
257 if test -z "$($(PKG)_SITE_METHOD)" ; then \
258 scheme="$(call geturischeme,$(1))" ; \
259 else \
260 scheme="$($(PKG)_SITE_METHOD)" ; \
261 fi ; \
262 case "$$scheme" in \
263 git) $($(DL_MODE)_GIT) && exit ;; \
264 svn) $($(DL_MODE)_SVN) && exit ;; \
265 cvs) $($(DL_MODE)_CVS) && exit ;; \
266 bzr) $($(DL_MODE)_BZR) && exit ;; \
267 file) $($(DL_MODE)_LOCALFILES) && exit ;; \
268 scp) $($(DL_MODE)_SCP) && exit ;; \
269 hg) $($(DL_MODE)_HG) && exit ;; \
270 *) $(call $(DL_MODE)_WGET,$(1),$(2)) && exit ;; \
271 esac ; \
272 fi ; \
273 if test -n "$(call qstrip,$(BR2_BACKUP_SITE))" ; then \
274 $(call $(DL_MODE)_WGET,$(BR2_BACKUP_SITE)/$(2),$(2)) && exit ; \
275 fi ; \
276 exit 1
277 endef