aboutsummaryrefslogtreecommitdiff
path: root/package/pkg-download.mk
blob: 5e745195c1dbe0ae505c11f0d26fef99da0dffc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
################################################################################
#
# This file contains the download helpers for the various package
# infrastructures. It is used to handle downloads from HTTP servers,
# FTP servers, Git repositories, Subversion repositories, Mercurial
# repositories, Bazaar repositories, and SCP servers.
#
################################################################################

# Download method commands
export WGET := $(call qstrip,$(BR2_WGET))
export SVN := $(call qstrip,$(BR2_SVN))
export CVS := $(call qstrip,$(BR2_CVS))
export BZR := $(call qstrip,$(BR2_BZR))
export GIT := $(call qstrip,$(BR2_GIT))
export HG := $(call qstrip,$(BR2_HG))
export SCP := $(call qstrip,$(BR2_SCP))
SSH := $(call qstrip,$(BR2_SSH))
export LOCALFILES := $(call qstrip,$(BR2_LOCALFILES))

DL_WRAPPER = support/download/dl-wrapper

# Default spider mode is 'DOWNLOAD'. Other possible values are 'SOURCE_CHECK'
# used by the _source-check target and 'SHOW_EXTERNAL_DEPS', used by the
# external-deps target.
DL_MODE = DOWNLOAD

# DL_DIR may have been set already from the environment
ifeq ($(origin DL_DIR),undefined)
DL_DIR ?= $(call qstrip,$(BR2_DL_DIR))
ifeq ($(DL_DIR),)
DL_DIR := $(TOPDIR)/dl
endif
else
# Restore the BR2_DL_DIR that was overridden by the .config file
BR2_DL_DIR = $(DL_DIR)
endif

# ensure it exists and a absolute path
DL_DIR := $(shell mkdir -p $(DL_DIR) && cd $(DL_DIR) >/dev/null && pwd)

