xref: /openbmc/openbmc/poky/meta/lib/oe/classextend.py (revision 028142bd)
1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6
7import collections
8
9def get_packages(d):
10    pkgs = d.getVar("PACKAGES_NONML")
11    extcls = d.getVar("EXTENDERCLASS")
12    return extcls.rename_packages_internal(pkgs)
13
14def get_depends(varprefix, d):
15    extcls = d.getVar("EXTENDERCLASS")
16    return extcls.map_depends_variable(varprefix + "_NONML")
17
18class ClassExtender(object):
19    def __init__(self, extname, d):
20        self.extname = extname
21        self.d = d
22        self.pkgs_mapping = []
23        self.d.setVar("EXTENDERCLASS", self)
24
25    def extend_name(self, name):
26        if name.startswith("kernel-") or name == "virtual/kernel":
27            return name
28        if name.startswith("rtld"):
29            return name
30        if name.endswith("-crosssdk"):
31            return name
32        if name.endswith("-" + self.extname):
33            name = name.replace("-" + self.extname, "")
34        if name.startswith("virtual/"):
35            # Assume large numbers of dashes means a triplet is present and we don't need to convert
36            if name.count("-") >= 3 and name.endswith(("-go", "-binutils", "-gcc", "-g++")):
37                return name
38            subs = name.split("/", 1)[1]
39            if not subs.startswith(self.extname):
40                return "virtual/" + self.extname + "-" + subs
41            return name
42        if name.startswith("/") or (name.startswith("${") and name.endswith("}")):
43            return name
44        if not name.startswith(self.extname):
45            return self.extname + "-" + name
46        return name
47
48    def map_variable(self, varname, setvar = True):
49        var = self.d.getVar(varname)
50        if not var:
51            return ""
52        var = var.split()
53        newvar = []
54        for v in var:
55            newvar.append(self.extend_name(v))
56        newdata =  " ".join(newvar)
57        if setvar:
58            self.d.setVar(varname, newdata)
59        return newdata
60
61    def map_regexp_variable(self, varname, setvar = True):
62        var = self.d.getVar(varname)
63        if not var:
64            return ""
65        var = var.split()
66        newvar = []
67        for v in var:
68            if v.startswith("^" + self.extname):
69                newvar.append(v)
70            elif v.startswith("^"):
71                newvar.append("^" + self.extname + "-" + v[1:])
72            else:
73                newvar.append(self.extend_name(v))
74        newdata =  " ".join(newvar)
75        if setvar:
76            self.d.setVar(varname, newdata)
77        return newdata
78
79    def map_depends(self, dep):
80        if dep.endswith(("-native", "-native-runtime")) or ('nativesdk-' in dep) or ('cross-canadian' in dep) or ('-crosssdk-' in dep):
81            return dep
82        else:
83            # Do not extend for that already have multilib prefix
84            var = self.d.getVar("MULTILIB_VARIANTS")
85            if var:
86                var = var.split()
87                for v in var:
88                    if dep.startswith(v):
89                        return dep
90            return self.extend_name(dep)
91
92    def map_depends_variable(self, varname, suffix = ""):
93        # We need to preserve EXTENDPKGV so it can be expanded correctly later
94        if suffix:
95            varname = varname + ":" + suffix
96        orig = self.d.getVar("EXTENDPKGV", False)
97        self.d.setVar("EXTENDPKGV", "EXTENDPKGV")
98        deps = self.d.getVar(varname)
99        if not deps:
100            self.d.setVar("EXTENDPKGV", orig)
101            return
102        deps = bb.utils.explode_dep_versions2(deps)
103        newdeps = collections.OrderedDict()
104        for dep in deps:
105            newdeps[self.map_depends(dep)] = deps[dep]
106
107        if not varname.endswith("_NONML"):
108            self.d.renameVar(varname, varname + "_NONML")
109            self.d.setVar(varname, "${@oe.classextend.get_depends('%s', d)}" % varname)
110            self.d.appendVarFlag(varname, "vardeps", " " + varname + "_NONML")
111        ret = bb.utils.join_deps(newdeps, False).replace("EXTENDPKGV", "${EXTENDPKGV}")
112        self.d.setVar("EXTENDPKGV", orig)
113        return ret
114
115    def map_packagevars(self):
116        for pkg in (self.d.getVar("PACKAGES").split() + [""]):
117            self.map_depends_variable("RDEPENDS", pkg)
118            self.map_depends_variable("RRECOMMENDS", pkg)
119            self.map_depends_variable("RSUGGESTS", pkg)
120            self.map_depends_variable("RPROVIDES", pkg)
121            self.map_depends_variable("RREPLACES", pkg)
122            self.map_depends_variable("RCONFLICTS", pkg)
123            self.map_depends_variable("PKG", pkg)
124
125    def rename_packages(self):
126        for pkg in (self.d.getVar("PACKAGES") or "").split():
127            if pkg.startswith(self.extname):
128               self.pkgs_mapping.append([pkg.split(self.extname + "-")[1], pkg])
129               continue
130            self.pkgs_mapping.append([pkg, self.extend_name(pkg)])
131
132        self.d.renameVar("PACKAGES", "PACKAGES_NONML")
133        self.d.setVar("PACKAGES", "${@oe.classextend.get_packages(d)}")
134
135    def rename_packages_internal(self, pkgs):
136        self.pkgs_mapping = []
137        for pkg in (self.d.expand(pkgs) or "").split():
138            if pkg.startswith(self.extname):
139               self.pkgs_mapping.append([pkg.split(self.extname + "-")[1], pkg])
140               continue
141            self.pkgs_mapping.append([pkg, self.extend_name(pkg)])
142
143        return " ".join([row[1] for row in self.pkgs_mapping])
144
145    def rename_package_variables(self, variables):
146        for pkg_mapping in self.pkgs_mapping:
147            if pkg_mapping[0].startswith("${") and pkg_mapping[0].endswith("}"):
148                continue
149            for subs in variables:
150                self.d.renameVar("%s:%s" % (subs, pkg_mapping[0]), "%s:%s" % (subs, pkg_mapping[1]))
151
152class NativesdkClassExtender(ClassExtender):
153    def map_depends(self, dep):
154        if dep.startswith(self.extname):
155            return dep
156        if dep.endswith(("-native", "-native-runtime")) or ('nativesdk-' in dep) or ('-cross-' in dep) or ('-crosssdk-' in dep):
157            return dep
158        else:
159            return self.extend_name(dep)
160