blob: b3d8ce330e8fece247bc61135300e843571ca647 [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 Bishop08902b02019-08-20 09:16:51 -04009DPKG_BUILDCMD ??= "dpkg-deb"
10
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011DPKG_ARCH ?= "${@debian_arch_map(d.getVar('TARGET_ARCH'), d.getVar('TUNE_FEATURES'))}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050012DPKG_ARCH[vardepvalue] = "${DPKG_ARCH}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013
14PKGWRITEDIRDEB = "${WORKDIR}/deploy-debs"
15
16APTCONF_TARGET = "${WORKDIR}"
17
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018APT_ARGS = "${@['', '--no-install-recommends'][d.getVar("NO_RECOMMENDATIONS") == "1"]}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020def debian_arch_map(arch, tune):
21 tune_features = tune.split()
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022 if arch == "allarch":
23 return "all"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050024 if arch in ["i586", "i686"]:
25 return "i386"
26 if arch == "x86_64":
27 if "mx32" in tune_features:
28 return "x32"
29 return "amd64"
30 if arch.startswith("mips"):
31 endian = ["el", ""]["bigendian" in tune_features]
32 if "n64" in tune_features:
33 return "mips64" + endian
34 if "n32" in tune_features:
35 return "mipsn32" + endian
36 return "mips" + endian
37 if arch == "powerpc":
38 return arch + ["", "spe"]["spe" in tune_features]
39 if arch == "aarch64":
40 return "arm64"
41 if arch == "arm":
42 return arch + ["el", "hf"]["callconvention-hard" in tune_features]
43 return arch
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044
45python do_package_deb () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 packages = d.getVar('PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 if not packages:
48 bb.debug(1, "PACKAGES not defined, nothing to package")
49 return
50
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
53 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
54
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080055 oe.utils.multiprocess_launch(deb_write_pkg, packages.split(), d, extraargs=(d,))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056}
57do_package_deb[vardeps] += "deb_write_pkg"
58do_package_deb[vardepsexclude] = "BB_NUMBER_THREADS"
59
60def deb_write_pkg(pkg, d):
61 import re, copy
62 import textwrap
63 import subprocess
64 import collections
65 import codecs
66
67 outdir = d.getVar('PKGWRITEDIRDEB')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 pkgdest = d.getVar('PKGDEST')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069
70 def cleanupcontrol(root):
71 for p in ['CONTROL', 'DEBIAN']:
72 p = os.path.join(root, p)
73 if os.path.exists(p):
74 bb.utils.prunedir(p)
75
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 localdata = bb.data.createCopy(d)
77 root = "%s/%s" % (pkgdest, pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
Brad Bishopd7bf8c12018-02-25 22:55:05 -050079 lf = bb.utils.lockfile(root + ".lock")
80 try:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
82 localdata.setVar('ROOT', '')
83 localdata.setVar('ROOT_%s' % pkg, root)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 pkgname = localdata.getVar('PKG_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 if not pkgname:
86 pkgname = pkg
87 localdata.setVar('PKG', pkgname)
88
89 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
90
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 basedir = os.path.join(os.path.dirname(root))
92
Brad Bishop6e60e8b2018-02-01 10:27:11 -050093 pkgoutdir = os.path.join(outdir, localdata.getVar('PACKAGE_ARCH'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 bb.utils.mkdirhier(pkgoutdir)
95
96 os.chdir(root)
97 cleanupcontrol(root)
98 from glob import glob
99 g = glob('*')
100 if not g and localdata.getVar('ALLOW_EMPTY', False) != "1":
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500101 bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV'), localdata.getVar('PKGR')))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103
104 controldir = os.path.join(root, 'DEBIAN')
105 bb.utils.mkdirhier(controldir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600106 os.chmod(controldir, 0o755)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107
108 ctrlfile = codecs.open(os.path.join(controldir, 'control'), 'w', 'utf-8')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109
110 fields = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500111 pe = d.getVar('PKGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 if pe and int(pe) > 0:
113 fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']])
114 else:
115 fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']])
116 fields.append(["Description: %s\n", ['DESCRIPTION']])
117 fields.append(["Section: %s\n", ['SECTION']])
118 fields.append(["Priority: %s\n", ['PRIORITY']])
119 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
120 fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
121 fields.append(["OE: %s\n", ['PN']])
122 fields.append(["PackageArch: %s\n", ['PACKAGE_ARCH']])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123 if d.getVar('HOMEPAGE'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
125
126 # Package, Version, Maintainer, Description - mandatory
127 # Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
128
129
130 def pullData(l, d):
131 l2 = []
132 for i in l:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500133 data = d.getVar(i)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500134 if data is None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500135 raise KeyError(i)
136 if i == 'DPKG_ARCH' and d.getVar('PACKAGE_ARCH') == 'all':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137 data = 'all'
138 elif i == 'PACKAGE_ARCH' or i == 'DPKG_ARCH':
139 # The params in deb package control don't allow character
140 # `_', so change the arch's `_' to `-'. Such as `x86_64'
141 # -->`x86-64'
142 data = data.replace('_', '-')
143 l2.append(data)
144 return l2
145
146 ctrlfile.write("Package: %s\n" % pkgname)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500147 if d.getVar('PACKAGE_ARCH') == "all":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148 ctrlfile.write("Multi-Arch: foreign\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 # check for required fields
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150 for (c, fs) in fields:
151 # Special behavior for description...
152 if 'DESCRIPTION' in fs:
153 summary = localdata.getVar('SUMMARY') or localdata.getVar('DESCRIPTION') or "."
154 ctrlfile.write('Description: %s\n' % summary)
155 description = localdata.getVar('DESCRIPTION') or "."
156 description = textwrap.dedent(description).strip()
157 if '\\n' in description:
158 # Manually indent
159 for t in description.split('\\n'):
160 ctrlfile.write(' %s\n' % (t.strip() or '.'))
161 else:
162 # Auto indent
163 ctrlfile.write('%s\n' % textwrap.fill(description.strip(), width=74, initial_indent=' ', subsequent_indent=' '))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500165 else:
166 ctrlfile.write(c % tuple(pullData(fs, localdata)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167
168 # more fields
169
170 custom_fields_chunk = get_package_additional_metadata("deb", localdata)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500171 if custom_fields_chunk:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600172 ctrlfile.write(custom_fields_chunk)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173 ctrlfile.write("\n")
174
175 mapping_rename_hook(localdata)
176
177 def debian_cmp_remap(var):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500178 # dpkg does not allow for '(', ')' or ':' in a dependency name
179 # Replace any instances of them with '__'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180 #
181 # In debian '>' and '<' do not mean what it appears they mean
182 # '<' = less or equal
183 # '>' = greater or equal
184 # adjust these to the '<<' and '>>' equivalents
185 #
Brad Bishop316dfdd2018-06-25 12:45:53 -0400186 for dep in list(var.keys()):
187 if '(' in dep or '/' in dep:
188 newdep = re.sub(r'[(:)/]', '__', dep)
189 if newdep.startswith("__"):
190 newdep = "A" + newdep
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191 if newdep != dep:
192 var[newdep] = var[dep]
193 del var[dep]
194 for dep in var:
195 for i, v in enumerate(var[dep]):
196 if (v or "").startswith("< "):
197 var[dep][i] = var[dep][i].replace("< ", "<< ")
198 elif (v or "").startswith("> "):
199 var[dep][i] = var[dep][i].replace("> ", ">> ")
200
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500201 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202 debian_cmp_remap(rdepends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600203 for dep in list(rdepends.keys()):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500204 if dep == pkg:
205 del rdepends[dep]
206 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500207 if '*' in dep:
208 del rdepends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500209 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500210 debian_cmp_remap(rrecommends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600211 for dep in list(rrecommends.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500212 if '*' in dep:
213 del rrecommends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500214 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500215 debian_cmp_remap(rsuggests)
216 # Deliberately drop version information here, not wanted/supported by deb
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500217 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES") or ""), [])
218 # Remove file paths if any from rprovides, debian does not support custom providers
219 for key in list(rprovides.keys()):
220 if key.startswith('/'):
221 del rprovides[key]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600222 rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500223 debian_cmp_remap(rprovides)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500224 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225 debian_cmp_remap(rreplaces)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500226 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227 debian_cmp_remap(rconflicts)
228 if rdepends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600229 ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 if rsuggests:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600231 ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232 if rrecommends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600233 ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234 if rprovides:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600235 ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236 if rreplaces:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600237 ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238 if rconflicts:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600239 ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500240 ctrlfile.close()
241
242 for script in ["preinst", "postinst", "prerm", "postrm"]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500243 scriptvar = localdata.getVar('pkg_%s' % script)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500244 if not scriptvar:
245 continue
246 scriptvar = scriptvar.strip()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500247 scriptfile = open(os.path.join(controldir, script), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248
249 if scriptvar.startswith("#!"):
250 pos = scriptvar.find("\n") + 1
251 scriptfile.write(scriptvar[:pos])
252 else:
253 pos = 0
254 scriptfile.write("#!/bin/sh\n")
255
256 # Prevent the prerm/postrm scripts from being run during an upgrade
257 if script in ('prerm', 'postrm'):
258 scriptfile.write('[ "$1" != "upgrade" ] || exit 0\n')
259
260 scriptfile.write(scriptvar[pos:])
261 scriptfile.write('\n')
262 scriptfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600263 os.chmod(os.path.join(controldir, script), 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500264
265 conffiles_str = ' '.join(get_conffiles(pkg, d))
266 if conffiles_str:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500267 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268 for f in conffiles_str.split():
269 if os.path.exists(oe.path.join(root, f)):
270 conffiles.write('%s\n' % f)
271 conffiles.close()
272
273 os.chdir(basedir)
Brad Bishop08902b02019-08-20 09:16:51 -0400274 subprocess.check_output("PATH=\"%s\" %s -b %s %s" % (localdata.getVar("PATH"), localdata.getVar("DPKG_BUILDCMD"),
275 root, pkgoutdir),
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500276 stderr=subprocess.STDOUT,
277 shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500278
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500279 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500280 cleanupcontrol(root)
281 bb.utils.unlockfile(lf)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500282
283# Otherwise allarch packages may change depending on override configuration
284deb_write_pkg[vardepsexclude] = "OVERRIDES"
285
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500286# Have to list any variables referenced as X_<pkg> that aren't in pkgdata here
287DEBEXTRAVARS = "PKGV PKGR PKGV DESCRIPTION SECTION PRIORITY MAINTAINER DPKG_ARCH PN HOMEPAGE PACKAGE_ADD_METADATA_DEB"
288do_package_write_deb[vardeps] += "${@gen_packagevar(d, 'DEBEXTRAVARS')}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500289
290SSTATETASKS += "do_package_write_deb"
291do_package_write_deb[sstate-inputdirs] = "${PKGWRITEDIRDEB}"
292do_package_write_deb[sstate-outputdirs] = "${DEPLOY_DIR_DEB}"
293
294python do_package_write_deb_setscene () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500295 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500296
297 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
298 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
299
300 sstate_setscene(d)
301}
302addtask do_package_write_deb_setscene
303
304python () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500305 if d.getVar('PACKAGES') != '':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500306 deps = ' dpkg-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot'
307 d.appendVarFlag('do_package_write_deb', 'depends', deps)
308 d.setVarFlag('do_package_write_deb', 'fakeroot', "1")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500309}
310
311python do_package_write_deb () {
312 bb.build.exec_func("read_subpackage_metadata", d)
313 bb.build.exec_func("do_package_deb", d)
314}
315do_package_write_deb[dirs] = "${PKGWRITEDIRDEB}"
316do_package_write_deb[cleandirs] = "${PKGWRITEDIRDEB}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500317do_package_write_deb[depends] += "${@oe.utils.build_depends_string(d.getVar('PACKAGE_WRITE_DEPS'), 'do_populate_sysroot')}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500318addtask package_write_deb after do_packagedata do_package
319
320
321PACKAGEINDEXDEPS += "dpkg-native:do_populate_sysroot"
322PACKAGEINDEXDEPS += "apt-native:do_populate_sysroot"
323
324do_build[recrdeptask] += "do_package_write_deb"