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