Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2006-2008 OpenedHand Ltd. |
| 3 | # |
| 4 | |
| 5 | inherit package |
| 6 | |
| 7 | IMAGE_PKGTYPE ?= "deb" |
| 8 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 9 | DPKG_ARCH ?= "${@debian_arch_map(d.getVar('TARGET_ARCH', True), d.getVar('TUNE_FEATURES', True))}" |
| 10 | DPKG_ARCH[vardepvalue] = "${DPKG_ARCH}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | |
| 12 | PKGWRITEDIRDEB = "${WORKDIR}/deploy-debs" |
| 13 | |
| 14 | APTCONF_TARGET = "${WORKDIR}" |
| 15 | |
| 16 | APT_ARGS = "${@['', '--no-install-recommends'][d.getVar("NO_RECOMMENDATIONS", True) == "1"]}" |
| 17 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 18 | def debian_arch_map(arch, tune): |
| 19 | tune_features = tune.split() |
| 20 | if arch in ["i586", "i686"]: |
| 21 | return "i386" |
| 22 | if arch == "x86_64": |
| 23 | if "mx32" in tune_features: |
| 24 | return "x32" |
| 25 | return "amd64" |
| 26 | if arch.startswith("mips"): |
| 27 | endian = ["el", ""]["bigendian" in tune_features] |
| 28 | if "n64" in tune_features: |
| 29 | return "mips64" + endian |
| 30 | if "n32" in tune_features: |
| 31 | return "mipsn32" + endian |
| 32 | return "mips" + endian |
| 33 | if arch == "powerpc": |
| 34 | return arch + ["", "spe"]["spe" in tune_features] |
| 35 | if arch == "aarch64": |
| 36 | return "arm64" |
| 37 | if arch == "arm": |
| 38 | return arch + ["el", "hf"]["callconvention-hard" in tune_features] |
| 39 | return arch |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | # |
| 41 | # install a bunch of packages using apt |
| 42 | # the following shell variables needs to be set before calling this func: |
| 43 | # INSTALL_ROOTFS_DEB - install root dir |
| 44 | # INSTALL_BASEARCH_DEB - install base architecutre |
| 45 | # INSTALL_ARCHS_DEB - list of available archs |
| 46 | # INSTALL_PACKAGES_NORMAL_DEB - packages to be installed |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 47 | # INSTALL_PACKAGES_ATTEMPTONLY_DEB - packages attempted to be installed only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | # INSTALL_PACKAGES_LINGUAS_DEB - additional packages for uclibc |
| 49 | # INSTALL_TASK_DEB - task name |
| 50 | |
| 51 | python do_package_deb () { |
| 52 | import re, copy |
| 53 | import textwrap |
| 54 | import subprocess |
| 55 | |
| 56 | workdir = d.getVar('WORKDIR', True) |
| 57 | if not workdir: |
| 58 | bb.error("WORKDIR not defined, unable to package") |
| 59 | return |
| 60 | |
| 61 | outdir = d.getVar('PKGWRITEDIRDEB', True) |
| 62 | if not outdir: |
| 63 | bb.error("PKGWRITEDIRDEB not defined, unable to package") |
| 64 | return |
| 65 | |
| 66 | packages = d.getVar('PACKAGES', True) |
| 67 | if not packages: |
| 68 | bb.debug(1, "PACKAGES not defined, nothing to package") |
| 69 | return |
| 70 | |
| 71 | tmpdir = d.getVar('TMPDIR', True) |
| 72 | |
| 73 | if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK): |
| 74 | os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN")) |
| 75 | |
| 76 | if packages == []: |
| 77 | bb.debug(1, "No packages; nothing to do") |
| 78 | return |
| 79 | |
| 80 | pkgdest = d.getVar('PKGDEST', True) |
| 81 | |
| 82 | def cleanupcontrol(root): |
| 83 | for p in ['CONTROL', 'DEBIAN']: |
| 84 | p = os.path.join(root, p) |
| 85 | if os.path.exists(p): |
| 86 | bb.utils.prunedir(p) |
| 87 | |
| 88 | for pkg in packages.split(): |
| 89 | localdata = bb.data.createCopy(d) |
| 90 | root = "%s/%s" % (pkgdest, pkg) |
| 91 | |
| 92 | lf = bb.utils.lockfile(root + ".lock") |
| 93 | |
| 94 | localdata.setVar('ROOT', '') |
| 95 | localdata.setVar('ROOT_%s' % pkg, root) |
| 96 | pkgname = localdata.getVar('PKG_%s' % pkg, True) |
| 97 | if not pkgname: |
| 98 | pkgname = pkg |
| 99 | localdata.setVar('PKG', pkgname) |
| 100 | |
| 101 | localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg) |
| 102 | |
| 103 | bb.data.update_data(localdata) |
| 104 | basedir = os.path.join(os.path.dirname(root)) |
| 105 | |
| 106 | pkgoutdir = os.path.join(outdir, localdata.getVar('PACKAGE_ARCH', True)) |
| 107 | bb.utils.mkdirhier(pkgoutdir) |
| 108 | |
| 109 | os.chdir(root) |
| 110 | cleanupcontrol(root) |
| 111 | from glob import glob |
| 112 | g = glob('*') |
| 113 | if not g and localdata.getVar('ALLOW_EMPTY', False) != "1": |
| 114 | bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV', True), localdata.getVar('PKGR', True))) |
| 115 | bb.utils.unlockfile(lf) |
| 116 | continue |
| 117 | |
| 118 | controldir = os.path.join(root, 'DEBIAN') |
| 119 | bb.utils.mkdirhier(controldir) |
| 120 | os.chmod(controldir, 0755) |
| 121 | try: |
| 122 | import codecs |
| 123 | ctrlfile = codecs.open(os.path.join(controldir, 'control'), 'w', 'utf-8') |
| 124 | except OSError: |
| 125 | bb.utils.unlockfile(lf) |
| 126 | raise bb.build.FuncFailed("unable to open control file for writing.") |
| 127 | |
| 128 | fields = [] |
| 129 | pe = d.getVar('PKGE', True) |
| 130 | if pe and int(pe) > 0: |
| 131 | fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']]) |
| 132 | else: |
| 133 | fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']]) |
| 134 | fields.append(["Description: %s\n", ['DESCRIPTION']]) |
| 135 | fields.append(["Section: %s\n", ['SECTION']]) |
| 136 | fields.append(["Priority: %s\n", ['PRIORITY']]) |
| 137 | fields.append(["Maintainer: %s\n", ['MAINTAINER']]) |
| 138 | fields.append(["Architecture: %s\n", ['DPKG_ARCH']]) |
| 139 | fields.append(["OE: %s\n", ['PN']]) |
| 140 | fields.append(["PackageArch: %s\n", ['PACKAGE_ARCH']]) |
| 141 | if d.getVar('HOMEPAGE', True): |
| 142 | fields.append(["Homepage: %s\n", ['HOMEPAGE']]) |
| 143 | |
| 144 | # Package, Version, Maintainer, Description - mandatory |
| 145 | # Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional |
| 146 | |
| 147 | |
| 148 | def pullData(l, d): |
| 149 | l2 = [] |
| 150 | for i in l: |
| 151 | data = d.getVar(i, True) |
| 152 | if data is None: |
| 153 | raise KeyError(f) |
| 154 | if i == 'DPKG_ARCH' and d.getVar('PACKAGE_ARCH', True) == 'all': |
| 155 | data = 'all' |
| 156 | elif i == 'PACKAGE_ARCH' or i == 'DPKG_ARCH': |
| 157 | # The params in deb package control don't allow character |
| 158 | # `_', so change the arch's `_' to `-'. Such as `x86_64' |
| 159 | # -->`x86-64' |
| 160 | data = data.replace('_', '-') |
| 161 | l2.append(data) |
| 162 | return l2 |
| 163 | |
| 164 | ctrlfile.write("Package: %s\n" % pkgname) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 165 | if d.getVar('PACKAGE_ARCH', True) == "all": |
| 166 | ctrlfile.write("Multi-Arch: foreign\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 167 | # check for required fields |
| 168 | try: |
| 169 | for (c, fs) in fields: |
| 170 | for f in fs: |
| 171 | if localdata.getVar(f, False) is None: |
| 172 | raise KeyError(f) |
| 173 | # Special behavior for description... |
| 174 | if 'DESCRIPTION' in fs: |
| 175 | summary = localdata.getVar('SUMMARY', True) or localdata.getVar('DESCRIPTION', True) or "." |
| 176 | ctrlfile.write('Description: %s\n' % unicode(summary,'utf-8')) |
| 177 | description = localdata.getVar('DESCRIPTION', True) or "." |
| 178 | description = textwrap.dedent(description).strip() |
| 179 | if '\\n' in description: |
| 180 | # Manually indent |
| 181 | for t in description.split('\\n'): |
| 182 | # We don't limit the width when manually indent, but we do |
| 183 | # need the textwrap.fill() to set the initial_indent and |
| 184 | # subsequent_indent, so set a large width |
| 185 | ctrlfile.write('%s\n' % unicode(textwrap.fill(t, width=100000, initial_indent=' ', subsequent_indent=' '),'utf-8')) |
| 186 | else: |
| 187 | # Auto indent |
| 188 | ctrlfile.write('%s\n' % unicode(textwrap.fill(description.strip(), width=74, initial_indent=' ', subsequent_indent=' '),'utf-8')) |
| 189 | |
| 190 | else: |
| 191 | ctrlfile.write(unicode(c % tuple(pullData(fs, localdata)),'utf-8')) |
| 192 | except KeyError: |
| 193 | import sys |
| 194 | (type, value, traceback) = sys.exc_info() |
| 195 | bb.utils.unlockfile(lf) |
| 196 | ctrlfile.close() |
| 197 | raise bb.build.FuncFailed("Missing field for deb generation: %s" % value) |
| 198 | except UnicodeDecodeError: |
| 199 | bb.utils.unlockfile(lf) |
| 200 | ctrlfile.close() |
| 201 | raise bb.build.FuncFailed("Non UTF-8 characters found in one of the fields") |
| 202 | |
| 203 | # more fields |
| 204 | |
| 205 | custom_fields_chunk = get_package_additional_metadata("deb", localdata) |
| 206 | if custom_fields_chunk is not None: |
| 207 | ctrlfile.write(unicode(custom_fields_chunk)) |
| 208 | ctrlfile.write("\n") |
| 209 | |
| 210 | mapping_rename_hook(localdata) |
| 211 | |
| 212 | def debian_cmp_remap(var): |
| 213 | # dpkg does not allow for '(' or ')' in a dependency name |
| 214 | # replace these instances with '__' and '__' |
| 215 | # |
| 216 | # In debian '>' and '<' do not mean what it appears they mean |
| 217 | # '<' = less or equal |
| 218 | # '>' = greater or equal |
| 219 | # adjust these to the '<<' and '>>' equivalents |
| 220 | # |
| 221 | for dep in var: |
| 222 | if '(' in dep: |
| 223 | newdep = dep.replace('(', '__') |
| 224 | newdep = newdep.replace(')', '__') |
| 225 | if newdep != dep: |
| 226 | var[newdep] = var[dep] |
| 227 | del var[dep] |
| 228 | for dep in var: |
| 229 | for i, v in enumerate(var[dep]): |
| 230 | if (v or "").startswith("< "): |
| 231 | var[dep][i] = var[dep][i].replace("< ", "<< ") |
| 232 | elif (v or "").startswith("> "): |
| 233 | var[dep][i] = var[dep][i].replace("> ", ">> ") |
| 234 | |
| 235 | rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS", True) or "") |
| 236 | debian_cmp_remap(rdepends) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 237 | for dep in rdepends.keys(): |
| 238 | if dep == pkg: |
| 239 | del rdepends[dep] |
| 240 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 241 | if '*' in dep: |
| 242 | del rdepends[dep] |
| 243 | rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS", True) or "") |
| 244 | debian_cmp_remap(rrecommends) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 245 | for dep in rrecommends.keys(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 246 | if '*' in dep: |
| 247 | del rrecommends[dep] |
| 248 | rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS", True) or "") |
| 249 | debian_cmp_remap(rsuggests) |
| 250 | # Deliberately drop version information here, not wanted/supported by deb |
| 251 | rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES", True) or ""), []) |
| 252 | debian_cmp_remap(rprovides) |
| 253 | rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES", True) or "") |
| 254 | debian_cmp_remap(rreplaces) |
| 255 | rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS", True) or "") |
| 256 | debian_cmp_remap(rconflicts) |
| 257 | if rdepends: |
| 258 | ctrlfile.write("Depends: %s\n" % unicode(bb.utils.join_deps(rdepends))) |
| 259 | if rsuggests: |
| 260 | ctrlfile.write("Suggests: %s\n" % unicode(bb.utils.join_deps(rsuggests))) |
| 261 | if rrecommends: |
| 262 | ctrlfile.write("Recommends: %s\n" % unicode(bb.utils.join_deps(rrecommends))) |
| 263 | if rprovides: |
| 264 | ctrlfile.write("Provides: %s\n" % unicode(bb.utils.join_deps(rprovides))) |
| 265 | if rreplaces: |
| 266 | ctrlfile.write("Replaces: %s\n" % unicode(bb.utils.join_deps(rreplaces))) |
| 267 | if rconflicts: |
| 268 | ctrlfile.write("Conflicts: %s\n" % unicode(bb.utils.join_deps(rconflicts))) |
| 269 | ctrlfile.close() |
| 270 | |
| 271 | for script in ["preinst", "postinst", "prerm", "postrm"]: |
| 272 | scriptvar = localdata.getVar('pkg_%s' % script, True) |
| 273 | if not scriptvar: |
| 274 | continue |
| 275 | scriptvar = scriptvar.strip() |
| 276 | try: |
| 277 | scriptfile = open(os.path.join(controldir, script), 'w') |
| 278 | except OSError: |
| 279 | bb.utils.unlockfile(lf) |
| 280 | raise bb.build.FuncFailed("unable to open %s script file for writing." % script) |
| 281 | |
| 282 | if scriptvar.startswith("#!"): |
| 283 | pos = scriptvar.find("\n") + 1 |
| 284 | scriptfile.write(scriptvar[:pos]) |
| 285 | else: |
| 286 | pos = 0 |
| 287 | scriptfile.write("#!/bin/sh\n") |
| 288 | |
| 289 | # Prevent the prerm/postrm scripts from being run during an upgrade |
| 290 | if script in ('prerm', 'postrm'): |
| 291 | scriptfile.write('[ "$1" != "upgrade" ] || exit 0\n') |
| 292 | |
| 293 | scriptfile.write(scriptvar[pos:]) |
| 294 | scriptfile.write('\n') |
| 295 | scriptfile.close() |
| 296 | os.chmod(os.path.join(controldir, script), 0755) |
| 297 | |
| 298 | conffiles_str = ' '.join(get_conffiles(pkg, d)) |
| 299 | if conffiles_str: |
| 300 | try: |
| 301 | conffiles = open(os.path.join(controldir, 'conffiles'), 'w') |
| 302 | except OSError: |
| 303 | bb.utils.unlockfile(lf) |
| 304 | raise bb.build.FuncFailed("unable to open conffiles for writing.") |
| 305 | for f in conffiles_str.split(): |
| 306 | if os.path.exists(oe.path.join(root, f)): |
| 307 | conffiles.write('%s\n' % f) |
| 308 | conffiles.close() |
| 309 | |
| 310 | os.chdir(basedir) |
| 311 | ret = subprocess.call("PATH=\"%s\" dpkg-deb -b %s %s" % (localdata.getVar("PATH", True), root, pkgoutdir), shell=True) |
| 312 | if ret != 0: |
| 313 | bb.utils.unlockfile(lf) |
| 314 | raise bb.build.FuncFailed("dpkg-deb execution failed") |
| 315 | |
| 316 | cleanupcontrol(root) |
| 317 | bb.utils.unlockfile(lf) |
| 318 | } |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 319 | # Indirect references to these vars |
| 320 | do_package_write_deb[vardeps] += "PKGV PKGR PKGV DESCRIPTION SECTION PRIORITY MAINTAINER DPKG_ARCH PN HOMEPAGE" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 321 | # Otherwise allarch packages may change depending on override configuration |
| 322 | do_package_deb[vardepsexclude] = "OVERRIDES" |
| 323 | |
| 324 | |
| 325 | SSTATETASKS += "do_package_write_deb" |
| 326 | do_package_write_deb[sstate-inputdirs] = "${PKGWRITEDIRDEB}" |
| 327 | do_package_write_deb[sstate-outputdirs] = "${DEPLOY_DIR_DEB}" |
| 328 | |
| 329 | python do_package_write_deb_setscene () { |
| 330 | tmpdir = d.getVar('TMPDIR', True) |
| 331 | |
| 332 | if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK): |
| 333 | os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN")) |
| 334 | |
| 335 | sstate_setscene(d) |
| 336 | } |
| 337 | addtask do_package_write_deb_setscene |
| 338 | |
| 339 | python () { |
| 340 | if d.getVar('PACKAGES', True) != '': |
| 341 | deps = ' dpkg-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot' |
| 342 | d.appendVarFlag('do_package_write_deb', 'depends', deps) |
| 343 | d.setVarFlag('do_package_write_deb', 'fakeroot', "1") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | python do_package_write_deb () { |
| 347 | bb.build.exec_func("read_subpackage_metadata", d) |
| 348 | bb.build.exec_func("do_package_deb", d) |
| 349 | } |
| 350 | do_package_write_deb[dirs] = "${PKGWRITEDIRDEB}" |
| 351 | do_package_write_deb[cleandirs] = "${PKGWRITEDIRDEB}" |
| 352 | do_package_write_deb[umask] = "022" |
| 353 | addtask package_write_deb after do_packagedata do_package |
| 354 | |
| 355 | |
| 356 | PACKAGEINDEXDEPS += "dpkg-native:do_populate_sysroot" |
| 357 | PACKAGEINDEXDEPS += "apt-native:do_populate_sysroot" |
| 358 | |
| 359 | do_build[recrdeptask] += "do_package_write_deb" |