blob: fb6034cab164c205aad5cc745a50c8ec4d1c1c5d [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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05009DPKG_ARCH ?= "${@debian_arch_map(d.getVar('TARGET_ARCH', True), d.getVar('TUNE_FEATURES', True))}"
10DPKG_ARCH[vardepvalue] = "${DPKG_ARCH}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
12PKGWRITEDIRDEB = "${WORKDIR}/deploy-debs"
13
14APTCONF_TARGET = "${WORKDIR}"
15
16APT_ARGS = "${@['', '--no-install-recommends'][d.getVar("NO_RECOMMENDATIONS", True) == "1"]}"
17
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050018def debian_arch_map(arch, tune):
19 tune_features = tune.split()
20 if arch in ["i586", "i686"]:
21 return "i386"
22 if arch == "x86_64":
23 if "mx32" in tune_features:
24 return "x32"
25 return "amd64"
26 if arch.startswith("mips"):
27 endian = ["el", ""]["bigendian" in tune_features]
28 if "n64" in tune_features:
29 return "mips64" + endian
30 if "n32" in tune_features:
31 return "mipsn32" + endian
32 return "mips" + endian
33 if arch == "powerpc":
34 return arch + ["", "spe"]["spe" in tune_features]
35 if arch == "aarch64":
36 return "arm64"
37 if arch == "arm":
38 return arch + ["el", "hf"]["callconvention-hard" in tune_features]
39 return arch
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040#
41# install a bunch of packages using apt
42# the following shell variables needs to be set before calling this func:
43# INSTALL_ROOTFS_DEB - install root dir
44# INSTALL_BASEARCH_DEB - install base architecutre
45# INSTALL_ARCHS_DEB - list of available archs
46# INSTALL_PACKAGES_NORMAL_DEB - packages to be installed
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047# INSTALL_PACKAGES_ATTEMPTONLY_DEB - packages attempted to be installed only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048# INSTALL_PACKAGES_LINGUAS_DEB - additional packages for uclibc
49# INSTALL_TASK_DEB - task name
50
51python do_package_deb () {
52 import re, copy
53 import textwrap
54 import subprocess
Patrick Williamsc0f7c042017-02-23 20:41:17 -060055 import collections
56
57 oldcwd = os.getcwd()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058
59 workdir = d.getVar('WORKDIR', True)
60 if not workdir:
61 bb.error("WORKDIR not defined, unable to package")
62 return
63
64 outdir = d.getVar('PKGWRITEDIRDEB', True)
65 if not outdir:
66 bb.error("PKGWRITEDIRDEB not defined, unable to package")
67 return
68
69 packages = d.getVar('PACKAGES', True)
70 if not packages:
71 bb.debug(1, "PACKAGES not defined, nothing to package")
72 return
73
74 tmpdir = d.getVar('TMPDIR', True)
75
76 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
77 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
78
79 if packages == []:
80 bb.debug(1, "No packages; nothing to do")
81 return
82
83 pkgdest = d.getVar('PKGDEST', True)
84
85 def cleanupcontrol(root):
86 for p in ['CONTROL', 'DEBIAN']:
87 p = os.path.join(root, p)
88 if os.path.exists(p):
89 bb.utils.prunedir(p)
90
91 for pkg in packages.split():
92 localdata = bb.data.createCopy(d)
93 root = "%s/%s" % (pkgdest, pkg)
94
95 lf = bb.utils.lockfile(root + ".lock")
96
97 localdata.setVar('ROOT', '')
98 localdata.setVar('ROOT_%s' % pkg, root)
99 pkgname = localdata.getVar('PKG_%s' % pkg, True)
100 if not pkgname:
101 pkgname = pkg
102 localdata.setVar('PKG', pkgname)
103
104 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
105
106 bb.data.update_data(localdata)
107 basedir = os.path.join(os.path.dirname(root))
108
109 pkgoutdir = os.path.join(outdir, localdata.getVar('PACKAGE_ARCH', True))
110 bb.utils.mkdirhier(pkgoutdir)
111
112 os.chdir(root)
113 cleanupcontrol(root)
114 from glob import glob
115 g = glob('*')
116 if not g and localdata.getVar('ALLOW_EMPTY', False) != "1":
117 bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV', True), localdata.getVar('PKGR', True)))
118 bb.utils.unlockfile(lf)
119 continue
120
121 controldir = os.path.join(root, 'DEBIAN')
122 bb.utils.mkdirhier(controldir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600123 os.chmod(controldir, 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124 try:
125 import codecs
126 ctrlfile = codecs.open(os.path.join(controldir, 'control'), 'w', 'utf-8')
127 except OSError:
128 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600129 bb.fatal("unable to open control file for writing")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130
131 fields = []
132 pe = d.getVar('PKGE', True)
133 if pe and int(pe) > 0:
134 fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']])
135 else:
136 fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']])
137 fields.append(["Description: %s\n", ['DESCRIPTION']])
138 fields.append(["Section: %s\n", ['SECTION']])
139 fields.append(["Priority: %s\n", ['PRIORITY']])
140 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
141 fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
142 fields.append(["OE: %s\n", ['PN']])
143 fields.append(["PackageArch: %s\n", ['PACKAGE_ARCH']])
144 if d.getVar('HOMEPAGE', True):
145 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
146
147 # Package, Version, Maintainer, Description - mandatory
148 # Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
149
150
151 def pullData(l, d):
152 l2 = []
153 for i in l:
154 data = d.getVar(i, True)
155 if data is None:
156 raise KeyError(f)
157 if i == 'DPKG_ARCH' and d.getVar('PACKAGE_ARCH', True) == 'all':
158 data = 'all'
159 elif i == 'PACKAGE_ARCH' or i == 'DPKG_ARCH':
160 # The params in deb package control don't allow character
161 # `_', so change the arch's `_' to `-'. Such as `x86_64'
162 # -->`x86-64'
163 data = data.replace('_', '-')
164 l2.append(data)
165 return l2
166
167 ctrlfile.write("Package: %s\n" % pkgname)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168 if d.getVar('PACKAGE_ARCH', True) == "all":
169 ctrlfile.write("Multi-Arch: foreign\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170 # check for required fields
171 try:
172 for (c, fs) in fields:
173 for f in fs:
174 if localdata.getVar(f, False) is None:
175 raise KeyError(f)
176 # Special behavior for description...
177 if 'DESCRIPTION' in fs:
178 summary = localdata.getVar('SUMMARY', True) or localdata.getVar('DESCRIPTION', True) or "."
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600179 ctrlfile.write('Description: %s\n' % summary)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180 description = localdata.getVar('DESCRIPTION', True) or "."
181 description = textwrap.dedent(description).strip()
182 if '\\n' in description:
183 # Manually indent
184 for t in description.split('\\n'):
185 # We don't limit the width when manually indent, but we do
186 # need the textwrap.fill() to set the initial_indent and
187 # subsequent_indent, so set a large width
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600188 ctrlfile.write('%s\n' % textwrap.fill(t, width=100000, initial_indent=' ', subsequent_indent=' '))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 else:
190 # Auto indent
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600191 ctrlfile.write('%s\n' % textwrap.fill(description.strip(), width=74, initial_indent=' ', subsequent_indent=' '))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192
193 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600194 ctrlfile.write(c % tuple(pullData(fs, localdata)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 except KeyError:
196 import sys
197 (type, value, traceback) = sys.exc_info()
198 bb.utils.unlockfile(lf)
199 ctrlfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600200 bb.fatal("Missing field for deb generation: %s" % value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201
202 # more fields
203
204 custom_fields_chunk = get_package_additional_metadata("deb", localdata)
205 if custom_fields_chunk is not None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600206 ctrlfile.write(custom_fields_chunk)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500207 ctrlfile.write("\n")
208
209 mapping_rename_hook(localdata)
210
211 def debian_cmp_remap(var):
212 # dpkg does not allow for '(' or ')' in a dependency name
213 # replace these instances with '__' and '__'
214 #
215 # In debian '>' and '<' do not mean what it appears they mean
216 # '<' = less or equal
217 # '>' = greater or equal
218 # adjust these to the '<<' and '>>' equivalents
219 #
220 for dep in var:
221 if '(' in dep:
222 newdep = dep.replace('(', '__')
223 newdep = newdep.replace(')', '__')
224 if newdep != dep:
225 var[newdep] = var[dep]
226 del var[dep]
227 for dep in var:
228 for i, v in enumerate(var[dep]):
229 if (v or "").startswith("< "):
230 var[dep][i] = var[dep][i].replace("< ", "<< ")
231 elif (v or "").startswith("> "):
232 var[dep][i] = var[dep][i].replace("> ", ">> ")
233
234 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS", True) or "")
235 debian_cmp_remap(rdepends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600236 for dep in list(rdepends.keys()):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500237 if dep == pkg:
238 del rdepends[dep]
239 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500240 if '*' in dep:
241 del rdepends[dep]
242 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS", True) or "")
243 debian_cmp_remap(rrecommends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600244 for dep in list(rrecommends.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500245 if '*' in dep:
246 del rrecommends[dep]
247 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS", True) or "")
248 debian_cmp_remap(rsuggests)
249 # Deliberately drop version information here, not wanted/supported by deb
250 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES", True) or ""), [])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600251 rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252 debian_cmp_remap(rprovides)
253 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES", True) or "")
254 debian_cmp_remap(rreplaces)
255 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS", True) or "")
256 debian_cmp_remap(rconflicts)
257 if rdepends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600258 ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500259 if rsuggests:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600260 ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500261 if rrecommends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600262 ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500263 if rprovides:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600264 ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500265 if rreplaces:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600266 ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500267 if rconflicts:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600268 ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500269 ctrlfile.close()
270
271 for script in ["preinst", "postinst", "prerm", "postrm"]:
272 scriptvar = localdata.getVar('pkg_%s' % script, True)
273 if not scriptvar:
274 continue
275 scriptvar = scriptvar.strip()
276 try:
277 scriptfile = open(os.path.join(controldir, script), 'w')
278 except OSError:
279 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600280 bb.fatal("unable to open %s script file for writing" % script)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281
282 if scriptvar.startswith("#!"):
283 pos = scriptvar.find("\n") + 1
284 scriptfile.write(scriptvar[:pos])
285 else:
286 pos = 0
287 scriptfile.write("#!/bin/sh\n")
288
289 # Prevent the prerm/postrm scripts from being run during an upgrade
290 if script in ('prerm', 'postrm'):
291 scriptfile.write('[ "$1" != "upgrade" ] || exit 0\n')
292
293 scriptfile.write(scriptvar[pos:])
294 scriptfile.write('\n')
295 scriptfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600296 os.chmod(os.path.join(controldir, script), 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500297
298 conffiles_str = ' '.join(get_conffiles(pkg, d))
299 if conffiles_str:
300 try:
301 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
302 except OSError:
303 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600304 bb.fatal("unable to open conffiles for writing")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500305 for f in conffiles_str.split():
306 if os.path.exists(oe.path.join(root, f)):
307 conffiles.write('%s\n' % f)
308 conffiles.close()
309
310 os.chdir(basedir)
311 ret = subprocess.call("PATH=\"%s\" dpkg-deb -b %s %s" % (localdata.getVar("PATH", True), root, pkgoutdir), shell=True)
312 if ret != 0:
313 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600314 bb.fatal("dpkg-deb execution failed")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500315
316 cleanupcontrol(root)
317 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600318 os.chdir(oldcwd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500319}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500320# Indirect references to these vars
321do_package_write_deb[vardeps] += "PKGV PKGR PKGV DESCRIPTION SECTION PRIORITY MAINTAINER DPKG_ARCH PN HOMEPAGE"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500322# Otherwise allarch packages may change depending on override configuration
323do_package_deb[vardepsexclude] = "OVERRIDES"
324
325
326SSTATETASKS += "do_package_write_deb"
327do_package_write_deb[sstate-inputdirs] = "${PKGWRITEDIRDEB}"
328do_package_write_deb[sstate-outputdirs] = "${DEPLOY_DIR_DEB}"
329
330python do_package_write_deb_setscene () {
331 tmpdir = d.getVar('TMPDIR', True)
332
333 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
334 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
335
336 sstate_setscene(d)
337}
338addtask do_package_write_deb_setscene
339
340python () {
341 if d.getVar('PACKAGES', True) != '':
342 deps = ' dpkg-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot'
343 d.appendVarFlag('do_package_write_deb', 'depends', deps)
344 d.setVarFlag('do_package_write_deb', 'fakeroot', "1")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500345}
346
347python do_package_write_deb () {
348 bb.build.exec_func("read_subpackage_metadata", d)
349 bb.build.exec_func("do_package_deb", d)
350}
351do_package_write_deb[dirs] = "${PKGWRITEDIRDEB}"
352do_package_write_deb[cleandirs] = "${PKGWRITEDIRDEB}"
353do_package_write_deb[umask] = "022"
354addtask package_write_deb after do_packagedata do_package
355
356
357PACKAGEINDEXDEPS += "dpkg-native:do_populate_sysroot"
358PACKAGEINDEXDEPS += "apt-native:do_populate_sysroot"
359
360do_build[recrdeptask] += "do_package_write_deb"