blob: 6f815916531e74af4b6b6697ac55be3cc5f64e13 [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 Bishop6e60e8b2018-02-01 10:27:11 -050044 packages = d.getVar('PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 if not packages:
46 bb.debug(1, "PACKAGES not defined, nothing to package")
47 return
48
Brad Bishop6e60e8b2018-02-01 10:27:11 -050049 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
51 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
52
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080053 oe.utils.multiprocess_launch(deb_write_pkg, packages.split(), d, extraargs=(d,))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054}
55do_package_deb[vardeps] += "deb_write_pkg"
56do_package_deb[vardepsexclude] = "BB_NUMBER_THREADS"
57
58def deb_write_pkg(pkg, d):
59 import re, copy
60 import textwrap
61 import subprocess
62 import collections
63 import codecs
64
65 outdir = d.getVar('PKGWRITEDIRDEB')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050066 pkgdest = d.getVar('PKGDEST')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067
68 def cleanupcontrol(root):
69 for p in ['CONTROL', 'DEBIAN']:
70 p = os.path.join(root, p)
71 if os.path.exists(p):
72 bb.utils.prunedir(p)
73
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074 localdata = bb.data.createCopy(d)
75 root = "%s/%s" % (pkgdest, pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 lf = bb.utils.lockfile(root + ".lock")
78 try:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079
80 localdata.setVar('ROOT', '')
81 localdata.setVar('ROOT_%s' % pkg, root)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 pkgname = localdata.getVar('PKG_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 if not pkgname:
84 pkgname = pkg
85 localdata.setVar('PKG', pkgname)
86
87 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
88
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 basedir = os.path.join(os.path.dirname(root))
90
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 pkgoutdir = os.path.join(outdir, localdata.getVar('PACKAGE_ARCH'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 bb.utils.mkdirhier(pkgoutdir)
93
94 os.chdir(root)
95 cleanupcontrol(root)
96 from glob import glob
97 g = glob('*')
98 if not g and localdata.getVar('ALLOW_EMPTY', False) != "1":
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV'), localdata.getVar('PKGR')))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500100 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
102 controldir = os.path.join(root, 'DEBIAN')
103 bb.utils.mkdirhier(controldir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600104 os.chmod(controldir, 0o755)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105
106 ctrlfile = codecs.open(os.path.join(controldir, 'control'), 'w', 'utf-8')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107
108 fields = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 pe = d.getVar('PKGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 if pe and int(pe) > 0:
111 fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']])
112 else:
113 fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']])
114 fields.append(["Description: %s\n", ['DESCRIPTION']])
115 fields.append(["Section: %s\n", ['SECTION']])
116 fields.append(["Priority: %s\n", ['PRIORITY']])
117 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
118 fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
119 fields.append(["OE: %s\n", ['PN']])
120 fields.append(["PackageArch: %s\n", ['PACKAGE_ARCH']])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 if d.getVar('HOMEPAGE'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
123
124 # Package, Version, Maintainer, Description - mandatory
125 # Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
126
127
128 def pullData(l, d):
129 l2 = []
130 for i in l:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131 data = d.getVar(i)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 if data is None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500133 raise KeyError(i)
134 if i == 'DPKG_ARCH' and d.getVar('PACKAGE_ARCH') == 'all':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135 data = 'all'
136 elif i == 'PACKAGE_ARCH' or i == 'DPKG_ARCH':
137 # The params in deb package control don't allow character
138 # `_', so change the arch's `_' to `-'. Such as `x86_64'
139 # -->`x86-64'
140 data = data.replace('_', '-')
141 l2.append(data)
142 return l2
143
144 ctrlfile.write("Package: %s\n" % pkgname)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145 if d.getVar('PACKAGE_ARCH') == "all":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146 ctrlfile.write("Multi-Arch: foreign\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147 # check for required fields
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 for (c, fs) in fields:
149 # Special behavior for description...
150 if 'DESCRIPTION' in fs:
151 summary = localdata.getVar('SUMMARY') or localdata.getVar('DESCRIPTION') or "."
152 ctrlfile.write('Description: %s\n' % summary)
153 description = localdata.getVar('DESCRIPTION') or "."
154 description = textwrap.dedent(description).strip()
155 if '\\n' in description:
156 # Manually indent
157 for t in description.split('\\n'):
158 ctrlfile.write(' %s\n' % (t.strip() or '.'))
159 else:
160 # Auto indent
161 ctrlfile.write('%s\n' % textwrap.fill(description.strip(), width=74, initial_indent=' ', subsequent_indent=' '))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500163 else:
164 ctrlfile.write(c % tuple(pullData(fs, localdata)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165
166 # more fields
167
168 custom_fields_chunk = get_package_additional_metadata("deb", localdata)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500169 if custom_fields_chunk:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600170 ctrlfile.write(custom_fields_chunk)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 ctrlfile.write("\n")
172
173 mapping_rename_hook(localdata)
174
175 def debian_cmp_remap(var):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500176 # dpkg does not allow for '(', ')' or ':' in a dependency name
177 # Replace any instances of them with '__'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500178 #
179 # In debian '>' and '<' do not mean what it appears they mean
180 # '<' = less or equal
181 # '>' = greater or equal
182 # adjust these to the '<<' and '>>' equivalents
183 #
Brad Bishop316dfdd2018-06-25 12:45:53 -0400184 for dep in list(var.keys()):
185 if '(' in dep or '/' in dep:
186 newdep = re.sub(r'[(:)/]', '__', dep)
187 if newdep.startswith("__"):
188 newdep = "A" + newdep
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 if newdep != dep:
190 var[newdep] = var[dep]
191 del var[dep]
192 for dep in var:
193 for i, v in enumerate(var[dep]):
194 if (v or "").startswith("< "):
195 var[dep][i] = var[dep][i].replace("< ", "<< ")
196 elif (v or "").startswith("> "):
197 var[dep][i] = var[dep][i].replace("> ", ">> ")
198
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500199 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200 debian_cmp_remap(rdepends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600201 for dep in list(rdepends.keys()):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500202 if dep == pkg:
203 del rdepends[dep]
204 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 if '*' in dep:
206 del rdepends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500207 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 debian_cmp_remap(rrecommends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600209 for dep in list(rrecommends.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500210 if '*' in dep:
211 del rrecommends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500212 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500213 debian_cmp_remap(rsuggests)
214 # Deliberately drop version information here, not wanted/supported by deb
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500215 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES") or ""), [])
216 # Remove file paths if any from rprovides, debian does not support custom providers
217 for key in list(rprovides.keys()):
218 if key.startswith('/'):
219 del rprovides[key]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600220 rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 debian_cmp_remap(rprovides)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500222 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500223 debian_cmp_remap(rreplaces)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500224 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225 debian_cmp_remap(rconflicts)
226 if rdepends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600227 ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 if rsuggests:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600229 ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 if rrecommends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600231 ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232 if rprovides:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600233 ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234 if rreplaces:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600235 ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236 if rconflicts:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600237 ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238 ctrlfile.close()
239
240 for script in ["preinst", "postinst", "prerm", "postrm"]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500241 scriptvar = localdata.getVar('pkg_%s' % script)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500242 if not scriptvar:
243 continue
244 scriptvar = scriptvar.strip()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500245 scriptfile = open(os.path.join(controldir, script), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500246
247 if scriptvar.startswith("#!"):
248 pos = scriptvar.find("\n") + 1
249 scriptfile.write(scriptvar[:pos])
250 else:
251 pos = 0
252 scriptfile.write("#!/bin/sh\n")
253
254 # Prevent the prerm/postrm scripts from being run during an upgrade
255 if script in ('prerm', 'postrm'):
256 scriptfile.write('[ "$1" != "upgrade" ] || exit 0\n')
257
258 scriptfile.write(scriptvar[pos:])
259 scriptfile.write('\n')
260 scriptfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600261 os.chmod(os.path.join(controldir, script), 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262
263 conffiles_str = ' '.join(get_conffiles(pkg, d))
264 if conffiles_str:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500265 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266 for f in conffiles_str.split():
267 if os.path.exists(oe.path.join(root, f)):
268 conffiles.write('%s\n' % f)
269 conffiles.close()
270
271 os.chdir(basedir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500272 subprocess.check_output("PATH=\"%s\" dpkg-deb -b %s %s" % (localdata.getVar("PATH"), root, pkgoutdir),
273 stderr=subprocess.STDOUT,
274 shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500275
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500276 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277 cleanupcontrol(root)
278 bb.utils.unlockfile(lf)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500279
280# Otherwise allarch packages may change depending on override configuration
281deb_write_pkg[vardepsexclude] = "OVERRIDES"
282
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500283# Indirect references to these vars
284do_package_write_deb[vardeps] += "PKGV PKGR PKGV DESCRIPTION SECTION PRIORITY MAINTAINER DPKG_ARCH PN HOMEPAGE"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500285
286SSTATETASKS += "do_package_write_deb"
287do_package_write_deb[sstate-inputdirs] = "${PKGWRITEDIRDEB}"
288do_package_write_deb[sstate-outputdirs] = "${DEPLOY_DIR_DEB}"
289
290python do_package_write_deb_setscene () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500291 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500292
293 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
294 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
295
296 sstate_setscene(d)
297}
298addtask do_package_write_deb_setscene
299
300python () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500301 if d.getVar('PACKAGES') != '':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500302 deps = ' dpkg-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot'
303 d.appendVarFlag('do_package_write_deb', 'depends', deps)
304 d.setVarFlag('do_package_write_deb', 'fakeroot', "1")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500305}
306
307python do_package_write_deb () {
308 bb.build.exec_func("read_subpackage_metadata", d)
309 bb.build.exec_func("do_package_deb", d)
310}
311do_package_write_deb[dirs] = "${PKGWRITEDIRDEB}"
312do_package_write_deb[cleandirs] = "${PKGWRITEDIRDEB}"
313do_package_write_deb[umask] = "022"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500314do_package_write_deb[depends] += "${@oe.utils.build_depends_string(d.getVar('PACKAGE_WRITE_DEPS'), 'do_populate_sysroot')}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500315addtask package_write_deb after do_packagedata do_package
316
317
318PACKAGEINDEXDEPS += "dpkg-native:do_populate_sysroot"
319PACKAGEINDEXDEPS += "apt-native:do_populate_sysroot"
320
321do_build[recrdeptask] += "do_package_write_deb"