blob: 5d297939b6ccb8e23d7b50440b5f0c0138129a0a [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
Brad Bishop6e60e8b2018-02-01 10:27:11 -05009DPKG_ARCH ?= "${@debian_arch_map(d.getVar('TARGET_ARCH'), d.getVar('TUNE_FEATURES'))}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050010DPKG_ARCH[vardepvalue] = "${DPKG_ARCH}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
12PKGWRITEDIRDEB = "${WORKDIR}/deploy-debs"
13
14APTCONF_TARGET = "${WORKDIR}"
15
Brad Bishop6e60e8b2018-02-01 10:27:11 -050016APT_ARGS = "${@['', '--no-install-recommends'][d.getVar("NO_RECOMMENDATIONS") == "1"]}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050018def debian_arch_map(arch, tune):
19 tune_features = tune.split()
Brad Bishop6e60e8b2018-02-01 10:27:11 -050020 if arch == "allarch":
21 return "all"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050022 if arch in ["i586", "i686"]:
23 return "i386"
24 if arch == "x86_64":
25 if "mx32" in tune_features:
26 return "x32"
27 return "amd64"
28 if arch.startswith("mips"):
29 endian = ["el", ""]["bigendian" in tune_features]
30 if "n64" in tune_features:
31 return "mips64" + endian
32 if "n32" in tune_features:
33 return "mipsn32" + endian
34 return "mips" + endian
35 if arch == "powerpc":
36 return arch + ["", "spe"]["spe" in tune_features]
37 if arch == "aarch64":
38 return "arm64"
39 if arch == "arm":
40 return arch + ["el", "hf"]["callconvention-hard" in tune_features]
41 return arch
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042
43python do_package_deb () {
Brad Bishopd7bf8c12018-02-25 22:55:05 -050044
45 import multiprocessing
46 import traceback
47
48 class DebianWritePkgProcess(multiprocessing.Process):
49 def __init__(self, *args, **kwargs):
50 multiprocessing.Process.__init__(self, *args, **kwargs)
51 self._pconn, self._cconn = multiprocessing.Pipe()
52 self._exception = None
53
54 def run(self):
55 try:
56 multiprocessing.Process.run(self)
57 self._cconn.send(None)
58 except Exception as e:
59 tb = traceback.format_exc()
60 self._cconn.send((e, tb))
61
62 @property
63 def exception(self):
64 if self._pconn.poll():
65 self._exception = self._pconn.recv()
66 return self._exception
Patrick Williamsc0f7c042017-02-23 20:41:17 -060067
68 oldcwd = os.getcwd()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070 packages = d.getVar('PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 if not packages:
72 bb.debug(1, "PACKAGES not defined, nothing to package")
73 return
74
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050079 max_process = int(d.getVar("BB_NUMBER_THREADS") or os.cpu_count() or 1)
80 launched = []
81 error = None
82 pkgs = packages.split()
83 while not error and pkgs:
84 if len(launched) < max_process:
85 p = DebianWritePkgProcess(target=deb_write_pkg, args=(pkgs.pop(), d))
86 p.start()
87 launched.append(p)
88 for q in launched:
89 # The finished processes are joined when calling is_alive()
90 if not q.is_alive():
91 launched.remove(q)
92 if q.exception:
93 error, traceback = q.exception
94 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096 for p in launched:
97 p.join()
98
99 os.chdir(oldcwd)
100
101 if error:
102 raise error
103}
104do_package_deb[vardeps] += "deb_write_pkg"
105do_package_deb[vardepsexclude] = "BB_NUMBER_THREADS"
106
107def deb_write_pkg(pkg, d):
108 import re, copy
109 import textwrap
110 import subprocess
111 import collections
112 import codecs
113
114 outdir = d.getVar('PKGWRITEDIRDEB')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 pkgdest = d.getVar('PKGDEST')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
117 def cleanupcontrol(root):
118 for p in ['CONTROL', 'DEBIAN']:
119 p = os.path.join(root, p)
120 if os.path.exists(p):
121 bb.utils.prunedir(p)
122
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500123 localdata = bb.data.createCopy(d)
124 root = "%s/%s" % (pkgdest, pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126 lf = bb.utils.lockfile(root + ".lock")
127 try:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128
129 localdata.setVar('ROOT', '')
130 localdata.setVar('ROOT_%s' % pkg, root)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131 pkgname = localdata.getVar('PKG_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 if not pkgname:
133 pkgname = pkg
134 localdata.setVar('PKG', pkgname)
135
136 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
137
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 basedir = os.path.join(os.path.dirname(root))
139
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140 pkgoutdir = os.path.join(outdir, localdata.getVar('PACKAGE_ARCH'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 bb.utils.mkdirhier(pkgoutdir)
142
143 os.chdir(root)
144 cleanupcontrol(root)
145 from glob import glob
146 g = glob('*')
147 if not g and localdata.getVar('ALLOW_EMPTY', False) != "1":
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV'), localdata.getVar('PKGR')))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500149 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150
151 controldir = os.path.join(root, 'DEBIAN')
152 bb.utils.mkdirhier(controldir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600153 os.chmod(controldir, 0o755)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154
155 ctrlfile = codecs.open(os.path.join(controldir, 'control'), 'w', 'utf-8')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156
157 fields = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 pe = d.getVar('PKGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159 if pe and int(pe) > 0:
160 fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']])
161 else:
162 fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']])
163 fields.append(["Description: %s\n", ['DESCRIPTION']])
164 fields.append(["Section: %s\n", ['SECTION']])
165 fields.append(["Priority: %s\n", ['PRIORITY']])
166 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
167 fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
168 fields.append(["OE: %s\n", ['PN']])
169 fields.append(["PackageArch: %s\n", ['PACKAGE_ARCH']])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 if d.getVar('HOMEPAGE'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
172
173 # Package, Version, Maintainer, Description - mandatory
174 # Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
175
176
177 def pullData(l, d):
178 l2 = []
179 for i in l:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500180 data = d.getVar(i)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181 if data is None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182 raise KeyError(i)
183 if i == 'DPKG_ARCH' and d.getVar('PACKAGE_ARCH') == 'all':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184 data = 'all'
185 elif i == 'PACKAGE_ARCH' or i == 'DPKG_ARCH':
186 # The params in deb package control don't allow character
187 # `_', so change the arch's `_' to `-'. Such as `x86_64'
188 # -->`x86-64'
189 data = data.replace('_', '-')
190 l2.append(data)
191 return l2
192
193 ctrlfile.write("Package: %s\n" % pkgname)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194 if d.getVar('PACKAGE_ARCH') == "all":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500195 ctrlfile.write("Multi-Arch: foreign\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196 # check for required fields
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500197 for (c, fs) in fields:
198 # Special behavior for description...
199 if 'DESCRIPTION' in fs:
200 summary = localdata.getVar('SUMMARY') or localdata.getVar('DESCRIPTION') or "."
201 ctrlfile.write('Description: %s\n' % summary)
202 description = localdata.getVar('DESCRIPTION') or "."
203 description = textwrap.dedent(description).strip()
204 if '\\n' in description:
205 # Manually indent
206 for t in description.split('\\n'):
207 ctrlfile.write(' %s\n' % (t.strip() or '.'))
208 else:
209 # Auto indent
210 ctrlfile.write('%s\n' % textwrap.fill(description.strip(), width=74, initial_indent=' ', subsequent_indent=' '))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500212 else:
213 ctrlfile.write(c % tuple(pullData(fs, localdata)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500214
215 # more fields
216
217 custom_fields_chunk = get_package_additional_metadata("deb", localdata)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500218 if custom_fields_chunk:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600219 ctrlfile.write(custom_fields_chunk)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220 ctrlfile.write("\n")
221
222 mapping_rename_hook(localdata)
223
224 def debian_cmp_remap(var):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500225 # dpkg does not allow for '(', ')' or ':' in a dependency name
226 # Replace any instances of them with '__'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227 #
228 # In debian '>' and '<' do not mean what it appears they mean
229 # '<' = less or equal
230 # '>' = greater or equal
231 # adjust these to the '<<' and '>>' equivalents
232 #
233 for dep in var:
234 if '(' in dep:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500235 newdep = re.sub(r'[(:)]', '__', dep)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236 if newdep != dep:
237 var[newdep] = var[dep]
238 del var[dep]
239 for dep in var:
240 for i, v in enumerate(var[dep]):
241 if (v or "").startswith("< "):
242 var[dep][i] = var[dep][i].replace("< ", "<< ")
243 elif (v or "").startswith("> "):
244 var[dep][i] = var[dep][i].replace("> ", ">> ")
245
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500246 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500247 debian_cmp_remap(rdepends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600248 for dep in list(rdepends.keys()):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500249 if dep == pkg:
250 del rdepends[dep]
251 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252 if '*' in dep:
253 del rdepends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500254 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255 debian_cmp_remap(rrecommends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600256 for dep in list(rrecommends.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257 if '*' in dep:
258 del rrecommends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500259 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260 debian_cmp_remap(rsuggests)
261 # Deliberately drop version information here, not wanted/supported by deb
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500262 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES") or ""), [])
263 # Remove file paths if any from rprovides, debian does not support custom providers
264 for key in list(rprovides.keys()):
265 if key.startswith('/'):
266 del rprovides[key]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600267 rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268 debian_cmp_remap(rprovides)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500269 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500270 debian_cmp_remap(rreplaces)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500271 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500272 debian_cmp_remap(rconflicts)
273 if rdepends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600274 ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500275 if rsuggests:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600276 ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277 if rrecommends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600278 ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500279 if rprovides:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600280 ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281 if rreplaces:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600282 ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500283 if rconflicts:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600284 ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500285 ctrlfile.close()
286
287 for script in ["preinst", "postinst", "prerm", "postrm"]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500288 scriptvar = localdata.getVar('pkg_%s' % script)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500289 if not scriptvar:
290 continue
291 scriptvar = scriptvar.strip()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500292 scriptfile = open(os.path.join(controldir, script), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500293
294 if scriptvar.startswith("#!"):
295 pos = scriptvar.find("\n") + 1
296 scriptfile.write(scriptvar[:pos])
297 else:
298 pos = 0
299 scriptfile.write("#!/bin/sh\n")
300
301 # Prevent the prerm/postrm scripts from being run during an upgrade
302 if script in ('prerm', 'postrm'):
303 scriptfile.write('[ "$1" != "upgrade" ] || exit 0\n')
304
305 scriptfile.write(scriptvar[pos:])
306 scriptfile.write('\n')
307 scriptfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600308 os.chmod(os.path.join(controldir, script), 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500309
310 conffiles_str = ' '.join(get_conffiles(pkg, d))
311 if conffiles_str:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500312 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500313 for f in conffiles_str.split():
314 if os.path.exists(oe.path.join(root, f)):
315 conffiles.write('%s\n' % f)
316 conffiles.close()
317
318 os.chdir(basedir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500319 subprocess.check_output("PATH=\"%s\" dpkg-deb -b %s %s" % (localdata.getVar("PATH"), root, pkgoutdir),
320 stderr=subprocess.STDOUT,
321 shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500322
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500323 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500324 cleanupcontrol(root)
325 bb.utils.unlockfile(lf)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500326
327# Otherwise allarch packages may change depending on override configuration
328deb_write_pkg[vardepsexclude] = "OVERRIDES"
329
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500330# Indirect references to these vars
331do_package_write_deb[vardeps] += "PKGV PKGR PKGV DESCRIPTION SECTION PRIORITY MAINTAINER DPKG_ARCH PN HOMEPAGE"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500332
333SSTATETASKS += "do_package_write_deb"
334do_package_write_deb[sstate-inputdirs] = "${PKGWRITEDIRDEB}"
335do_package_write_deb[sstate-outputdirs] = "${DEPLOY_DIR_DEB}"
336
337python do_package_write_deb_setscene () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500338 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500339
340 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
341 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
342
343 sstate_setscene(d)
344}
345addtask do_package_write_deb_setscene
346
347python () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500348 if d.getVar('PACKAGES') != '':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500349 deps = ' dpkg-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot'
350 d.appendVarFlag('do_package_write_deb', 'depends', deps)
351 d.setVarFlag('do_package_write_deb', 'fakeroot', "1")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500352}
353
354python do_package_write_deb () {
355 bb.build.exec_func("read_subpackage_metadata", d)
356 bb.build.exec_func("do_package_deb", d)
357}
358do_package_write_deb[dirs] = "${PKGWRITEDIRDEB}"
359do_package_write_deb[cleandirs] = "${PKGWRITEDIRDEB}"
360do_package_write_deb[umask] = "022"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500361do_package_write_deb[depends] += "${@oe.utils.build_depends_string(d.getVar('PACKAGE_WRITE_DEPS'), 'do_populate_sysroot')}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500362addtask package_write_deb after do_packagedata do_package
363
364
365PACKAGEINDEXDEPS += "dpkg-native:do_populate_sysroot"
366PACKAGEINDEXDEPS += "apt-native:do_populate_sysroot"
367
368do_build[recrdeptask] += "do_package_write_deb"