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