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