blob: d2fea4fcc2ac516f5358747fefb224b6848f7ccb [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# Copyright 2006-2008 OpenedHand Ltd.
3#
4
5inherit package
6
7IMAGE_PKGTYPE ?= "deb"
8
9DPKG_ARCH ?= "${TARGET_ARCH}"
10
11PKGWRITEDIRDEB = "${WORKDIR}/deploy-debs"
12
13APTCONF_TARGET = "${WORKDIR}"
14
15APT_ARGS = "${@['', '--no-install-recommends'][d.getVar("NO_RECOMMENDATIONS", True) == "1"]}"
16
17#
18# install a bunch of packages using apt
19# the following shell variables needs to be set before calling this func:
20# INSTALL_ROOTFS_DEB - install root dir
21# INSTALL_BASEARCH_DEB - install base architecutre
22# INSTALL_ARCHS_DEB - list of available archs
23# INSTALL_PACKAGES_NORMAL_DEB - packages to be installed
24# INSTALL_PACKAGES_ATTEMPTONLY_DEB - packages attemped to be installed only
25# INSTALL_PACKAGES_LINGUAS_DEB - additional packages for uclibc
26# INSTALL_TASK_DEB - task name
27
28python do_package_deb () {
29 import re, copy
30 import textwrap
31 import subprocess
32
33 workdir = d.getVar('WORKDIR', True)
34 if not workdir:
35 bb.error("WORKDIR not defined, unable to package")
36 return
37
38 outdir = d.getVar('PKGWRITEDIRDEB', True)
39 if not outdir:
40 bb.error("PKGWRITEDIRDEB not defined, unable to package")
41 return
42
43 packages = d.getVar('PACKAGES', True)
44 if not packages:
45 bb.debug(1, "PACKAGES not defined, nothing to package")
46 return
47
48 tmpdir = d.getVar('TMPDIR', True)
49
50 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
51 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
52
53 if packages == []:
54 bb.debug(1, "No packages; nothing to do")
55 return
56
57 pkgdest = d.getVar('PKGDEST', True)
58
59 def cleanupcontrol(root):
60 for p in ['CONTROL', 'DEBIAN']:
61 p = os.path.join(root, p)
62 if os.path.exists(p):
63 bb.utils.prunedir(p)
64
65 for pkg in packages.split():
66 localdata = bb.data.createCopy(d)
67 root = "%s/%s" % (pkgdest, pkg)
68
69 lf = bb.utils.lockfile(root + ".lock")
70
71 localdata.setVar('ROOT', '')
72 localdata.setVar('ROOT_%s' % pkg, root)
73 pkgname = localdata.getVar('PKG_%s' % pkg, True)
74 if not pkgname:
75 pkgname = pkg
76 localdata.setVar('PKG', pkgname)
77
78 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
79
80 bb.data.update_data(localdata)
81 basedir = os.path.join(os.path.dirname(root))
82
83 pkgoutdir = os.path.join(outdir, localdata.getVar('PACKAGE_ARCH', True))
84 bb.utils.mkdirhier(pkgoutdir)
85
86 os.chdir(root)
87 cleanupcontrol(root)
88 from glob import glob
89 g = glob('*')
90 if not g and localdata.getVar('ALLOW_EMPTY', False) != "1":
91 bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV', True), localdata.getVar('PKGR', True)))
92 bb.utils.unlockfile(lf)
93 continue
94
95 controldir = os.path.join(root, 'DEBIAN')
96 bb.utils.mkdirhier(controldir)
97 os.chmod(controldir, 0755)
98 try:
99 import codecs
100 ctrlfile = codecs.open(os.path.join(controldir, 'control'), 'w', 'utf-8')
101 except OSError:
102 bb.utils.unlockfile(lf)
103 raise bb.build.FuncFailed("unable to open control file for writing.")
104
105 fields = []
106 pe = d.getVar('PKGE', True)
107 if pe and int(pe) > 0:
108 fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']])
109 else:
110 fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']])
111 fields.append(["Description: %s\n", ['DESCRIPTION']])
112 fields.append(["Section: %s\n", ['SECTION']])
113 fields.append(["Priority: %s\n", ['PRIORITY']])
114 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
115 fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
116 fields.append(["OE: %s\n", ['PN']])
117 fields.append(["PackageArch: %s\n", ['PACKAGE_ARCH']])
118 if d.getVar('HOMEPAGE', True):
119 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
120
121 # Package, Version, Maintainer, Description - mandatory
122 # Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
123
124
125 def pullData(l, d):
126 l2 = []
127 for i in l:
128 data = d.getVar(i, True)
129 if data is None:
130 raise KeyError(f)
131 if i == 'DPKG_ARCH' and d.getVar('PACKAGE_ARCH', True) == 'all':
132 data = 'all'
133 elif i == 'PACKAGE_ARCH' or i == 'DPKG_ARCH':
134 # The params in deb package control don't allow character
135 # `_', so change the arch's `_' to `-'. Such as `x86_64'
136 # -->`x86-64'
137 data = data.replace('_', '-')
138 l2.append(data)
139 return l2
140
141 ctrlfile.write("Package: %s\n" % pkgname)
142 # check for required fields
143 try:
144 for (c, fs) in fields:
145 for f in fs:
146 if localdata.getVar(f, False) is None:
147 raise KeyError(f)
148 # Special behavior for description...
149 if 'DESCRIPTION' in fs:
150 summary = localdata.getVar('SUMMARY', True) or localdata.getVar('DESCRIPTION', True) or "."
151 ctrlfile.write('Description: %s\n' % unicode(summary,'utf-8'))
152 description = localdata.getVar('DESCRIPTION', True) or "."
153 description = textwrap.dedent(description).strip()
154 if '\\n' in description:
155 # Manually indent
156 for t in description.split('\\n'):
157 # We don't limit the width when manually indent, but we do
158 # need the textwrap.fill() to set the initial_indent and
159 # subsequent_indent, so set a large width
160 ctrlfile.write('%s\n' % unicode(textwrap.fill(t, width=100000, initial_indent=' ', subsequent_indent=' '),'utf-8'))
161 else:
162 # Auto indent
163 ctrlfile.write('%s\n' % unicode(textwrap.fill(description.strip(), width=74, initial_indent=' ', subsequent_indent=' '),'utf-8'))
164
165 else:
166 ctrlfile.write(unicode(c % tuple(pullData(fs, localdata)),'utf-8'))
167 except KeyError:
168 import sys
169 (type, value, traceback) = sys.exc_info()
170 bb.utils.unlockfile(lf)
171 ctrlfile.close()
172 raise bb.build.FuncFailed("Missing field for deb generation: %s" % value)
173 except UnicodeDecodeError:
174 bb.utils.unlockfile(lf)
175 ctrlfile.close()
176 raise bb.build.FuncFailed("Non UTF-8 characters found in one of the fields")
177
178 # more fields
179
180 custom_fields_chunk = get_package_additional_metadata("deb", localdata)
181 if custom_fields_chunk is not None:
182 ctrlfile.write(unicode(custom_fields_chunk))
183 ctrlfile.write("\n")
184
185 mapping_rename_hook(localdata)
186
187 def debian_cmp_remap(var):
188 # dpkg does not allow for '(' or ')' in a dependency name
189 # replace these instances with '__' and '__'
190 #
191 # In debian '>' and '<' do not mean what it appears they mean
192 # '<' = less or equal
193 # '>' = greater or equal
194 # adjust these to the '<<' and '>>' equivalents
195 #
196 for dep in var:
197 if '(' in dep:
198 newdep = dep.replace('(', '__')
199 newdep = newdep.replace(')', '__')
200 if newdep != dep:
201 var[newdep] = var[dep]
202 del var[dep]
203 for dep in var:
204 for i, v in enumerate(var[dep]):
205 if (v or "").startswith("< "):
206 var[dep][i] = var[dep][i].replace("< ", "<< ")
207 elif (v or "").startswith("> "):
208 var[dep][i] = var[dep][i].replace("> ", ">> ")
209
210 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS", True) or "")
211 debian_cmp_remap(rdepends)
212 for dep in rdepends:
213 if '*' in dep:
214 del rdepends[dep]
215 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS", True) or "")
216 debian_cmp_remap(rrecommends)
217 for dep in rrecommends:
218 if '*' in dep:
219 del rrecommends[dep]
220 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS", True) or "")
221 debian_cmp_remap(rsuggests)
222 # Deliberately drop version information here, not wanted/supported by deb
223 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES", True) or ""), [])
224 debian_cmp_remap(rprovides)
225 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES", True) or "")
226 debian_cmp_remap(rreplaces)
227 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS", True) or "")
228 debian_cmp_remap(rconflicts)
229 if rdepends:
230 ctrlfile.write("Depends: %s\n" % unicode(bb.utils.join_deps(rdepends)))
231 if rsuggests:
232 ctrlfile.write("Suggests: %s\n" % unicode(bb.utils.join_deps(rsuggests)))
233 if rrecommends:
234 ctrlfile.write("Recommends: %s\n" % unicode(bb.utils.join_deps(rrecommends)))
235 if rprovides:
236 ctrlfile.write("Provides: %s\n" % unicode(bb.utils.join_deps(rprovides)))
237 if rreplaces:
238 ctrlfile.write("Replaces: %s\n" % unicode(bb.utils.join_deps(rreplaces)))
239 if rconflicts:
240 ctrlfile.write("Conflicts: %s\n" % unicode(bb.utils.join_deps(rconflicts)))
241 ctrlfile.close()
242
243 for script in ["preinst", "postinst", "prerm", "postrm"]:
244 scriptvar = localdata.getVar('pkg_%s' % script, True)
245 if not scriptvar:
246 continue
247 scriptvar = scriptvar.strip()
248 try:
249 scriptfile = open(os.path.join(controldir, script), 'w')
250 except OSError:
251 bb.utils.unlockfile(lf)
252 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
253
254 if scriptvar.startswith("#!"):
255 pos = scriptvar.find("\n") + 1
256 scriptfile.write(scriptvar[:pos])
257 else:
258 pos = 0
259 scriptfile.write("#!/bin/sh\n")
260
261 # Prevent the prerm/postrm scripts from being run during an upgrade
262 if script in ('prerm', 'postrm'):
263 scriptfile.write('[ "$1" != "upgrade" ] || exit 0\n')
264
265 scriptfile.write(scriptvar[pos:])
266 scriptfile.write('\n')
267 scriptfile.close()
268 os.chmod(os.path.join(controldir, script), 0755)
269
270 conffiles_str = ' '.join(get_conffiles(pkg, d))
271 if conffiles_str:
272 try:
273 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
274 except OSError:
275 bb.utils.unlockfile(lf)
276 raise bb.build.FuncFailed("unable to open conffiles for writing.")
277 for f in conffiles_str.split():
278 if os.path.exists(oe.path.join(root, f)):
279 conffiles.write('%s\n' % f)
280 conffiles.close()
281
282 os.chdir(basedir)
283 ret = subprocess.call("PATH=\"%s\" dpkg-deb -b %s %s" % (localdata.getVar("PATH", True), root, pkgoutdir), shell=True)
284 if ret != 0:
285 bb.utils.unlockfile(lf)
286 raise bb.build.FuncFailed("dpkg-deb execution failed")
287
288 cleanupcontrol(root)
289 bb.utils.unlockfile(lf)
290}
291# Otherwise allarch packages may change depending on override configuration
292do_package_deb[vardepsexclude] = "OVERRIDES"
293
294
295SSTATETASKS += "do_package_write_deb"
296do_package_write_deb[sstate-inputdirs] = "${PKGWRITEDIRDEB}"
297do_package_write_deb[sstate-outputdirs] = "${DEPLOY_DIR_DEB}"
298
299python do_package_write_deb_setscene () {
300 tmpdir = d.getVar('TMPDIR', True)
301
302 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
303 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
304
305 sstate_setscene(d)
306}
307addtask do_package_write_deb_setscene
308
309python () {
310 if d.getVar('PACKAGES', True) != '':
311 deps = ' dpkg-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot'
312 d.appendVarFlag('do_package_write_deb', 'depends', deps)
313 d.setVarFlag('do_package_write_deb', 'fakeroot', "1")
314
315 # Map TARGET_ARCH to Debian's ideas about architectures
316 darch = d.getVar('DPKG_ARCH', True)
317 if darch in ["x86", "i486", "i586", "i686", "pentium"]:
318 d.setVar('DPKG_ARCH', 'i386')
319 elif darch == "x86_64":
320 d.setVar('DPKG_ARCH', 'amd64')
321 elif darch == "arm":
322 d.setVar('DPKG_ARCH', 'armel')
323}
324
325python do_package_write_deb () {
326 bb.build.exec_func("read_subpackage_metadata", d)
327 bb.build.exec_func("do_package_deb", d)
328}
329do_package_write_deb[dirs] = "${PKGWRITEDIRDEB}"
330do_package_write_deb[cleandirs] = "${PKGWRITEDIRDEB}"
331do_package_write_deb[umask] = "022"
332addtask package_write_deb after do_packagedata do_package
333
334
335PACKAGEINDEXDEPS += "dpkg-native:do_populate_sysroot"
336PACKAGEINDEXDEPS += "apt-native:do_populate_sysroot"
337
338do_build[recrdeptask] += "do_package_write_deb"