blob: eacabcdb61a0054957a08ae6691a6b0b5b917326 [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#
43# install a bunch of packages using apt
44# the following shell variables needs to be set before calling this func:
45# INSTALL_ROOTFS_DEB - install root dir
46# INSTALL_BASEARCH_DEB - install base architecutre
47# INSTALL_ARCHS_DEB - list of available archs
48# INSTALL_PACKAGES_NORMAL_DEB - packages to be installed
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050049# INSTALL_PACKAGES_ATTEMPTONLY_DEB - packages attempted to be installed only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050# INSTALL_PACKAGES_LINGUAS_DEB - additional packages for uclibc
51# INSTALL_TASK_DEB - task name
52
53python do_package_deb () {
54 import re, copy
55 import textwrap
56 import subprocess
Patrick Williamsc0f7c042017-02-23 20:41:17 -060057 import collections
Brad Bishop6e60e8b2018-02-01 10:27:11 -050058 import codecs
Patrick Williamsc0f7c042017-02-23 20:41:17 -060059
60 oldcwd = os.getcwd()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061
Brad Bishop6e60e8b2018-02-01 10:27:11 -050062 workdir = d.getVar('WORKDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 if not workdir:
64 bb.error("WORKDIR not defined, unable to package")
65 return
66
Brad Bishop6e60e8b2018-02-01 10:27:11 -050067 outdir = d.getVar('PKGWRITEDIRDEB')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 if not outdir:
69 bb.error("PKGWRITEDIRDEB not defined, unable to package")
70 return
71
Brad Bishop6e60e8b2018-02-01 10:27:11 -050072 packages = d.getVar('PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 if not packages:
74 bb.debug(1, "PACKAGES not defined, nothing to package")
75 return
76
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
79 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
80 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
81
82 if packages == []:
83 bb.debug(1, "No packages; nothing to do")
84 return
85
Brad Bishop6e60e8b2018-02-01 10:27:11 -050086 pkgdest = d.getVar('PKGDEST')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
88 def cleanupcontrol(root):
89 for p in ['CONTROL', 'DEBIAN']:
90 p = os.path.join(root, p)
91 if os.path.exists(p):
92 bb.utils.prunedir(p)
93
94 for pkg in packages.split():
95 localdata = bb.data.createCopy(d)
96 root = "%s/%s" % (pkgdest, pkg)
97
98 lf = bb.utils.lockfile(root + ".lock")
99
100 localdata.setVar('ROOT', '')
101 localdata.setVar('ROOT_%s' % pkg, root)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102 pkgname = localdata.getVar('PKG_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 if not pkgname:
104 pkgname = pkg
105 localdata.setVar('PKG', pkgname)
106
107 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
108
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 basedir = os.path.join(os.path.dirname(root))
110
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500111 pkgoutdir = os.path.join(outdir, localdata.getVar('PACKAGE_ARCH'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 bb.utils.mkdirhier(pkgoutdir)
113
114 os.chdir(root)
115 cleanupcontrol(root)
116 from glob import glob
117 g = glob('*')
118 if not g and localdata.getVar('ALLOW_EMPTY', False) != "1":
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119 bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV'), localdata.getVar('PKGR')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120 bb.utils.unlockfile(lf)
121 continue
122
123 controldir = os.path.join(root, 'DEBIAN')
124 bb.utils.mkdirhier(controldir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600125 os.chmod(controldir, 0o755)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500126
127 ctrlfile = codecs.open(os.path.join(controldir, 'control'), 'w', 'utf-8')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128
129 fields = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500130 pe = d.getVar('PKGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131 if pe and int(pe) > 0:
132 fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']])
133 else:
134 fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']])
135 fields.append(["Description: %s\n", ['DESCRIPTION']])
136 fields.append(["Section: %s\n", ['SECTION']])
137 fields.append(["Priority: %s\n", ['PRIORITY']])
138 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
139 fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
140 fields.append(["OE: %s\n", ['PN']])
141 fields.append(["PackageArch: %s\n", ['PACKAGE_ARCH']])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500142 if d.getVar('HOMEPAGE'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500143 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
144
145 # Package, Version, Maintainer, Description - mandatory
146 # Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
147
148
149 def pullData(l, d):
150 l2 = []
151 for i in l:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500152 data = d.getVar(i)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153 if data is None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154 raise KeyError(i)
155 if i == 'DPKG_ARCH' and d.getVar('PACKAGE_ARCH') == 'all':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 data = 'all'
157 elif i == 'PACKAGE_ARCH' or i == 'DPKG_ARCH':
158 # The params in deb package control don't allow character
159 # `_', so change the arch's `_' to `-'. Such as `x86_64'
160 # -->`x86-64'
161 data = data.replace('_', '-')
162 l2.append(data)
163 return l2
164
165 ctrlfile.write("Package: %s\n" % pkgname)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500166 if d.getVar('PACKAGE_ARCH') == "all":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167 ctrlfile.write("Multi-Arch: foreign\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 # check for required fields
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500169 for (c, fs) in fields:
170 # Special behavior for description...
171 if 'DESCRIPTION' in fs:
172 summary = localdata.getVar('SUMMARY') or localdata.getVar('DESCRIPTION') or "."
173 ctrlfile.write('Description: %s\n' % summary)
174 description = localdata.getVar('DESCRIPTION') or "."
175 description = textwrap.dedent(description).strip()
176 if '\\n' in description:
177 # Manually indent
178 for t in description.split('\\n'):
179 ctrlfile.write(' %s\n' % (t.strip() or '.'))
180 else:
181 # Auto indent
182 ctrlfile.write('%s\n' % textwrap.fill(description.strip(), width=74, initial_indent=' ', subsequent_indent=' '))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500184 else:
185 ctrlfile.write(c % tuple(pullData(fs, localdata)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186
187 # more fields
188
189 custom_fields_chunk = get_package_additional_metadata("deb", localdata)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500190 if custom_fields_chunk:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600191 ctrlfile.write(custom_fields_chunk)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192 ctrlfile.write("\n")
193
194 mapping_rename_hook(localdata)
195
196 def debian_cmp_remap(var):
197 # dpkg does not allow for '(' or ')' in a dependency name
198 # replace these instances with '__' and '__'
199 #
200 # In debian '>' and '<' do not mean what it appears they mean
201 # '<' = less or equal
202 # '>' = greater or equal
203 # adjust these to the '<<' and '>>' equivalents
204 #
205 for dep in var:
206 if '(' in dep:
207 newdep = dep.replace('(', '__')
208 newdep = newdep.replace(')', '__')
209 if newdep != dep:
210 var[newdep] = var[dep]
211 del var[dep]
212 for dep in var:
213 for i, v in enumerate(var[dep]):
214 if (v or "").startswith("< "):
215 var[dep][i] = var[dep][i].replace("< ", "<< ")
216 elif (v or "").startswith("> "):
217 var[dep][i] = var[dep][i].replace("> ", ">> ")
218
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500219 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220 debian_cmp_remap(rdepends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600221 for dep in list(rdepends.keys()):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500222 if dep == pkg:
223 del rdepends[dep]
224 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225 if '*' in dep:
226 del rdepends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500227 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 debian_cmp_remap(rrecommends)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600229 for dep in list(rrecommends.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 if '*' in dep:
231 del rrecommends[dep]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500232 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 debian_cmp_remap(rsuggests)
234 # Deliberately drop version information here, not wanted/supported by deb
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500235 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES") or ""), [])
236 # Remove file paths if any from rprovides, debian does not support custom providers
237 for key in list(rprovides.keys()):
238 if key.startswith('/'):
239 del rprovides[key]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600240 rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 debian_cmp_remap(rprovides)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500242 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243 debian_cmp_remap(rreplaces)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500244 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500245 debian_cmp_remap(rconflicts)
246 if rdepends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600247 ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 if rsuggests:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600249 ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250 if rrecommends:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600251 ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252 if rprovides:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600253 ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500254 if rreplaces:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600255 ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256 if rconflicts:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600257 ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258 ctrlfile.close()
259
260 for script in ["preinst", "postinst", "prerm", "postrm"]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261 scriptvar = localdata.getVar('pkg_%s' % script)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262 if not scriptvar:
263 continue
264 scriptvar = scriptvar.strip()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500265 scriptfile = open(os.path.join(controldir, script), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266
267 if scriptvar.startswith("#!"):
268 pos = scriptvar.find("\n") + 1
269 scriptfile.write(scriptvar[:pos])
270 else:
271 pos = 0
272 scriptfile.write("#!/bin/sh\n")
273
274 # Prevent the prerm/postrm scripts from being run during an upgrade
275 if script in ('prerm', 'postrm'):
276 scriptfile.write('[ "$1" != "upgrade" ] || exit 0\n')
277
278 scriptfile.write(scriptvar[pos:])
279 scriptfile.write('\n')
280 scriptfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600281 os.chmod(os.path.join(controldir, script), 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500282
283 conffiles_str = ' '.join(get_conffiles(pkg, d))
284 if conffiles_str:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500285 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500286 for f in conffiles_str.split():
287 if os.path.exists(oe.path.join(root, f)):
288 conffiles.write('%s\n' % f)
289 conffiles.close()
290
291 os.chdir(basedir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500292 subprocess.check_output("PATH=\"%s\" dpkg-deb -b %s %s" % (localdata.getVar("PATH"), root, pkgoutdir), shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500293
294 cleanupcontrol(root)
295 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600296 os.chdir(oldcwd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500297}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500298# Indirect references to these vars
299do_package_write_deb[vardeps] += "PKGV PKGR PKGV DESCRIPTION SECTION PRIORITY MAINTAINER DPKG_ARCH PN HOMEPAGE"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500300# Otherwise allarch packages may change depending on override configuration
301do_package_deb[vardepsexclude] = "OVERRIDES"
302
303
304SSTATETASKS += "do_package_write_deb"
305do_package_write_deb[sstate-inputdirs] = "${PKGWRITEDIRDEB}"
306do_package_write_deb[sstate-outputdirs] = "${DEPLOY_DIR_DEB}"
307
308python do_package_write_deb_setscene () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500309 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500310
311 if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
312 os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
313
314 sstate_setscene(d)
315}
316addtask do_package_write_deb_setscene
317
318python () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500319 if d.getVar('PACKAGES') != '':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500320 deps = ' dpkg-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot'
321 d.appendVarFlag('do_package_write_deb', 'depends', deps)
322 d.setVarFlag('do_package_write_deb', 'fakeroot', "1")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500323}
324
325python do_package_write_deb () {
326 bb.build.exec_func("read_subpackage_metadata", d)
327 bb.build.exec_func("do_package_deb", d)
328}
329do_package_write_deb[dirs] = "${PKGWRITEDIRDEB}"
330do_package_write_deb[cleandirs] = "${PKGWRITEDIRDEB}"
331do_package_write_deb[umask] = "022"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500332do_package_write_deb[depends] += "${@oe.utils.build_depends_string(d.getVar('PACKAGE_WRITE_DEPS'), 'do_populate_sysroot')}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500333addtask package_write_deb after do_packagedata do_package
334
335
336PACKAGEINDEXDEPS += "dpkg-native:do_populate_sysroot"
337PACKAGEINDEXDEPS += "apt-native:do_populate_sysroot"
338
339do_build[recrdeptask] += "do_package_write_deb"