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 Williams# This class is used to help the alternatives system which is useful when
8*92b42cb3SPatrick Williams# multiple sources provide same command. You can use update-alternatives
9*92b42cb3SPatrick Williams# command directly in your recipe, but in most cases this class simplifies
10*92b42cb3SPatrick Williams# that job.
11*92b42cb3SPatrick Williams#
12*92b42cb3SPatrick Williams# To use this class a number of variables should be defined:
13*92b42cb3SPatrick Williams#
14*92b42cb3SPatrick Williams# List all of the alternatives needed by a package:
15*92b42cb3SPatrick Williams# ALTERNATIVE:<pkg> = "name1 name2 name3 ..."
16*92b42cb3SPatrick Williams#
17*92b42cb3SPatrick Williams#   i.e. ALTERNATIVE:busybox = "sh sed test bracket"
18*92b42cb3SPatrick Williams#
19*92b42cb3SPatrick Williams# The pathname of the link
20*92b42cb3SPatrick Williams# ALTERNATIVE_LINK_NAME[name] = "target"
21*92b42cb3SPatrick Williams#
22*92b42cb3SPatrick Williams#   This is the name of the binary once it's been installed onto the runtime.
23*92b42cb3SPatrick Williams#   This name is global to all split packages in this recipe, and should match
24*92b42cb3SPatrick Williams#   other recipes with the same functionality.
25*92b42cb3SPatrick Williams#   i.e. ALTERNATIVE_LINK_NAME[bracket] = "/usr/bin/["
26*92b42cb3SPatrick Williams#
27*92b42cb3SPatrick Williams# NOTE: If ALTERNATIVE_LINK_NAME is not defined, it defaults to ${bindir}/name
28*92b42cb3SPatrick Williams#
29*92b42cb3SPatrick Williams# The default link to create for all targets
30*92b42cb3SPatrick Williams# ALTERNATIVE_TARGET = "target"
31*92b42cb3SPatrick Williams#
32*92b42cb3SPatrick Williams#   This is useful in a multicall binary case
33*92b42cb3SPatrick Williams#   i.e. ALTERNATIVE_TARGET = "/bin/busybox"
34*92b42cb3SPatrick Williams#
35*92b42cb3SPatrick Williams# A non-default link to create for a target
36*92b42cb3SPatrick Williams# ALTERNATIVE_TARGET[name] = "target"
37*92b42cb3SPatrick Williams#
38*92b42cb3SPatrick Williams#   This is the name of the binary as it's been install by do_install
39*92b42cb3SPatrick Williams#   i.e. ALTERNATIVE_TARGET[sh] = "/bin/bash"
40*92b42cb3SPatrick Williams#
41*92b42cb3SPatrick Williams# A package specific link for a target
42*92b42cb3SPatrick Williams# ALTERNATIVE_TARGET_<pkg>[name] = "target"
43*92b42cb3SPatrick Williams#
44*92b42cb3SPatrick Williams#   This is useful when a recipe provides multiple alternatives for the
45*92b42cb3SPatrick Williams#   same item.
46*92b42cb3SPatrick Williams#
47*92b42cb3SPatrick Williams# NOTE: If ALTERNATIVE_TARGET is not defined, it will inherit the value
48*92b42cb3SPatrick Williams# from ALTERNATIVE_LINK_NAME.
49*92b42cb3SPatrick Williams#
50*92b42cb3SPatrick Williams# NOTE: If the ALTERNATIVE_LINK_NAME and ALTERNATIVE_TARGET are the same,
51*92b42cb3SPatrick Williams# ALTERNATIVE_TARGET will have '.{BPN}' appended to it.  If the file
52*92b42cb3SPatrick Williams# referenced has not been renamed, it will also be renamed.  (This avoids
53*92b42cb3SPatrick Williams# the need to rename alternative files in the do_install step, but still
54*92b42cb3SPatrick Williams# supports it if necessary for some reason.)
55*92b42cb3SPatrick Williams#
56*92b42cb3SPatrick Williams# The default priority for any alternatives
57*92b42cb3SPatrick Williams# ALTERNATIVE_PRIORITY = "priority"
58*92b42cb3SPatrick Williams#
59*92b42cb3SPatrick Williams#   i.e. default is ALTERNATIVE_PRIORITY = "10"
60*92b42cb3SPatrick Williams#
61*92b42cb3SPatrick Williams# The non-default priority for a specific target
62*92b42cb3SPatrick Williams# ALTERNATIVE_PRIORITY[name] = "priority"
63*92b42cb3SPatrick Williams#
64*92b42cb3SPatrick Williams# The package priority for a specific target
65*92b42cb3SPatrick Williams# ALTERNATIVE_PRIORITY_<pkg>[name] = "priority"
66*92b42cb3SPatrick Williams
67*92b42cb3SPatrick WilliamsALTERNATIVE_PRIORITY = "10"
68*92b42cb3SPatrick Williams
69*92b42cb3SPatrick Williams# We need special processing for vardeps because it can not work on
70*92b42cb3SPatrick Williams# modified flag values.  So we aggregate the flags into a new variable
71*92b42cb3SPatrick Williams# and include that vairable in the set.
72*92b42cb3SPatrick WilliamsUPDALTVARS  = "ALTERNATIVE ALTERNATIVE_LINK_NAME ALTERNATIVE_TARGET ALTERNATIVE_PRIORITY"
73*92b42cb3SPatrick Williams
74*92b42cb3SPatrick WilliamsPACKAGE_WRITE_DEPS += "virtual/update-alternatives-native"
75*92b42cb3SPatrick Williams
76*92b42cb3SPatrick Williamsdef gen_updatealternativesvardeps(d):
77*92b42cb3SPatrick Williams    pkgs = (d.getVar("PACKAGES") or "").split()
78*92b42cb3SPatrick Williams    vars = (d.getVar("UPDALTVARS") or "").split()
79*92b42cb3SPatrick Williams
80*92b42cb3SPatrick Williams    # First compute them for non_pkg versions
81*92b42cb3SPatrick Williams    for v in vars:
82*92b42cb3SPatrick Williams        for flag in sorted((d.getVarFlags(v) or {}).keys()):
83*92b42cb3SPatrick Williams            if flag == "doc" or flag == "vardeps" or flag == "vardepsexp":
84*92b42cb3SPatrick Williams                continue
85*92b42cb3SPatrick Williams            d.appendVar('%s_VARDEPS' % (v), ' %s:%s' % (flag, d.getVarFlag(v, flag, False)))
86*92b42cb3SPatrick Williams
87*92b42cb3SPatrick Williams    for p in pkgs:
88*92b42cb3SPatrick Williams        for v in vars:
89*92b42cb3SPatrick Williams            for flag in sorted((d.getVarFlags("%s_%s" % (v,p)) or {}).keys()):
90*92b42cb3SPatrick Williams                if flag == "doc" or flag == "vardeps" or flag == "vardepsexp":
91*92b42cb3SPatrick Williams                    continue
92*92b42cb3SPatrick Williams                d.appendVar('%s_VARDEPS_%s' % (v,p), ' %s:%s' % (flag, d.getVarFlag('%s_%s' % (v,p), flag, False)))
93*92b42cb3SPatrick Williams
94*92b42cb3SPatrick Williamsdef ua_extend_depends(d):
95*92b42cb3SPatrick Williams    if not 'virtual/update-alternatives' in d.getVar('PROVIDES'):
96*92b42cb3SPatrick Williams        d.appendVar('DEPENDS', ' virtual/${MLPREFIX}update-alternatives')
97*92b42cb3SPatrick Williams
98*92b42cb3SPatrick Williamsdef update_alternatives_enabled(d):
99*92b42cb3SPatrick Williams    # Update Alternatives only works on target packages...
100*92b42cb3SPatrick Williams    if bb.data.inherits_class('native', d) or \
101*92b42cb3SPatrick Williams       bb.data.inherits_class('cross', d) or bb.data.inherits_class('crosssdk', d) or \
102*92b42cb3SPatrick Williams       bb.data.inherits_class('cross-canadian', d):
103*92b42cb3SPatrick Williams        return False
104*92b42cb3SPatrick Williams
105*92b42cb3SPatrick Williams    # Disable when targeting mingw32 (no target support)
106*92b42cb3SPatrick Williams    if d.getVar("TARGET_OS") == "mingw32":
107*92b42cb3SPatrick Williams        return False
108*92b42cb3SPatrick Williams
109*92b42cb3SPatrick Williams    return True
110*92b42cb3SPatrick Williams
111*92b42cb3SPatrick Williamspython __anonymous() {
112*92b42cb3SPatrick Williams    if not update_alternatives_enabled(d):
113*92b42cb3SPatrick Williams        return
114*92b42cb3SPatrick Williams
115*92b42cb3SPatrick Williams    # compute special vardeps
116*92b42cb3SPatrick Williams    gen_updatealternativesvardeps(d)
117*92b42cb3SPatrick Williams
118*92b42cb3SPatrick Williams    # extend the depends to include virtual/update-alternatives
119*92b42cb3SPatrick Williams    ua_extend_depends(d)
120*92b42cb3SPatrick Williams}
121*92b42cb3SPatrick Williams
122*92b42cb3SPatrick Williamsdef gen_updatealternativesvars(d):
123*92b42cb3SPatrick Williams    ret = []
124*92b42cb3SPatrick Williams    pkgs = (d.getVar("PACKAGES") or "").split()
125*92b42cb3SPatrick Williams    vars = (d.getVar("UPDALTVARS") or "").split()
126*92b42cb3SPatrick Williams
127*92b42cb3SPatrick Williams    for v in vars:
128*92b42cb3SPatrick Williams        ret.append(v + "_VARDEPS")
129*92b42cb3SPatrick Williams
130*92b42cb3SPatrick Williams    for p in pkgs:
131*92b42cb3SPatrick Williams        for v in vars:
132*92b42cb3SPatrick Williams            ret.append(v + ":" + p)
133*92b42cb3SPatrick Williams            ret.append(v + "_VARDEPS_" + p)
134*92b42cb3SPatrick Williams    return " ".join(ret)
135*92b42cb3SPatrick Williams
136*92b42cb3SPatrick Williams# Now the new stuff, we use a custom function to generate the right values
137*92b42cb3SPatrick Williamspopulate_packages[vardeps] += "${UPDALTVARS} ${@gen_updatealternativesvars(d)}"
138*92b42cb3SPatrick Williams
139*92b42cb3SPatrick Williams# We need to do the rename after the image creation step, but before
140*92b42cb3SPatrick Williams# the split and strip steps..  PACKAGE_PREPROCESS_FUNCS is the right
141*92b42cb3SPatrick Williams# place for that.
142*92b42cb3SPatrick WilliamsPACKAGE_PREPROCESS_FUNCS += "apply_update_alternative_renames"
143*92b42cb3SPatrick Williamspython apply_update_alternative_renames () {
144*92b42cb3SPatrick Williams    if not update_alternatives_enabled(d):
145*92b42cb3SPatrick Williams       return
146*92b42cb3SPatrick Williams
147*92b42cb3SPatrick Williams    import re
148*92b42cb3SPatrick Williams
149*92b42cb3SPatrick Williams    def update_files(alt_target, alt_target_rename, pkg, d):
150*92b42cb3SPatrick Williams        f = d.getVar('FILES:' + pkg)
151*92b42cb3SPatrick Williams        if f:
152*92b42cb3SPatrick Williams            f = re.sub(r'(^|\s)%s(\s|$)' % re.escape (alt_target), r'\1%s\2' % alt_target_rename, f)
153*92b42cb3SPatrick Williams            d.setVar('FILES:' + pkg, f)
154*92b42cb3SPatrick Williams
155*92b42cb3SPatrick Williams    # Check for deprecated usage...
156*92b42cb3SPatrick Williams    pn = d.getVar('BPN')
157*92b42cb3SPatrick Williams    if d.getVar('ALTERNATIVE_LINKS') != None:
158*92b42cb3SPatrick 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)
159*92b42cb3SPatrick Williams
160*92b42cb3SPatrick Williams    # Do actual update alternatives processing
161*92b42cb3SPatrick Williams    pkgdest = d.getVar('PKGD')
162*92b42cb3SPatrick Williams    for pkg in (d.getVar('PACKAGES') or "").split():
163*92b42cb3SPatrick Williams        # If the src == dest, we know we need to rename the dest by appending ${BPN}
164*92b42cb3SPatrick Williams        link_rename = []
165*92b42cb3SPatrick Williams        for alt_name in (d.getVar('ALTERNATIVE:%s' % pkg) or "").split():
166*92b42cb3SPatrick Williams            alt_link     = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name)
167*92b42cb3SPatrick Williams            if not alt_link:
168*92b42cb3SPatrick Williams                alt_link = "%s/%s" % (d.getVar('bindir'), alt_name)
169*92b42cb3SPatrick Williams                d.setVarFlag('ALTERNATIVE_LINK_NAME', alt_name, alt_link)
170*92b42cb3SPatrick Williams            if alt_link.startswith(os.path.join(d.getVar('sysconfdir'), 'init.d')):
171*92b42cb3SPatrick Williams                # Managing init scripts does not work (bug #10433), foremost
172*92b42cb3SPatrick Williams                # because of a race with update-rc.d
173*92b42cb3SPatrick Williams                bb.fatal("Using update-alternatives for managing SysV init scripts is not supported")
174*92b42cb3SPatrick Williams
175*92b42cb3SPatrick Williams            alt_target   = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name) or d.getVarFlag('ALTERNATIVE_TARGET', alt_name)
176*92b42cb3SPatrick Williams            alt_target   = alt_target or d.getVar('ALTERNATIVE_TARGET_%s' % pkg) or d.getVar('ALTERNATIVE_TARGET') or alt_link
177*92b42cb3SPatrick Williams            # Sometimes alt_target is specified as relative to the link name.
178*92b42cb3SPatrick Williams            alt_target   = os.path.join(os.path.dirname(alt_link), alt_target)
179*92b42cb3SPatrick Williams
180*92b42cb3SPatrick Williams            # If the link and target are the same name, we need to rename the target.
181*92b42cb3SPatrick Williams            if alt_link == alt_target:
182*92b42cb3SPatrick Williams                src = '%s/%s' % (pkgdest, alt_target)
183*92b42cb3SPatrick Williams                alt_target_rename = '%s.%s' % (alt_target, pn)
184*92b42cb3SPatrick Williams                dest = '%s/%s' % (pkgdest, alt_target_rename)
185*92b42cb3SPatrick Williams                if os.path.lexists(dest):
186*92b42cb3SPatrick Williams                    bb.note('%s: Already renamed: %s' % (pn, alt_target_rename))
187*92b42cb3SPatrick Williams                elif os.path.lexists(src):
188*92b42cb3SPatrick Williams                    if os.path.islink(src):
189*92b42cb3SPatrick Williams                        # Delay rename of links
190*92b42cb3SPatrick Williams                        link_rename.append((alt_target, alt_target_rename))
191*92b42cb3SPatrick Williams                    else:
192*92b42cb3SPatrick Williams                        bb.note('%s: Rename %s -> %s' % (pn, alt_target, alt_target_rename))
193*92b42cb3SPatrick Williams                        bb.utils.rename(src, dest)
194*92b42cb3SPatrick Williams                        update_files(alt_target, alt_target_rename, pkg, d)
195*92b42cb3SPatrick Williams                else:
196*92b42cb3SPatrick Williams                    bb.warn("%s: alternative target (%s or %s) does not exist, skipping..." % (pn, alt_target, alt_target_rename))
197*92b42cb3SPatrick Williams                    continue
198*92b42cb3SPatrick Williams                d.setVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name, alt_target_rename)
199*92b42cb3SPatrick Williams
200*92b42cb3SPatrick Williams        # Process delayed link names
201*92b42cb3SPatrick Williams        # Do these after other renames so we can correct broken links
202*92b42cb3SPatrick Williams        for (alt_target, alt_target_rename) in link_rename:
203*92b42cb3SPatrick Williams            src = '%s/%s' % (pkgdest, alt_target)
204*92b42cb3SPatrick Williams            dest = '%s/%s' % (pkgdest, alt_target_rename)
205*92b42cb3SPatrick Williams            link_target = oe.path.realpath(src, pkgdest, True)
206*92b42cb3SPatrick Williams
207*92b42cb3SPatrick Williams            if os.path.lexists(link_target):
208*92b42cb3SPatrick Williams                # Ok, the link_target exists, we can rename
209*92b42cb3SPatrick Williams                bb.note('%s: Rename (link) %s -> %s' % (pn, alt_target, alt_target_rename))
210*92b42cb3SPatrick Williams                bb.utils.rename(src, dest)
211*92b42cb3SPatrick Williams            else:
212*92b42cb3SPatrick Williams                # Try to resolve the broken link to link.${BPN}
213*92b42cb3SPatrick Williams                link_maybe = '%s.%s' % (os.readlink(src), pn)
214*92b42cb3SPatrick Williams                if os.path.lexists(os.path.join(os.path.dirname(src), link_maybe)):
215*92b42cb3SPatrick Williams                    # Ok, the renamed link target exists.. create a new link, and remove the original
216*92b42cb3SPatrick Williams                    bb.note('%s: Creating new link %s -> %s' % (pn, alt_target_rename, link_maybe))
217*92b42cb3SPatrick Williams                    os.symlink(link_maybe, dest)
218*92b42cb3SPatrick Williams                    os.unlink(src)
219*92b42cb3SPatrick Williams                else:
220*92b42cb3SPatrick Williams                    bb.warn('%s: Unable to resolve dangling symlink: %s' % (pn, alt_target))
221*92b42cb3SPatrick Williams                    continue
222*92b42cb3SPatrick Williams            update_files(alt_target, alt_target_rename, pkg, d)
223*92b42cb3SPatrick Williams}
224*92b42cb3SPatrick Williams
225*92b42cb3SPatrick Williamsdef update_alternatives_alt_targets(d, pkg):
226*92b42cb3SPatrick Williams    """
227*92b42cb3SPatrick Williams    Returns the update-alternatives metadata for a package.
228*92b42cb3SPatrick Williams
229*92b42cb3SPatrick Williams    The returned format is a list of tuples where the tuple contains:
230*92b42cb3SPatrick Williams    alt_name:     The binary name
231*92b42cb3SPatrick Williams    alt_link:     The path for the binary (Shared by different packages)
232*92b42cb3SPatrick Williams    alt_target:   The path for the renamed binary (Unique per package)
233*92b42cb3SPatrick Williams    alt_priority: The priority of the alt_target
234*92b42cb3SPatrick Williams
235*92b42cb3SPatrick Williams    All the alt_targets will be installed into the sysroot. The alt_link is
236*92b42cb3SPatrick Williams    a symlink pointing to the alt_target with the highest priority.
237*92b42cb3SPatrick Williams    """
238*92b42cb3SPatrick Williams
239*92b42cb3SPatrick Williams    pn = d.getVar('BPN')
240*92b42cb3SPatrick Williams    pkgdest = d.getVar('PKGD')
241*92b42cb3SPatrick Williams    updates = list()
242*92b42cb3SPatrick Williams    for alt_name in (d.getVar('ALTERNATIVE:%s' % pkg) or "").split():
243*92b42cb3SPatrick Williams        alt_link     = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name)
244*92b42cb3SPatrick Williams        alt_target   = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name) or \
245*92b42cb3SPatrick Williams                       d.getVarFlag('ALTERNATIVE_TARGET', alt_name) or \
246*92b42cb3SPatrick Williams                       d.getVar('ALTERNATIVE_TARGET_%s' % pkg) or \
247*92b42cb3SPatrick Williams                       d.getVar('ALTERNATIVE_TARGET') or \
248*92b42cb3SPatrick Williams                       alt_link
249*92b42cb3SPatrick Williams        alt_priority = d.getVarFlag('ALTERNATIVE_PRIORITY_%s' % pkg,  alt_name) or \
250*92b42cb3SPatrick Williams                       d.getVarFlag('ALTERNATIVE_PRIORITY',  alt_name) or \
251*92b42cb3SPatrick Williams                       d.getVar('ALTERNATIVE_PRIORITY_%s' % pkg) or  \
252*92b42cb3SPatrick Williams                       d.getVar('ALTERNATIVE_PRIORITY')
253*92b42cb3SPatrick Williams
254*92b42cb3SPatrick Williams        # This shouldn't trigger, as it should have been resolved earlier!
255*92b42cb3SPatrick Williams        if alt_link == alt_target:
256*92b42cb3SPatrick Williams            bb.note('alt_link == alt_target: %s == %s -- correcting, this should not happen!' % (alt_link, alt_target))
257*92b42cb3SPatrick Williams            alt_target = '%s.%s' % (alt_target, pn)
258*92b42cb3SPatrick Williams
259*92b42cb3SPatrick Williams        if not os.path.lexists('%s/%s' % (pkgdest, alt_target)):
260*92b42cb3SPatrick Williams            bb.warn('%s: NOT adding alternative provide %s: %s does not exist' % (pn, alt_link, alt_target))
261*92b42cb3SPatrick Williams            continue
262*92b42cb3SPatrick Williams
263*92b42cb3SPatrick Williams        alt_target = os.path.normpath(alt_target)
264*92b42cb3SPatrick Williams        updates.append( (alt_name, alt_link, alt_target, alt_priority) )
265*92b42cb3SPatrick Williams
266*92b42cb3SPatrick Williams    return updates
267*92b42cb3SPatrick Williams
268*92b42cb3SPatrick WilliamsPACKAGESPLITFUNCS:prepend = "populate_packages_updatealternatives "
269*92b42cb3SPatrick Williams
270*92b42cb3SPatrick Williamspython populate_packages_updatealternatives () {
271*92b42cb3SPatrick Williams    if not update_alternatives_enabled(d):
272*92b42cb3SPatrick Williams        return
273*92b42cb3SPatrick Williams
274*92b42cb3SPatrick Williams    # Do actual update alternatives processing
275*92b42cb3SPatrick Williams    for pkg in (d.getVar('PACKAGES') or "").split():
276*92b42cb3SPatrick Williams        # Create post install/removal scripts
277*92b42cb3SPatrick Williams        alt_setup_links = ""
278*92b42cb3SPatrick Williams        alt_remove_links = ""
279*92b42cb3SPatrick Williams        updates = update_alternatives_alt_targets(d, pkg)
280*92b42cb3SPatrick Williams        for alt_name, alt_link, alt_target, alt_priority in updates:
281*92b42cb3SPatrick Williams            alt_setup_links  += '\tupdate-alternatives --install %s %s %s %s\n' % (alt_link, alt_name, alt_target, alt_priority)
282*92b42cb3SPatrick Williams            alt_remove_links += '\tupdate-alternatives --remove  %s %s\n' % (alt_name, alt_target)
283*92b42cb3SPatrick Williams
284*92b42cb3SPatrick Williams        if alt_setup_links:
285*92b42cb3SPatrick Williams            # RDEPENDS setup
286*92b42cb3SPatrick Williams            provider = d.getVar('VIRTUAL-RUNTIME_update-alternatives')
287*92b42cb3SPatrick Williams            if provider:
288*92b42cb3SPatrick Williams                #bb.note('adding runtime requirement for update-alternatives for %s' % pkg)
289*92b42cb3SPatrick Williams                d.appendVar('RDEPENDS:%s' % pkg, ' ' + d.getVar('MLPREFIX', False) + provider)
290*92b42cb3SPatrick Williams
291*92b42cb3SPatrick Williams            bb.note('adding update-alternatives calls to postinst/prerm for %s' % pkg)
292*92b42cb3SPatrick Williams            bb.note('%s' % alt_setup_links)
293*92b42cb3SPatrick Williams            postinst = d.getVar('pkg_postinst:%s' % pkg)
294*92b42cb3SPatrick Williams            if postinst:
295*92b42cb3SPatrick Williams                postinst = alt_setup_links + postinst
296*92b42cb3SPatrick Williams            else:
297*92b42cb3SPatrick Williams                postinst = '#!/bin/sh\n' + alt_setup_links
298*92b42cb3SPatrick Williams            d.setVar('pkg_postinst:%s' % pkg, postinst)
299*92b42cb3SPatrick Williams
300*92b42cb3SPatrick Williams            bb.note('%s' % alt_remove_links)
301*92b42cb3SPatrick Williams            prerm = d.getVar('pkg_prerm:%s' % pkg) or '#!/bin/sh\n'
302*92b42cb3SPatrick Williams            prerm += alt_remove_links
303*92b42cb3SPatrick Williams            d.setVar('pkg_prerm:%s' % pkg, prerm)
304*92b42cb3SPatrick Williams}
305*92b42cb3SPatrick Williams
306*92b42cb3SPatrick Williamspython package_do_filedeps:append () {
307*92b42cb3SPatrick Williams    if update_alternatives_enabled(d):
308*92b42cb3SPatrick Williams        apply_update_alternative_provides(d)
309*92b42cb3SPatrick Williams}
310*92b42cb3SPatrick Williams
311*92b42cb3SPatrick Williamsdef apply_update_alternative_provides(d):
312*92b42cb3SPatrick Williams    pn = d.getVar('BPN')
313*92b42cb3SPatrick Williams    pkgdest = d.getVar('PKGDEST')
314*92b42cb3SPatrick Williams
315*92b42cb3SPatrick Williams    for pkg in d.getVar('PACKAGES').split():
316*92b42cb3SPatrick Williams        for alt_name in (d.getVar('ALTERNATIVE:%s' % pkg) or "").split():
317*92b42cb3SPatrick Williams            alt_link     = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name)
318*92b42cb3SPatrick Williams            alt_target   = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name) or d.getVarFlag('ALTERNATIVE_TARGET', alt_name)
319*92b42cb3SPatrick Williams            alt_target   = alt_target or d.getVar('ALTERNATIVE_TARGET_%s' % pkg) or d.getVar('ALTERNATIVE_TARGET') or alt_link
320*92b42cb3SPatrick Williams
321*92b42cb3SPatrick Williams            if alt_link == alt_target:
322*92b42cb3SPatrick Williams                bb.warn('%s: alt_link == alt_target: %s == %s' % (pn, alt_link, alt_target))
323*92b42cb3SPatrick Williams                alt_target = '%s.%s' % (alt_target, pn)
324*92b42cb3SPatrick Williams
325*92b42cb3SPatrick Williams            if not os.path.lexists('%s/%s/%s' % (pkgdest, pkg, alt_target)):
326*92b42cb3SPatrick Williams                continue
327*92b42cb3SPatrick Williams
328*92b42cb3SPatrick Williams            # Add file provide
329*92b42cb3SPatrick Williams            trans_target = oe.package.file_translate(alt_target)
330*92b42cb3SPatrick Williams            d.appendVar('FILERPROVIDES:%s:%s' % (trans_target, pkg), " " + alt_link)
331*92b42cb3SPatrick Williams            if not trans_target in (d.getVar('FILERPROVIDESFLIST:%s' % pkg) or ""):
332*92b42cb3SPatrick Williams                d.appendVar('FILERPROVIDESFLIST:%s' % pkg, " " + trans_target)
333*92b42cb3SPatrick Williams
334