xref: /openbmc/openbmc/poky/meta/lib/oe/package.py (revision 03514f19)
1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6
7import errno
8import fnmatch
9import itertools
10import os
11import shlex
12import re
13import glob
14import stat
15import mmap
16import subprocess
17
18import oe.cachedpath
19
20def runstrip(arg):
21    # Function to strip a single file, called from split_and_strip_files below
22    # A working 'file' (one which works on the target architecture)
23    #
24    # The elftype is a bit pattern (explained in is_elf below) to tell
25    # us what type of file we're processing...
26    # 4 - executable
27    # 8 - shared library
28    # 16 - kernel module
29
30    if len(arg) == 3:
31        (file, elftype, strip) = arg
32        extra_strip_sections = ''
33    else:
34        (file, elftype, strip, extra_strip_sections) = arg
35
36    newmode = None
37    if not os.access(file, os.W_OK) or os.access(file, os.R_OK):
38        origmode = os.stat(file)[stat.ST_MODE]
39        newmode = origmode | stat.S_IWRITE | stat.S_IREAD
40        os.chmod(file, newmode)
41
42    stripcmd = [strip]
43    skip_strip = False
44    # kernel module
45    if elftype & 16:
46        if is_kernel_module_signed(file):
47            bb.debug(1, "Skip strip on signed module %s" % file)
48            skip_strip = True
49        else:
50            stripcmd.extend(["--strip-debug", "--remove-section=.comment",
51                "--remove-section=.note", "--preserve-dates"])
52    # .so and shared library
53    elif ".so" in file and elftype & 8:
54        stripcmd.extend(["--remove-section=.comment", "--remove-section=.note", "--strip-unneeded"])
55    # shared or executable:
56    elif elftype & 8 or elftype & 4:
57        stripcmd.extend(["--remove-section=.comment", "--remove-section=.note"])
58        if extra_strip_sections != '':
59            for section in extra_strip_sections.split():
60                stripcmd.extend(["--remove-section=" + section])
61
62    stripcmd.append(file)
63    bb.debug(1, "runstrip: %s" % stripcmd)
64
65    if not skip_strip:
66        output = subprocess.check_output(stripcmd, stderr=subprocess.STDOUT)
67
68    if newmode:
69        os.chmod(file, origmode)
70
71# Detect .ko module by searching for "vermagic=" string
72def is_kernel_module(path):
73    with open(path) as f:
74        return mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ).find(b"vermagic=") >= 0
75
76# Detect if .ko module is signed
77def is_kernel_module_signed(path):
78    with open(path, "rb") as f:
79        f.seek(-28, 2)
80        module_tail = f.read()
81        return "Module signature appended" in "".join(chr(c) for c in bytearray(module_tail))
82
83# Return type (bits):
84# 0 - not elf
85# 1 - ELF
86# 2 - stripped
87# 4 - executable
88# 8 - shared library
89# 16 - kernel module
90def is_elf(path):
91    exec_type = 0
92    result = subprocess.check_output(["file", "-b", path], stderr=subprocess.STDOUT).decode("utf-8")
93
94    if "ELF" in result:
95        exec_type |= 1
96        if "not stripped" not in result:
97            exec_type |= 2
98        if "executable" in result:
99            exec_type |= 4
100        if "shared" in result:
101            exec_type |= 8
102        if "relocatable" in result:
103            if path.endswith(".ko") and path.find("/lib/modules/") != -1 and is_kernel_module(path):
104                exec_type |= 16
105    return (path, exec_type)
106
107def is_static_lib(path):
108    if path.endswith('.a') and not os.path.islink(path):
109        with open(path, 'rb') as fh:
110            # The magic must include the first slash to avoid
111            # matching golang static libraries
112            magic = b'!<arch>\x0a/'
113            start = fh.read(len(magic))
114            return start == magic
115    return False
116
117def strip_execs(pn, dstdir, strip_cmd, libdir, base_libdir, max_process, qa_already_stripped=False):
118    """
119    Strip executable code (like executables, shared libraries) _in_place_
120    - Based on sysroot_strip in staging.bbclass
121    :param dstdir: directory in which to strip files
122    :param strip_cmd: Strip command (usually ${STRIP})
123    :param libdir: ${libdir} - strip .so files in this directory
124    :param base_libdir: ${base_libdir} - strip .so files in this directory
125    :param max_process: number of stripping processes started in parallel
126    :param qa_already_stripped: Set to True if already-stripped' in ${INSANE_SKIP}
127    This is for proper logging and messages only.
128    """
129    import stat, errno, oe.path, oe.utils
130
131    elffiles = {}
132    inodes = {}
133    libdir = os.path.abspath(dstdir + os.sep + libdir)
134    base_libdir = os.path.abspath(dstdir + os.sep + base_libdir)
135    exec_mask = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
136    #
137    # First lets figure out all of the files we may have to process
138    #
139    checkelf = []
140    inodecache = {}
141    for root, dirs, files in os.walk(dstdir):
142        for f in files:
143            file = os.path.join(root, f)
144
145            try:
146                ltarget = oe.path.realpath(file, dstdir, False)
147                s = os.lstat(ltarget)
148            except OSError as e:
149                (err, strerror) = e.args
150                if err != errno.ENOENT:
151                    raise
152                # Skip broken symlinks
153                continue
154            if not s:
155                continue
156            # Check its an excutable
157            if s[stat.ST_MODE] & exec_mask \
158                    or ((file.startswith(libdir) or file.startswith(base_libdir)) and ".so" in f) \
159                    or file.endswith('.ko'):
160                # If it's a symlink, and points to an ELF file, we capture the readlink target
161                if os.path.islink(file):
162                    continue
163
164                # It's a file (or hardlink), not a link
165                # ...but is it ELF, and is it already stripped?
166                checkelf.append(file)
167                inodecache[file] = s.st_ino
168    results = oe.utils.multiprocess_launch_mp(is_elf, checkelf, max_process)
169    for (file, elf_file) in results:
170                #elf_file = is_elf(file)
171                if elf_file & 1:
172                    if elf_file & 2:
173                        if qa_already_stripped:
174                            bb.note("Skipping file %s from %s for already-stripped QA test" % (file[len(dstdir):], pn))
175                        else:
176                            bb.warn("File '%s' from %s was already stripped, this will prevent future debugging!" % (file[len(dstdir):], pn))
177                        continue
178
179                    if inodecache[file] in inodes:
180                        os.unlink(file)
181                        os.link(inodes[inodecache[file]], file)
182                    else:
183                        # break hardlinks so that we do not strip the original.
184                        inodes[inodecache[file]] = file
185                        bb.utils.break_hardlinks(file)
186                        elffiles[file] = elf_file
187
188    #
189    # Now strip them (in parallel)
190    #
191    sfiles = []
192    for file in elffiles:
193        elf_file = int(elffiles[file])
194        sfiles.append((file, elf_file, strip_cmd))
195
196    oe.utils.multiprocess_launch_mp(runstrip, sfiles, max_process)
197
198
199def file_translate(file):
200    ft = file.replace("@", "@at@")
201    ft = ft.replace(" ", "@space@")
202    ft = ft.replace("\t", "@tab@")
203    ft = ft.replace("[", "@openbrace@")
204    ft = ft.replace("]", "@closebrace@")
205    ft = ft.replace("_", "@underscore@")
206    return ft
207
208def filedeprunner(arg):
209    import re, subprocess, shlex
210
211    (pkg, pkgfiles, rpmdeps, pkgdest) = arg
212    provides = {}
213    requires = {}
214
215    file_re = re.compile(r'\s+\d+\s(.*)')
216    dep_re = re.compile(r'\s+(\S)\s+(.*)')
217    r = re.compile(r'[<>=]+\s+\S*')
218
219    def process_deps(pipe, pkg, pkgdest, provides, requires):
220        file = None
221        for line in pipe.split("\n"):
222
223            m = file_re.match(line)
224            if m:
225                file = m.group(1)
226                file = file.replace(pkgdest + "/" + pkg, "")
227                file = file_translate(file)
228                continue
229
230            m = dep_re.match(line)
231            if not m or not file:
232                continue
233
234            type, dep = m.groups()
235
236            if type == 'R':
237                i = requires
238            elif type == 'P':
239                i = provides
240            else:
241               continue
242
243            if dep.startswith("python("):
244                continue
245
246            # Ignore all perl(VMS::...) and perl(Mac::...) dependencies. These
247            # are typically used conditionally from the Perl code, but are
248            # generated as unconditional dependencies.
249            if dep.startswith('perl(VMS::') or dep.startswith('perl(Mac::'):
250                continue
251
252            # Ignore perl dependencies on .pl files.
253            if dep.startswith('perl(') and dep.endswith('.pl)'):
254                continue
255
256            # Remove perl versions and perl module versions since they typically
257            # do not make sense when used as package versions.
258            if dep.startswith('perl') and r.search(dep):
259                dep = dep.split()[0]
260
261            # Put parentheses around any version specifications.
262            dep = r.sub(r'(\g<0>)',dep)
263
264            if file not in i:
265                i[file] = []
266            i[file].append(dep)
267
268        return provides, requires
269
270    output = subprocess.check_output(shlex.split(rpmdeps) + pkgfiles, stderr=subprocess.STDOUT).decode("utf-8")
271    provides, requires = process_deps(output, pkg, pkgdest, provides, requires)
272
273    return (pkg, provides, requires)
274
275
276def read_shlib_providers(d):
277    import re
278
279    shlib_provider = {}
280    shlibs_dirs = d.getVar('SHLIBSDIRS').split()
281    list_re = re.compile(r'^(.*)\.list$')
282    # Go from least to most specific since the last one found wins
283    for dir in reversed(shlibs_dirs):
284        bb.debug(2, "Reading shlib providers in %s" % (dir))
285        if not os.path.exists(dir):
286            continue
287        for file in sorted(os.listdir(dir)):
288            m = list_re.match(file)
289            if m:
290                dep_pkg = m.group(1)
291                try:
292                    fd = open(os.path.join(dir, file))
293                except IOError:
294                    # During a build unrelated shlib files may be deleted, so
295                    # handle files disappearing between the listdirs and open.
296                    continue
297                lines = fd.readlines()
298                fd.close()
299                for l in lines:
300                    s = l.strip().split(":")
301                    if s[0] not in shlib_provider:
302                        shlib_provider[s[0]] = {}
303                    shlib_provider[s[0]][s[1]] = (dep_pkg, s[2])
304    return shlib_provider
305
306# We generate a master list of directories to process, we start by
307# seeding this list with reasonable defaults, then load from
308# the fs-perms.txt files
309def fixup_perms(d):
310    import pwd, grp
311
312    cpath = oe.cachedpath.CachedPath()
313    dvar = d.getVar('PKGD')
314
315    # init using a string with the same format as a line as documented in
316    # the fs-perms.txt file
317    # <path> <mode> <uid> <gid> <walk> <fmode> <fuid> <fgid>
318    # <path> link <link target>
319    #
320    # __str__ can be used to print out an entry in the input format
321    #
322    # if fs_perms_entry.path is None:
323    #    an error occurred
324    # if fs_perms_entry.link, you can retrieve:
325    #    fs_perms_entry.path = path
326    #    fs_perms_entry.link = target of link
327    # if not fs_perms_entry.link, you can retrieve:
328    #    fs_perms_entry.path = path
329    #    fs_perms_entry.mode = expected dir mode or None
330    #    fs_perms_entry.uid = expected uid or -1
331    #    fs_perms_entry.gid = expected gid or -1
332    #    fs_perms_entry.walk = 'true' or something else
333    #    fs_perms_entry.fmode = expected file mode or None
334    #    fs_perms_entry.fuid = expected file uid or -1
335    #    fs_perms_entry_fgid = expected file gid or -1
336    class fs_perms_entry():
337        def __init__(self, line):
338            lsplit = line.split()
339            if len(lsplit) == 3 and lsplit[1].lower() == "link":
340                self._setlink(lsplit[0], lsplit[2])
341            elif len(lsplit) == 8:
342                self._setdir(lsplit[0], lsplit[1], lsplit[2], lsplit[3], lsplit[4], lsplit[5], lsplit[6], lsplit[7])
343            else:
344                msg = "Fixup Perms: invalid config line %s" % line
345                oe.qa.handle_error("perm-config", msg, d)
346                self.path = None
347                self.link = None
348
349        def _setdir(self, path, mode, uid, gid, walk, fmode, fuid, fgid):
350            self.path = os.path.normpath(path)
351            self.link = None
352            self.mode = self._procmode(mode)
353            self.uid  = self._procuid(uid)
354            self.gid  = self._procgid(gid)
355            self.walk = walk.lower()
356            self.fmode = self._procmode(fmode)
357            self.fuid = self._procuid(fuid)
358            self.fgid = self._procgid(fgid)
359
360        def _setlink(self, path, link):
361            self.path = os.path.normpath(path)
362            self.link = link
363
364        def _procmode(self, mode):
365            if not mode or (mode and mode == "-"):
366                return None
367            else:
368                return int(mode,8)
369
370        # Note uid/gid -1 has special significance in os.lchown
371        def _procuid(self, uid):
372            if uid is None or uid == "-":
373                return -1
374            elif uid.isdigit():
375                return int(uid)
376            else:
377                return pwd.getpwnam(uid).pw_uid
378
379        def _procgid(self, gid):
380            if gid is None or gid == "-":
381                return -1
382            elif gid.isdigit():
383                return int(gid)
384            else:
385                return grp.getgrnam(gid).gr_gid
386
387        # Use for debugging the entries
388        def __str__(self):
389            if self.link:
390                return "%s link %s" % (self.path, self.link)
391            else:
392                mode = "-"
393                if self.mode:
394                    mode = "0%o" % self.mode
395                fmode = "-"
396                if self.fmode:
397                    fmode = "0%o" % self.fmode
398                uid = self._mapugid(self.uid)
399                gid = self._mapugid(self.gid)
400                fuid = self._mapugid(self.fuid)
401                fgid = self._mapugid(self.fgid)
402                return "%s %s %s %s %s %s %s %s" % (self.path, mode, uid, gid, self.walk, fmode, fuid, fgid)
403
404        def _mapugid(self, id):
405            if id is None or id == -1:
406                return "-"
407            else:
408                return "%d" % id
409
410    # Fix the permission, owner and group of path
411    def fix_perms(path, mode, uid, gid, dir):
412        if mode and not os.path.islink(path):
413            #bb.note("Fixup Perms: chmod 0%o %s" % (mode, dir))
414            os.chmod(path, mode)
415        # -1 is a special value that means don't change the uid/gid
416        # if they are BOTH -1, don't bother to lchown
417        if not (uid == -1 and gid == -1):
418            #bb.note("Fixup Perms: lchown %d:%d %s" % (uid, gid, dir))
419            os.lchown(path, uid, gid)
420
421    # Return a list of configuration files based on either the default
422    # files/fs-perms.txt or the contents of FILESYSTEM_PERMS_TABLES
423    # paths are resolved via BBPATH
424    def get_fs_perms_list(d):
425        str = ""
426        bbpath = d.getVar('BBPATH')
427        fs_perms_tables = d.getVar('FILESYSTEM_PERMS_TABLES') or ""
428        for conf_file in fs_perms_tables.split():
429            confpath = bb.utils.which(bbpath, conf_file)
430            if confpath:
431                str += " %s" % bb.utils.which(bbpath, conf_file)
432            else:
433                bb.warn("cannot find %s specified in FILESYSTEM_PERMS_TABLES" % conf_file)
434        return str
435
436    fs_perms_table = {}
437    fs_link_table = {}
438
439    # By default all of the standard directories specified in
440    # bitbake.conf will get 0755 root:root.
441    target_path_vars = [    'base_prefix',
442                'prefix',
443                'exec_prefix',
444                'base_bindir',
445                'base_sbindir',
446                'base_libdir',
447                'datadir',
448                'sysconfdir',
449                'servicedir',
450                'sharedstatedir',
451                'localstatedir',
452                'infodir',
453                'mandir',
454                'docdir',
455                'bindir',
456                'sbindir',
457                'libexecdir',
458                'libdir',
459                'includedir' ]
460
461    for path in target_path_vars:
462        dir = d.getVar(path) or ""
463        if dir == "":
464            continue
465        fs_perms_table[dir] = fs_perms_entry(d.expand("%s 0755 root root false - - -" % (dir)))
466
467    # Now we actually load from the configuration files
468    for conf in get_fs_perms_list(d).split():
469        if not os.path.exists(conf):
470            continue
471        with open(conf) as f:
472            for line in f:
473                if line.startswith('#'):
474                    continue
475                lsplit = line.split()
476                if len(lsplit) == 0:
477                    continue
478                if len(lsplit) != 8 and not (len(lsplit) == 3 and lsplit[1].lower() == "link"):
479                    msg = "Fixup perms: %s invalid line: %s" % (conf, line)
480                    oe.qa.handle_error("perm-line", msg, d)
481                    continue
482                entry = fs_perms_entry(d.expand(line))
483                if entry and entry.path:
484                    if entry.link:
485                        fs_link_table[entry.path] = entry
486                        if entry.path in fs_perms_table:
487                            fs_perms_table.pop(entry.path)
488                    else:
489                        fs_perms_table[entry.path] = entry
490                        if entry.path in fs_link_table:
491                            fs_link_table.pop(entry.path)
492
493    # Debug -- list out in-memory table
494    #for dir in fs_perms_table:
495    #    bb.note("Fixup Perms: %s: %s" % (dir, str(fs_perms_table[dir])))
496    #for link in fs_link_table:
497    #    bb.note("Fixup Perms: %s: %s" % (link, str(fs_link_table[link])))
498
499    # We process links first, so we can go back and fixup directory ownership
500    # for any newly created directories
501    # Process in sorted order so /run gets created before /run/lock, etc.
502    for entry in sorted(fs_link_table.values(), key=lambda x: x.link):
503        link = entry.link
504        dir = entry.path
505        origin = dvar + dir
506        if not (cpath.exists(origin) and cpath.isdir(origin) and not cpath.islink(origin)):
507            continue
508
509        if link[0] == "/":
510            target = dvar + link
511            ptarget = link
512        else:
513            target = os.path.join(os.path.dirname(origin), link)
514            ptarget = os.path.join(os.path.dirname(dir), link)
515        if os.path.exists(target):
516            msg = "Fixup Perms: Unable to correct directory link, target already exists: %s -> %s" % (dir, ptarget)
517            oe.qa.handle_error("perm-link", msg, d)
518            continue
519
520        # Create path to move directory to, move it, and then setup the symlink
521        bb.utils.mkdirhier(os.path.dirname(target))
522        #bb.note("Fixup Perms: Rename %s -> %s" % (dir, ptarget))
523        bb.utils.rename(origin, target)
524        #bb.note("Fixup Perms: Link %s -> %s" % (dir, link))
525        os.symlink(link, origin)
526
527    for dir in fs_perms_table:
528        origin = dvar + dir
529        if not (cpath.exists(origin) and cpath.isdir(origin)):
530            continue
531
532        fix_perms(origin, fs_perms_table[dir].mode, fs_perms_table[dir].uid, fs_perms_table[dir].gid, dir)
533
534        if fs_perms_table[dir].walk == 'true':
535            for root, dirs, files in os.walk(origin):
536                for dr in dirs:
537                    each_dir = os.path.join(root, dr)
538                    fix_perms(each_dir, fs_perms_table[dir].mode, fs_perms_table[dir].uid, fs_perms_table[dir].gid, dir)
539                for f in files:
540                    each_file = os.path.join(root, f)
541                    fix_perms(each_file, fs_perms_table[dir].fmode, fs_perms_table[dir].fuid, fs_perms_table[dir].fgid, dir)
542
543# Get a list of files from file vars by searching files under current working directory
544# The list contains symlinks, directories and normal files.
545def files_from_filevars(filevars):
546    cpath = oe.cachedpath.CachedPath()
547    files = []
548    for f in filevars:
549        if os.path.isabs(f):
550            f = '.' + f
551        if not f.startswith("./"):
552            f = './' + f
553        globbed = glob.glob(f, recursive=True)
554        if globbed:
555            if [ f ] != globbed:
556                files += globbed
557                continue
558        files.append(f)
559
560    symlink_paths = []
561    for ind, f in enumerate(files):
562        # Handle directory symlinks. Truncate path to the lowest level symlink
563        parent = ''
564        for dirname in f.split('/')[:-1]:
565            parent = os.path.join(parent, dirname)
566            if dirname == '.':
567                continue
568            if cpath.islink(parent):
569                bb.warn("FILES contains file '%s' which resides under a "
570                        "directory symlink. Please fix the recipe and use the "
571                        "real path for the file." % f[1:])
572                symlink_paths.append(f)
573                files[ind] = parent
574                f = parent
575                break
576
577        if not cpath.islink(f):
578            if cpath.isdir(f):
579                newfiles = [ os.path.join(f,x) for x in os.listdir(f) ]
580                if newfiles:
581                    files += newfiles
582
583    return files, symlink_paths
584
585# Called in package_<rpm,ipk,deb>.bbclass to get the correct list of configuration files
586def get_conffiles(pkg, d):
587    pkgdest = d.getVar('PKGDEST')
588    root = os.path.join(pkgdest, pkg)
589    cwd = os.getcwd()
590    os.chdir(root)
591
592    conffiles = d.getVar('CONFFILES:%s' % pkg);
593    if conffiles == None:
594        conffiles = d.getVar('CONFFILES')
595    if conffiles == None:
596        conffiles = ""
597    conffiles = conffiles.split()
598    conf_orig_list = files_from_filevars(conffiles)[0]
599
600    # Remove links and directories from conf_orig_list to get conf_list which only contains normal files
601    conf_list = []
602    for f in conf_orig_list:
603        if os.path.isdir(f):
604            continue
605        if os.path.islink(f):
606            continue
607        if not os.path.exists(f):
608            continue
609        conf_list.append(f)
610
611    # Remove the leading './'
612    for i in range(0, len(conf_list)):
613        conf_list[i] = conf_list[i][1:]
614
615    os.chdir(cwd)
616    return sorted(conf_list)
617
618def legitimize_package_name(s):
619    """
620    Make sure package names are legitimate strings
621    """
622
623    def fixutf(m):
624        cp = m.group(1)
625        if cp:
626            return ('\\u%s' % cp).encode('latin-1').decode('unicode_escape')
627
628    # Handle unicode codepoints encoded as <U0123>, as in glibc locale files.
629    s = re.sub(r'<U([0-9A-Fa-f]{1,4})>', fixutf, s)
630
631    # Remaining package name validity fixes
632    return s.lower().replace('_', '-').replace('@', '+').replace(',', '+').replace('/', '-')
633
634def split_locales(d):
635    cpath = oe.cachedpath.CachedPath()
636    if (d.getVar('PACKAGE_NO_LOCALE') == '1'):
637        bb.debug(1, "package requested not splitting locales")
638        return
639
640    packages = (d.getVar('PACKAGES') or "").split()
641
642    dvar = d.getVar('PKGD')
643    pn = d.getVar('LOCALEBASEPN')
644
645    try:
646        locale_index = packages.index(pn + '-locale')
647        packages.pop(locale_index)
648    except ValueError:
649        locale_index = len(packages)
650
651    localepaths = []
652    locales = set()
653    for localepath in (d.getVar('LOCALE_PATHS') or "").split():
654        localedir = dvar + localepath
655        if not cpath.isdir(localedir):
656            bb.debug(1, 'No locale files in %s' % localepath)
657            continue
658
659        localepaths.append(localepath)
660        with os.scandir(localedir) as it:
661            for entry in it:
662                if entry.is_dir():
663                    locales.add(entry.name)
664
665    if len(locales) == 0:
666        bb.debug(1, "No locale files in this package")
667        return
668
669    summary = d.getVar('SUMMARY') or pn
670    description = d.getVar('DESCRIPTION') or ""
671    locale_section = d.getVar('LOCALE_SECTION')
672    mlprefix = d.getVar('MLPREFIX') or ""
673    for l in sorted(locales):
674        ln = legitimize_package_name(l)
675        pkg = pn + '-locale-' + ln
676        packages.insert(locale_index, pkg)
677        locale_index += 1
678        files = []
679        for localepath in localepaths:
680            files.append(os.path.join(localepath, l))
681        d.setVar('FILES:' + pkg, " ".join(files))
682        d.setVar('RRECOMMENDS:' + pkg, '%svirtual-locale-%s' % (mlprefix, ln))
683        d.setVar('RPROVIDES:' + pkg, '%s-locale %s%s-translation' % (pn, mlprefix, ln))
684        d.setVar('SUMMARY:' + pkg, '%s - %s translations' % (summary, l))
685        d.setVar('DESCRIPTION:' + pkg, '%s  This package contains language translation files for the %s locale.' % (description, l))
686        if locale_section:
687            d.setVar('SECTION:' + pkg, locale_section)
688
689    d.setVar('PACKAGES', ' '.join(packages))
690
691    # Disabled by RP 18/06/07
692    # Wildcards aren't supported in debian
693    # They break with ipkg since glibc-locale* will mean that
694    # glibc-localedata-translit* won't install as a dependency
695    # for some other package which breaks meta-toolchain
696    # Probably breaks since virtual-locale- isn't provided anywhere
697    #rdep = (d.getVar('RDEPENDS:%s' % pn) or "").split()
698    #rdep.append('%s-locale*' % pn)
699    #d.setVar('RDEPENDS:%s' % pn, ' '.join(rdep))
700
701def package_debug_vars(d):
702    # We default to '.debug' style
703    if d.getVar('PACKAGE_DEBUG_SPLIT_STYLE') == 'debug-file-directory':
704        # Single debug-file-directory style debug info
705        debug_vars = {
706            "append": ".debug",
707            "staticappend": "",
708            "dir": "",
709            "staticdir": "",
710            "libdir": "/usr/lib/debug",
711            "staticlibdir": "/usr/lib/debug-static",
712            "srcdir": "/usr/src/debug",
713        }
714    elif d.getVar('PACKAGE_DEBUG_SPLIT_STYLE') == 'debug-without-src':
715        # Original OE-core, a.k.a. ".debug", style debug info, but without sources in /usr/src/debug
716        debug_vars = {
717            "append": "",
718            "staticappend": "",
719            "dir": "/.debug",
720            "staticdir": "/.debug-static",
721            "libdir": "",
722            "staticlibdir": "",
723            "srcdir": "",
724        }
725    elif d.getVar('PACKAGE_DEBUG_SPLIT_STYLE') == 'debug-with-srcpkg':
726        debug_vars = {
727            "append": "",
728            "staticappend": "",
729            "dir": "/.debug",
730            "staticdir": "/.debug-static",
731            "libdir": "",
732            "staticlibdir": "",
733            "srcdir": "/usr/src/debug",
734        }
735    else:
736        # Original OE-core, a.k.a. ".debug", style debug info
737        debug_vars = {
738            "append": "",
739            "staticappend": "",
740            "dir": "/.debug",
741            "staticdir": "/.debug-static",
742            "libdir": "",
743            "staticlibdir": "",
744            "srcdir": "/usr/src/debug",
745        }
746
747    return debug_vars
748
749
750def parse_debugsources_from_dwarfsrcfiles_output(dwarfsrcfiles_output):
751    debugfiles = {}
752
753    for line in dwarfsrcfiles_output.splitlines():
754        if line.startswith("\t"):
755            debugfiles[os.path.normpath(line.split()[0])] = ""
756
757    return debugfiles.keys()
758
759def source_info(file, d, fatal=True):
760    cmd = ["dwarfsrcfiles", file]
761    try:
762        output = subprocess.check_output(cmd, universal_newlines=True, stderr=subprocess.STDOUT)
763        retval = 0
764    except subprocess.CalledProcessError as exc:
765        output = exc.output
766        retval = exc.returncode
767
768    # 255 means a specific file wasn't fully parsed to get the debug file list, which is not a fatal failure
769    if retval != 0 and retval != 255:
770        msg = "dwarfsrcfiles failed with exit code %s (cmd was %s)%s" % (retval, cmd, ":\n%s" % output if output else "")
771        if fatal:
772            bb.fatal(msg)
773        bb.note(msg)
774
775    debugsources = parse_debugsources_from_dwarfsrcfiles_output(output)
776
777    return list(debugsources)
778
779def splitdebuginfo(file, dvar, dv, d):
780    # Function to split a single file into two components, one is the stripped
781    # target system binary, the other contains any debugging information. The
782    # two files are linked to reference each other.
783    #
784    # return a mapping of files:debugsources
785
786    src = file[len(dvar):]
787    dest = dv["libdir"] + os.path.dirname(src) + dv["dir"] + "/" + os.path.basename(src) + dv["append"]
788    debugfile = dvar + dest
789    sources = []
790
791    if file.endswith(".ko") and file.find("/lib/modules/") != -1:
792        if oe.package.is_kernel_module_signed(file):
793            bb.debug(1, "Skip strip on signed module %s" % file)
794            return (file, sources)
795
796    # Split the file...
797    bb.utils.mkdirhier(os.path.dirname(debugfile))
798    #bb.note("Split %s -> %s" % (file, debugfile))
799    # Only store off the hard link reference if we successfully split!
800
801    dvar = d.getVar('PKGD')
802    objcopy = d.getVar("OBJCOPY")
803
804    newmode = None
805    if not os.access(file, os.W_OK) or os.access(file, os.R_OK):
806        origmode = os.stat(file)[stat.ST_MODE]
807        newmode = origmode | stat.S_IWRITE | stat.S_IREAD
808        os.chmod(file, newmode)
809
810    # We need to extract the debug src information here...
811    if dv["srcdir"]:
812        sources = source_info(file, d)
813
814    bb.utils.mkdirhier(os.path.dirname(debugfile))
815
816    subprocess.check_output([objcopy, '--only-keep-debug', file, debugfile], stderr=subprocess.STDOUT)
817
818    # Set the debuglink to have the view of the file path on the target
819    subprocess.check_output([objcopy, '--add-gnu-debuglink', debugfile, file], stderr=subprocess.STDOUT)
820
821    if newmode:
822        os.chmod(file, origmode)
823
824    return (file, sources)
825
826def splitstaticdebuginfo(file, dvar, dv, d):
827    # Unlike the function above, there is no way to split a static library
828    # two components.  So to get similar results we will copy the unmodified
829    # static library (containing the debug symbols) into a new directory.
830    # We will then strip (preserving symbols) the static library in the
831    # typical location.
832    #
833    # return a mapping of files:debugsources
834
835    src = file[len(dvar):]
836    dest = dv["staticlibdir"] + os.path.dirname(src) + dv["staticdir"] + "/" + os.path.basename(src) + dv["staticappend"]
837    debugfile = dvar + dest
838    sources = []
839
840    # Copy the file...
841    bb.utils.mkdirhier(os.path.dirname(debugfile))
842    #bb.note("Copy %s -> %s" % (file, debugfile))
843
844    dvar = d.getVar('PKGD')
845
846    newmode = None
847    if not os.access(file, os.W_OK) or os.access(file, os.R_OK):
848        origmode = os.stat(file)[stat.ST_MODE]
849        newmode = origmode | stat.S_IWRITE | stat.S_IREAD
850        os.chmod(file, newmode)
851
852    # We need to extract the debug src information here...
853    if dv["srcdir"]:
854        sources = source_info(file, d)
855
856    bb.utils.mkdirhier(os.path.dirname(debugfile))
857
858    # Copy the unmodified item to the debug directory
859    shutil.copy2(file, debugfile)
860
861    if newmode:
862        os.chmod(file, origmode)
863
864    return (file, sources)
865
866def inject_minidebuginfo(file, dvar, dv, d):
867    # Extract just the symbols from debuginfo into minidebuginfo,
868    # compress it with xz and inject it back into the binary in a .gnu_debugdata section.
869    # https://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html
870
871    readelf = d.getVar('READELF')
872    nm = d.getVar('NM')
873    objcopy = d.getVar('OBJCOPY')
874
875    minidebuginfodir = d.expand('${WORKDIR}/minidebuginfo')
876
877    src = file[len(dvar):]
878    dest = dv["libdir"] + os.path.dirname(src) + dv["dir"] + "/" + os.path.basename(src) + dv["append"]
879    debugfile = dvar + dest
880    minidebugfile = minidebuginfodir + src + '.minidebug'
881    bb.utils.mkdirhier(os.path.dirname(minidebugfile))
882
883    # If we didn't produce debuginfo for any reason, we can't produce minidebuginfo either
884    # so skip it.
885    if not os.path.exists(debugfile):
886        bb.debug(1, 'ELF file {} has no debuginfo, skipping minidebuginfo injection'.format(file))
887        return
888
889    # minidebuginfo does not make sense to apply to ELF objects other than
890    # executables and shared libraries, skip applying the minidebuginfo
891    # generation for objects like kernel modules.
892    for line in subprocess.check_output([readelf, '-h', debugfile], universal_newlines=True).splitlines():
893        if not line.strip().startswith("Type:"):
894            continue
895        elftype = line.split(":")[1].strip()
896        if not any(elftype.startswith(i) for i in ["EXEC", "DYN"]):
897            bb.debug(1, 'ELF file {} is not executable/shared, skipping minidebuginfo injection'.format(file))
898            return
899        break
900
901    # Find non-allocated PROGBITS, NOTE, and NOBITS sections in the debuginfo.
902    # We will exclude all of these from minidebuginfo to save space.
903    remove_section_names = []
904    for line in subprocess.check_output([readelf, '-W', '-S', debugfile], universal_newlines=True).splitlines():
905        # strip the leading "  [ 1]" section index to allow splitting on space
906        if ']' not in line:
907            continue
908        fields = line[line.index(']') + 1:].split()
909        if len(fields) < 7:
910            continue
911        name = fields[0]
912        type = fields[1]
913        flags = fields[6]
914        # .debug_ sections will be removed by objcopy -S so no need to explicitly remove them
915        if name.startswith('.debug_'):
916            continue
917        if 'A' not in flags and type in ['PROGBITS', 'NOTE', 'NOBITS']:
918            remove_section_names.append(name)
919
920    # List dynamic symbols in the binary. We can exclude these from minidebuginfo
921    # because they are always present in the binary.
922    dynsyms = set()
923    for line in subprocess.check_output([nm, '-D', file, '--format=posix', '--defined-only'], universal_newlines=True).splitlines():
924        dynsyms.add(line.split()[0])
925
926    # Find all function symbols from debuginfo which aren't in the dynamic symbols table.
927    # These are the ones we want to keep in minidebuginfo.
928    keep_symbols_file = minidebugfile + '.symlist'
929    found_any_symbols = False
930    with open(keep_symbols_file, 'w') as f:
931        for line in subprocess.check_output([nm, debugfile, '--format=sysv', '--defined-only'], universal_newlines=True).splitlines():
932            fields = line.split('|')
933            if len(fields) < 7:
934                continue
935            name = fields[0].strip()
936            type = fields[3].strip()
937            if type == 'FUNC' and name not in dynsyms:
938                f.write('{}\n'.format(name))
939                found_any_symbols = True
940
941    if not found_any_symbols:
942        bb.debug(1, 'ELF file {} contains no symbols, skipping minidebuginfo injection'.format(file))
943        return
944
945    bb.utils.remove(minidebugfile)
946    bb.utils.remove(minidebugfile + '.xz')
947
948    subprocess.check_call([objcopy, '-S'] +
949                          ['--remove-section={}'.format(s) for s in remove_section_names] +
950                          ['--keep-symbols={}'.format(keep_symbols_file), debugfile, minidebugfile])
951
952    subprocess.check_call(['xz', '--keep', minidebugfile])
953
954    subprocess.check_call([objcopy, '--add-section', '.gnu_debugdata={}.xz'.format(minidebugfile), file])
955
956def copydebugsources(debugsrcdir, sources, d):
957    # The debug src information written out to sourcefile is further processed
958    # and copied to the destination here.
959
960    cpath = oe.cachedpath.CachedPath()
961
962    if debugsrcdir and sources:
963        sourcefile = d.expand("${WORKDIR}/debugsources.list")
964        bb.utils.remove(sourcefile)
965
966        # filenames are null-separated - this is an artefact of the previous use
967        # of rpm's debugedit, which was writing them out that way, and the code elsewhere
968        # is still assuming that.
969        debuglistoutput = '\0'.join(sources) + '\0'
970        with open(sourcefile, 'a') as sf:
971           sf.write(debuglistoutput)
972
973        dvar = d.getVar('PKGD')
974        strip = d.getVar("STRIP")
975        objcopy = d.getVar("OBJCOPY")
976        workdir = d.getVar("WORKDIR")
977        sdir = d.getVar("S")
978        cflags = d.expand("${CFLAGS}")
979
980        prefixmap = {}
981        for flag in cflags.split():
982            if not flag.startswith("-fdebug-prefix-map"):
983                continue
984            if "recipe-sysroot" in flag:
985                continue
986            flag = flag.split("=")
987            prefixmap[flag[1]] = flag[2]
988
989        nosuchdir = []
990        basepath = dvar
991        for p in debugsrcdir.split("/"):
992            basepath = basepath + "/" + p
993            if not cpath.exists(basepath):
994                nosuchdir.append(basepath)
995        bb.utils.mkdirhier(basepath)
996        cpath.updatecache(basepath)
997
998        for pmap in prefixmap:
999            # Ignore files from the recipe sysroots (target and native)
1000            cmd =  "LC_ALL=C ; sort -z -u '%s' | egrep -v -z '((<internal>|<built-in>)$|/.*recipe-sysroot.*/)' | " % sourcefile
1001            # We need to ignore files that are not actually ours
1002            # we do this by only paying attention to items from this package
1003            cmd += "fgrep -zw '%s' | " % prefixmap[pmap]
1004            # Remove prefix in the source paths
1005            cmd += "sed 's#%s/##g' | " % (prefixmap[pmap])
1006            cmd += "(cd '%s' ; cpio -pd0mlL --no-preserve-owner '%s%s' 2>/dev/null)" % (pmap, dvar, prefixmap[pmap])
1007
1008            try:
1009                subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
1010            except subprocess.CalledProcessError:
1011                # Can "fail" if internal headers/transient sources are attempted
1012                pass
1013            # cpio seems to have a bug with -lL together and symbolic links are just copied, not dereferenced.
1014            # Work around this by manually finding and copying any symbolic links that made it through.
1015            cmd = "find %s%s -type l -print0 -delete | sed s#%s%s/##g | (cd '%s' ; cpio -pd0mL --no-preserve-owner '%s%s')" % \
1016                    (dvar, prefixmap[pmap], dvar, prefixmap[pmap], pmap, dvar, prefixmap[pmap])
1017            subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
1018
1019        # debugsources.list may be polluted from the host if we used externalsrc,
1020        # cpio uses copy-pass and may have just created a directory structure
1021        # matching the one from the host, if thats the case move those files to
1022        # debugsrcdir to avoid host contamination.
1023        # Empty dir structure will be deleted in the next step.
1024
1025        # Same check as above for externalsrc
1026        if workdir not in sdir:
1027            if os.path.exists(dvar + debugsrcdir + sdir):
1028                cmd = "mv %s%s%s/* %s%s" % (dvar, debugsrcdir, sdir, dvar,debugsrcdir)
1029                subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
1030
1031        # The copy by cpio may have resulted in some empty directories!  Remove these
1032        cmd = "find %s%s -empty -type d -delete" % (dvar, debugsrcdir)
1033        subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
1034
1035        # Also remove debugsrcdir if its empty
1036        for p in nosuchdir[::-1]:
1037            if os.path.exists(p) and not os.listdir(p):
1038                os.rmdir(p)
1039
1040
1041def process_split_and_strip_files(d):
1042    cpath = oe.cachedpath.CachedPath()
1043
1044    dvar = d.getVar('PKGD')
1045    pn = d.getVar('PN')
1046    hostos = d.getVar('HOST_OS')
1047
1048    oldcwd = os.getcwd()
1049    os.chdir(dvar)
1050
1051    dv = package_debug_vars(d)
1052
1053    #
1054    # First lets figure out all of the files we may have to process ... do this only once!
1055    #
1056    elffiles = {}
1057    symlinks = {}
1058    staticlibs = []
1059    inodes = {}
1060    libdir = os.path.abspath(dvar + os.sep + d.getVar("libdir"))
1061    baselibdir = os.path.abspath(dvar + os.sep + d.getVar("base_libdir"))
1062    skipfiles = (d.getVar("INHIBIT_PACKAGE_STRIP_FILES") or "").split()
1063    if (d.getVar('INHIBIT_PACKAGE_STRIP') != '1' or \
1064            d.getVar('INHIBIT_PACKAGE_DEBUG_SPLIT') != '1'):
1065        checkelf = {}
1066        checkelflinks = {}
1067        for root, dirs, files in cpath.walk(dvar):
1068            for f in files:
1069                file = os.path.join(root, f)
1070
1071                # Skip debug files
1072                if dv["append"] and file.endswith(dv["append"]):
1073                    continue
1074                if dv["dir"] and dv["dir"] in os.path.dirname(file[len(dvar):]):
1075                    continue
1076
1077                if file in skipfiles:
1078                    continue
1079
1080                if oe.package.is_static_lib(file):
1081                    staticlibs.append(file)
1082                    continue
1083
1084                try:
1085                    ltarget = cpath.realpath(file, dvar, False)
1086                    s = cpath.lstat(ltarget)
1087                except OSError as e:
1088                    (err, strerror) = e.args
1089                    if err != errno.ENOENT:
1090                        raise
1091                    # Skip broken symlinks
1092                    continue
1093                if not s:
1094                    continue
1095                # Check its an executable
1096                if (s[stat.ST_MODE] & stat.S_IXUSR) or (s[stat.ST_MODE] & stat.S_IXGRP) \
1097                        or (s[stat.ST_MODE] & stat.S_IXOTH) \
1098                        or ((file.startswith(libdir) or file.startswith(baselibdir)) \
1099                        and (".so" in f or ".node" in f)) \
1100                        or (f.startswith('vmlinux') or ".ko" in f):
1101
1102                    if cpath.islink(file):
1103                        checkelflinks[file] = ltarget
1104                        continue
1105                    # Use a reference of device ID and inode number to identify files
1106                    file_reference = "%d_%d" % (s.st_dev, s.st_ino)
1107                    checkelf[file] = (file, file_reference)
1108
1109        results = oe.utils.multiprocess_launch(oe.package.is_elf, checkelflinks.values(), d)
1110        results_map = {}
1111        for (ltarget, elf_file) in results:
1112            results_map[ltarget] = elf_file
1113        for file in checkelflinks:
1114            ltarget = checkelflinks[file]
1115            # If it's a symlink, and points to an ELF file, we capture the readlink target
1116            if results_map[ltarget]:
1117                target = os.readlink(file)
1118                #bb.note("Sym: %s (%d)" % (ltarget, results_map[ltarget]))
1119                symlinks[file] = target
1120
1121        results = oe.utils.multiprocess_launch(oe.package.is_elf, checkelf.keys(), d)
1122
1123        # Sort results by file path. This ensures that the files are always
1124        # processed in the same order, which is important to make sure builds
1125        # are reproducible when dealing with hardlinks
1126        results.sort(key=lambda x: x[0])
1127
1128        for (file, elf_file) in results:
1129            # It's a file (or hardlink), not a link
1130            # ...but is it ELF, and is it already stripped?
1131            if elf_file & 1:
1132                if elf_file & 2:
1133                    if 'already-stripped' in (d.getVar('INSANE_SKIP:' + pn) or "").split():
1134                        bb.note("Skipping file %s from %s for already-stripped QA test" % (file[len(dvar):], pn))
1135                    else:
1136                        msg = "File '%s' from %s was already stripped, this will prevent future debugging!" % (file[len(dvar):], pn)
1137                        oe.qa.handle_error("already-stripped", msg, d)
1138                    continue
1139
1140                # At this point we have an unstripped elf file. We need to:
1141                #  a) Make sure any file we strip is not hardlinked to anything else outside this tree
1142                #  b) Only strip any hardlinked file once (no races)
1143                #  c) Track any hardlinks between files so that we can reconstruct matching debug file hardlinks
1144
1145                # Use a reference of device ID and inode number to identify files
1146                file_reference = checkelf[file][1]
1147                if file_reference in inodes:
1148                    os.unlink(file)
1149                    os.link(inodes[file_reference][0], file)
1150                    inodes[file_reference].append(file)
1151                else:
1152                    inodes[file_reference] = [file]
1153                    # break hardlink
1154                    bb.utils.break_hardlinks(file)
1155                    elffiles[file] = elf_file
1156                # Modified the file so clear the cache
1157                cpath.updatecache(file)
1158
1159    def strip_pkgd_prefix(f):
1160        nonlocal dvar
1161
1162        if f.startswith(dvar):
1163            return f[len(dvar):]
1164
1165        return f
1166
1167    #
1168    # First lets process debug splitting
1169    #
1170    if (d.getVar('INHIBIT_PACKAGE_DEBUG_SPLIT') != '1'):
1171        results = oe.utils.multiprocess_launch(splitdebuginfo, list(elffiles), d, extraargs=(dvar, dv, d))
1172
1173        if dv["srcdir"] and not hostos.startswith("mingw"):
1174            if (d.getVar('PACKAGE_DEBUG_STATIC_SPLIT') == '1'):
1175                results = oe.utils.multiprocess_launch(splitstaticdebuginfo, staticlibs, d, extraargs=(dvar, dv, d))
1176            else:
1177                for file in staticlibs:
1178                    results.append( (file,source_info(file, d)) )
1179
1180        d.setVar("PKGDEBUGSOURCES", {strip_pkgd_prefix(f): sorted(s) for f, s in results})
1181
1182        sources = set()
1183        for r in results:
1184            sources.update(r[1])
1185
1186        # Hardlink our debug symbols to the other hardlink copies
1187        for ref in inodes:
1188            if len(inodes[ref]) == 1:
1189                continue
1190
1191            target = inodes[ref][0][len(dvar):]
1192            for file in inodes[ref][1:]:
1193                src = file[len(dvar):]
1194                dest = dv["libdir"] + os.path.dirname(src) + dv["dir"] + "/" + os.path.basename(target) + dv["append"]
1195                fpath = dvar + dest
1196                ftarget = dvar + dv["libdir"] + os.path.dirname(target) + dv["dir"] + "/" + os.path.basename(target) + dv["append"]
1197                bb.utils.mkdirhier(os.path.dirname(fpath))
1198                # Only one hardlink of separated debug info file in each directory
1199                if not os.access(fpath, os.R_OK):
1200                    #bb.note("Link %s -> %s" % (fpath, ftarget))
1201                    os.link(ftarget, fpath)
1202
1203        # Create symlinks for all cases we were able to split symbols
1204        for file in symlinks:
1205            src = file[len(dvar):]
1206            dest = dv["libdir"] + os.path.dirname(src) + dv["dir"] + "/" + os.path.basename(src) + dv["append"]
1207            fpath = dvar + dest
1208            # Skip it if the target doesn't exist
1209            try:
1210                s = os.stat(fpath)
1211            except OSError as e:
1212                (err, strerror) = e.args
1213                if err != errno.ENOENT:
1214                    raise
1215                continue
1216
1217            ltarget = symlinks[file]
1218            lpath = os.path.dirname(ltarget)
1219            lbase = os.path.basename(ltarget)
1220            ftarget = ""
1221            if lpath and lpath != ".":
1222                ftarget += lpath + dv["dir"] + "/"
1223            ftarget += lbase + dv["append"]
1224            if lpath.startswith(".."):
1225                ftarget = os.path.join("..", ftarget)
1226            bb.utils.mkdirhier(os.path.dirname(fpath))
1227            #bb.note("Symlink %s -> %s" % (fpath, ftarget))
1228            os.symlink(ftarget, fpath)
1229
1230        # Process the dv["srcdir"] if requested...
1231        # This copies and places the referenced sources for later debugging...
1232        copydebugsources(dv["srcdir"], sources, d)
1233    #
1234    # End of debug splitting
1235    #
1236
1237    #
1238    # Now lets go back over things and strip them
1239    #
1240    if (d.getVar('INHIBIT_PACKAGE_STRIP') != '1'):
1241        strip = d.getVar("STRIP")
1242        sfiles = []
1243        for file in elffiles:
1244            elf_file = int(elffiles[file])
1245            #bb.note("Strip %s" % file)
1246            sfiles.append((file, elf_file, strip))
1247        if (d.getVar('PACKAGE_STRIP_STATIC') == '1' or d.getVar('PACKAGE_DEBUG_STATIC_SPLIT') == '1'):
1248            for f in staticlibs:
1249                sfiles.append((f, 16, strip))
1250
1251        oe.utils.multiprocess_launch(oe.package.runstrip, sfiles, d)
1252
1253    # Build "minidebuginfo" and reinject it back into the stripped binaries
1254    if bb.utils.contains('DISTRO_FEATURES', 'minidebuginfo', True, False, d):
1255        oe.utils.multiprocess_launch(inject_minidebuginfo, list(elffiles), d,
1256                                     extraargs=(dvar, dv, d))
1257
1258    #
1259    # End of strip
1260    #
1261    os.chdir(oldcwd)
1262
1263
1264def populate_packages(d):
1265    cpath = oe.cachedpath.CachedPath()
1266
1267    workdir = d.getVar('WORKDIR')
1268    outdir = d.getVar('DEPLOY_DIR')
1269    dvar = d.getVar('PKGD')
1270    packages = d.getVar('PACKAGES').split()
1271    pn = d.getVar('PN')
1272
1273    bb.utils.mkdirhier(outdir)
1274    os.chdir(dvar)
1275
1276    autodebug = not (d.getVar("NOAUTOPACKAGEDEBUG") or False)
1277
1278    split_source_package = (d.getVar('PACKAGE_DEBUG_SPLIT_STYLE') == 'debug-with-srcpkg')
1279
1280    # If debug-with-srcpkg mode is enabled then add the source package if it
1281    # doesn't exist and add the source file contents to the source package.
1282    if split_source_package:
1283        src_package_name = ('%s-src' % d.getVar('PN'))
1284        if not src_package_name in packages:
1285            packages.append(src_package_name)
1286        d.setVar('FILES:%s' % src_package_name, '/usr/src/debug')
1287
1288    # Sanity check PACKAGES for duplicates
1289    # Sanity should be moved to sanity.bbclass once we have the infrastructure
1290    package_dict = {}
1291
1292    for i, pkg in enumerate(packages):
1293        if pkg in package_dict:
1294            msg = "%s is listed in PACKAGES multiple times, this leads to packaging errors." % pkg
1295            oe.qa.handle_error("packages-list", msg, d)
1296        # Ensure the source package gets the chance to pick up the source files
1297        # before the debug package by ordering it first in PACKAGES. Whether it
1298        # actually picks up any source files is controlled by
1299        # PACKAGE_DEBUG_SPLIT_STYLE.
1300        elif pkg.endswith("-src"):
1301            package_dict[pkg] = (10, i)
1302        elif autodebug and pkg.endswith("-dbg"):
1303            package_dict[pkg] = (30, i)
1304        else:
1305            package_dict[pkg] = (50, i)
1306    packages = sorted(package_dict.keys(), key=package_dict.get)
1307    d.setVar('PACKAGES', ' '.join(packages))
1308    pkgdest = d.getVar('PKGDEST')
1309
1310    seen = []
1311
1312    # os.mkdir masks the permissions with umask so we have to unset it first
1313    oldumask = os.umask(0)
1314
1315    debug = []
1316    for root, dirs, files in cpath.walk(dvar):
1317        dir = root[len(dvar):]
1318        if not dir:
1319            dir = os.sep
1320        for f in (files + dirs):
1321            path = "." + os.path.join(dir, f)
1322            if "/.debug/" in path or "/.debug-static/" in path or path.endswith("/.debug"):
1323                debug.append(path)
1324
1325    for pkg in packages:
1326        root = os.path.join(pkgdest, pkg)
1327        bb.utils.mkdirhier(root)
1328
1329        filesvar = d.getVar('FILES:%s' % pkg) or ""
1330        if "//" in filesvar:
1331            msg = "FILES variable for package %s contains '//' which is invalid. Attempting to fix this but you should correct the metadata.\n" % pkg
1332            oe.qa.handle_error("files-invalid", msg, d)
1333            filesvar.replace("//", "/")
1334
1335        origfiles = filesvar.split()
1336        files, symlink_paths = oe.package.files_from_filevars(origfiles)
1337
1338        if autodebug and pkg.endswith("-dbg"):
1339            files.extend(debug)
1340
1341        for file in files:
1342            if (not cpath.islink(file)) and (not cpath.exists(file)):
1343                continue
1344            if file in seen:
1345                continue
1346            seen.append(file)
1347
1348            def mkdir(src, dest, p):
1349                src = os.path.join(src, p)
1350                dest = os.path.join(dest, p)
1351                fstat = cpath.stat(src)
1352                os.mkdir(dest)
1353                os.chmod(dest, fstat.st_mode)
1354                os.chown(dest, fstat.st_uid, fstat.st_gid)
1355                if p not in seen:
1356                    seen.append(p)
1357                cpath.updatecache(dest)
1358
1359            def mkdir_recurse(src, dest, paths):
1360                if cpath.exists(dest + '/' + paths):
1361                    return
1362                while paths.startswith("./"):
1363                    paths = paths[2:]
1364                p = "."
1365                for c in paths.split("/"):
1366                    p = os.path.join(p, c)
1367                    if not cpath.exists(os.path.join(dest, p)):
1368                        mkdir(src, dest, p)
1369
1370            if cpath.isdir(file) and not cpath.islink(file):
1371                mkdir_recurse(dvar, root, file)
1372                continue
1373
1374            mkdir_recurse(dvar, root, os.path.dirname(file))
1375            fpath = os.path.join(root,file)
1376            if not cpath.islink(file):
1377                os.link(file, fpath)
1378                continue
1379            ret = bb.utils.copyfile(file, fpath)
1380            if ret is False or ret == 0:
1381                bb.fatal("File population failed")
1382
1383        # Check if symlink paths exist
1384        for file in symlink_paths:
1385            if not os.path.exists(os.path.join(root,file)):
1386                bb.fatal("File '%s' cannot be packaged into '%s' because its "
1387                         "parent directory structure does not exist. One of "
1388                         "its parent directories is a symlink whose target "
1389                         "directory is not included in the package." %
1390                         (file, pkg))
1391
1392    os.umask(oldumask)
1393    os.chdir(workdir)
1394
1395    # Handle excluding packages with incompatible licenses
1396    package_list = []
1397    for pkg in packages:
1398        licenses = d.getVar('_exclude_incompatible-' + pkg)
1399        if licenses:
1400            msg = "Excluding %s from packaging as it has incompatible license(s): %s" % (pkg, licenses)
1401            oe.qa.handle_error("incompatible-license", msg, d)
1402        else:
1403            package_list.append(pkg)
1404    d.setVar('PACKAGES', ' '.join(package_list))
1405
1406    unshipped = []
1407    for root, dirs, files in cpath.walk(dvar):
1408        dir = root[len(dvar):]
1409        if not dir:
1410            dir = os.sep
1411        for f in (files + dirs):
1412            path = os.path.join(dir, f)
1413            if ('.' + path) not in seen:
1414                unshipped.append(path)
1415
1416    if unshipped != []:
1417        msg = pn + ": Files/directories were installed but not shipped in any package:"
1418        if "installed-vs-shipped" in (d.getVar('INSANE_SKIP:' + pn) or "").split():
1419            bb.note("Package %s skipping QA tests: installed-vs-shipped" % pn)
1420        else:
1421            for f in unshipped:
1422                msg = msg + "\n  " + f
1423            msg = msg + "\nPlease set FILES such that these items are packaged. Alternatively if they are unneeded, avoid installing them or delete them within do_install.\n"
1424            msg = msg + "%s: %d installed and not shipped files." % (pn, len(unshipped))
1425            oe.qa.handle_error("installed-vs-shipped", msg, d)
1426
1427def process_fixsymlinks(pkgfiles, d):
1428    cpath = oe.cachedpath.CachedPath()
1429    pkgdest = d.getVar('PKGDEST')
1430    packages = d.getVar("PACKAGES", False).split()
1431
1432    dangling_links = {}
1433    pkg_files = {}
1434    for pkg in packages:
1435        dangling_links[pkg] = []
1436        pkg_files[pkg] = []
1437        inst_root = os.path.join(pkgdest, pkg)
1438        for path in pkgfiles[pkg]:
1439                rpath = path[len(inst_root):]
1440                pkg_files[pkg].append(rpath)
1441                rtarget = cpath.realpath(path, inst_root, True, assume_dir = True)
1442                if not cpath.lexists(rtarget):
1443                    dangling_links[pkg].append(os.path.normpath(rtarget[len(inst_root):]))
1444
1445    newrdepends = {}
1446    for pkg in dangling_links:
1447        for l in dangling_links[pkg]:
1448            found = False
1449            bb.debug(1, "%s contains dangling link %s" % (pkg, l))
1450            for p in packages:
1451                if l in pkg_files[p]:
1452                        found = True
1453                        bb.debug(1, "target found in %s" % p)
1454                        if p == pkg:
1455                            break
1456                        if pkg not in newrdepends:
1457                            newrdepends[pkg] = []
1458                        newrdepends[pkg].append(p)
1459                        break
1460            if found == False:
1461                bb.note("%s contains dangling symlink to %s" % (pkg, l))
1462
1463    for pkg in newrdepends:
1464        rdepends = bb.utils.explode_dep_versions2(d.getVar('RDEPENDS:' + pkg) or "")
1465        for p in newrdepends[pkg]:
1466            if p not in rdepends:
1467                rdepends[p] = []
1468        d.setVar('RDEPENDS:' + pkg, bb.utils.join_deps(rdepends, commasep=False))
1469
1470def process_filedeps(pkgfiles, d):
1471    """
1472    Collect perfile run-time dependency metadata
1473    Output:
1474     FILERPROVIDESFLIST:pkg - list of all files w/ deps
1475     FILERPROVIDES:filepath:pkg - per file dep
1476
1477      FILERDEPENDSFLIST:pkg - list of all files w/ deps
1478      FILERDEPENDS:filepath:pkg - per file dep
1479    """
1480    if d.getVar('SKIP_FILEDEPS') == '1':
1481        return
1482
1483    pkgdest = d.getVar('PKGDEST')
1484    packages = d.getVar('PACKAGES')
1485    rpmdeps = d.getVar('RPMDEPS')
1486
1487    def chunks(files, n):
1488        return [files[i:i+n] for i in range(0, len(files), n)]
1489
1490    pkglist = []
1491    for pkg in packages.split():
1492        if d.getVar('SKIP_FILEDEPS:' + pkg) == '1':
1493            continue
1494        if pkg.endswith('-dbg') or pkg.endswith('-doc') or pkg.find('-locale-') != -1 or pkg.find('-localedata-') != -1 or pkg.find('-gconv-') != -1 or pkg.find('-charmap-') != -1 or pkg.startswith('kernel-module-') or pkg.endswith('-src'):
1495            continue
1496        for files in chunks(pkgfiles[pkg], 100):
1497            pkglist.append((pkg, files, rpmdeps, pkgdest))
1498
1499    processed = oe.utils.multiprocess_launch(oe.package.filedeprunner, pkglist, d)
1500
1501    provides_files = {}
1502    requires_files = {}
1503
1504    for result in processed:
1505        (pkg, provides, requires) = result
1506
1507        if pkg not in provides_files:
1508            provides_files[pkg] = []
1509        if pkg not in requires_files:
1510            requires_files[pkg] = []
1511
1512        for file in sorted(provides):
1513            provides_files[pkg].append(file)
1514            key = "FILERPROVIDES:" + file + ":" + pkg
1515            d.appendVar(key, " " + " ".join(provides[file]))
1516
1517        for file in sorted(requires):
1518            requires_files[pkg].append(file)
1519            key = "FILERDEPENDS:" + file + ":" + pkg
1520            d.appendVar(key, " " + " ".join(requires[file]))
1521
1522    for pkg in requires_files:
1523        d.setVar("FILERDEPENDSFLIST:" + pkg, " ".join(sorted(requires_files[pkg])))
1524    for pkg in provides_files:
1525        d.setVar("FILERPROVIDESFLIST:" + pkg, " ".join(sorted(provides_files[pkg])))
1526
1527def process_shlibs(pkgfiles, d):
1528    cpath = oe.cachedpath.CachedPath()
1529
1530    exclude_shlibs = d.getVar('EXCLUDE_FROM_SHLIBS', False)
1531    if exclude_shlibs:
1532        bb.note("not generating shlibs")
1533        return
1534
1535    lib_re = re.compile(r"^.*\.so")
1536    libdir_re = re.compile(r".*/%s$" % d.getVar('baselib'))
1537
1538    packages = d.getVar('PACKAGES')
1539
1540    shlib_pkgs = []
1541    exclusion_list = d.getVar("EXCLUDE_PACKAGES_FROM_SHLIBS")
1542    if exclusion_list:
1543        for pkg in packages.split():
1544            if pkg not in exclusion_list.split():
1545                shlib_pkgs.append(pkg)
1546            else:
1547                bb.note("not generating shlibs for %s" % pkg)
1548    else:
1549        shlib_pkgs = packages.split()
1550
1551    hostos = d.getVar('HOST_OS')
1552
1553    workdir = d.getVar('WORKDIR')
1554
1555    ver = d.getVar('PKGV')
1556    if not ver:
1557        msg = "PKGV not defined"
1558        oe.qa.handle_error("pkgv-undefined", msg, d)
1559        return
1560
1561    pkgdest = d.getVar('PKGDEST')
1562
1563    shlibswork_dir = d.getVar('SHLIBSWORKDIR')
1564
1565    def linux_so(file, pkg, pkgver, d):
1566        needs_ldconfig = False
1567        needed = set()
1568        sonames = set()
1569        renames = []
1570        ldir = os.path.dirname(file).replace(pkgdest + "/" + pkg, '')
1571        cmd = d.getVar('OBJDUMP') + " -p " + shlex.quote(file) + " 2>/dev/null"
1572        fd = os.popen(cmd)
1573        lines = fd.readlines()
1574        fd.close()
1575        rpath = tuple()
1576        for l in lines:
1577            m = re.match(r"\s+RPATH\s+([^\s]*)", l)
1578            if m:
1579                rpaths = m.group(1).replace("$ORIGIN", ldir).split(":")
1580                rpath = tuple(map(os.path.normpath, rpaths))
1581        for l in lines:
1582            m = re.match(r"\s+NEEDED\s+([^\s]*)", l)
1583            if m:
1584                dep = m.group(1)
1585                if dep not in needed:
1586                    needed.add((dep, file, rpath))
1587            m = re.match(r"\s+SONAME\s+([^\s]*)", l)
1588            if m:
1589                this_soname = m.group(1)
1590                prov = (this_soname, ldir, pkgver)
1591                if not prov in sonames:
1592                    # if library is private (only used by package) then do not build shlib for it
1593                    if not private_libs or len([i for i in private_libs if fnmatch.fnmatch(this_soname, i)]) == 0:
1594                        sonames.add(prov)
1595                if libdir_re.match(os.path.dirname(file)):
1596                    needs_ldconfig = True
1597                if needs_ldconfig and snap_symlinks and (os.path.basename(file) != this_soname):
1598                    renames.append((file, os.path.join(os.path.dirname(file), this_soname)))
1599        return (needs_ldconfig, needed, sonames, renames)
1600
1601    def darwin_so(file, needed, sonames, renames, pkgver):
1602        if not os.path.exists(file):
1603            return
1604        ldir = os.path.dirname(file).replace(pkgdest + "/" + pkg, '')
1605
1606        def get_combinations(base):
1607            #
1608            # Given a base library name, find all combinations of this split by "." and "-"
1609            #
1610            combos = []
1611            options = base.split(".")
1612            for i in range(1, len(options) + 1):
1613                combos.append(".".join(options[0:i]))
1614            options = base.split("-")
1615            for i in range(1, len(options) + 1):
1616                combos.append("-".join(options[0:i]))
1617            return combos
1618
1619        if (file.endswith('.dylib') or file.endswith('.so')) and not pkg.endswith('-dev') and not pkg.endswith('-dbg') and not pkg.endswith('-src'):
1620            # Drop suffix
1621            name = os.path.basename(file).rsplit(".",1)[0]
1622            # Find all combinations
1623            combos = get_combinations(name)
1624            for combo in combos:
1625                if not combo in sonames:
1626                    prov = (combo, ldir, pkgver)
1627                    sonames.add(prov)
1628        if file.endswith('.dylib') or file.endswith('.so'):
1629            rpath = []
1630            p = subprocess.Popen([d.expand("${HOST_PREFIX}otool"), '-l', file], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
1631            out, err = p.communicate()
1632            # If returned successfully, process stdout for results
1633            if p.returncode == 0:
1634                for l in out.split("\n"):
1635                    l = l.strip()
1636                    if l.startswith('path '):
1637                        rpath.append(l.split()[1])
1638
1639        p = subprocess.Popen([d.expand("${HOST_PREFIX}otool"), '-L', file], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
1640        out, err = p.communicate()
1641        # If returned successfully, process stdout for results
1642        if p.returncode == 0:
1643            for l in out.split("\n"):
1644                l = l.strip()
1645                if not l or l.endswith(":"):
1646                    continue
1647                if "is not an object file" in l:
1648                    continue
1649                name = os.path.basename(l.split()[0]).rsplit(".", 1)[0]
1650                if name and name not in needed[pkg]:
1651                     needed[pkg].add((name, file, tuple()))
1652
1653    def mingw_dll(file, needed, sonames, renames, pkgver):
1654        if not os.path.exists(file):
1655            return
1656
1657        if file.endswith(".dll"):
1658            # assume all dlls are shared objects provided by the package
1659            sonames.add((os.path.basename(file), os.path.dirname(file).replace(pkgdest + "/" + pkg, ''), pkgver))
1660
1661        if (file.endswith(".dll") or file.endswith(".exe")):
1662            # use objdump to search for "DLL Name: .*\.dll"
1663            p = subprocess.Popen([d.expand("${OBJDUMP}"), "-p", file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1664            out, err = p.communicate()
1665            # process the output, grabbing all .dll names
1666            if p.returncode == 0:
1667                for m in re.finditer(r"DLL Name: (.*?\.dll)$", out.decode(), re.MULTILINE | re.IGNORECASE):
1668                    dllname = m.group(1)
1669                    if dllname:
1670                        needed[pkg].add((dllname, file, tuple()))
1671
1672    if d.getVar('PACKAGE_SNAP_LIB_SYMLINKS') == "1":
1673        snap_symlinks = True
1674    else:
1675        snap_symlinks = False
1676
1677    needed = {}
1678
1679    shlib_provider = oe.package.read_shlib_providers(d)
1680
1681    for pkg in shlib_pkgs:
1682        private_libs = d.getVar('PRIVATE_LIBS:' + pkg) or d.getVar('PRIVATE_LIBS') or ""
1683        private_libs = private_libs.split()
1684        needs_ldconfig = False
1685        bb.debug(2, "calculating shlib provides for %s" % pkg)
1686
1687        pkgver = d.getVar('PKGV:' + pkg)
1688        if not pkgver:
1689            pkgver = d.getVar('PV_' + pkg)
1690        if not pkgver:
1691            pkgver = ver
1692
1693        needed[pkg] = set()
1694        sonames = set()
1695        renames = []
1696        linuxlist = []
1697        for file in pkgfiles[pkg]:
1698                soname = None
1699                if cpath.islink(file):
1700                    continue
1701                if hostos.startswith("darwin"):
1702                    darwin_so(file, needed, sonames, renames, pkgver)
1703                elif hostos.startswith("mingw"):
1704                    mingw_dll(file, needed, sonames, renames, pkgver)
1705                elif os.access(file, os.X_OK) or lib_re.match(file):
1706                    linuxlist.append(file)
1707
1708        if linuxlist:
1709            results = oe.utils.multiprocess_launch(linux_so, linuxlist, d, extraargs=(pkg, pkgver, d))
1710            for r in results:
1711                ldconfig = r[0]
1712                needed[pkg] |= r[1]
1713                sonames |= r[2]
1714                renames.extend(r[3])
1715                needs_ldconfig = needs_ldconfig or ldconfig
1716
1717        for (old, new) in renames:
1718            bb.note("Renaming %s to %s" % (old, new))
1719            bb.utils.rename(old, new)
1720            pkgfiles[pkg].remove(old)
1721
1722        shlibs_file = os.path.join(shlibswork_dir, pkg + ".list")
1723        if len(sonames):
1724            with open(shlibs_file, 'w') as fd:
1725                for s in sorted(sonames):
1726                    if s[0] in shlib_provider and s[1] in shlib_provider[s[0]]:
1727                        (old_pkg, old_pkgver) = shlib_provider[s[0]][s[1]]
1728                        if old_pkg != pkg:
1729                            bb.warn('%s-%s was registered as shlib provider for %s, changing it to %s-%s because it was built later' % (old_pkg, old_pkgver, s[0], pkg, pkgver))
1730                    bb.debug(1, 'registering %s-%s as shlib provider for %s' % (pkg, pkgver, s[0]))
1731                    fd.write(s[0] + ':' + s[1] + ':' + s[2] + '\n')
1732                    if s[0] not in shlib_provider:
1733                        shlib_provider[s[0]] = {}
1734                    shlib_provider[s[0]][s[1]] = (pkg, pkgver)
1735        if needs_ldconfig:
1736            bb.debug(1, 'adding ldconfig call to postinst for %s' % pkg)
1737            postinst = d.getVar('pkg_postinst:%s' % pkg)
1738            if not postinst:
1739                postinst = '#!/bin/sh\n'
1740            postinst += d.getVar('ldconfig_postinst_fragment')
1741            d.setVar('pkg_postinst:%s' % pkg, postinst)
1742        bb.debug(1, 'LIBNAMES: pkg %s sonames %s' % (pkg, sonames))
1743
1744    assumed_libs = d.getVar('ASSUME_SHLIBS')
1745    if assumed_libs:
1746        libdir = d.getVar("libdir")
1747        for e in assumed_libs.split():
1748            l, dep_pkg = e.split(":")
1749            lib_ver = None
1750            dep_pkg = dep_pkg.rsplit("_", 1)
1751            if len(dep_pkg) == 2:
1752                lib_ver = dep_pkg[1]
1753            dep_pkg = dep_pkg[0]
1754            if l not in shlib_provider:
1755                shlib_provider[l] = {}
1756            shlib_provider[l][libdir] = (dep_pkg, lib_ver)
1757
1758    libsearchpath = [d.getVar('libdir'), d.getVar('base_libdir')]
1759
1760    for pkg in shlib_pkgs:
1761        bb.debug(2, "calculating shlib requirements for %s" % pkg)
1762
1763        private_libs = d.getVar('PRIVATE_LIBS:' + pkg) or d.getVar('PRIVATE_LIBS') or ""
1764        private_libs = private_libs.split()
1765
1766        deps = list()
1767        for n in needed[pkg]:
1768            # if n is in private libraries, don't try to search provider for it
1769            # this could cause problem in case some abc.bb provides private
1770            # /opt/abc/lib/libfoo.so.1 and contains /usr/bin/abc depending on system library libfoo.so.1
1771            # but skipping it is still better alternative than providing own
1772            # version and then adding runtime dependency for the same system library
1773            if private_libs and len([i for i in private_libs if fnmatch.fnmatch(n[0], i)]) > 0:
1774                bb.debug(2, '%s: Dependency %s covered by PRIVATE_LIBS' % (pkg, n[0]))
1775                continue
1776            if n[0] in shlib_provider.keys():
1777                shlib_provider_map = shlib_provider[n[0]]
1778                matches = set()
1779                for p in itertools.chain(list(n[2]), sorted(shlib_provider_map.keys()), libsearchpath):
1780                    if p in shlib_provider_map:
1781                        matches.add(p)
1782                if len(matches) > 1:
1783                    matchpkgs = ', '.join([shlib_provider_map[match][0] for match in matches])
1784                    bb.error("%s: Multiple shlib providers for %s: %s (used by files: %s)" % (pkg, n[0], matchpkgs, n[1]))
1785                elif len(matches) == 1:
1786                    (dep_pkg, ver_needed) = shlib_provider_map[matches.pop()]
1787
1788                    bb.debug(2, '%s: Dependency %s requires package %s (used by files: %s)' % (pkg, n[0], dep_pkg, n[1]))
1789
1790                    if dep_pkg == pkg:
1791                        continue
1792
1793                    if ver_needed:
1794                        dep = "%s (>= %s)" % (dep_pkg, ver_needed)
1795                    else:
1796                        dep = dep_pkg
1797                    if not dep in deps:
1798                        deps.append(dep)
1799                    continue
1800            bb.note("Couldn't find shared library provider for %s, used by files: %s" % (n[0], n[1]))
1801
1802        deps_file = os.path.join(pkgdest, pkg + ".shlibdeps")
1803        if os.path.exists(deps_file):
1804            os.remove(deps_file)
1805        if deps:
1806            with open(deps_file, 'w') as fd:
1807                for dep in sorted(deps):
1808                    fd.write(dep + '\n')
1809
1810def process_pkgconfig(pkgfiles, d):
1811    packages = d.getVar('PACKAGES')
1812    workdir = d.getVar('WORKDIR')
1813    pkgdest = d.getVar('PKGDEST')
1814
1815    shlibs_dirs = d.getVar('SHLIBSDIRS').split()
1816    shlibswork_dir = d.getVar('SHLIBSWORKDIR')
1817
1818    pc_re = re.compile(r'(.*)\.pc$')
1819    var_re = re.compile(r'(.*)=(.*)')
1820    field_re = re.compile(r'(.*): (.*)')
1821
1822    pkgconfig_provided = {}
1823    pkgconfig_needed = {}
1824    for pkg in packages.split():
1825        pkgconfig_provided[pkg] = []
1826        pkgconfig_needed[pkg] = []
1827        for file in sorted(pkgfiles[pkg]):
1828                m = pc_re.match(file)
1829                if m:
1830                    pd = bb.data.init()
1831                    name = m.group(1)
1832                    pkgconfig_provided[pkg].append(os.path.basename(name))
1833                    if not os.access(file, os.R_OK):
1834                        continue
1835                    with open(file, 'r') as f:
1836                        lines = f.readlines()
1837                    for l in lines:
1838                        m = field_re.match(l)
1839                        if m:
1840                            hdr = m.group(1)
1841                            exp = pd.expand(m.group(2))
1842                            if hdr == 'Requires':
1843                                pkgconfig_needed[pkg] += exp.replace(',', ' ').split()
1844                                continue
1845                        m = var_re.match(l)
1846                        if m:
1847                            name = m.group(1)
1848                            val = m.group(2)
1849                            pd.setVar(name, pd.expand(val))
1850
1851    for pkg in packages.split():
1852        pkgs_file = os.path.join(shlibswork_dir, pkg + ".pclist")
1853        if pkgconfig_provided[pkg] != []:
1854            with open(pkgs_file, 'w') as f:
1855                for p in sorted(pkgconfig_provided[pkg]):
1856                    f.write('%s\n' % p)
1857
1858    # Go from least to most specific since the last one found wins
1859    for dir in reversed(shlibs_dirs):
1860        if not os.path.exists(dir):
1861            continue
1862        for file in sorted(os.listdir(dir)):
1863            m = re.match(r'^(.*)\.pclist$', file)
1864            if m:
1865                pkg = m.group(1)
1866                with open(os.path.join(dir, file)) as fd:
1867                    lines = fd.readlines()
1868                pkgconfig_provided[pkg] = []
1869                for l in lines:
1870                    pkgconfig_provided[pkg].append(l.rstrip())
1871
1872    for pkg in packages.split():
1873        deps = []
1874        for n in pkgconfig_needed[pkg]:
1875            found = False
1876            for k in pkgconfig_provided.keys():
1877                if n in pkgconfig_provided[k]:
1878                    if k != pkg and not (k in deps):
1879                        deps.append(k)
1880                    found = True
1881            if found == False:
1882                bb.note("couldn't find pkgconfig module '%s' in any package" % n)
1883        deps_file = os.path.join(pkgdest, pkg + ".pcdeps")
1884        if len(deps):
1885            with open(deps_file, 'w') as fd:
1886                for dep in deps:
1887                    fd.write(dep + '\n')
1888
1889def read_libdep_files(d):
1890    pkglibdeps = {}
1891    packages = d.getVar('PACKAGES').split()
1892    for pkg in packages:
1893        pkglibdeps[pkg] = {}
1894        for extension in ".shlibdeps", ".pcdeps", ".clilibdeps":
1895            depsfile = d.expand("${PKGDEST}/" + pkg + extension)
1896            if os.access(depsfile, os.R_OK):
1897                with open(depsfile) as fd:
1898                    lines = fd.readlines()
1899                for l in lines:
1900                    l.rstrip()
1901                    deps = bb.utils.explode_dep_versions2(l)
1902                    for dep in deps:
1903                        if not dep in pkglibdeps[pkg]:
1904                            pkglibdeps[pkg][dep] = deps[dep]
1905    return pkglibdeps
1906
1907def process_depchains(pkgfiles, d):
1908    """
1909    For a given set of prefix and postfix modifiers, make those packages
1910    RRECOMMENDS on the corresponding packages for its RDEPENDS.
1911
1912    Example:  If package A depends upon package B, and A's .bb emits an
1913    A-dev package, this would make A-dev Recommends: B-dev.
1914
1915    If only one of a given suffix is specified, it will take the RRECOMMENDS
1916    based on the RDEPENDS of *all* other packages. If more than one of a given
1917    suffix is specified, its will only use the RDEPENDS of the single parent
1918    package.
1919    """
1920
1921    packages  = d.getVar('PACKAGES')
1922    postfixes = (d.getVar('DEPCHAIN_POST') or '').split()
1923    prefixes  = (d.getVar('DEPCHAIN_PRE') or '').split()
1924
1925    def pkg_adddeprrecs(pkg, base, suffix, getname, depends, d):
1926
1927        #bb.note('depends for %s is %s' % (base, depends))
1928        rreclist = bb.utils.explode_dep_versions2(d.getVar('RRECOMMENDS:' + pkg) or "")
1929
1930        for depend in sorted(depends):
1931            if depend.find('-native') != -1 or depend.find('-cross') != -1 or depend.startswith('virtual/'):
1932                #bb.note("Skipping %s" % depend)
1933                continue
1934            if depend.endswith('-dev'):
1935                depend = depend[:-4]
1936            if depend.endswith('-dbg'):
1937                depend = depend[:-4]
1938            pkgname = getname(depend, suffix)
1939            #bb.note("Adding %s for %s" % (pkgname, depend))
1940            if pkgname not in rreclist and pkgname != pkg:
1941                rreclist[pkgname] = []
1942
1943        #bb.note('setting: RRECOMMENDS:%s=%s' % (pkg, ' '.join(rreclist)))
1944        d.setVar('RRECOMMENDS:%s' % pkg, bb.utils.join_deps(rreclist, commasep=False))
1945
1946    def pkg_addrrecs(pkg, base, suffix, getname, rdepends, d):
1947
1948        #bb.note('rdepends for %s is %s' % (base, rdepends))
1949        rreclist = bb.utils.explode_dep_versions2(d.getVar('RRECOMMENDS:' + pkg) or "")
1950
1951        for depend in sorted(rdepends):
1952            if depend.find('virtual-locale-') != -1:
1953                #bb.note("Skipping %s" % depend)
1954                continue
1955            if depend.endswith('-dev'):
1956                depend = depend[:-4]
1957            if depend.endswith('-dbg'):
1958                depend = depend[:-4]
1959            pkgname = getname(depend, suffix)
1960            #bb.note("Adding %s for %s" % (pkgname, depend))
1961            if pkgname not in rreclist and pkgname != pkg:
1962                rreclist[pkgname] = []
1963
1964        #bb.note('setting: RRECOMMENDS:%s=%s' % (pkg, ' '.join(rreclist)))
1965        d.setVar('RRECOMMENDS:%s' % pkg, bb.utils.join_deps(rreclist, commasep=False))
1966
1967    def add_dep(list, dep):
1968        if dep not in list:
1969            list.append(dep)
1970
1971    depends = []
1972    for dep in bb.utils.explode_deps(d.getVar('DEPENDS') or ""):
1973        add_dep(depends, dep)
1974
1975    rdepends = []
1976    for pkg in packages.split():
1977        for dep in bb.utils.explode_deps(d.getVar('RDEPENDS:' + pkg) or ""):
1978            add_dep(rdepends, dep)
1979
1980    #bb.note('rdepends is %s' % rdepends)
1981
1982    def post_getname(name, suffix):
1983        return '%s%s' % (name, suffix)
1984    def pre_getname(name, suffix):
1985        return '%s%s' % (suffix, name)
1986
1987    pkgs = {}
1988    for pkg in packages.split():
1989        for postfix in postfixes:
1990            if pkg.endswith(postfix):
1991                if not postfix in pkgs:
1992                    pkgs[postfix] = {}
1993                pkgs[postfix][pkg] = (pkg[:-len(postfix)], post_getname)
1994
1995        for prefix in prefixes:
1996            if pkg.startswith(prefix):
1997                if not prefix in pkgs:
1998                    pkgs[prefix] = {}
1999                pkgs[prefix][pkg] = (pkg[:-len(prefix)], pre_getname)
2000
2001    if "-dbg" in pkgs:
2002        pkglibdeps = read_libdep_files(d)
2003        pkglibdeplist = []
2004        for pkg in pkglibdeps:
2005            for k in pkglibdeps[pkg]:
2006                add_dep(pkglibdeplist, k)
2007        dbgdefaultdeps = ((d.getVar('DEPCHAIN_DBGDEFAULTDEPS') == '1') or (bb.data.inherits_class('packagegroup', d)))
2008
2009    for suffix in pkgs:
2010        for pkg in pkgs[suffix]:
2011            if d.getVarFlag('RRECOMMENDS:' + pkg, 'nodeprrecs'):
2012                continue
2013            (base, func) = pkgs[suffix][pkg]
2014            if suffix == "-dev":
2015                pkg_adddeprrecs(pkg, base, suffix, func, depends, d)
2016            elif suffix == "-dbg":
2017                if not dbgdefaultdeps:
2018                    pkg_addrrecs(pkg, base, suffix, func, pkglibdeplist, d)
2019                    continue
2020            if len(pkgs[suffix]) == 1:
2021                pkg_addrrecs(pkg, base, suffix, func, rdepends, d)
2022            else:
2023                rdeps = []
2024                for dep in bb.utils.explode_deps(d.getVar('RDEPENDS:' + base) or ""):
2025                    add_dep(rdeps, dep)
2026                pkg_addrrecs(pkg, base, suffix, func, rdeps, d)
2027