192b42cb3SPatrick Williams#
292b42cb3SPatrick Williams# Copyright OpenEmbedded Contributors
392b42cb3SPatrick Williams#
492b42cb3SPatrick Williams# SPDX-License-Identifier: MIT
592b42cb3SPatrick Williams#
692b42cb3SPatrick Williams
792b42cb3SPatrick Williams# This class is used to help the alternatives system which is useful when
8864cc43bSPatrick Williams# multiple sources provide the same command. You can use update-alternatives
992b42cb3SPatrick Williams# command directly in your recipe, but in most cases this class simplifies
1092b42cb3SPatrick Williams# that job.
1192b42cb3SPatrick Williams#
1292b42cb3SPatrick Williams# To use this class a number of variables should be defined:
1392b42cb3SPatrick Williams#
1492b42cb3SPatrick Williams# List all of the alternatives needed by a package:
1592b42cb3SPatrick Williams# ALTERNATIVE:<pkg> = "name1 name2 name3 ..."
1692b42cb3SPatrick Williams#
1792b42cb3SPatrick Williams#   i.e. ALTERNATIVE:busybox = "sh sed test bracket"
1892b42cb3SPatrick Williams#
1992b42cb3SPatrick Williams# The pathname of the link
2092b42cb3SPatrick Williams# ALTERNATIVE_LINK_NAME[name] = "target"
2192b42cb3SPatrick Williams#
2292b42cb3SPatrick Williams#   This is the name of the binary once it's been installed onto the runtime.
2392b42cb3SPatrick Williams#   This name is global to all split packages in this recipe, and should match
2492b42cb3SPatrick Williams#   other recipes with the same functionality.
2592b42cb3SPatrick Williams#   i.e. ALTERNATIVE_LINK_NAME[bracket] = "/usr/bin/["
2692b42cb3SPatrick Williams#
2792b42cb3SPatrick Williams# NOTE: If ALTERNATIVE_LINK_NAME is not defined, it defaults to ${bindir}/name
2892b42cb3SPatrick Williams#
2992b42cb3SPatrick Williams# The default link to create for all targets
3092b42cb3SPatrick Williams# ALTERNATIVE_TARGET = "target"
3192b42cb3SPatrick Williams#
3292b42cb3SPatrick Williams#   This is useful in a multicall binary case
3392b42cb3SPatrick Williams#   i.e. ALTERNATIVE_TARGET = "/bin/busybox"
3492b42cb3SPatrick Williams#
3592b42cb3SPatrick Williams# A non-default link to create for a target
3692b42cb3SPatrick Williams# ALTERNATIVE_TARGET[name] = "target"
3792b42cb3SPatrick Williams#
38864cc43bSPatrick Williams#   This is the name of the binary as it's been installed by do_install
3992b42cb3SPatrick Williams#   i.e. ALTERNATIVE_TARGET[sh] = "/bin/bash"
4092b42cb3SPatrick Williams#
4192b42cb3SPatrick Williams# A package specific link for a target
4292b42cb3SPatrick Williams# ALTERNATIVE_TARGET_<pkg>[name] = "target"
4392b42cb3SPatrick Williams#
4492b42cb3SPatrick Williams#   This is useful when a recipe provides multiple alternatives for the
4592b42cb3SPatrick Williams#   same item.
4692b42cb3SPatrick Williams#
4792b42cb3SPatrick Williams# NOTE: If ALTERNATIVE_TARGET is not defined, it will inherit the value
4892b42cb3SPatrick Williams# from ALTERNATIVE_LINK_NAME.
4992b42cb3SPatrick Williams#
5092b42cb3SPatrick Williams# NOTE: If the ALTERNATIVE_LINK_NAME and ALTERNATIVE_TARGET are the same,
5192b42cb3SPatrick Williams# ALTERNATIVE_TARGET will have '.{BPN}' appended to it.  If the file
5292b42cb3SPatrick Williams# referenced has not been renamed, it will also be renamed.  (This avoids
5392b42cb3SPatrick Williams# the need to rename alternative files in the do_install step, but still
5492b42cb3SPatrick Williams# supports it if necessary for some reason.)
5592b42cb3SPatrick Williams#
5692b42cb3SPatrick Williams# The default priority for any alternatives
5792b42cb3SPatrick Williams# ALTERNATIVE_PRIORITY = "priority"
5892b42cb3SPatrick Williams#
5992b42cb3SPatrick Williams#   i.e. default is ALTERNATIVE_PRIORITY = "10"
6092b42cb3SPatrick Williams#
6192b42cb3SPatrick Williams# The non-default priority for a specific target
6292b42cb3SPatrick Williams# ALTERNATIVE_PRIORITY[name] = "priority"
6392b42cb3SPatrick Williams#
6492b42cb3SPatrick Williams# The package priority for a specific target
6592b42cb3SPatrick Williams# ALTERNATIVE_PRIORITY_<pkg>[name] = "priority"
6692b42cb3SPatrick Williams
6792b42cb3SPatrick WilliamsALTERNATIVE_PRIORITY = "10"
6892b42cb3SPatrick Williams
6992b42cb3SPatrick Williams# We need special processing for vardeps because it can not work on
7092b42cb3SPatrick Williams# modified flag values.  So we aggregate the flags into a new variable
71864cc43bSPatrick Williams# and include that variable in the set.
7292b42cb3SPatrick WilliamsUPDALTVARS  = "ALTERNATIVE ALTERNATIVE_LINK_NAME ALTERNATIVE_TARGET ALTERNATIVE_PRIORITY"
7392b42cb3SPatrick Williams
7492b42cb3SPatrick WilliamsPACKAGE_WRITE_DEPS += "virtual/update-alternatives-native"
7592b42cb3SPatrick Williams
7692b42cb3SPatrick Williamsdef gen_updatealternativesvardeps(d):
7792b42cb3SPatrick Williams    pkgs = (d.getVar("PACKAGES") or "").split()
7892b42cb3SPatrick Williams    vars = (d.getVar("UPDALTVARS") or "").split()
7992b42cb3SPatrick Williams
8092b42cb3SPatrick Williams    # First compute them for non_pkg versions
8192b42cb3SPatrick Williams    for v in vars:
8292b42cb3SPatrick Williams        for flag in sorted((d.getVarFlags(v) or {}).keys()):
8392b42cb3SPatrick Williams            if flag == "doc" or flag == "vardeps" or flag == "vardepsexp":
8492b42cb3SPatrick Williams                continue
8592b42cb3SPatrick Williams            d.appendVar('%s_VARDEPS' % (v), ' %s:%s' % (flag, d.getVarFlag(v, flag, False)))
8692b42cb3SPatrick Williams
8792b42cb3SPatrick Williams    for p in pkgs:
8892b42cb3SPatrick Williams        for v in vars:
89*028142bdSAndrew Geissler            for flag in sorted((d.getVarFlags("%s:%s" % (v,p)) or {}).keys()):
9092b42cb3SPatrick Williams                if flag == "doc" or flag == "vardeps" or flag == "vardepsexp":
9192b42cb3SPatrick Williams                    continue
92*028142bdSAndrew Geissler                d.appendVar('%s_VARDEPS_%s' % (v,p), ' %s:%s' % (flag, d.getVarFlag('%s:%s' % (v,p), flag, False)))
9392b42cb3SPatrick Williams
9492b42cb3SPatrick Williamsdef ua_extend_depends(d):
9592b42cb3SPatrick Williams    if not 'virtual/update-alternatives' in d.getVar('PROVIDES'):
9692b42cb3SPatrick Williams        d.appendVar('DEPENDS', ' virtual/${MLPREFIX}update-alternatives')
9792b42cb3SPatrick Williams
9892b42cb3SPatrick Williamsdef update_alternatives_enabled(d):
9992b42cb3SPatrick Williams    # Update Alternatives only works on target packages...
10092b42cb3SPatrick Williams    if bb.data.inherits_class('native', d) or \
10192b42cb3SPatrick Williams       bb.data.inherits_class('cross', d) or bb.data.inherits_class('crosssdk', d) or \
10292b42cb3SPatrick Williams       bb.data.inherits_class('cross-canadian', d):
10392b42cb3SPatrick Williams        return False
10492b42cb3SPatrick Williams
10592b42cb3SPatrick Williams    # Disable when targeting mingw32 (no target support)
10692b42cb3SPatrick Williams    if d.getVar("TARGET_OS") == "mingw32":
10792b42cb3SPatrick Williams        return False
10892b42cb3SPatrick Williams
10992b42cb3SPatrick Williams    return True
11092b42cb3SPatrick Williams
11192b42cb3SPatrick Williamspython __anonymous() {
11292b42cb3SPatrick Williams    if not update_alternatives_enabled(d):
11392b42cb3SPatrick Williams        return
11492b42cb3SPatrick Williams
11592b42cb3SPatrick Williams    # compute special vardeps
11692b42cb3SPatrick Williams    gen_updatealternativesvardeps(d)
11792b42cb3SPatrick Williams
11892b42cb3SPatrick Williams    # extend the depends to include virtual/update-alternatives
11992b42cb3SPatrick Williams    ua_extend_depends(d)
12092b42cb3SPatrick Williams}
12192b42cb3SPatrick Williams
12292b42cb3SPatrick Williamsdef gen_updatealternativesvars(d):
12392b42cb3SPatrick Williams    ret = []
12492b42cb3SPatrick Williams    pkgs = (d.getVar("PACKAGES") or "").split()
12592b42cb3SPatrick Williams    vars = (d.getVar("UPDALTVARS") or "").split()
12692b42cb3SPatrick Williams
12792b42cb3SPatrick Williams    for v in vars:
12892b42cb3SPatrick Williams        ret.append(v + "_VARDEPS")
12992b42cb3SPatrick Williams
13092b42cb3SPatrick Williams    for p in pkgs:
13192b42cb3SPatrick Williams        for v in vars:
13292b42cb3SPatrick Williams            ret.append(v + ":" + p)
13392b42cb3SPatrick Williams            ret.append(v + "_VARDEPS_" + p)
13492b42cb3SPatrick Williams    return " ".join(ret)
13592b42cb3SPatrick Williams
13692b42cb3SPatrick Williams# Now the new stuff, we use a custom function to generate the right values
13792b42cb3SPatrick Williamspopulate_packages[vardeps] += "${UPDALTVARS} ${@gen_updatealternativesvars(d)}"
13892b42cb3SPatrick Williams
13992b42cb3SPatrick Williams# We need to do the rename after the image creation step, but before
14092b42cb3SPatrick Williams# the split and strip steps..  PACKAGE_PREPROCESS_FUNCS is the right
14192b42cb3SPatrick Williams# place for that.
14292b42cb3SPatrick WilliamsPACKAGE_PREPROCESS_FUNCS += "apply_update_alternative_renames"
14392b42cb3SPatrick Williamspython apply_update_alternative_renames () {
14492b42cb3SPatrick Williams    if not update_alternatives_enabled(d):
14592b42cb3SPatrick Williams       return
14692b42cb3SPatrick Williams
14792b42cb3SPatrick Williams    import re
14892b42cb3SPatrick Williams
14992b42cb3SPatrick Williams    def update_files(alt_target, alt_target_rename, pkg, d):
15092b42cb3SPatrick Williams        f = d.getVar('FILES:' + pkg)
15192b42cb3SPatrick Williams        if f:
15292b42cb3SPatrick Williams            f = re.sub(r'(^|\s)%s(\s|$)' % re.escape (alt_target), r'\1%s\2' % alt_target_rename, f)
15392b42cb3SPatrick Williams            d.setVar('FILES:' + pkg, f)
15492b42cb3SPatrick Williams
15592b42cb3SPatrick Williams    # Check for deprecated usage...
15692b42cb3SPatrick Williams    pn = d.getVar('BPN')
15792b42cb3SPatrick Williams    if d.getVar('ALTERNATIVE_LINKS') != None:
15892b42cb3SPatrick Williams        bb.fatal('%s: Use of ALTERNATIVE_LINKS/ALTERNATIVE_PATH/ALTERNATIVE_NAME is no longer supported, please convert to the updated syntax, see update-alternatives.bbclass for more info.' % pn)
15992b42cb3SPatrick Williams
16092b42cb3SPatrick Williams    # Do actual update alternatives processing
16192b42cb3SPatrick Williams    pkgdest = d.getVar('PKGD')
16292b42cb3SPatrick Williams    for pkg in (d.getVar('PACKAGES') or "").split():
16392b42cb3SPatrick Williams        # If the src == dest, we know we need to rename the dest by appending ${BPN}
16492b42cb3SPatrick Williams        link_rename = []
16592b42cb3SPatrick Williams        for alt_name in (d.getVar('ALTERNATIVE:%s' % pkg) or "").split():
16692b42cb3SPatrick Williams            alt_link     = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name)
16792b42cb3SPatrick Williams            if not alt_link:
16892b42cb3SPatrick Williams                alt_link = "%s/%s" % (d.getVar('bindir'), alt_name)
16992b42cb3SPatrick Williams                d.setVarFlag('ALTERNATIVE_LINK_NAME', alt_name, alt_link)
17092b42cb3SPatrick Williams            if alt_link.startswith(os.path.join(d.getVar('sysconfdir'), 'init.d')):
17192b42cb3SPatrick Williams                # Managing init scripts does not work (bug #10433), foremost
17292b42cb3SPatrick Williams                # because of a race with update-rc.d
17392b42cb3SPatrick Williams                bb.fatal("Using update-alternatives for managing SysV init scripts is not supported")
17492b42cb3SPatrick Williams
17592b42cb3SPatrick Williams            alt_target   = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name) or d.getVarFlag('ALTERNATIVE_TARGET', alt_name)
17692b42cb3SPatrick Williams            alt_target   = alt_target or d.getVar('ALTERNATIVE_TARGET_%s' % pkg) or d.getVar('ALTERNATIVE_TARGET') or alt_link
17792b42cb3SPatrick Williams            # Sometimes alt_target is specified as relative to the link name.
17892b42cb3SPatrick Williams            alt_target   = os.path.join(os.path.dirname(alt_link), alt_target)
17992b42cb3SPatrick Williams
18092b42cb3SPatrick Williams            # If the link and target are the same name, we need to rename the target.
18192b42cb3SPatrick Williams            if alt_link == alt_target:
18292b42cb3SPatrick Williams                src = '%s/%s' % (pkgdest, alt_target)
18392b42cb3SPatrick Williams                alt_target_rename = '%s.%s' % (alt_target, pn)
18492b42cb3SPatrick Williams                dest = '%s/%s' % (pkgdest, alt_target_rename)
18592b42cb3SPatrick Williams                if os.path.lexists(dest):
18692b42cb3SPatrick Williams                    bb.note('%s: Already renamed: %s' % (pn, alt_target_rename))
18792b42cb3SPatrick Williams                elif os.path.lexists(src):
18892b42cb3SPatrick Williams                    if os.path.islink(src):
18992b42cb3SPatrick Williams                        # Delay rename of links
19092b42cb3SPatrick Williams                        link_rename.append((alt_target, alt_target_rename))
19192b42cb3SPatrick Williams                    else:
19292b42cb3SPatrick Williams                        bb.note('%s: Rename %s -> %s' % (pn, alt_target, alt_target_rename))
19392b42cb3SPatrick Williams                        bb.utils.rename(src, dest)
19492b42cb3SPatrick Williams                        update_files(alt_target, alt_target_rename, pkg, d)
19592b42cb3SPatrick Williams                else:
19692b42cb3SPatrick Williams                    bb.warn("%s: alternative target (%s or %s) does not exist, skipping..." % (pn, alt_target, alt_target_rename))
19792b42cb3SPatrick Williams                    continue
19892b42cb3SPatrick Williams                d.setVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name, alt_target_rename)
19992b42cb3SPatrick Williams
20092b42cb3SPatrick Williams        # Process delayed link names
20192b42cb3SPatrick Williams        # Do these after other renames so we can correct broken links
20292b42cb3SPatrick Williams        for (alt_target, alt_target_rename) in link_rename:
20392b42cb3SPatrick Williams            src = '%s/%s' % (pkgdest, alt_target)
20492b42cb3SPatrick Williams            dest = '%s/%s' % (pkgdest, alt_target_rename)
20592b42cb3SPatrick Williams            link_target = oe.path.realpath(src, pkgdest, True)
20692b42cb3SPatrick Williams
20792b42cb3SPatrick Williams            if os.path.lexists(link_target):
20892b42cb3SPatrick Williams                # Ok, the link_target exists, we can rename
20992b42cb3SPatrick Williams                bb.note('%s: Rename (link) %s -> %s' % (pn, alt_target, alt_target_rename))
21092b42cb3SPatrick Williams                bb.utils.rename(src, dest)
21192b42cb3SPatrick Williams            else:
21292b42cb3SPatrick Williams                # Try to resolve the broken link to link.${BPN}
21392b42cb3SPatrick Williams                link_maybe = '%s.%s' % (os.readlink(src), pn)
21492b42cb3SPatrick Williams                if os.path.lexists(os.path.join(os.path.dirname(src), link_maybe)):
21592b42cb3SPatrick Williams                    # Ok, the renamed link target exists.. create a new link, and remove the original
21692b42cb3SPatrick Williams                    bb.note('%s: Creating new link %s -> %s' % (pn, alt_target_rename, link_maybe))
21792b42cb3SPatrick Williams                    os.symlink(link_maybe, dest)
21892b42cb3SPatrick Williams                    os.unlink(src)
21992b42cb3SPatrick Williams                else:
22092b42cb3SPatrick Williams                    bb.warn('%s: Unable to resolve dangling symlink: %s' % (pn, alt_target))
22192b42cb3SPatrick Williams                    continue
22292b42cb3SPatrick Williams            update_files(alt_target, alt_target_rename, pkg, d)
22392b42cb3SPatrick Williams}
22492b42cb3SPatrick Williams
22592b42cb3SPatrick Williamsdef update_alternatives_alt_targets(d, pkg):
22692b42cb3SPatrick Williams    """
22792b42cb3SPatrick Williams    Returns the update-alternatives metadata for a package.
22892b42cb3SPatrick Williams
22992b42cb3SPatrick Williams    The returned format is a list of tuples where the tuple contains:
23092b42cb3SPatrick Williams    alt_name:     The binary name
23192b42cb3SPatrick Williams    alt_link:     The path for the binary (Shared by different packages)
23292b42cb3SPatrick Williams    alt_target:   The path for the renamed binary (Unique per package)
23392b42cb3SPatrick Williams    alt_priority: The priority of the alt_target
23492b42cb3SPatrick Williams
23592b42cb3SPatrick Williams    All the alt_targets will be installed into the sysroot. The alt_link is
23692b42cb3SPatrick Williams    a symlink pointing to the alt_target with the highest priority.
23792b42cb3SPatrick Williams    """
23892b42cb3SPatrick Williams
23992b42cb3SPatrick Williams    pn = d.getVar('BPN')
24092b42cb3SPatrick Williams    pkgdest = d.getVar('PKGD')
24192b42cb3SPatrick Williams    updates = list()
24292b42cb3SPatrick Williams    for alt_name in (d.getVar('ALTERNATIVE:%s' % pkg) or "").split():
24392b42cb3SPatrick Williams        alt_link     = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name)
24492b42cb3SPatrick Williams        alt_target   = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name) or \
24592b42cb3SPatrick Williams                       d.getVarFlag('ALTERNATIVE_TARGET', alt_name) or \
24692b42cb3SPatrick Williams                       d.getVar('ALTERNATIVE_TARGET_%s' % pkg) or \
24792b42cb3SPatrick Williams                       d.getVar('ALTERNATIVE_TARGET') or \
24892b42cb3SPatrick Williams                       alt_link
24992b42cb3SPatrick Williams        alt_priority = d.getVarFlag('ALTERNATIVE_PRIORITY_%s' % pkg,  alt_name) or \
25092b42cb3SPatrick Williams                       d.getVarFlag('ALTERNATIVE_PRIORITY',  alt_name) or \
25192b42cb3SPatrick Williams                       d.getVar('ALTERNATIVE_PRIORITY_%s' % pkg) or  \
25292b42cb3SPatrick Williams                       d.getVar('ALTERNATIVE_PRIORITY')
25392b42cb3SPatrick Williams
25492b42cb3SPatrick Williams        # This shouldn't trigger, as it should have been resolved earlier!
25592b42cb3SPatrick Williams        if alt_link == alt_target:
25692b42cb3SPatrick Williams            bb.note('alt_link == alt_target: %s == %s -- correcting, this should not happen!' % (alt_link, alt_target))
25792b42cb3SPatrick Williams            alt_target = '%s.%s' % (alt_target, pn)
25892b42cb3SPatrick Williams
25992b42cb3SPatrick Williams        if not os.path.lexists('%s/%s' % (pkgdest, alt_target)):
26092b42cb3SPatrick Williams            bb.warn('%s: NOT adding alternative provide %s: %s does not exist' % (pn, alt_link, alt_target))
26192b42cb3SPatrick Williams            continue
26292b42cb3SPatrick Williams
26392b42cb3SPatrick Williams        alt_target = os.path.normpath(alt_target)
26492b42cb3SPatrick Williams        updates.append( (alt_name, alt_link, alt_target, alt_priority) )
26592b42cb3SPatrick Williams
26692b42cb3SPatrick Williams    return updates
26792b42cb3SPatrick Williams
268517393d9SAndrew GeisslerPACKAGESPLITFUNCS =+ "populate_packages_updatealternatives"
26992b42cb3SPatrick Williams
27092b42cb3SPatrick Williamspython populate_packages_updatealternatives () {
27192b42cb3SPatrick Williams    if not update_alternatives_enabled(d):
27292b42cb3SPatrick Williams        return
27392b42cb3SPatrick Williams
27492b42cb3SPatrick Williams    # Do actual update alternatives processing
27592b42cb3SPatrick Williams    for pkg in (d.getVar('PACKAGES') or "").split():
27692b42cb3SPatrick Williams        # Create post install/removal scripts
27792b42cb3SPatrick Williams        alt_setup_links = ""
27892b42cb3SPatrick Williams        alt_remove_links = ""
27992b42cb3SPatrick Williams        updates = update_alternatives_alt_targets(d, pkg)
28092b42cb3SPatrick Williams        for alt_name, alt_link, alt_target, alt_priority in updates:
28192b42cb3SPatrick Williams            alt_setup_links  += '\tupdate-alternatives --install %s %s %s %s\n' % (alt_link, alt_name, alt_target, alt_priority)
28292b42cb3SPatrick Williams            alt_remove_links += '\tupdate-alternatives --remove  %s %s\n' % (alt_name, alt_target)
28392b42cb3SPatrick Williams
28492b42cb3SPatrick Williams        if alt_setup_links:
28592b42cb3SPatrick Williams            # RDEPENDS setup
28692b42cb3SPatrick Williams            provider = d.getVar('VIRTUAL-RUNTIME_update-alternatives')
28792b42cb3SPatrick Williams            if provider:
28892b42cb3SPatrick Williams                #bb.note('adding runtime requirement for update-alternatives for %s' % pkg)
28992b42cb3SPatrick Williams                d.appendVar('RDEPENDS:%s' % pkg, ' ' + d.getVar('MLPREFIX', False) + provider)
29092b42cb3SPatrick Williams
29192b42cb3SPatrick Williams            bb.note('adding update-alternatives calls to postinst/prerm for %s' % pkg)
29292b42cb3SPatrick Williams            bb.note('%s' % alt_setup_links)
29392b42cb3SPatrick Williams            postinst = d.getVar('pkg_postinst:%s' % pkg)
29492b42cb3SPatrick Williams            if postinst:
29592b42cb3SPatrick Williams                postinst = alt_setup_links + postinst
29692b42cb3SPatrick Williams            else:
29792b42cb3SPatrick Williams                postinst = '#!/bin/sh\n' + alt_setup_links
29892b42cb3SPatrick Williams            d.setVar('pkg_postinst:%s' % pkg, postinst)
29992b42cb3SPatrick Williams
30092b42cb3SPatrick Williams            bb.note('%s' % alt_remove_links)
30192b42cb3SPatrick Williams            prerm = d.getVar('pkg_prerm:%s' % pkg) or '#!/bin/sh\n'
30292b42cb3SPatrick Williams            prerm += alt_remove_links
30392b42cb3SPatrick Williams            d.setVar('pkg_prerm:%s' % pkg, prerm)
30492b42cb3SPatrick Williams}
30592b42cb3SPatrick Williams
30692b42cb3SPatrick Williamspython package_do_filedeps:append () {
30792b42cb3SPatrick Williams    if update_alternatives_enabled(d):
30892b42cb3SPatrick Williams        apply_update_alternative_provides(d)
30992b42cb3SPatrick Williams}
31092b42cb3SPatrick Williams
31192b42cb3SPatrick Williamsdef apply_update_alternative_provides(d):
31292b42cb3SPatrick Williams    pn = d.getVar('BPN')
31392b42cb3SPatrick Williams    pkgdest = d.getVar('PKGDEST')
31492b42cb3SPatrick Williams
31592b42cb3SPatrick Williams    for pkg in d.getVar('PACKAGES').split():
31692b42cb3SPatrick Williams        for alt_name in (d.getVar('ALTERNATIVE:%s' % pkg) or "").split():
31792b42cb3SPatrick Williams            alt_link     = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name)
31892b42cb3SPatrick Williams            alt_target   = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name) or d.getVarFlag('ALTERNATIVE_TARGET', alt_name)
31992b42cb3SPatrick Williams            alt_target   = alt_target or d.getVar('ALTERNATIVE_TARGET_%s' % pkg) or d.getVar('ALTERNATIVE_TARGET') or alt_link
32092b42cb3SPatrick Williams
32192b42cb3SPatrick Williams            if alt_link == alt_target:
32292b42cb3SPatrick Williams                bb.warn('%s: alt_link == alt_target: %s == %s' % (pn, alt_link, alt_target))
32392b42cb3SPatrick Williams                alt_target = '%s.%s' % (alt_target, pn)
32492b42cb3SPatrick Williams
32592b42cb3SPatrick Williams            if not os.path.lexists('%s/%s/%s' % (pkgdest, pkg, alt_target)):
32692b42cb3SPatrick Williams                continue
32792b42cb3SPatrick Williams
32892b42cb3SPatrick Williams            # Add file provide
32992b42cb3SPatrick Williams            trans_target = oe.package.file_translate(alt_target)
33092b42cb3SPatrick Williams            d.appendVar('FILERPROVIDES:%s:%s' % (trans_target, pkg), " " + alt_link)
33192b42cb3SPatrick Williams            if not trans_target in (d.getVar('FILERPROVIDESFLIST:%s' % pkg) or ""):
33292b42cb3SPatrick Williams                d.appendVar('FILERPROVIDESFLIST:%s' % pkg, " " + trans_target)
33392b42cb3SPatrick Williams
334