Merge branch 'org.openembedded.dev' of git.openembedded.net:openembedded into org...
[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     packages = bb.data.getVar('PACKAGES', d, 1)
90     if not packages:
91         bb.debug(1, "PACKAGES not defined, nothing to package")
92         return
93
94     tmpdir = bb.data.getVar('TMPDIR', d, 1)
95
96     if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
97         os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
98
99     if packages == []:
100         bb.debug(1, "No packages; nothing to do")
101         return
102
103     for pkg in packages.split():
104         localdata = bb.data.createCopy(d)
105         pkgdest = bb.data.getVar('PKGDEST', d, 1)
106         root = "%s/%s" % (pkgdest, pkg)
107
108         lf = bb.utils.lockfile(root + ".lock")
109
110         bb.data.setVar('ROOT', '', localdata)
111         bb.data.setVar('ROOT_%s' % pkg, root, localdata)
112         pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
113         if not pkgname:
114             pkgname = pkg
115         bb.data.setVar('PKG', pkgname, localdata)
116
117         overrides = bb.data.getVar('OVERRIDES', localdata)
118         if not overrides:
119             raise bb.build.FuncFailed('OVERRIDES not defined')
120         overrides = bb.data.expand(overrides, localdata)
121         bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
122
123         bb.data.update_data(localdata)
124         basedir = os.path.join(os.path.dirname(root))
125
126         pkgoutdir = os.path.join(outdir, bb.data.getVar('PACKAGE_ARCH', localdata, 1))
127         bb.mkdirhier(pkgoutdir)
128
129         os.chdir(root)
130         from glob import glob
131         g = glob('*') + glob('.[!.]*')
132         try:
133             del g[g.index('DEBIAN')]
134             del g[g.index('./DEBIAN')]
135         except ValueError:
136             pass
137         if not g and bb.data.getVar('ALLOW_EMPTY', localdata) != "1":
138             from bb import note
139             note("Not creating empty archive for %s-%s" % (pkg, bb.data.expand('${PV}-${PR}${DISTRO_PR}', localdata, True)))
140             bb.utils.unlockfile(lf)
141             continue
142
143         controldir = os.path.join(root, 'DEBIAN')
144         bb.mkdirhier(controldir)
145         os.chmod(controldir, 0755)
146         try:
147             ctrlfile = file(os.path.join(controldir, 'control'), 'wb')
148             # import codecs
149             # ctrlfile = codecs.open("someFile", "w", "utf-8")
150         except OSError:
151             bb.utils.unlockfile(lf)
152             raise bb.build.FuncFailed("unable to open control file for writing.")
153
154         fields = []
155         pe = bb.data.getVar('PE', d, 1)
156         if pe and int(pe) > 0:
157             fields.append(["Version: %s:%s-%s%s\n", ['PE', 'PV', 'PR', 'DISTRO_PR']])
158         else:
159             fields.append(["Version: %s-%s%s\n", ['PV', 'PR', 'DISTRO_PR']])
160         fields.append(["Description: %s\n", ['DESCRIPTION']])
161         fields.append(["Section: %s\n", ['SECTION']])
162         fields.append(["Priority: %s\n", ['PRIORITY']])
163         fields.append(["Maintainer: %s\n", ['MAINTAINER']])
164         fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
165         fields.append(["OE: %s\n", ['PN']])
166         fields.append(["Homepage: %s\n", ['HOMEPAGE']])
167
168 #        Package, Version, Maintainer, Description - mandatory
169 #        Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
170
171
172         def pullData(l, d):
173             l2 = []
174             for i in l:
175                 data = bb.data.getVar(i, d, 1)
176                 if data is None:
177                     raise KeyError(f)
178                 if i == 'DPKG_ARCH' and bb.data.getVar('PACKAGE_ARCH', d, 1) == 'all':
179                     data = 'all'
180                 l2.append(data)
181             return l2
182
183         ctrlfile.write("Package: %s\n" % pkgname)
184         # check for required fields
185         try:
186             for (c, fs) in fields:
187                 ctrlfile.write(unicode(c % tuple(pullData(fs, localdata))))
188         except KeyError:
189             import sys
190             (type, value, traceback) = sys.exc_info()
191             bb.utils.unlockfile(lf)
192             ctrlfile.close()
193             raise bb.build.FuncFailed("Missing field for deb generation: %s" % value)
194         # more fields
195
196         bb.build.exec_func("mapping_rename_hook", localdata)
197
198         rdepends = explode_deps(unicode(bb.data.getVar("RDEPENDS", localdata, 1) or ""))
199         rdepends = [dep for dep in rdepends if not '*' in dep]
200         rrecommends = explode_deps(unicode(bb.data.getVar("RRECOMMENDS", localdata, 1) or ""))
201         rrecommends = [rec for rec in rrecommends if not '*' in rec]
202         rsuggests = (unicode(bb.data.getVar("RSUGGESTS", localdata, 1) or "")).split()
203         rprovides = (unicode(bb.data.getVar("RPROVIDES", localdata, 1) or "")).split()
204         rreplaces = (unicode(bb.data.getVar("RREPLACES", localdata, 1) or "")).split()
205         rconflicts = (unicode(bb.data.getVar("RCONFLICTS", localdata, 1) or "")).split()
206         if rdepends:
207             ctrlfile.write(u"Depends: %s\n" % ", ".join(rdepends))
208         if rsuggests:
209             ctrlfile.write(u"Suggests: %s\n" % ", ".join(rsuggests))
210         if rrecommends:
211             ctrlfile.write(u"Recommends: %s\n" % ", ".join(rrecommends))
212         if rprovides:
213             ctrlfile.write(u"Provides: %s\n" % ", ".join(rprovides))
214         if rreplaces:
215             ctrlfile.write(u"Replaces: %s\n" % ", ".join(rreplaces))
216         if rconflicts:
217             ctrlfile.write(u"Conflicts: %s\n" % ", ".join(rconflicts))
218         ctrlfile.close()
219
220         for script in ["preinst", "postinst", "prerm", "postrm"]:
221             scriptvar = bb.data.getVar('pkg_%s' % script, localdata, 1)
222             if not scriptvar:
223                 continue
224             try:
225                 scriptfile = file(os.path.join(controldir, script), 'w')
226             except OSError:
227                 bb.utils.unlockfile(lf)
228                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
229             scriptfile.write("#!/bin/sh\n")
230             scriptfile.write(scriptvar)
231             scriptfile.close()
232             os.chmod(os.path.join(controldir, script), 0755)
233
234         conffiles_str = bb.data.getVar("CONFFILES", localdata, 1)
235         if conffiles_str:
236             try:
237                 conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
238             except OSError:
239                 bb.utils.unlockfile(lf)
240                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
241             for f in conffiles_str.split():
242                 conffiles.write('%s\n' % f)
243             conffiles.close()
244
245         try:
246             write_package_md5sums(root, os.path.join(controldir, 'md5sums'),
247                                   ['DEBIAN'])
248         except:
249             bb.utils.unlockfile(lf)
250             raise
251
252         os.chdir(basedir)
253         ret = os.system("PATH=\"%s\" fakeroot dpkg-deb -b %s %s" % (bb.data.getVar("PATH", localdata, 1), root, pkgoutdir))
254         if ret != 0:
255             bb.utils.unlockfile(lf)
256             raise bb.build.FuncFailed("dpkg-deb execution failed")
257
258         bb.utils.prunedir(controldir)
259         bb.utils.unlockfile(lf)
260 }
261
262 python () {
263     if bb.data.getVar('PACKAGES', d, True) != '':
264         deps = (bb.data.getVarFlag('do_package_write_deb', 'depends', d) or "").split()
265         deps.append('dpkg-native:do_populate_staging')
266         deps.append('fakeroot-native:do_populate_staging')
267         bb.data.setVarFlag('do_package_write_deb', 'depends', " ".join(deps), d)
268 }
269
270 python do_package_write_deb () {
271         bb.build.exec_func("read_subpackage_metadata", d)
272         bb.build.exec_func("do_package_deb", d)
273 }
274 do_package_write_deb[dirs] = "${D}"
275 addtask package_write_deb before do_package_write after do_package
276