blob: 2e8d17d3c71d05137dab704091f2a0f53d38f1ac [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 #
Brad Bishop316dfdd2018-06-25 12:45:53 -0400233 for dep in list(var.keys()):
234 if '(' in dep or '/' in dep:
235 newdep = re.sub(r'[(:)/]', '__', dep)
236 if newdep.startswith("__"):
237 newdep = "A" + newdep
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238 if newdep != dep:
239 var[newdep] = var[dep]
240 del var[dep]
241 for dep in var:
242 for i, v in enumerate(var[dep]):
243 if (v or "").startswith("< "):
244 var[dep][i] = var[dep][i].replace("< ", "<< ")
245 elif (v or "").startswith("> "):
246 var[dep][i] = var[dep][i].replace("> ", ">> ")
247
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500248 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249 debian_cmp_remap(rdepends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600250 for dep in list(rdepends.keys()):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500251 if dep == pkg:
252 del rdepends[dep]
253 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500254 if '*' in dep:
255 del rdepends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500256 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257 debian_cmp_remap(rrecommends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600258 for dep in list(rrecommends.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500259 if '*' in dep:
260 del rrecommends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262 debian_cmp_remap(rsuggests)
263 # Deliberately drop version information here, not wanted/supported by deb
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500264 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES") or ""), [])
265 # Remove file paths if any from rprovides, debian does not support custom providers
266 for key in list(rprovides.keys()):
267 if key.startswith('/'):
268 del rprovides[key]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600269 rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500270 debian_cmp_remap(rprovides)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500271 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500272 debian_cmp_remap(rreplaces)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500273 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500274 debian_cmp_remap(rconflicts)
275 if rdepends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600276 ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277 if rsuggests:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600278 ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500279 if rrecommends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600280 ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281 if rprovides:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600282 ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500283 if rreplaces:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600284 ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500285 if rconflicts:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600286 ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287 ctrlfile.close()
288
289 for script in ["preinst", "postinst", "prerm", "postrm"]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500290 scriptvar = localdata.getVar('pkg_%s' % script)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500291 if not scriptvar:
292 continue
293 scriptvar = scriptvar.strip()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500294 scriptfile = open(os.path.join(controldir, script), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500295
296 if scriptvar.startswith("#!"):
297 pos = scriptvar.find("\n") + 1
298 scriptfile.write(scriptvar[:pos])
299 else:
300 pos = 0
301 scriptfile.write("#!/bin/sh\n")
302
303 # Prevent the prerm/postrm scripts from being run during an upgrade
304 if script in ('prerm', 'postrm'):
305 scriptfile.write('[ "$1" != "upgrade" ] || exit 0\n')
306
307 scriptfile.write(scriptvar[pos:])
308 scriptfile.write('\n')
309 scriptfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600310 os.chmod(os.path.join(controldir, script), 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500311
312 conffiles_str = ' '.join(get_conffiles(pkg, d))
313 if conffiles_str:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500314 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500315 for f in conffiles_str.split():
316 if os.path.exists(oe.path.join(root, f)):
317 conffiles.write('%s\n' % f)
318 conffiles.close()
319
320 os.chdir(basedir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500321 subprocess.check_output("PATH=\"%s\" dpkg-deb -b %s %s" % (localdata.getVar("PATH"), root, pkgoutdir),
322 stderr=subprocess.STDOUT,
323 shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500324
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500325 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500326 cleanupcontrol(root)
327 bb.utils.unlockfile(lf)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500328
329# Otherwise allarch packages may change depending on override configuration
330deb_write_pkg[vardepsexclude] = "OVERRIDES"
331
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500332# Indirect references to these vars
333do_package_write_deb[vardeps] += "PKGV PKGR PKGV DESCRIPTION SECTION PRIORITY MAINTAINER DPKG_ARCH PN HOMEPAGE"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500334
335SSTATETASKS += "do_package_write_deb"
336do_package_write_deb[sstate-inputdirs] = "${PKGWRITEDIRDEB}"
337do_package_write_deb[sstate-outputdirs] = "${DEPLOY_DIR_DEB}"
338
339python do_package_write_deb_setscene () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500340 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500341
342 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
343 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
344
345 sstate_setscene(d)
346}
347addtask do_package_write_deb_setscene
348
349python () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500350 if d.getVar('PACKAGES') != '':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500351 deps = ' dpkg-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot'
352 d.appendVarFlag('do_package_write_deb', 'depends', deps)
353 d.setVarFlag('do_package_write_deb', 'fakeroot', "1")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500354}
355
356python do_package_write_deb () {
357 bb.build.exec_func("read_subpackage_metadata", d)
358 bb.build.exec_func("do_package_deb", d)
359}
360do_package_write_deb[dirs] = "${PKGWRITEDIRDEB}"
361do_package_write_deb[cleandirs] = "${PKGWRITEDIRDEB}"
362do_package_write_deb[umask] = "022"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500363do_package_write_deb[depends] += "${@oe.utils.build_depends_string(d.getVar('PACKAGE_WRITE_DEPS'), 'do_populate_sysroot')}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500364addtask package_write_deb after do_packagedata do_package
365
366
367PACKAGEINDEXDEPS += "dpkg-native:do_populate_sysroot"
368PACKAGEINDEXDEPS += "apt-native:do_populate_sysroot"
369
370do_build[recrdeptask] += "do_package_write_deb"