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