Updated post r19769, integrates the missing dl_method bzr hunk from #6370, uses the...
[openwrt/openwrt.git] / include / download.mk
1 #
2 # Copyright (C) 2006-2010 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 DOWNLOAD_RDEP=$(STAMP_PREPARED) $(HOST_STAMP_PREPARED)
9
10 # Try to guess the download method from the URL
11 define dl_method
12 $(strip \
13 $(if $(2),$(2), \
14 $(if $(filter @GNOME/% @GNU/% @KERNEL/% @SF/% ftp://% http://% file://%,$(1)),default, \
15 $(if $(filter git://%,$(1)),git, \
16 $(if $(filter svn://%,$(1)),svn, \
17 $(if $(filter cvs://%,$(1)),cvs, \
18 $(if $(filter hg://%,$(1)),hg, \
19 $(if $(filter sftp://%,$(1)),bzr, \
20 unknown \
21 ) \
22 ) \
23 ) \
24 ) \
25 ) \
26 ) \
27 ) \
28 )
29 endef
30
31 # code for creating tarballs from cvs/svn/git/bzr/hg checkouts - useful for mirror support
32 dl_pack/bz2=$(TAR) cfj $(1) $(2)
33 dl_pack/gz=$(TAR) cfz $(1) $(2)
34 dl_pack/unknown=echo "ERROR: Unknown pack format for file $(1)"; false
35 define dl_pack
36 $(if $(dl_pack/$(call ext,$(1))),$(dl_pack/$(call ext,$(1))),$(dl_pack/unknown))
37 endef
38
39 define DownloadMethod/unknown
40 @echo "ERROR: No download method available"; false
41 endef
42
43 define DownloadMethod/default
44 $(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "$(MD5SUM)" $(URL)
45 endef
46
47 define wrap_mirror
48 $(if $(MIRROR),@$(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(FILE)" "x" || ( $(1) ),$(1))
49 endef
50
51 define DownloadMethod/cvs
52 $(call wrap_mirror, \
53 echo "Checking out files from the cvs repository..."; \
54 mkdir -p $(TMP_DIR)/dl && \
55 cd $(TMP_DIR)/dl && \
56 rm -rf $(SUBDIR) && \
57 [ \! -d $(SUBDIR) ] && \
58 cvs -d $(URL) export $(VERSION) $(SUBDIR) && \
59 echo "Packing checkout..." && \
60 $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
61 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
62 rm -rf $(SUBDIR); \
63 )
64 endef
65
66 define DownloadMethod/svn
67 $(call wrap_mirror, \
68 echo "Checking out files from the svn repository..."; \
69 mkdir -p $(TMP_DIR)/dl && \
70 cd $(TMP_DIR)/dl && \
71 rm -rf $(SUBDIR) && \
72 [ \! -d $(SUBDIR) ] && \
73 svn export --non-interactive -r$(VERSION) $(URL) $(SUBDIR) && \
74 echo "Packing checkout..." && \
75 $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
76 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
77 rm -rf $(SUBDIR); \
78 )
79 endef
80
81 define DownloadMethod/git
82 $(call wrap_mirror, \
83 echo "Checking out files from the git repository..."; \
84 mkdir -p $(TMP_DIR)/dl && \
85 cd $(TMP_DIR)/dl && \
86 rm -rf $(SUBDIR) && \
87 [ \! -d $(SUBDIR) ] && \
88 git clone $(URL) $(SUBDIR) && \
89 (cd $(SUBDIR) && git checkout $(VERSION)) && \
90 echo "Packing checkout..." && \
91 rm -rf $(SUBDIR)/.git && \
92 $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
93 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
94 rm -rf $(SUBDIR); \
95 )
96 endef
97
98 define DownloadMethod/bzr
99 $(call wrap_mirror, \
100 echo "Checking out files from the bzr repository..."; \
101 mkdir -p $(TMP_DIR)/dl && \
102 cd $(TMP_DIR)/dl && \
103 rm -rf $(SUBDIR) && \
104 [ \! -d $(SUBDIR) ] && \
105 bzr export -r$(VERSION) $(URL) $(SUBDIR) && \
106 echo "Packing checkout..." && \
107 $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
108 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ &&
109 rm -rf $(SUBDIR); \
110 )
111 endef
112
113 define DownloadMethod/hg
114 $(call wrap_mirror, \
115 echo "Checking out files from the hg repository..."; \
116 mkdir -p $(TMP_DIR)/dl && \
117 cd $(TMP_DIR)/dl && \
118 rm -rf $(SUBDIR) && \
119 [ \! -d $(SUBDIR) ] && \
120 hg clone -r $(VERSION) $(URL) $(SUBDIR) && \
121 find $(SUBDIR) -name .hg | xargs rm -rf && \
122 echo "Packing checkout..." && \
123 $(call dl_pack,$(TMP_DIR)/dl/$(FILE),$(SUBDIR)) && \
124 mv $(TMP_DIR)/dl/$(FILE) $(DL_DIR)/ && \
125 rm -rf $(SUBDIR); \
126 )
127 endef
128
129 Validate/cvs=VERSION SUBDIR
130 Validate/svn=VERSION SUBDIR
131 Validate/git=VERSION SUBDIR
132 Validate/bzr=VERSION SUBDIR
133 Validate/hg=VERSION SUBDIR
134
135 define Download/Defaults
136 URL:=
137 FILE:=
138 PROTO:=
139 MD5SUM:=
140 SUBDIR:=
141 MIRROR:=1
142 VERSION:=
143 endef
144
145 define Download
146 $(eval $(Download/Defaults))
147 $(eval $(Download/$(1)))
148 $(foreach FIELD,URL FILE $(Validate/$(call dl_method,$(URL),$(PROTO))),
149 ifeq ($($(FIELD)),)
150 $$(error Download/$(1) is missing the $(FIELD) field.)
151 endif
152 )
153
154 $(foreach dep,$(DOWNLOAD_RDEP),
155 $(dep): $(DL_DIR)/$(FILE)
156 )
157 download: $(DL_DIR)/$(FILE)
158
159 $(DL_DIR)/$(FILE):
160 mkdir -p $(DL_DIR)
161 $(if $(DownloadMethod/$(call dl_method,$(URL),$(PROTO))),$(DownloadMethod/$(call dl_method,$(URL),$(PROTO))),$(DownloadMethod/unknown))
162
163 endef