Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | inherit package |
| 2 | |
| 3 | IMAGE_PKGTYPE ?= "ipk" |
| 4 | |
| 5 | IPKGCONF_TARGET = "${WORKDIR}/opkg.conf" |
| 6 | IPKGCONF_SDK = "${WORKDIR}/opkg-sdk.conf" |
| 7 | |
| 8 | PKGWRITEDIRIPK = "${WORKDIR}/deploy-ipks" |
| 9 | |
| 10 | # Program to be used to build opkg packages |
| 11 | OPKGBUILDCMD ??= "opkg-build" |
| 12 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 13 | OPKG_ARGS += "--force_postinstall --prefer-arch-to-version" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | OPKG_ARGS += "${@['', '--no-install-recommends'][d.getVar("NO_RECOMMENDATIONS", True) == "1"]}" |
| 15 | OPKG_ARGS += "${@['', '--add-exclude ' + ' --add-exclude '.join((d.getVar('PACKAGE_EXCLUDE', True) or "").split())][(d.getVar("PACKAGE_EXCLUDE", True) or "") != ""]}" |
| 16 | |
| 17 | OPKGLIBDIR = "${localstatedir}/lib" |
| 18 | |
| 19 | python do_package_ipk () { |
| 20 | import re, copy |
| 21 | import textwrap |
| 22 | import subprocess |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 23 | import collections |
| 24 | |
| 25 | oldcwd = os.getcwd() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | |
| 27 | workdir = d.getVar('WORKDIR', True) |
| 28 | outdir = d.getVar('PKGWRITEDIRIPK', True) |
| 29 | tmpdir = d.getVar('TMPDIR', True) |
| 30 | pkgdest = d.getVar('PKGDEST', True) |
| 31 | if not workdir or not outdir or not tmpdir: |
| 32 | bb.error("Variables incorrectly set, unable to package") |
| 33 | return |
| 34 | |
| 35 | packages = d.getVar('PACKAGES', True) |
| 36 | if not packages or packages == '': |
| 37 | bb.debug(1, "No packages; nothing to do") |
| 38 | return |
| 39 | |
| 40 | # We're about to add new packages so the index needs to be checked |
| 41 | # so remove the appropriate stamp file. |
| 42 | if os.access(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"), os.R_OK): |
| 43 | os.unlink(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN")) |
| 44 | |
| 45 | def cleanupcontrol(root): |
| 46 | for p in ['CONTROL', 'DEBIAN']: |
| 47 | p = os.path.join(root, p) |
| 48 | if os.path.exists(p): |
| 49 | bb.utils.prunedir(p) |
| 50 | |
| 51 | for pkg in packages.split(): |
| 52 | localdata = bb.data.createCopy(d) |
| 53 | root = "%s/%s" % (pkgdest, pkg) |
| 54 | |
| 55 | lf = bb.utils.lockfile(root + ".lock") |
| 56 | |
| 57 | localdata.setVar('ROOT', '') |
| 58 | localdata.setVar('ROOT_%s' % pkg, root) |
| 59 | pkgname = localdata.getVar('PKG_%s' % pkg, True) |
| 60 | if not pkgname: |
| 61 | pkgname = pkg |
| 62 | localdata.setVar('PKG', pkgname) |
| 63 | |
| 64 | localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg) |
| 65 | |
| 66 | bb.data.update_data(localdata) |
| 67 | basedir = os.path.join(os.path.dirname(root)) |
| 68 | arch = localdata.getVar('PACKAGE_ARCH', True) |
| 69 | |
| 70 | if localdata.getVar('IPK_HIERARCHICAL_FEED', False) == "1": |
| 71 | # Spread packages across subdirectories so each isn't too crowded |
| 72 | if pkgname.startswith('lib'): |
| 73 | pkg_prefix = 'lib' + pkgname[3] |
| 74 | else: |
| 75 | pkg_prefix = pkgname[0] |
| 76 | |
| 77 | # Keep -dbg, -dev, -doc, -staticdev, -locale and -locale-* packages |
| 78 | # together. These package suffixes are taken from the definitions of |
| 79 | # PACKAGES and PACKAGES_DYNAMIC in meta/conf/bitbake.conf |
| 80 | if pkgname[-4:] in ('-dbg', '-dev', '-doc'): |
| 81 | pkg_subdir = pkgname[:-4] |
| 82 | elif pkgname.endswith('-staticdev'): |
| 83 | pkg_subdir = pkgname[:-10] |
| 84 | elif pkgname.endswith('-locale'): |
| 85 | pkg_subdir = pkgname[:-7] |
| 86 | elif '-locale-' in pkgname: |
| 87 | pkg_subdir = pkgname[:pkgname.find('-locale-')] |
| 88 | else: |
| 89 | pkg_subdir = pkgname |
| 90 | |
| 91 | pkgoutdir = "%s/%s/%s/%s" % (outdir, arch, pkg_prefix, pkg_subdir) |
| 92 | else: |
| 93 | pkgoutdir = "%s/%s" % (outdir, arch) |
| 94 | |
| 95 | bb.utils.mkdirhier(pkgoutdir) |
| 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": |
| 101 | bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV', True), localdata.getVar('PKGR', True))) |
| 102 | bb.utils.unlockfile(lf) |
| 103 | continue |
| 104 | |
| 105 | controldir = os.path.join(root, 'CONTROL') |
| 106 | bb.utils.mkdirhier(controldir) |
| 107 | try: |
| 108 | ctrlfile = open(os.path.join(controldir, 'control'), 'w') |
| 109 | except OSError: |
| 110 | bb.utils.unlockfile(lf) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | bb.fatal("unable to open control file for writing") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | |
| 113 | fields = [] |
| 114 | pe = d.getVar('PKGE', True) |
| 115 | if pe and int(pe) > 0: |
| 116 | fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']]) |
| 117 | else: |
| 118 | fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']]) |
| 119 | fields.append(["Description: %s\n", ['DESCRIPTION']]) |
| 120 | fields.append(["Section: %s\n", ['SECTION']]) |
| 121 | fields.append(["Priority: %s\n", ['PRIORITY']]) |
| 122 | fields.append(["Maintainer: %s\n", ['MAINTAINER']]) |
| 123 | fields.append(["License: %s\n", ['LICENSE']]) |
| 124 | fields.append(["Architecture: %s\n", ['PACKAGE_ARCH']]) |
| 125 | fields.append(["OE: %s\n", ['PN']]) |
| 126 | if d.getVar('HOMEPAGE', True): |
| 127 | fields.append(["Homepage: %s\n", ['HOMEPAGE']]) |
| 128 | |
| 129 | def pullData(l, d): |
| 130 | l2 = [] |
| 131 | for i in l: |
| 132 | l2.append(d.getVar(i, True)) |
| 133 | return l2 |
| 134 | |
| 135 | ctrlfile.write("Package: %s\n" % pkgname) |
| 136 | # check for required fields |
| 137 | try: |
| 138 | for (c, fs) in fields: |
| 139 | for f in fs: |
| 140 | if localdata.getVar(f, False) is None: |
| 141 | raise KeyError(f) |
| 142 | # Special behavior for description... |
| 143 | if 'DESCRIPTION' in fs: |
| 144 | summary = localdata.getVar('SUMMARY', True) or localdata.getVar('DESCRIPTION', True) or "." |
| 145 | ctrlfile.write('Description: %s\n' % summary) |
| 146 | description = localdata.getVar('DESCRIPTION', True) or "." |
| 147 | description = textwrap.dedent(description).strip() |
| 148 | if '\\n' in description: |
| 149 | # Manually indent |
| 150 | for t in description.split('\\n'): |
| 151 | # We don't limit the width when manually indent, but we do |
| 152 | # need the textwrap.fill() to set the initial_indent and |
| 153 | # subsequent_indent, so set a large width |
| 154 | ctrlfile.write('%s\n' % textwrap.fill(t.strip(), width=100000, initial_indent=' ', subsequent_indent=' ')) |
| 155 | else: |
| 156 | # Auto indent |
| 157 | ctrlfile.write('%s\n' % textwrap.fill(description, width=74, initial_indent=' ', subsequent_indent=' ')) |
| 158 | else: |
| 159 | ctrlfile.write(c % tuple(pullData(fs, localdata))) |
| 160 | except KeyError: |
| 161 | import sys |
| 162 | (type, value, traceback) = sys.exc_info() |
| 163 | ctrlfile.close() |
| 164 | bb.utils.unlockfile(lf) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 165 | bb.fatal("Missing field for ipk generation: %s" % value) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | # more fields |
| 167 | |
| 168 | custom_fields_chunk = get_package_additional_metadata("ipk", localdata) |
| 169 | if custom_fields_chunk is not None: |
| 170 | ctrlfile.write(custom_fields_chunk) |
| 171 | ctrlfile.write("\n") |
| 172 | |
| 173 | mapping_rename_hook(localdata) |
| 174 | |
| 175 | def debian_cmp_remap(var): |
| 176 | # In debian '>' and '<' do not mean what it appears they mean |
| 177 | # '<' = less or equal |
| 178 | # '>' = greater or equal |
| 179 | # adjust these to the '<<' and '>>' equivalents |
| 180 | # |
| 181 | for dep in var: |
| 182 | for i, v in enumerate(var[dep]): |
| 183 | if (v or "").startswith("< "): |
| 184 | var[dep][i] = var[dep][i].replace("< ", "<< ") |
| 185 | elif (v or "").startswith("> "): |
| 186 | var[dep][i] = var[dep][i].replace("> ", ">> ") |
| 187 | |
| 188 | rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS", True) or "") |
| 189 | debian_cmp_remap(rdepends) |
| 190 | rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS", True) or "") |
| 191 | debian_cmp_remap(rrecommends) |
| 192 | rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS", True) or "") |
| 193 | debian_cmp_remap(rsuggests) |
| 194 | # Deliberately drop version information here, not wanted/supported by ipk |
| 195 | rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES", True) or ""), []) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 196 | rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 197 | debian_cmp_remap(rprovides) |
| 198 | rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES", True) or "") |
| 199 | debian_cmp_remap(rreplaces) |
| 200 | rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS", True) or "") |
| 201 | debian_cmp_remap(rconflicts) |
| 202 | |
| 203 | if rdepends: |
| 204 | ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends)) |
| 205 | if rsuggests: |
| 206 | ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests)) |
| 207 | if rrecommends: |
| 208 | ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends)) |
| 209 | if rprovides: |
| 210 | ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides)) |
| 211 | if rreplaces: |
| 212 | ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces)) |
| 213 | if rconflicts: |
| 214 | ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts)) |
| 215 | src_uri = localdata.getVar("SRC_URI", True).strip() or "None" |
| 216 | if src_uri: |
| 217 | src_uri = re.sub("\s+", " ", src_uri) |
| 218 | ctrlfile.write("Source: %s\n" % " ".join(src_uri.split())) |
| 219 | ctrlfile.close() |
| 220 | |
| 221 | for script in ["preinst", "postinst", "prerm", "postrm"]: |
| 222 | scriptvar = localdata.getVar('pkg_%s' % script, True) |
| 223 | if not scriptvar: |
| 224 | continue |
| 225 | try: |
| 226 | scriptfile = open(os.path.join(controldir, script), 'w') |
| 227 | except OSError: |
| 228 | bb.utils.unlockfile(lf) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 229 | bb.fatal("unable to open %s script file for writing" % script) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 230 | scriptfile.write(scriptvar) |
| 231 | scriptfile.close() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 232 | os.chmod(os.path.join(controldir, script), 0o755) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 233 | |
| 234 | conffiles_str = ' '.join(get_conffiles(pkg, d)) |
| 235 | if conffiles_str: |
| 236 | try: |
| 237 | conffiles = open(os.path.join(controldir, 'conffiles'), 'w') |
| 238 | except OSError: |
| 239 | bb.utils.unlockfile(lf) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 240 | bb.fatal("unable to open conffiles for writing") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 241 | for f in conffiles_str.split(): |
| 242 | if os.path.exists(oe.path.join(root, f)): |
| 243 | conffiles.write('%s\n' % f) |
| 244 | conffiles.close() |
| 245 | |
| 246 | os.chdir(basedir) |
| 247 | ret = subprocess.call("PATH=\"%s\" %s %s %s" % (localdata.getVar("PATH", True), |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 248 | d.getVar("OPKGBUILDCMD", True), pkg, pkgoutdir), shell=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 249 | if ret != 0: |
| 250 | bb.utils.unlockfile(lf) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 251 | bb.fatal("opkg-build execution failed") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 252 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 253 | if d.getVar('IPK_SIGN_PACKAGES', True) == '1': |
| 254 | ipkver = "%s-%s" % (d.getVar('PKGV', True), d.getVar('PKGR', True)) |
| 255 | ipk_to_sign = "%s/%s_%s_%s.ipk" % (pkgoutdir, pkgname, ipkver, d.getVar('PACKAGE_ARCH', True)) |
| 256 | sign_ipk(d, ipk_to_sign) |
| 257 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 258 | cleanupcontrol(root) |
| 259 | bb.utils.unlockfile(lf) |
| 260 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 261 | os.chdir(oldcwd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 262 | } |
| 263 | # Otherwise allarch packages may change depending on override configuration |
| 264 | do_package_ipk[vardepsexclude] = "OVERRIDES" |
| 265 | |
| 266 | SSTATETASKS += "do_package_write_ipk" |
| 267 | do_package_write_ipk[sstate-inputdirs] = "${PKGWRITEDIRIPK}" |
| 268 | do_package_write_ipk[sstate-outputdirs] = "${DEPLOY_DIR_IPK}" |
| 269 | |
| 270 | python do_package_write_ipk_setscene () { |
| 271 | tmpdir = d.getVar('TMPDIR', True) |
| 272 | |
| 273 | if os.access(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"), os.R_OK): |
| 274 | os.unlink(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN")) |
| 275 | |
| 276 | sstate_setscene(d) |
| 277 | } |
| 278 | addtask do_package_write_ipk_setscene |
| 279 | |
| 280 | python () { |
| 281 | if d.getVar('PACKAGES', True) != '': |
| 282 | deps = ' opkg-utils-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot' |
| 283 | d.appendVarFlag('do_package_write_ipk', 'depends', deps) |
| 284 | d.setVarFlag('do_package_write_ipk', 'fakeroot', "1") |
| 285 | } |
| 286 | |
| 287 | python do_package_write_ipk () { |
| 288 | bb.build.exec_func("read_subpackage_metadata", d) |
| 289 | bb.build.exec_func("do_package_ipk", d) |
| 290 | } |
| 291 | do_package_write_ipk[dirs] = "${PKGWRITEDIRIPK}" |
| 292 | do_package_write_ipk[cleandirs] = "${PKGWRITEDIRIPK}" |
| 293 | do_package_write_ipk[umask] = "022" |
| 294 | addtask package_write_ipk after do_packagedata do_package |
| 295 | |
| 296 | PACKAGEINDEXDEPS += "opkg-utils-native:do_populate_sysroot" |
| 297 | PACKAGEINDEXDEPS += "opkg-native:do_populate_sysroot" |
| 298 | |
| 299 | do_build[recrdeptask] += "do_package_write_ipk" |