firefox: Perform a number of cleanups and fix consistency issues.
[openembedded.git] / classes / package_deb.bbclass
1 #
2 # Copyright 2006-2008 OpenedHand Ltd.
3 #
4
5 inherit package
6
7 BOOTSTRAP_EXTRA_RDEPENDS += "dpkg"
8 DISTRO_EXTRA_RDEPENDS += "dpkg"
9 IMAGE_PKGTYPE ?= "deb"
10
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
19 python package_deb_fn () {
20     bb.data.setVar('PKGFN', bb.data.getVar('PKG',d), d)
21 }
22
23 addtask package_deb_install
24 python do_package_deb_install () {
25     pkg = bb.data.getVar('PKG', d, 1)
26     pkgfn = bb.data.getVar('PKGFN', d, 1)
27     rootfs = bb.data.getVar('IMAGE_ROOTFS', d, 1)
28     debdir = bb.data.getVar('DEPLOY_DIR_DEB', d, 1)
29     apt_config = bb.data.expand('${STAGING_ETCDIR_NATIVE}/apt/apt.conf', d)
30     stagingbindir = bb.data.getVar('STAGING_BINDIR_NATIVE', d, 1)
31     tmpdir = bb.data.getVar('TMPDIR', d, 1)
32
33     if None in (pkg,pkgfn,rootfs):
34         raise bb.build.FuncFailed("missing variables (one or more of PKG, PKGFN, IMAGE_ROOTFS)")
35     try:
36         if not os.exists(rootfs):
37             os.makedirs(rootfs)
38         os.chdir(rootfs)
39     except OSError:
40         import sys
41         raise bb.build.FuncFailed(str(sys.exc_value))
42
43     # update packages file
44     (exitstatus, output) = commands.getstatusoutput('dpkg-scanpackages %s > %s/Packages' % (debdir, debdir))
45     if (exitstatus != 0 ):
46         raise bb.build.FuncFailed(output)
47
48     f = open(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"), "w")
49     f.close()
50
51     # NOTE: this env stuff is racy at best, we need something more capable
52     # than 'commands' for command execution, which includes manipulating the
53     # env of the fork+execve'd processs
54
55     # Set up environment
56     apt_config_backup = os.getenv('APT_CONFIG')
57     os.putenv('APT_CONFIG', apt_config)
58     path = os.getenv('PATH')
59     os.putenv('PATH', '%s:%s' % (stagingbindir, os.getenv('PATH')))
60
61     # install package
62     commands.getstatusoutput('apt-get update')
63     commands.getstatusoutput('apt-get install -y %s' % pkgfn)
64
65     # revert environment
66     os.putenv('APT_CONFIG', apt_config_backup)
67     os.putenv('PATH', path)
68 }
69
70 python do_package_deb () {
71     import re, copy
72
73     workdir = bb.data.getVar('WORKDIR', d, 1)
74     if not workdir:
75         bb.error("WORKDIR not defined, unable to package")
76         return
77
78     outdir = bb.data.getVar('DEPLOY_DIR_DEB', d, 1)
79     if not outdir:
80         bb.error("DEPLOY_DIR_DEB not defined, unable to package")
81         return
82
83     dvar = bb.data.getVar('D', d, 1)
84     if not dvar:
85         bb.error("D not defined, unable to package")
86         return
87     bb.mkdirhier(dvar)
88
89     tmpdir = bb.data.getVar('TMPDIR', d, 1)
90
91     if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
92         os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
93
94     packages = bb.data.getVar('PACKAGES', d, 1)
95     for pkg in packages.split():
96         localdata = bb.data.createCopy(d)
97         pkgdest = bb.data.getVar('PKGDEST', d, 1)
98         root = "%s/%s" % (pkgdest, pkg)
99
100         lf = bb.utils.lockfile(root + ".lock")
101
102         bb.data.setVar('ROOT', '', localdata)
103         bb.data.setVar('ROOT_%s' % pkg, root, localdata)
104         pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
105         if not pkgname:
106             pkgname = pkg
107         bb.data.setVar('PKG', pkgname, localdata)
108
109         overrides = bb.data.getVar('OVERRIDES', localdata)
110         if not overrides:
111             raise bb.build.FuncFailed('OVERRIDES not defined')
112         overrides = bb.data.expand(overrides, localdata)
113         bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
114
115         bb.data.update_data(localdata)
116         basedir = os.path.join(os.path.dirname(root))
117
118         pkgoutdir = os.path.join(outdir, bb.data.getVar('PACKAGE_ARCH', localdata, 1))
119         bb.mkdirhier(pkgoutdir)
120
121         os.chdir(root)
122         from glob import glob
123         g = glob('*') + glob('.[!.]*')
124         try:
125             del g[g.index('DEBIAN')]
126             del g[g.index('./DEBIAN')]
127         except ValueError:
128             pass
129         if not g and bb.data.getVar('ALLOW_EMPTY', localdata) != "1":
130             from bb import note
131             note("Not creating empty archive for %s-%s" % (pkg, bb.data.expand('${PV}-${PR}${DISTRO_PR}', localdata, True)))
132             bb.utils.unlockfile(lf)
133             continue
134
135         controldir = os.path.join(root, 'DEBIAN')
136         bb.mkdirhier(controldir)
137         os.chmod(controldir, 0755)
138         try:
139             ctrlfile = file(os.path.join(controldir, 'control'), 'wb')
140             # import codecs
141             # ctrlfile = codecs.open("someFile", "w", "utf-8")
142         except OSError:
143             bb.utils.unlockfile(lf)
144             raise bb.build.FuncFailed("unable to open control file for writing.")
145
146         fields = []
147         pe = bb.data.getVar('PE', d, 1)
148         if pe and int(pe) > 0:
149             fields.append(["Version: %s:%s-%s%s\n", ['PE', 'PV', 'PR', 'DISTRO_PR']])
150         else:
151             fields.append(["Version: %s-%s%s\n", ['PV', 'PR', 'DISTRO_PR']])
152         fields.append(["Description: %s\n", ['DESCRIPTION']])
153         fields.append(["Section: %s\n", ['SECTION']])
154         fields.append(["Priority: %s\n", ['PRIORITY']])
155         fields.append(["Maintainer: %s\n", ['MAINTAINER']])
156         fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
157         fields.append(["OE: %s\n", ['PN']])
158         fields.append(["Homepage: %s\n", ['HOMEPAGE']])
159
160 #        Package, Version, Maintainer, Description - mandatory
161 #        Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
162
163
164         def pullData(l, d):
165             l2 = []
166             for i in l:
167                 data = bb.data.getVar(i, d, 1)
168                 if data is None:
169                     raise KeyError(f)
170                 if i == 'DPKG_ARCH' and bb.data.getVar('PACKAGE_ARCH', d, 1) == 'all':
171                     data = 'all'
172                 l2.append(data)
173             return l2
174
175         ctrlfile.write("Package: %s\n" % pkgname)
176         # check for required fields
177         try:
178             for (c, fs) in fields:
179                 ctrlfile.write(unicode(c % tuple(pullData(fs, localdata))))
180         except KeyError:
181             import sys
182             (type, value, traceback) = sys.exc_info()
183             bb.utils.unlockfile(lf)
184             ctrlfile.close()
185             raise bb.build.FuncFailed("Missing field for deb generation: %s" % value)
186         # more fields
187
188         bb.build.exec_func("mapping_rename_hook", localdata)
189
190         rdepends = explode_deps(unicode(bb.data.getVar("RDEPENDS", localdata, 1) or ""))
191         rdepends = [dep for dep in rdepends if not '*' in dep]
192         rrecommends = explode_deps(unicode(bb.data.getVar("RRECOMMENDS", localdata, 1) or ""))
193         rrecommends = [rec for rec in rrecommends if not '*' in rec]
194         rsuggests = (unicode(bb.data.getVar("RSUGGESTS", localdata, 1) or "")).split()
195         rprovides = (unicode(bb.data.getVar("RPROVIDES", localdata, 1) or "")).split()
196         rreplaces = (unicode(bb.data.getVar("RREPLACES", localdata, 1) or "")).split()
197         rconflicts = (unicode(bb.data.getVar("RCONFLICTS", localdata, 1) or "")).split()
198         if rdepends:
199             ctrlfile.write(u"Depends: %s\n" % ", ".join(rdepends))
200         if rsuggests:
201             ctrlfile.write(u"Suggests: %s\n" % ", ".join(rsuggests))
202         if rrecommends:
203             ctrlfile.write(u"Recommends: %s\n" % ", ".join(rrecommends))
204         if rprovides:
205             ctrlfile.write(u"Provides: %s\n" % ", ".join(rprovides))
206         if rreplaces:
207             ctrlfile.write(u"Replaces: %s\n" % ", ".join(rreplaces))
208         if rconflicts:
209             ctrlfile.write(u"Conflicts: %s\n" % ", ".join(rconflicts))
210         ctrlfile.close()
211
212         for script in ["preinst", "postinst", "prerm", "postrm"]:
213             scriptvar = bb.data.getVar('pkg_%s' % script, localdata, 1)
214             if not scriptvar:
215                 continue
216             try:
217                 scriptfile = file(os.path.join(controldir, script), 'w')
218             except OSError:
219                 bb.utils.unlockfile(lf)
220                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
221             scriptfile.write("#!/bin/sh\n")
222             scriptfile.write(scriptvar)
223             scriptfile.close()
224             os.chmod(os.path.join(controldir, script), 0755)
225
226         conffiles_str = bb.data.getVar("CONFFILES", localdata, 1)
227         if conffiles_str:
228             try:
229                 conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
230             except OSError:
231                 bb.utils.unlockfile(lf)
232                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
233             for f in conffiles_str.split():
234                 conffiles.write('%s\n' % f)
235             conffiles.close()
236
237         try:
238             write_package_md5sums(root, os.path.join(controldir, 'md5sums'),
239                                   ['DEBIAN'])
240         except:
241             bb.utils.unlockfile(lf)
242             raise
243
244         os.chdir(basedir)
245         ret = os.system("PATH=\"%s\" fakeroot dpkg-deb -b %s %s" % (bb.data.getVar("PATH", localdata, 1), root, pkgoutdir))
246         if ret != 0:
247             bb.utils.unlockfile(lf)
248             raise bb.build.FuncFailed("dpkg-deb execution failed")
249
250         bb.utils.prunedir(controldir)
251         bb.utils.unlockfile(lf)
252 }
253
254 python () {
255     if bb.data.getVar('PACKAGES', d, True) != '':
256         deps = (bb.data.getVarFlag('do_package_write_deb', 'depends', d) or "").split()
257         deps.append('dpkg-native:do_populate_staging')
258         deps.append('fakeroot-native:do_populate_staging')
259         bb.data.setVarFlag('do_package_write_deb', 'depends', " ".join(deps), d)
260 }
261
262 python do_package_write_deb () {
263     packages = bb.data.getVar('PACKAGES', d, True)
264     if not packages:
265         bb.debug(1, "No PACKAGES defined, nothing to package")
266         return
267
268     bb.build.exec_func("read_subpackage_metadata", d)
269     bb.build.exec_func("do_package_deb", d)
270 }
271 do_package_write_deb[dirs] = "${D}"
272 addtask package_write_deb before do_package_write after do_package
273