summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/action/dumpsymbols.py
blob: 0af2c1c4e576bb558e643ff6f927839ac735dd6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import argparse
import os
import shutil
import subprocess
import sys

import buildconfig


def dump_symbols(target, tracking_file, count_ctors=False):
    # Our tracking file, if present, will contain path(s) to the previously generated
    # symbols. Remove them in this case so we don't simply accumulate old symbols
    # during incremental builds.
    if os.path.isfile(os.path.normpath(tracking_file)):
        with open(tracking_file, "r") as fh:
            files = fh.read().splitlines()
        dirs = set(os.path.dirname(f) for f in files)
        for d in dirs:
            shutil.rmtree(
                os.path.join(buildconfig.topobjdir, "dist", "crashreporter-symbols", d),
                ignore_errors=True,
            )

    # Build default args for symbolstore.py based on platform.
    sym_store_args = []

    dump_syms_bin = buildconfig.substs["DUMP_SYMS"]
    os_arch = buildconfig.substs["OS_ARCH"]
    if os_arch == "WINNT":
        sym_store_args.extend(["-c", "--vcs-info"])
        if "PDBSTR" in buildconfig.substs:
            sym_store_args.append("-i")
    elif os_arch == "Darwin":
        cpu = {
            "x86": "i386",
            "aarch64": "arm64",
        }.get(buildconfig.substs["TARGET_CPU"], buildconfig.substs["TARGET_CPU"])
        sym_store_args.extend(["-c", "-a", cpu, "--vcs-info"])
    elif os_arch == "Linux":
        sym_store_args.extend(["-c", "--vcs-info"])

    sym_store_args.append(
        "--install-manifest=%s,%s"
        % (
            os.path.join(
                buildconfig.topobjdir, "_build_manifests", "install", "dist_include"
            ),
            os.path.join(buildconfig.topobjdir, "dist", "include"),
        )
    )
    objcopy = buildconfig.substs.get("OBJCOPY")
    if objcopy:
        os.environ["OBJCOPY"] = objcopy

    if buildconfig.substs.get("MOZ_THUNDERBIRD"):
        sym_store_args.extend(["-s", os.path.join(buildconfig.topsrcdir, "comm")])

    args = (
        [
            sys.executable,
            os.path.join(
                buildconfig.topsrcdir,
                "toolkit",
                "crashreporter",
                "tools",
                "symbolstore.py",
            ),
        ]
        + sym_store_args
        + [
            "-s",
            buildconfig.topsrcdir,
            dump_syms_bin,
            os.path.join(buildconfig.topobjdir, "dist", "crashreporter-symbols"),
            os.path.abspath(target),
        ]
    )
    if count_ctors:
        args.append("--count-ctors")
    print("Running: %s" % " ".join(args))
    out_files = subprocess.check_output(args, universal_newlines=True)
    with open(tracking_file, "w", encoding="utf-8", newline="\n") as fh:
        fh.write(out_files)
        fh.flush()


def main(argv):
    parser = argparse.ArgumentParser(
        usage="Usage: dumpsymbols.py <library or program> <tracking file>"
    )
    parser.add_argument(
        "--count-ctors",
        action="store_true",
        default=False,
        help="Count static initializers",
    )
    parser.add_argument("library_or_program", help="Path to library or program")
    parser.add_argument("tracking_file", help="Tracking file")
    args = parser.parse_args()

    return dump_symbols(args.library_or_program, args.tracking_file, args.count_ctors)


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))