tsmd: install also a little helper script to control the touchscreen daemon
[openembedded.git] / classes / package_deb.bbclass
blobd1e7f73e2ac4e93b41fc5daeb7b4cf9821dc51b9
2 # Copyright 2006-2008 OpenedHand Ltd.
5 inherit package
7 BOOTSTRAP_EXTRA_RDEPENDS += "apt"
8 DISTRO_EXTRA_RDEPENDS += "apt"
9 IMAGE_PKGTYPE ?= "deb"
11 # Map TARGET_ARCH to Debian's ideas about architectures
12 DPKG_ARCH ?= "${TARGET_ARCH}"
13 DPKG_ARCH_x86 ?= "i386"
14 DPKG_ARCH_i486 ?= "i386"
15 DPKG_ARCH_i586 ?= "i386"
16 DPKG_ARCH_i686 ?= "i386"
17 DPKG_ARCH_pentium ?= "i386"
18 DPKG_ARCH_linux-gnueabi ?= "armel"
19 DPKG_ARCH_linux-uclibceabi ?= "armel"
21 python package_deb_fn () {
22     bb.data.setVar('PKGFN', bb.data.getVar('PKG',d), d)
25 addtask package_deb_install
26 python do_package_deb_install () {
27     pkg = bb.data.getVar('PKG', d, 1)
28     pkgfn = bb.data.getVar('PKGFN', d, 1)
29     rootfs = bb.data.getVar('IMAGE_ROOTFS', d, 1)
30     debdir = bb.data.getVar('DEPLOY_DIR_DEB', d, 1)
31     apt_config = bb.data.expand('${STAGING_ETCDIR_NATIVE}/apt/apt.conf', d)
32     stagingbindir = bb.data.getVar('STAGING_BINDIR_NATIVE', d, 1)
33     tmpdir = bb.data.getVar('TMPDIR', d, 1)
35     if None in (pkg,pkgfn,rootfs):
36         raise bb.build.FuncFailed("missing variables (one or more of PKG, PKGFN, IMAGE_ROOTFS)")
37     try:
38         if not os.exists(rootfs):
39             os.makedirs(rootfs)
40         os.chdir(rootfs)
41     except OSError:
42         import sys
43         raise bb.build.FuncFailed(str(sys.exc_value))
45     # update packages file
46     (exitstatus, output) = commands.getstatusoutput('dpkg-scanpackages %s > %s/Packages' % (debdir, debdir))
47     if (exitstatus != 0 ):
48         raise bb.build.FuncFailed(output)
50     f = open(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"), "w")
51     f.close()
53     # NOTE: this env stuff is racy at best, we need something more capable
54     # than 'commands' for command execution, which includes manipulating the
55     # env of the fork+execve'd processs
57     # Set up environment
58     apt_config_backup = os.getenv('APT_CONFIG')
59     os.putenv('APT_CONFIG', apt_config)
60     path = os.getenv('PATH')
61     os.putenv('PATH', '%s:%s' % (stagingbindir, os.getenv('PATH')))
63     # install package
64     commands.getstatusoutput('apt-get update')
65     commands.getstatusoutput('apt-get install -y %s' % pkgfn)
67     # revert environment
68     os.putenv('APT_CONFIG', apt_config_backup)
69     os.putenv('PATH', path)
72 python do_package_deb () {
73     import re, copy
75     workdir = bb.data.getVar('WORKDIR', d, 1)
76     if not workdir:
77         bb.error("WORKDIR not defined, unable to package")
78         return
80     outdir = bb.data.getVar('DEPLOY_DIR_DEB', d, 1)
81     if not outdir:
82         bb.error("DEPLOY_DIR_DEB not defined, unable to package")
83         return
85     dvar = bb.data.getVar('D', d, 1)
86     if not dvar:
87         bb.error("D not defined, unable to package")
88         return
89     bb.mkdirhier(dvar)
91     tmpdir = bb.data.getVar('TMPDIR', d, 1)
93     if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
94         os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
96     packages = bb.data.getVar('PACKAGES', d, 1)
97     for pkg in packages.split():
98         localdata = bb.data.createCopy(d)
99         pkgdest = bb.data.getVar('PKGDEST', d, 1)
100         root = "%s/%s" % (pkgdest, pkg)
102         lf = bb.utils.lockfile(root + ".lock")
104         bb.data.setVar('ROOT', '', localdata)
105         bb.data.setVar('ROOT_%s' % pkg, root, localdata)
106         pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
107         if not pkgname:
108             pkgname = pkg
109         bb.data.setVar('PKG', pkgname, localdata)
111         overrides = bb.data.getVar('OVERRIDES', localdata)
112         if not overrides:
113             raise bb.build.FuncFailed('OVERRIDES not defined')
114         overrides = bb.data.expand(overrides, localdata)
115         bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
117         bb.data.update_data(localdata)
118         basedir = os.path.join(os.path.dirname(root))
120         pkgoutdir = os.path.join(outdir, bb.data.getVar('PACKAGE_ARCH', localdata, 1))
121         bb.mkdirhier(pkgoutdir)
123         os.chdir(root)
124         from glob import glob
125         g = glob('*') + glob('.[!.]*')
126         try:
127             del g[g.index('DEBIAN')]
128             del g[g.index('./DEBIAN')]
129         except ValueError:
130             pass
131         if not g and bb.data.getVar('ALLOW_EMPTY', localdata) != "1":
132             from bb import note
133             note("Not creating empty archive for %s-%s" % (pkg, bb.data.expand('${PV}-${PR}${DISTRO_PR}', localdata, True)))
134             bb.utils.unlockfile(lf)
135             continue
137         controldir = os.path.join(root, 'DEBIAN')
138         bb.mkdirhier(controldir)
139         os.chmod(controldir, 0755)
140         try:
141             ctrlfile = file(os.path.join(controldir, 'control'), 'wb')
142             # import codecs
143             # ctrlfile = codecs.open("someFile", "w", "utf-8")
144         except OSError:
145             bb.utils.unlockfile(lf)
146             raise bb.build.FuncFailed("unable to open control file for writing.")
148         fields = []
149         version = bb.data.getVar('PKGV', d, 1)
150         if re.match('[^0-9]+$', version):
151                 version = '%s0' % version
152         pe = bb.data.getVar('PE', d, 1)
153         if pe and int(pe) > 0:
154             fields.append(["Version: %%s:%s-%%s\n" % version, ['PE', 'PKGR']])
155         else:
156             fields.append(["Version: %s-%%s\n" % version, ['PKGR']])
157         fields.append(["Description: %s\n", ['DESCRIPTION']])
158         fields.append(["Section: %s\n", ['SECTION']])
159         fields.append(["Priority: %s\n", ['PRIORITY']])
160         fields.append(["Maintainer: %s\n", ['MAINTAINER']])
161         fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
162         fields.append(["OE: %s\n", ['PN']])
163         fields.append(["Homepage: %s\n", ['HOMEPAGE']])
165 #        Package, Version, Maintainer, Description - mandatory
166 #        Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
169         def pullData(l, d):
170             l2 = []
171             for i in l:
172                 data = bb.data.getVar(i, d, 1)
173                 if data is None:
174                     raise KeyError(f)
175                 if i == 'DPKG_ARCH' and bb.data.getVar('PACKAGE_ARCH', d, 1) == 'all':
176                     data = 'all'
177                 l2.append(data)
178             return l2
180         ctrlfile.write("Package: %s\n" % pkgname)
181         # check for required fields
182         try:
183             for (c, fs) in fields:
184                 ctrlfile.write(unicode(c % tuple(pullData(fs, localdata))))
185         except KeyError:
186             import sys
187             (type, value, traceback) = sys.exc_info()
188             bb.utils.unlockfile(lf)
189             ctrlfile.close()
190             raise bb.build.FuncFailed("Missing field for deb generation: %s" % value)
191         # more fields
193         bb.build.exec_func("mapping_rename_hook", localdata)
195         rdepends = explode_deps(unicode(bb.data.getVar("RDEPENDS", localdata, 1) or ""))
196         rdepends = [dep for dep in rdepends if not '*' in dep]
197         rrecommends = explode_deps(unicode(bb.data.getVar("RRECOMMENDS", localdata, 1) or ""))
198         rrecommends = [rec for rec in rrecommends if not '*' in rec]
199         rsuggests = (unicode(bb.data.getVar("RSUGGESTS", localdata, 1) or "")).split()
200         rprovides = (unicode(bb.data.getVar("RPROVIDES", localdata, 1) or "")).split()
201         rreplaces = (unicode(bb.data.getVar("RREPLACES", localdata, 1) or "")).split()
202         rconflicts = (unicode(bb.data.getVar("RCONFLICTS", localdata, 1) or "")).split()
203         if rdepends:
204             ctrlfile.write(u"Depends: %s\n" % ", ".join(rdepends))
205         if rsuggests:
206             ctrlfile.write(u"Suggests: %s\n" % ", ".join(rsuggests))
207         if rrecommends:
208             ctrlfile.write(u"Recommends: %s\n" % ", ".join(rrecommends))
209         if rprovides:
210             ctrlfile.write(u"Provides: %s\n" % ", ".join(rprovides))
211         if rreplaces:
212             ctrlfile.write(u"Replaces: %s\n" % ", ".join(rreplaces))
213         if rconflicts:
214             ctrlfile.write(u"Conflicts: %s\n" % ", ".join(rconflicts))
215         ctrlfile.close()
217         for script in ["preinst", "postinst", "prerm", "postrm"]:
218             scriptvar = bb.data.getVar('pkg_%s' % script, localdata, 1)
219             if not scriptvar:
220                 continue
221             try:
222                 scriptfile = file(os.path.join(controldir, script), 'w')
223             except OSError:
224                 bb.utils.unlockfile(lf)
225                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
226             scriptfile.write("#!/bin/sh\n")
227             scriptfile.write(scriptvar)
228             scriptfile.close()
229             os.chmod(os.path.join(controldir, script), 0755)
231         conffiles_str = bb.data.getVar("CONFFILES", localdata, 1)
232         if conffiles_str:
233             try:
234                 conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
235             except OSError:
236                 bb.utils.unlockfile(lf)
237                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
238             for f in conffiles_str.split():
239                 conffiles.write('%s\n' % f)
240             conffiles.close()
242         try:
243             write_package_md5sums(root, os.path.join(controldir, 'md5sums'),
244                                   ['DEBIAN'])
245         except:
246             bb.utils.unlockfile(lf)
247             raise
249         os.chdir(basedir)
250         ret = os.system("PATH=\"%s\" fakeroot dpkg-deb -b %s %s" % (bb.data.getVar("PATH", localdata, 1), root, pkgoutdir))
251         if ret != 0:
252             bb.utils.unlockfile(lf)
253             raise bb.build.FuncFailed("dpkg-deb execution failed")
255         bb.utils.prunedir(controldir)
256         bb.utils.unlockfile(lf)
259 python () {
260     if bb.data.getVar('PACKAGES', d, True) != '':
261         deps = (bb.data.getVarFlag('do_package_write_deb', 'depends', d) or "").split()
262         deps.append('dpkg-native:do_populate_sysroot')
263         deps.append('fakeroot-native:do_populate_sysroot')
264         bb.data.setVarFlag('do_package_write_deb', 'depends', " ".join(deps), d)
267 python do_package_write_deb () {
268     packages = bb.data.getVar('PACKAGES', d, True)
269     if not packages:
270         bb.debug(1, "No PACKAGES defined, nothing to package")
271         return
273     bb.build.exec_func("read_subpackage_metadata", d)
274     bb.build.exec_func("do_package_deb", d)
276 do_package_write_deb[dirs] = "${D}"
277 addtask package_write_deb before do_package_write after do_package
279 do_package_update_index_deb[lockfiles] = "${DEPLOY_DIR_DEB}.lock"
280 do_package_update_index_deb[nostamp] = "1"
281 do_package_update_index_deb[recrdeptask] += "do_package_write_deb"
282 do_package_update_index_deb[depends] += "dpkg-native:do_populate_sysroot"
284 do_package_update_index_deb () {
285         set -x
287         if [ ! -z "${DEPLOY_KEEP_PACKAGES}" ]; then
288                 return
289         fi
291         cd "${DEPLOY_DIR_DEB}"
292         for arch in ${PACKAGE_ARCHS}; do
293                 if [ -e "$arch" ] ; then
294                         dpkg-scanpackages "$arch" | gzip > "$arch/Packages.gz"
295                 fi
296                 if [ -e "${BUILD_ARCH}-$arch-sdk" ] ; then
297                         dpkg-scanpackages "${BUILD_ARCH}-$arch-sdk" | gzip > "${BUILD_ARCH}-$arch-sdk/Packages.gz"
298                 fi
299                 if [ -e "${SDK_SYS}-sdk-$arch" ] ; then
300                         dpkg-scanpackages "${SDK_SYS}-sdk-$arch" | gzip > "${SDK_SYS}-sdk-$arch/Packages.gz"
301                 fi
302         done
303         cd -
306 addtask package_update_index_deb before do_rootfs