blob: 1fdd681315949c2554c991770d577e1c24a14f5a [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
61ALTERNATIVE_PRIORITY = "10"
62
63# We need special processing for vardeps because it can not work on
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050064# modified flag values. So we aggregate the flags into a new variable
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065# and include that vairable in the set.
66UPDALTVARS = "ALTERNATIVE ALTERNATIVE_LINK_NAME ALTERNATIVE_TARGET ALTERNATIVE_PRIORITY"
67
68def gen_updatealternativesvardeps(d):
69 pkgs = (d.getVar("PACKAGES", True) or "").split()
70 vars = (d.getVar("UPDALTVARS", True) or "").split()
71
72 # First compute them for non_pkg versions
73 for v in vars:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060074 for flag in sorted((d.getVarFlags(v) or {}).keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075 if flag == "doc" or flag == "vardeps" or flag == "vardepsexp":
76 continue
77 d.appendVar('%s_VARDEPS' % (v), ' %s:%s' % (flag, d.getVarFlag(v, flag, False)))
78
79 for p in pkgs:
80 for v in vars:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060081 for flag in sorted((d.getVarFlags("%s_%s" % (v,p)) or {}).keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 if flag == "doc" or flag == "vardeps" or flag == "vardepsexp":
83 continue
84 d.appendVar('%s_VARDEPS_%s' % (v,p), ' %s:%s' % (flag, d.getVarFlag('%s_%s' % (v,p), flag, False)))
85
86def ua_extend_depends(d):
87 if not 'virtual/update-alternatives' in d.getVar('PROVIDES', True):
88 d.appendVar('DEPENDS', ' virtual/${MLPREFIX}update-alternatives')
89
90python __anonymous() {
91 # Update Alternatives only works on target packages...
92 if bb.data.inherits_class('native', d) or \
93 bb.data.inherits_class('cross', d) or bb.data.inherits_class('crosssdk', d) or \
94 bb.data.inherits_class('cross-canadian', d):
95 return
96
97 # compute special vardeps
98 gen_updatealternativesvardeps(d)
99
100 # extend the depends to include virtual/update-alternatives
101 ua_extend_depends(d)
102}
103
104def gen_updatealternativesvars(d):
105 ret = []
106 pkgs = (d.getVar("PACKAGES", True) or "").split()
107 vars = (d.getVar("UPDALTVARS", True) or "").split()
108
109 for v in vars:
110 ret.append(v + "_VARDEPS")
111
112 for p in pkgs:
113 for v in vars:
114 ret.append(v + "_" + p)
115 ret.append(v + "_VARDEPS_" + p)
116 return " ".join(ret)
117
118# Now the new stuff, we use a custom function to generate the right values
119populate_packages[vardeps] += "${UPDALTVARS} ${@gen_updatealternativesvars(d)}"
120
121# We need to do the rename after the image creation step, but before
122# the split and strip steps.. packagecopy seems to be the earliest reasonable
123# place.
124python perform_packagecopy_append () {
125 # Check for deprecated usage...
126 pn = d.getVar('BPN', True)
127 if d.getVar('ALTERNATIVE_LINKS', True) != None:
128 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)
129
130 # Do actual update alternatives processing
131 pkgdest = d.getVar('PKGD', True)
132 for pkg in (d.getVar('PACKAGES', True) or "").split():
133 # If the src == dest, we know we need to rename the dest by appending ${BPN}
134 link_rename = {}
135 for alt_name in (d.getVar('ALTERNATIVE_%s' % pkg, True) or "").split():
136 alt_link = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name, True)
137 if not alt_link:
138 alt_link = "%s/%s" % (d.getVar('bindir', True), alt_name)
139 d.setVarFlag('ALTERNATIVE_LINK_NAME', alt_name, alt_link)
140
141 alt_target = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name, True) or d.getVarFlag('ALTERNATIVE_TARGET', alt_name, True)
142 alt_target = alt_target or d.getVar('ALTERNATIVE_TARGET_%s' % pkg, True) or d.getVar('ALTERNATIVE_TARGET', True) or alt_link
143 # Sometimes alt_target is specified as relative to the link name.
144 alt_target = os.path.join(os.path.dirname(alt_link), alt_target)
145
146 # If the link and target are the same name, we need to rename the target.
147 if alt_link == alt_target:
148 src = '%s/%s' % (pkgdest, alt_target)
149 alt_target_rename = '%s.%s' % (alt_target, pn)
150 dest = '%s/%s' % (pkgdest, alt_target_rename)
151 if os.path.lexists(dest):
152 bb.note('%s: Already renamed: %s' % (pn, alt_target_rename))
153 elif os.path.lexists(src):
154 if os.path.islink(src):
155 # Delay rename of links
156 link_rename[alt_target] = alt_target_rename
157 else:
158 bb.note('%s: Rename %s -> %s' % (pn, alt_target, alt_target_rename))
159 os.rename(src, dest)
160 else:
161 bb.warn("%s: alternative target (%s or %s) does not exist, skipping..." % (pn, alt_target, alt_target_rename))
162 continue
163 d.setVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name, alt_target_rename)
164
165 # Process delayed link names
166 # Do these after other renames so we can correct broken links
167 for alt_target in link_rename:
168 src = '%s/%s' % (pkgdest, alt_target)
169 dest = '%s/%s' % (pkgdest, link_rename[alt_target])
170 link = os.readlink(src)
171 link_target = oe.path.realpath(src, pkgdest, True)
172
173 if os.path.lexists(link_target):
174 # Ok, the link_target exists, we can rename
175 bb.note('%s: Rename (link) %s -> %s' % (pn, alt_target, link_rename[alt_target]))
176 os.rename(src, dest)
177 else:
178 # Try to resolve the broken link to link.${BPN}
179 link_maybe = '%s.%s' % (os.readlink(src), pn)
180 if os.path.lexists(os.path.join(os.path.dirname(src), link_maybe)):
181 # Ok, the renamed link target exists.. create a new link, and remove the original
182 bb.note('%s: Creating new link %s -> %s' % (pn, link_rename[alt_target], link_maybe))
183 os.symlink(link_maybe, dest)
184 os.unlink(src)
185 else:
186 bb.warn('%s: Unable to resolve dangling symlink: %s' % (pn, alt_target))
187}
188
189PACKAGESPLITFUNCS_prepend = "populate_packages_updatealternatives "
190
191python populate_packages_updatealternatives () {
192 pn = d.getVar('BPN', True)
193
194 # Do actual update alternatives processing
195 pkgdest = d.getVar('PKGD', True)
196 for pkg in (d.getVar('PACKAGES', True) or "").split():
197 # Create post install/removal scripts
198 alt_setup_links = ""
199 alt_remove_links = ""
200 for alt_name in (d.getVar('ALTERNATIVE_%s' % pkg, True) or "").split():
201 alt_link = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name, True)
202 alt_target = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name, True) or d.getVarFlag('ALTERNATIVE_TARGET', alt_name, True)
203 alt_target = alt_target or d.getVar('ALTERNATIVE_TARGET_%s' % pkg, True) or d.getVar('ALTERNATIVE_TARGET', True) or alt_link
204 # Sometimes alt_target is specified as relative to the link name.
205 alt_target = os.path.join(os.path.dirname(alt_link), alt_target)
206
207 alt_priority = d.getVarFlag('ALTERNATIVE_PRIORITY_%s' % pkg, alt_name, True) or d.getVarFlag('ALTERNATIVE_PRIORITY', alt_name, True)
208 alt_priority = alt_priority or d.getVar('ALTERNATIVE_PRIORITY_%s' % pkg, True) or d.getVar('ALTERNATIVE_PRIORITY', True)
209
210 # This shouldn't trigger, as it should have been resolved earlier!
211 if alt_link == alt_target:
212 bb.note('alt_link == alt_target: %s == %s -- correcting, this should not happen!' % (alt_link, alt_target))
213 alt_target = '%s.%s' % (alt_target, pn)
214
215 if not os.path.lexists('%s/%s' % (pkgdest, alt_target)):
216 bb.warn('%s: NOT adding alternative provide %s: %s does not exist' % (pn, alt_link, alt_target))
217 continue
218
219 # Default to generate shell script.. eventually we may want to change this...
220 alt_target = os.path.normpath(alt_target)
221
222 alt_setup_links += '\tupdate-alternatives --install %s %s %s %s\n' % (alt_link, alt_name, alt_target, alt_priority)
223 alt_remove_links += '\tupdate-alternatives --remove %s %s\n' % (alt_name, alt_target)
224
225 if alt_setup_links:
226 # RDEPENDS setup
227 provider = d.getVar('VIRTUAL-RUNTIME_update-alternatives', True)
228 if provider:
229 #bb.note('adding runtime requirement for update-alternatives for %s' % pkg)
230 d.appendVar('RDEPENDS_%s' % pkg, ' ' + d.getVar('MLPREFIX', False) + provider)
231
232 bb.note('adding update-alternatives calls to postinst/prerm for %s' % pkg)
233 bb.note('%s' % alt_setup_links)
234 postinst = d.getVar('pkg_postinst_%s' % pkg, True) or '#!/bin/sh\n'
235 postinst += alt_setup_links
236 d.setVar('pkg_postinst_%s' % pkg, postinst)
237
238 bb.note('%s' % alt_remove_links)
239 prerm = d.getVar('pkg_prerm_%s' % pkg, True) or '#!/bin/sh\n'
240 prerm += alt_remove_links
241 d.setVar('pkg_prerm_%s' % pkg, prerm)
242}
243
244python package_do_filedeps_append () {
245 pn = d.getVar('BPN', True)
246 pkgdest = d.getVar('PKGDEST', True)
247
248 for pkg in packages.split():
249 for alt_name in (d.getVar('ALTERNATIVE_%s' % pkg, True) or "").split():
250 alt_link = d.getVarFlag('ALTERNATIVE_LINK_NAME', alt_name, True)
251 alt_target = d.getVarFlag('ALTERNATIVE_TARGET_%s' % pkg, alt_name, True) or d.getVarFlag('ALTERNATIVE_TARGET', alt_name, True)
252 alt_target = alt_target or d.getVar('ALTERNATIVE_TARGET_%s' % pkg, True) or d.getVar('ALTERNATIVE_TARGET', True) or alt_link
253
254 if alt_link == alt_target:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500255 bb.warn('%s: alt_link == alt_target: %s == %s' % (pn, alt_link, alt_target))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256 alt_target = '%s.%s' % (alt_target, pn)
257
258 if not os.path.lexists('%s/%s/%s' % (pkgdest, pkg, alt_target)):
259 continue
260
261 # Add file provide
262 trans_target = oe.package.file_translate(alt_target)
263 d.appendVar('FILERPROVIDES_%s_%s' % (trans_target, pkg), " " + alt_link)
264 if not trans_target in (d.getVar('FILERPROVIDESFLIST_%s' % pkg, True) or ""):
265 d.appendVar('FILERPROVIDESFLIST_%s' % pkg, " " + trans_target)
266}
267