blob: c01f71c928d9fef5f78f676c7d690870d0168a49 [file] [log] [blame]
Eric Fiselier2d9feb52015-03-20 22:09:29 +00001#!/usr/bin/env python
Eric Fiseliera9e91f32016-01-19 21:58:49 +00002#===----------------------------------------------------------------------===##
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#===----------------------------------------------------------------------===##
Eric Fiselier2d9feb52015-03-20 22:09:29 +000010"""
11sym_diff - Compare two symbol lists and output the differences.
12"""
Eric Fiseliera9e91f32016-01-19 21:58:49 +000013
Eric Fiselier2d9feb52015-03-20 22:09:29 +000014from argparse import ArgumentParser
15import sys
Eric Fiselier11477532017-02-09 22:53:14 +000016from libcxx.sym_check import diff, util
Eric Fiselier2d9feb52015-03-20 22:09:29 +000017
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(
Eric Fiselier6dbed462016-09-16 00:00:48 +000027 '--removed-only', dest='removed_only',
28 help='Only print removed symbols',
29 action='store_true', default=False)
Eric Fiselier0d694182016-11-18 01:40:20 +000030 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)
Eric Fiselier08305aa2017-01-18 00:05:01 +000033 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)
Eric Fiselier6dbed462016-09-16 00:00:48 +000037 parser.add_argument(
Eric Fiselier2d9feb52015-03-20 22:09:29 +000038 '-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
Eric Fiselier0d694182016-11-18 01:40:20 +000054 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
Eric Fiselier2d9feb52015-03-20 22:09:29 +000058 added, removed, changed = diff.diff(old_syms_list, new_syms_list)
Eric Fiselier6dbed462016-09-16 00:00:48 +000059 if args.removed_only:
60 added = {}
Eric Fiselier08305aa2017-01-18 00:05:01 +000061 report, is_break, is_different = diff.report_diff(
62 added, removed, changed, names_only=args.names_only,
63 demangle=args.demangle)
Eric Fiselier2d9feb52015-03-20 22:09:29 +000064 if args.output is None:
65 print(report)
66 else:
67 with open(args.output, 'w') as f:
68 f.write(report + '\n')
Eric Fiselier08305aa2017-01-18 00:05:01 +000069 exit_code = 1 if is_break or (args.strict and is_different) else 0
70 sys.exit(exit_code)
Eric Fiselier2d9feb52015-03-20 22:09:29 +000071
72if __name__ == '__main__':
73 main()