#
# URI scheme helper functions
# Example URIs:
# * http://www.example.com/dir/file
# * scp://www.example.com:dir/file (with domainseparator :)
#
# geturischeme: http
geturischeme = $(firstword $(subst ://, ,$(call qstrip,$(1))))
# stripurischeme: www.example.com/dir/file
stripurischeme = $(lastword $(subst ://, ,$(call qstrip,$(1))))
# domain: www.example.com
domain = $(firstword $(subst $(call domainseparator,$(2)), ,$(call stripurischeme,$(1))))
# notdomain: dir/file
notdomain = $(patsubst $(call domain,$(1),$(2))$(call domainseparator,$(2))%,%,$(call stripurischeme,$(1)))
#
# default domainseparator is /, specify alternative value as first argument
domainseparator = $(if $(1),$(1),/)

# github(user,package,version): returns site of GitHub repository
github = https://github.com/$(1)/$(2)/archive/$(3)

################################################################################
# The DOWNLOAD_* helpers are in charge of getting a working copy
# of the source repository for their corresponding SCM,
# checking out the requested version / commit / tag, and create an
# archive out of it. DOWNLOAD_SCP uses scp to obtain a remote file with
# ssh authentication. DOWNLOAD_WGET is the normal wget-based download
# mechanism.
#
# The SOURCE_CHECK_* helpers are in charge of simply checking that the source
# is available for download. This can be used to make sure one will be able
# to get all the sources needed for one's build configuration.
#
# The SHOW_EXTERNAL_DEPS_* helpers simply output to the console the names
# of the files that will be downloaded, or path and revision of the
# source repositories, producing a list of all the "external dependencies"
# of a given build configuration.
################################################################################

# Try a shallow clone - but that only works if the version is a ref (tag or
# branch). Before trying to do a shallow clone we check if $($(PKG)_DL_VERSION)
# is in the list provided by git ls-remote. If not we fall back on a full clone.
#
# Messages for the type of clone used are provided to ease debugging in case of
# problems
define DOWNLOAD_GIT
	$(EXTRA_ENV) $(DL_WRAPPER) -b git \
		-o $(DL_DIR)/$($(PKG)_SOURCE) \
		-H $(PKGDIR)/$($(PKG)_RAWNAME).hash \
		$(QUIET) \
		-- \
		$($(PKG)_SITE) \
		$($(PKG)_DL_VERSION) \
		$($(PKG)_BASE_NAME)
endef

# TODO: improve to check that the given PKG_DL_VERSION exists on the remote
# repository
define SOURCE_CHECK_GIT
  $(GIT) ls-remote --heads $($(PKG)_SITE) > /dev/null
endef

define SHOW_EXTERNAL_DEPS_GIT
	echo $($(PKG)_SOURCE)
endef


define DOWNLOAD_BZR
	$(EXTRA_ENV) $(DL_WRAPPER) -b bzr \
		-o $(DL_DIR)/$($(PKG)_SOURCE) \
		-H $(PKGDIR)/$($(PKG)_RAWNAME).hash \
		$(QUIET) \
		-- \
		$($(PKG)_SITE) \
		$($(PKG)_DL_VERSION) \
		$($(PKG)_BASE_NAME)
endef

define SOURCE_CHECK_BZR
	$(BZR) ls --quiet $($(PKG)_SITE) > /dev/null
endef

define SHOW_EXTERNAL_DEPS_BZR
	echo $($(PKG)_SOURCE)
endef

define DOWNLOAD_CVS
	$(EXTRA_ENV) $(DL_WRAPPER) -b cvs \
		-o $(DL_DIR)/$($(PKG)_SOURCE) \
		-H $(PKGDIR)/$($(PKG)_RAWNAME).hash \
		$(QUIET) \
		-- \
		$(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) \
		$($(PKG)_DL_VERSION) \
		$($(PKG)_RAWNAME) \
		$($(PKG)_BASE_NAME)
endef

# Not all CVS servers support ls/rls, use login to see if we can connect
define SOURCE_CHECK_CVS
	$(CVS) -d:pserver:anonymous:@$(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) login
endef

define SHOW_EXTERNAL_DEPS_CVS
	echo $($(PKG)_SOURCE)
endef

define DOWNLOAD_SVN
	$(EXTRA_ENV) $(DL_WRAPPER) -b svn \
		-o $(DL_DIR)/$($(PKG)_SOURCE) \
		-H $(PKGDIR)/$($(PKG)_RAWNAME).hash \
		$(QUIET) \
		-- \
		$($(PKG)_SITE) \
		$($(PKG)_DL_VERSION) \
		$($(PKG)_BASE_NAME)
endef

define SOURCE_CHECK_SVN
  $(SVN) ls $($(PKG)_SITE)@$($(PKG)_DL_VERSION) > /dev/null
endef

define SHOW_EXTERNAL_DEPS_SVN
  echo $($(PKG)_SOURCE)
endef

# SCP URIs should be of the form scp://[user@]host:filepath
# Note that filepath is relative to the user's home directory, so you may want
# to prepend the path with a slash: scp://[user@]host:/absolutepath
define DOWNLOAD_SCP
	$(EXTRA_ENV) $(DL_WRAPPER) -b scp \
		-o $(DL_DIR)/$(2) \
		-H $(PKGDIR)/$($(PKG)_RAWNAME).hash \
		$(QUIET) \
		-- \
		'$(call stripurischeme,$(call qstrip,$(1)))'
endef

define SOURCE_CHECK_SCP
	$(SSH) $(call domain,$(1),:) ls '$(call notdomain,$(1),:)' > /dev/null
endef

define SHOW_EXTERNAL_DEPS_SCP
	echo $(2)
endef


define DOWNLOAD_HG
	$(EXTRA_ENV) $(DL_WRAPPER) -b hg \
		-o $(DL_DIR)/$($(PKG)_SOURCE) \
		-H $(PKGDIR)/$($(PKG)_RAWNAME).hash \
		$(QUIET) \
		-- \
		$($(PKG)_SITE) \
		$($(PKG)_DL_VERSION) \
		$($(PKG)_BASE_NAME)
endef

# TODO: improve to check that the given PKG_DL_VERSION exists on the remote
# repository
define SOURCE_CHECK_HG
  $(HG) incoming --force -l1 $($(PKG)_SITE) > /dev/null
endef

define SHOW_EXTERNAL_DEPS_HG
  echo $($(PKG)_SOURCE)
endef


define DOWNLOAD_WGET
	$(EXTRA_ENV) $(DL_WRAPPER) -b wget \
		-o $(DL_DIR)/$(2) \
		-H $(PKGDIR)/$($(PKG)_RAWNAME).hash \
		$(QUIET) \
		-- \
		'$(call qstrip,$(1))'
endef

define SOURCE_CHECK_WGET
  $(WGET) --spider '$(call qstrip,$(1))'
endef

define SHOW_EXTERNAL_DEPS_WGET
  echo $(2)
endef

define DOWNLOAD_LOCALFILES
	$(EXTRA_ENV) $(DL_WRAPPER) -b cp \
		-o $(DL_DIR)/$(2) \
		-H $(PKGDIR)/$($(PKG)_RAWNAME).hash \
		$(QUIET) \
		-- \
		$(call stripurischeme,$(call qstrip,$(1)))
endef

define SOURCE_CHECK_LOCALFILES
  test -e $(call stripurischeme,$(call qstrip,$(1)))
endef

define SHOW_EXTERNAL_DEPS_LOCALFILES
  echo $(2)
endef

################################################################################
# DOWNLOAD -- Download helper. Will try to download source from:
# 1) BR2_PRIMARY_SITE if enabled
# 2) Download site, unless BR2_PRIMARY_SITE_ONLY is set
# 3) BR2_BACKUP_SITE if enabled, unless BR2_PRIMARY_SITE_ONLY is set
#
# Argument 1 is the source location
#
# E.G. use like this:
# $(call DOWNLOAD,$(FOO_SITE))
################################################################################

define DOWNLOAD
	$(call DOWNLOAD_INNER,$(1),$(notdir $(1)))
endef

define DOWNLOAD_INNER
	$(Q)if test -n "$(call qstrip,$(BR2_PRIMARY_SITE))" ; then \
		case "$(call geturischeme,$(BR2_PRIMARY_SITE))" in \
			scp) $(call $(DL_MODE)_SCP,$(BR2_PRIMARY_SITE)/$(2),$(2)) && exit ;; \
			*) $(call $(DL_MODE)_WGET,$(BR2_PRIMARY_SITE)/$(2),$(2)) && exit ;; \
		esac ; \
	fi ; \
	if test "$(BR2_PRIMARY_SITE_ONLY)" = "y" ; then \
		exit 1 ; \
	fi ; \
	if test -n "$(1)" ; then \
		if test -z "$($(PKG)_SITE_METHOD)" ; then \
			scheme="$(call geturischeme,$(1))" ; \
		else \
			scheme="$($(PKG)_SITE_METHOD)" ; \
		fi ; \
		case "$$scheme" in \
			git) $($(DL_MODE)_GIT) && exit ;; \
			svn) $($(DL_MODE)_SVN) && exit ;; \
			cvs) $($(DL_MODE)_CVS) && exit ;; \
			bzr) $($(DL_MODE)_BZR) && exit ;; \
			file) $($(DL_MODE)_LOCALFILES) && exit ;; \
			scp) $($(DL_MODE)_SCP) && exit ;; \
			hg) $($(DL_MODE)_HG) && exit ;; \
			*) $(call $(DL_MODE)_WGET,$(1),$(2)) && exit ;; \
		esac ; \
	fi ; \
	if test -n "$(call qstrip,$(BR2_BACKUP_SITE))" ; then \
		$(call $(DL_MODE)_WGET,$(BR2_BACKUP_SITE)/$(2),$(2)) && exit ; \
	fi ; \
	exit 1
endef