Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 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 |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 8 | # multiple sources provide the same command. You can use update-alternatives |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 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 | # |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 38 | # This is the name of the binary as it's been installed by do_install |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 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 | |
| 67 | ALTERNATIVE_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 |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 71 | # and include that variable in the set. |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 72 | UPDALTVARS = "ALTERNATIVE ALTERNATIVE_LINK_NAME ALTERNATIVE_TARGET ALTERNATIVE_PRIORITY" |
| 73 | |
| 74 | PACKAGE_WRITE_DEPS += "virtual/update-alternatives-native" |
| 75 | |
| 76 | def gen_updatealternativesvardeps(d): |
| 77 | pkgs = (d.getVar("PACKAGES") or "").split() |
| 78 | vars = (d.getVar("UPDALTVARS") or "").split() |
| 79 | |
| 80 | # First compute them for non_pkg versions |
| 81 | for v in vars: |
| 82 | for flag in sorted((d.getVarFlags(v) or {}).keys()): |
| 83 | if flag == "doc" or flag == "vardeps" or flag == "vardepsexp": |
| 84 | continue |
| 85 | d.appendVar('%s_VARDEPS' % (v), ' %s:%s' % (flag, d.getVarFlag(v, flag, False))) |
| 86 | |
| 87 | for p in pkgs: |
| 88 | for v in vars: |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 89 | for flag in sorted((d.getVarFlags("%s:%s" % (v,p)) or {}).keys()): |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 90 | if flag == "doc" or flag == "vardeps" or flag == "vardepsexp": |
| 91 | continue |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 92 | d.appendVar('%s_VARDEPS_%s' % (v,p), ' %s:%s' % (flag, d.getVarFlag('%s:%s' % (v,p), flag, False))) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 93 | |
| 94 | def ua_extend_depends(d): |
| 95 | if not 'virtual/update-alternatives' in d.getVar('PROVIDES'): |
| 96 | d.appendVar('DEPENDS', ' virtual/${MLPREFIX}update-alternatives') |
| 97 | |
| 98 | def update_alternatives_enabled(d): |
| 99 | # Update Alternatives only works on target packages... |
| 100 | if bb.data.inherits_class('native', d) or \ |
| 101 | bb.data.inherits_class('cross', d) or bb.data.inherits_class('crosssdk', d) or \ |
| 102 | bb.data.inherits_class('cross-canadian', d): |
| 103 | return False |
| 104 | |
| 105 | # Disable when targeting mingw32 (no target support) |
| 106 | if d.getVar("TARGET_OS") == "mingw32": |
| 107 | return False |
| 108 | |
| 109 | return True |
| 110 | |
| 111 | python __anonymous() { |
| 112 | if not update_alternatives_enabled(d): |
| 113 | return |
| 114 | |
| 115 | # compute special vardeps |
| 116 | gen_updatealternativesvardeps(d) |
| 117 | |
| 118 | # extend the depends to include virtual/update-alternatives |
| 119 | ua_extend_depends(d) |
| 120 | } |
| 121 | |
| 122 | def gen_updatealternativesvars(d): |
| 123 | ret = [] |
| 124 | pkgs = (d.getVar("PACKAGES") or "").split() |
| 125 | vars = (d.getVar("UPDALTVARS") or "").split() |
| 126 | |
| 127 | for v in vars: |
| 128 | ret.append(v + "_VARDEPS") |
| 129 | |
| 130 | for p in pkgs: |
| 131 | for v in vars: |
| 132 | ret.append(v + ":" + p) |
| 133 | ret.append(v + "_VARDEPS_" + p) |
| 134 | return " ".join(ret) |
| 135 | |
| 136 | # Now the new stuff, we use a custom function to generate the right values |
| 137 | populate_packages[vardeps] += "${UPDALTVARS} ${@gen_updatealternativesvars(d)}" |
| 138 | |
| 139 | # We need to do the rename after the image creation step, but before |
| 140 | # the split and strip steps.. PACKAGE_PREPROCESS_FUNCS is the right |
| 141 | # place for that. |
| 142 | PACKAGE_PREPROCESS_FUNCS += "apply_update_alternative_renames" |
| 143 | python apply_update_alternative_renames () { |
| 144 | if not update_alternatives_enabled(d): |
| 145 | return |
| 146 | |
| 147 | import re |
| 148 | |
| 149 | def update_files(alt_target, alt_target_rename, pkg, d): |
| 150 | f = d.getVar('FILES:' + pkg) |
| 151 | if f: |
| 152 | f = re.sub(r'(^|\s)%s(\s|$)' % re.escape (alt_target), r'\1%s\2' % alt_target_rename, f) |
| 153 | d.setVar('FILES:' + pkg, f) |
| 154 | |
| 155 | # Check for deprecated usage... |
| 156 | pn = d.getVar('BPN') |
| 157 | if d.getVar('ALTERNATIVE_LINKS') != None: |
| 158 | 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 | |
| 160 | # Do actual update alternatives processing |
| 161 | pkgdest = d.getVar('PKGD') |
| 162 | for pkg in (d.getVar('PACKAGES') or "").split(): |
| 163 | # If the src == dest, we know we need to rename the dest by appending ${BPN} |
| 164 | link_rename = [] |
| 165 | for alt_name in (d.getVar('ALTERNATIVE:%s' % pkg) or "").split(): |
| 166 | alt_link = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name) |
| 167 | if not alt_link: |
| 168 | alt_link = "%s/%s" % (d.getVar('bindir'), alt_name) |
| 169 | d.setVarFlag('ALTERNATIVE_LINK_NAME', alt_name, alt_link) |
| 170 | if alt_link.startswith(os.path.join(d.getVar('sysconfdir'), 'init.d')): |
| 171 | # Managing init scripts does not work (bug #10433), foremost |
| 172 | # because of a race with update-rc.d |
| 173 | bb.fatal("Using update-alternatives for managing SysV init scripts is not supported") |
| 174 | |
| 175 | alt_target = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name) or d.getVarFlag('ALTERNATIVE_TARGET', alt_name) |
| 176 | alt_target = alt_target or d.getVar('ALTERNATIVE_TARGET_%s' % pkg) or d.getVar('ALTERNATIVE_TARGET') or alt_link |
| 177 | # Sometimes alt_target is specified as relative to the link name. |
| 178 | alt_target = os.path.join(os.path.dirname(alt_link), alt_target) |
| 179 | |
| 180 | # If the link and target are the same name, we need to rename the target. |
| 181 | if alt_link == alt_target: |
| 182 | src = '%s/%s' % (pkgdest, alt_target) |
| 183 | alt_target_rename = '%s.%s' % (alt_target, pn) |
| 184 | dest = '%s/%s' % (pkgdest, alt_target_rename) |
| 185 | if os.path.lexists(dest): |
| 186 | bb.note('%s: Already renamed: %s' % (pn, alt_target_rename)) |
| 187 | elif os.path.lexists(src): |
| 188 | if os.path.islink(src): |
| 189 | # Delay rename of links |
| 190 | link_rename.append((alt_target, alt_target_rename)) |
| 191 | else: |
| 192 | bb.note('%s: Rename %s -> %s' % (pn, alt_target, alt_target_rename)) |
| 193 | bb.utils.rename(src, dest) |
| 194 | update_files(alt_target, alt_target_rename, pkg, d) |
| 195 | else: |
| 196 | bb.warn("%s: alternative target (%s or %s) does not exist, skipping..." % (pn, alt_target, alt_target_rename)) |
| 197 | continue |
| 198 | d.setVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name, alt_target_rename) |
| 199 | |
| 200 | # Process delayed link names |
| 201 | # Do these after other renames so we can correct broken links |
| 202 | for (alt_target, alt_target_rename) in link_rename: |
| 203 | src = '%s/%s' % (pkgdest, alt_target) |
| 204 | dest = '%s/%s' % (pkgdest, alt_target_rename) |
| 205 | link_target = oe.path.realpath(src, pkgdest, True) |
| 206 | |
| 207 | if os.path.lexists(link_target): |
| 208 | # Ok, the link_target exists, we can rename |
| 209 | bb.note('%s: Rename (link) %s -> %s' % (pn, alt_target, alt_target_rename)) |
| 210 | bb.utils.rename(src, dest) |
| 211 | else: |
| 212 | # Try to resolve the broken link to link.${BPN} |
| 213 | link_maybe = '%s.%s' % (os.readlink(src), pn) |
| 214 | if os.path.lexists(os.path.join(os.path.dirname(src), link_maybe)): |
| 215 | # Ok, the renamed link target exists.. create a new link, and remove the original |
| 216 | bb.note('%s: Creating new link %s -> %s' % (pn, alt_target_rename, link_maybe)) |
| 217 | os.symlink(link_maybe, dest) |
| 218 | os.unlink(src) |
| 219 | else: |
| 220 | bb.warn('%s: Unable to resolve dangling symlink: %s' % (pn, alt_target)) |
| 221 | continue |
| 222 | update_files(alt_target, alt_target_rename, pkg, d) |
| 223 | } |
| 224 | |
| 225 | def update_alternatives_alt_targets(d, pkg): |
| 226 | """ |
| 227 | Returns the update-alternatives metadata for a package. |
| 228 | |
| 229 | The returned format is a list of tuples where the tuple contains: |
| 230 | alt_name: The binary name |
| 231 | alt_link: The path for the binary (Shared by different packages) |
| 232 | alt_target: The path for the renamed binary (Unique per package) |
| 233 | alt_priority: The priority of the alt_target |
| 234 | |
| 235 | All the alt_targets will be installed into the sysroot. The alt_link is |
| 236 | a symlink pointing to the alt_target with the highest priority. |
| 237 | """ |
| 238 | |
| 239 | pn = d.getVar('BPN') |
| 240 | pkgdest = d.getVar('PKGD') |
| 241 | updates = list() |
| 242 | for alt_name in (d.getVar('ALTERNATIVE:%s' % pkg) or "").split(): |
| 243 | alt_link = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name) |
| 244 | alt_target = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name) or \ |
| 245 | d.getVarFlag('ALTERNATIVE_TARGET', alt_name) or \ |
| 246 | d.getVar('ALTERNATIVE_TARGET_%s' % pkg) or \ |
| 247 | d.getVar('ALTERNATIVE_TARGET') or \ |
| 248 | alt_link |
| 249 | alt_priority = d.getVarFlag('ALTERNATIVE_PRIORITY_%s' % pkg, alt_name) or \ |
| 250 | d.getVarFlag('ALTERNATIVE_PRIORITY', alt_name) or \ |
| 251 | d.getVar('ALTERNATIVE_PRIORITY_%s' % pkg) or \ |
| 252 | d.getVar('ALTERNATIVE_PRIORITY') |
| 253 | |
| 254 | # This shouldn't trigger, as it should have been resolved earlier! |
| 255 | if alt_link == alt_target: |
| 256 | bb.note('alt_link == alt_target: %s == %s -- correcting, this should not happen!' % (alt_link, alt_target)) |
| 257 | alt_target = '%s.%s' % (alt_target, pn) |
| 258 | |
| 259 | if not os.path.lexists('%s/%s' % (pkgdest, alt_target)): |
| 260 | bb.warn('%s: NOT adding alternative provide %s: %s does not exist' % (pn, alt_link, alt_target)) |
| 261 | continue |
| 262 | |
| 263 | alt_target = os.path.normpath(alt_target) |
| 264 | updates.append( (alt_name, alt_link, alt_target, alt_priority) ) |
| 265 | |
| 266 | return updates |
| 267 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 268 | PACKAGESPLITFUNCS =+ "populate_packages_updatealternatives" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 269 | |
| 270 | python populate_packages_updatealternatives () { |
| 271 | if not update_alternatives_enabled(d): |
| 272 | return |
| 273 | |
| 274 | # Do actual update alternatives processing |
| 275 | for pkg in (d.getVar('PACKAGES') or "").split(): |
| 276 | # Create post install/removal scripts |
| 277 | alt_setup_links = "" |
| 278 | alt_remove_links = "" |
| 279 | updates = update_alternatives_alt_targets(d, pkg) |
| 280 | for alt_name, alt_link, alt_target, alt_priority in updates: |
| 281 | alt_setup_links += '\tupdate-alternatives --install %s %s %s %s\n' % (alt_link, alt_name, alt_target, alt_priority) |
| 282 | alt_remove_links += '\tupdate-alternatives --remove %s %s\n' % (alt_name, alt_target) |
| 283 | |
| 284 | if alt_setup_links: |
| 285 | # RDEPENDS setup |
| 286 | provider = d.getVar('VIRTUAL-RUNTIME_update-alternatives') |
| 287 | if provider: |
| 288 | #bb.note('adding runtime requirement for update-alternatives for %s' % pkg) |
| 289 | d.appendVar('RDEPENDS:%s' % pkg, ' ' + d.getVar('MLPREFIX', False) + provider) |
| 290 | |
| 291 | bb.note('adding update-alternatives calls to postinst/prerm for %s' % pkg) |
| 292 | bb.note('%s' % alt_setup_links) |
| 293 | postinst = d.getVar('pkg_postinst:%s' % pkg) |
| 294 | if postinst: |
| 295 | postinst = alt_setup_links + postinst |
| 296 | else: |
| 297 | postinst = '#!/bin/sh\n' + alt_setup_links |
| 298 | d.setVar('pkg_postinst:%s' % pkg, postinst) |
| 299 | |
| 300 | bb.note('%s' % alt_remove_links) |
| 301 | prerm = d.getVar('pkg_prerm:%s' % pkg) or '#!/bin/sh\n' |
| 302 | prerm += alt_remove_links |
| 303 | d.setVar('pkg_prerm:%s' % pkg, prerm) |
| 304 | } |
| 305 | |
| 306 | python package_do_filedeps:append () { |
| 307 | if update_alternatives_enabled(d): |
| 308 | apply_update_alternative_provides(d) |
| 309 | } |
| 310 | |
| 311 | def apply_update_alternative_provides(d): |
| 312 | pn = d.getVar('BPN') |
| 313 | pkgdest = d.getVar('PKGDEST') |
| 314 | |
| 315 | for pkg in d.getVar('PACKAGES').split(): |
| 316 | for alt_name in (d.getVar('ALTERNATIVE:%s' % pkg) or "").split(): |
| 317 | alt_link = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name) |
| 318 | alt_target = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name) or d.getVarFlag('ALTERNATIVE_TARGET', alt_name) |
| 319 | alt_target = alt_target or d.getVar('ALTERNATIVE_TARGET_%s' % pkg) or d.getVar('ALTERNATIVE_TARGET') or alt_link |
| 320 | |
| 321 | if alt_link == alt_target: |
| 322 | bb.warn('%s: alt_link == alt_target: %s == %s' % (pn, alt_link, alt_target)) |
| 323 | alt_target = '%s.%s' % (alt_target, pn) |
| 324 | |
| 325 | if not os.path.lexists('%s/%s/%s' % (pkgdest, pkg, alt_target)): |
| 326 | continue |
| 327 | |
| 328 | # Add file provide |
| 329 | trans_target = oe.package.file_translate(alt_target) |
| 330 | d.appendVar('FILERPROVIDES:%s:%s' % (trans_target, pkg), " " + alt_link) |
| 331 | if not trans_target in (d.getVar('FILERPROVIDESFLIST:%s' % pkg) or ""): |
| 332 | d.appendVar('FILERPROVIDESFLIST:%s' % pkg, " " + trans_target) |
| 333 | |