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