]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - package/pkg-download.mk
infra: replace BUILDROOT_DL_DIR with BR2_DL_DIR.
[coffee/buildroot.git] / package / pkg-download.mk
1 ################################################################################
2 #
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.
7 #
8 ################################################################################
9
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))
20
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
25
26 # DL_DIR may have been set already from the environment
27 DL_DIR ?= $(call qstrip,$(BR2_DL_DIR))
28 ifeq ($(DL_DIR),)
29 DL_DIR := $(TOPDIR)/dl
30 endif
31
32 # ensure it exists and a absolute path
33 DL_DIR := $(shell mkdir -p $(DL_DIR) && cd $(DL_DIR) >/dev/null && pwd)
34
35 #
36 # URI scheme helper functions
37 # Example URIs:
38 # * http://www.example.com/dir/file
39 # * scp://www.example.com:dir/file (with domainseparator :)
40 #
41 # geturischeme: http
42 geturischeme=$(firstword $(subst ://, ,$(call qstrip,$(1))))
43 # stripurischeme: www.example.com/dir/file
44 stripurischeme=$(lastword $(subst ://, ,$(call qstrip,$(1))))
45 # domain: www.example.com
46 domain=$(firstword $(subst $(call domainseparator,$(2)), ,$(call stripurischeme,$(1))))
47 # notdomain: dir/file
48 notdomain=$(patsubst $(call domain,$(1),$(2))$(call domainseparator,$(2))%,%,$(call stripurischeme,$(1)))
49 #
50 # default domainseparator is /, specify alternative value as first argument
51 domainseparator=$(if $(1),$(1),/)
52
53 # github(user,package,version): returns site of github repository
54 github = https://github.com/$(1)/$(2)/tarball/$(3)
55
56 ################################################################################
57 # The DOWNLOAD_* helpers are in charge of getting a working copy
58 # of the source repository for their corresponding SCM,
59 # checking out the requested version / commit / tag, and create an
60 # archive out of it. DOWNLOAD_SCP uses scp to obtain a remote file with
61 # ssh authentication. DOWNLOAD_WGET is the normal wget-based download
62 # mechanism.
63 #
64 # The SOURCE_CHECK_* helpers are in charge of simply checking that the source
65 # is available for download. This can be used to make sure one will be able
66 # to get all the sources needed for one's build configuration.
67 #
68 # The SHOW_EXTERNAL_DEPS_* helpers simply output to the console the names
69 # of the files that will be downloaded, or path and revision of the
70 # source repositories, producing a list of all the "external dependencies"
71 # of a given build configuration.
72 ################################################################################
73
74 # Try a shallow clone - but that only works if the version is a ref (tag or
75 # branch). Before trying to do a shallow clone we check if $($(PKG)_DL_VERSION)
76 # is in the list provided by git ls-remote. If not we fall back on a full clone.
77 #
78 # Messages for the type of clone used are provided to ease debugging in case of
79 # problems
80 define DOWNLOAD_GIT
81         test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
82         (pushd $(DL_DIR) > /dev/null && \
83          ((test "`git ls-remote $($(PKG)_SITE) $($(PKG)_DL_VERSION)`" && \
84            echo "Doing shallow clone" && \
85            $(GIT) clone --depth 1 -b $($(PKG)_DL_VERSION) --bare $($(PKG)_SITE) $($(PKG)_BASE_NAME)) || \
86           (echo "Doing full clone" && \
87            $(GIT) clone --bare $($(PKG)_SITE) $($(PKG)_BASE_NAME))) && \
88         pushd $($(PKG)_BASE_NAME) > /dev/null && \
89         $(GIT) archive --format=tar --prefix=$($(PKG)_BASE_NAME)/ -o $(DL_DIR)/.$($(PKG)_SOURCE).tmp $($(PKG)_DL_VERSION) && \
90         gzip -c $(DL_DIR)/.$($(PKG)_SOURCE).tmp > $(DL_DIR)/$($(PKG)_SOURCE) && \
91         rm -f $(DL_DIR)/.$($(PKG)_SOURCE).tmp && \
92         popd > /dev/null && \
93         rm -rf $($(PKG)_DL_DIR) && \
94         popd > /dev/null)
95 endef
96
97 # TODO: improve to check that the given PKG_DL_VERSION exists on the remote
98 # repository
99 define SOURCE_CHECK_GIT
100   $(GIT) ls-remote --heads $($(PKG)_SITE) > /dev/null
101 endef
102
103 define SHOW_EXTERNAL_DEPS_GIT
104         echo $($(PKG)_SOURCE)
105 endef
106
107
108 define DOWNLOAD_BZR
109         test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
110         $(BZR) export $(DL_DIR)/$($(PKG)_SOURCE) $($(PKG)_SITE) -r $($(PKG)_DL_VERSION)
111 endef
112
113 define SOURCE_CHECK_BZR
114         $(BZR) ls --quiet $($(PKG)_SITE) > /dev/null
115 endef
116
117 define SHOW_EXTERNAL_DEPS_BZR
118         echo $($(PKG)_SOURCE)
119 endef
120
121 define DOWNLOAD_CVS
122         test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
123         (pushd $(DL_DIR) > /dev/null && \
124         $(CVS) -z3 -d:pserver:anonymous@$(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) \
125                 co -d $($(PKG)_BASE_NAME) -r :$($(PKG)_DL_VERSION) -P $($(PKG)_RAWNAME) && \
126         $(TAR) czf $($(PKG)_SOURCE) $($(PKG)_BASE_NAME)/ && \
127         rm -rf $($(PKG)_DL_DIR) && \
128         popd > /dev/null)
129 endef
130
131 # Not all CVS servers support ls/rls, use login to see if we can connect
132 define SOURCE_CHECK_CVS
133         $(CVS) -d:pserver:anonymous:@$(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) login
134 endef
135
136 define SHOW_EXTERNAL_DEPS_CVS
137         echo $($(PKG)_SOURCE)
138 endef
139
140 define DOWNLOAD_SVN
141         test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
142         (pushd $(DL_DIR) > /dev/null && \
143         $(SVN) export -r $($(PKG)_DL_VERSION) $($(PKG)_SITE) $($(PKG)_DL_DIR) && \
144         $(TAR) czf $($(PKG)_SOURCE) $($(PKG)_BASE_NAME)/ && \
145         rm -rf $($(PKG)_DL_DIR) && \
146         popd > /dev/null)
147 endef
148
149 define SOURCE_CHECK_SVN
150   $(SVN) ls $($(PKG)_SITE) > /dev/null
151 endef
152
153 define SHOW_EXTERNAL_DEPS_SVN
154   echo $($(PKG)_SOURCE)
155 endef
156
157 # SCP URIs should be of the form scp://[user@]host:filepath
158 # Note that filepath is relative to the user's home directory, so you may want
159 # to prepend the path with a slash: scp://[user@]host:/absolutepath
160 define DOWNLOAD_SCP
161         test -e $(DL_DIR)/$(2) || \
162         $(SCP) '$(call stripurischeme,$(call qstrip,$(1)))' $(DL_DIR)/$(2)
163 endef
164
165 define SOURCE_CHECK_SCP
166         $(SSH) $(call domain,$(1),:) ls '$(call notdomain,$(1),:)' > /dev/null
167 endef
168
169 define SHOW_EXTERNAL_DEPS_SCP
170         echo $(2)
171 endef
172
173
174 define DOWNLOAD_HG
175         test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
176         (pushd $(DL_DIR) > /dev/null && \
177         rm -rf $($(PKG)_BASE_NAME) && \
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
184
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
190
191 define SHOW_EXTERNAL_DEPS_HG
192   echo $($(PKG)_SOURCE)
193 endef
194
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
207
208 define SOURCE_CHECK_WGET
209   $(WGET) --spider '$(call qstrip,$(1))'
210 endef
211
212 define SHOW_EXTERNAL_DEPS_WGET
213   echo $(2)
214 endef
215
216 define DOWNLOAD_LOCALFILES
217         test -e $(DL_DIR)/$(2) || \
218                 $(LOCALFILES) $(call stripurischeme,$(call qstrip,$(1))) $(DL_DIR)
219 endef
220
221 define SOURCE_CHECK_LOCALFILES
222   test -e $(call stripurischeme,$(call qstrip,$(1)))
223 endef
224
225 define SHOW_EXTERNAL_DEPS_LOCALFILES
226   echo $(2)
227 endef
228
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
234 #
235 # Argument 1 is the source location
236 # Argument 2 is the source filename
237 #
238 # E.G. use like this:
239 # $(call DOWNLOAD,$(FOO_SITE),$(FOO_SOURCE))
240 ################################################################################
241
242 define DOWNLOAD
243         $(call DOWNLOAD_INNER,$(1),$(if $(2),$(2),$(notdir $(1))))
244 endef
245
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