1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# Debian package renaming only occurs when a package is built
8# We therefore have to make sure we build all runtime packages
9# before building the current package to make the packages runtime
10# depends are correct
11#
12# Custom library package names can be defined setting
13# DEBIANNAME: + pkgname to the desired name.
14#
15# Better expressed as ensure all RDEPENDS package before we package
16# This means we can't have circular RDEPENDS/RRECOMMENDS
17
18AUTO_LIBNAME_PKGS = "${PACKAGES}"
19
20inherit package
21
22python debian_package_name_hook () {
23    import glob, copy, stat, errno, re, pathlib, subprocess
24
25    pkgdest = d.getVar("PKGDEST")
26    packages = d.getVar('PACKAGES')
27    so_re = re.compile(r"lib.*\.so")
28
29    def socrunch(s):
30        s = s.lower().replace('_', '-')
31        m = re.match(r"^(.*)(.)\.so\.(.*)$", s)
32        if m is None:
33            return None
34        if m.group(2) in '0123456789':
35            bin = '%s%s-%s' % (m.group(1), m.group(2), m.group(3))
36        else:
37            bin = m.group(1) + m.group(2) + m.group(3)
38        dev = m.group(1) + m.group(2)
39        return (bin, dev)
40
41    def isexec(path):
42        try:
43            s = os.stat(path)
44        except (os.error, AttributeError):
45            return 0
46        return (s[stat.ST_MODE] & stat.S_IEXEC)
47
48    def add_rprovides(pkg, d):
49        newpkg = d.getVar('PKG:' + pkg)
50        if newpkg and newpkg != pkg:
51            provs = (d.getVar('RPROVIDES:' + pkg) or "").split()
52            if pkg not in provs:
53                d.appendVar('RPROVIDES:' + pkg, " " + pkg + " (=" + d.getVar("PKGV") + ")")
54
55    def auto_libname(packages, orig_pkg):
56        p = lambda var: pathlib.PurePath(d.getVar(var))
57        libdirs = (p("base_libdir"), p("libdir"))
58        bindirs = (p("base_bindir"), p("base_sbindir"), p("bindir"), p("sbindir"))
59
60        sonames = []
61        has_bins = 0
62        has_libs = 0
63        for f in pkgfiles[orig_pkg]:
64            # This is .../packages-split/orig_pkg/
65            pkgpath = pathlib.PurePath(pkgdest, orig_pkg)
66            # Strip pkgpath off the full path to a file in the package, re-root
67            # so it is absolute, and then get the parent directory of the file.
68            path = pathlib.PurePath("/") / (pathlib.PurePath(f).relative_to(pkgpath).parent)
69            if path in bindirs:
70                has_bins = 1
71            if path in libdirs:
72                has_libs = 1
73                if so_re.match(os.path.basename(f)):
74                    try:
75                        cmd = [d.expand("${TARGET_PREFIX}objdump"), "-p", f]
76                        output = subprocess.check_output(cmd).decode("utf-8")
77                        for m in re.finditer(r"\s+SONAME\s+([^\s]+)", output):
78                            if m.group(1) not in sonames:
79                                sonames.append(m.group(1))
80                    except subprocess.CalledProcessError:
81                        pass
82        bb.debug(1, 'LIBNAMES: pkg %s libs %d bins %d sonames %s' % (orig_pkg, has_libs, has_bins, sonames))
83        soname = None
84        if len(sonames) == 1:
85            soname = sonames[0]
86        elif len(sonames) > 1:
87            lead = d.getVar('LEAD_SONAME')
88            if lead:
89                r = re.compile(lead)
90                filtered = []
91                for s in sonames:
92                    if r.match(s):
93                        filtered.append(s)
94                if len(filtered) == 1:
95                    soname = filtered[0]
96                elif len(filtered) > 1:
97                    bb.note("Multiple matches (%s) for LEAD_SONAME '%s'" % (", ".join(filtered), lead))
98                else:
99                    bb.note("Multiple libraries (%s) found, but LEAD_SONAME '%s' doesn't match any of them" % (", ".join(sonames), lead))
100            else:
101                bb.note("Multiple libraries (%s) found and LEAD_SONAME not defined" % ", ".join(sonames))
102
103        if has_libs and not has_bins and soname:
104            soname_result = socrunch(soname)
105            if soname_result:
106                (pkgname, devname) = soname_result
107                for pkg in packages.split():
108                    if (d.getVar('PKG:' + pkg, False) or d.getVar('DEBIAN_NOAUTONAME:' + pkg, False)):
109                        add_rprovides(pkg, d)
110                        continue
111                    debian_pn = d.getVar('DEBIANNAME:' + pkg, False)
112                    if debian_pn:
113                        newpkg = debian_pn
114                    elif pkg == orig_pkg:
115                        newpkg = pkgname
116                    else:
117                        newpkg = pkg.replace(orig_pkg, devname, 1)
118                    mlpre=d.getVar('MLPREFIX')
119                    if mlpre:
120                        if not newpkg.find(mlpre) == 0:
121                            newpkg = mlpre + newpkg
122                    if newpkg != pkg:
123                        bb.note("debian: renaming %s to %s" % (pkg, newpkg))
124                        d.setVar('PKG:' + pkg, newpkg)
125                        add_rprovides(pkg, d)
126        else:
127            add_rprovides(orig_pkg, d)
128
129    # reversed sort is needed when some package is substring of another
130    # ie in ncurses we get without reverse sort:
131    # DEBUG: LIBNAMES: pkgname libtic5 devname libtic pkg ncurses-libtic orig_pkg ncurses-libtic debian_pn None newpkg libtic5
132    # and later
133    # DEBUG: LIBNAMES: pkgname libtic5 devname libtic pkg ncurses-libticw orig_pkg ncurses-libtic debian_pn None newpkg libticw
134    # so we need to handle ncurses-libticw->libticw5 before ncurses-libtic->libtic5
135    for pkg in sorted((d.getVar('AUTO_LIBNAME_PKGS') or "").split(), reverse=True):
136        auto_libname(packages, pkg)
137}
138
139EXPORT_FUNCTIONS package_name_hook
140
141DEBIAN_NAMES = "1"
142