xref: /aosp_15_r20/external/toolchain-utils/debug_info_test/debug_info_test.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2018 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Test for debug info."""
8
9
10import os
11import subprocess
12import sys
13
14import check_cus
15import check_exist
16import check_icf
17
18
19elf_checks = [
20    check_exist.check_exist_all,
21    check_cus.check_compile_units,
22    check_icf.check_identical_code_folding,
23]
24
25
26def scanelf(root):
27    """Find ELFs in root.
28
29    Args:
30      root: root dir to start with the search.
31
32    Returns:
33      Filenames of ELFs in root.
34    """
35    p = subprocess.Popen(
36        ["scanelf", "-y", "-B", "-F", "%F", "-R", root],
37        stdout=subprocess.PIPE,
38        encoding="utf-8",
39    )
40    return [l.strip() for l in p.stdout]
41
42
43def Main(argv):
44    if len(argv) < 2:
45        print("usage: %s [file|dir]")
46        return 1
47
48    files = []
49    cand = argv[1]
50    if os.path.isfile(cand):
51        files = [cand]
52    elif os.path.isdir(cand):
53        files = scanelf(cand)
54    else:
55        print("usage: %s [file|dir]")
56        return 1
57
58    failed = False
59    for f in files:
60        for c in elf_checks:
61            if not c(f):
62                failed = True
63
64    if failed:
65        return 1
66    return 0
67
68
69if __name__ == "__main__":
70    sys.exit(Main(sys.argv))
71