1#!/usr/bin/env python
2#===----------------------------------------------------------------------===##
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8#===----------------------------------------------------------------------===##
9"""
10sym_diff - Compare two symbol lists and output the differences.
11"""
12
13from argparse import ArgumentParser
14import sys
15from libcxx.sym_check import diff, util
16
17
18def main():
19    parser = ArgumentParser(
20        description='Extract a list of symbols from a shared library.')
21    parser.add_argument(
22        '--names-only', dest='names_only',
23        help='Only print symbol names',
24        action='store_true', default=False)
25    parser.add_argument(
26        '--removed-only', dest='removed_only',
27        help='Only print removed symbols',
28        action='store_true', default=False)
29    parser.add_argument('--only-stdlib-symbols', dest='only_stdlib',
30                        help="Filter all symbols not related to the stdlib",
31                        action='store_true', default=False)
32    parser.add_argument('--strict', dest='strict',
33                        help="Exit with a non-zero status if any symbols "
34                             "differ",
35                        action='store_true', default=False)
36    parser.add_argument(
37        '-o', '--output', dest='output',
38        help='The output file. stdout is used if not given',
39        type=str, action='store', default=None)
40    parser.add_argument(
41        '--demangle', dest='demangle', action='store_true', default=False)
42    parser.add_argument(
43        'old_syms', metavar='old-syms', type=str,
44        help='The file containing the old symbol list or a library')
45    parser.add_argument(
46        'new_syms', metavar='new-syms', type=str,
47        help='The file containing the new symbol list or a library')
48    args = parser.parse_args()
49
50    old_syms_list = util.extract_or_load(args.old_syms)
51    new_syms_list = util.extract_or_load(args.new_syms)
52
53    if args.only_stdlib:
54        old_syms_list, _ = util.filter_stdlib_symbols(old_syms_list)
55        new_syms_list, _ = util.filter_stdlib_symbols(new_syms_list)
56
57    added, removed, changed = diff.diff(old_syms_list, new_syms_list)
58    if args.removed_only:
59        added = {}
60    report, is_break, is_different = diff.report_diff(
61        added, removed, changed, names_only=args.names_only,
62        demangle=args.demangle)
63    if args.output is None:
64        print(report)
65    else:
66        with open(args.output, 'w') as f:
67            f.write(report + '\n')
68    exit_code = 1 if is_break or (args.strict and is_different) else 0
69    sys.exit(exit_code)
70
71if __name__ == '__main__':
72    main()
73