Eric Fiselier | 2d9feb5 | 2015-03-20 22:09:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Eric Fiselier | a9e91f3 | 2016-01-19 21:58:49 +0000 | [diff] [blame] | 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 | #===----------------------------------------------------------------------===## |
Eric Fiselier | 2d9feb5 | 2015-03-20 22:09:29 +0000 | [diff] [blame] | 10 | """ |
| 11 | sym_diff - Compare two symbol lists and output the differences. |
| 12 | """ |
Eric Fiselier | a9e91f3 | 2016-01-19 21:58:49 +0000 | [diff] [blame] | 13 | |
Eric Fiselier | 2d9feb5 | 2015-03-20 22:09:29 +0000 | [diff] [blame] | 14 | from argparse import ArgumentParser |
| 15 | import sys |
Eric Fiselier | 1147753 | 2017-02-09 22:53:14 +0000 | [diff] [blame] | 16 | from libcxx.sym_check import diff, util |
Eric Fiselier | 2d9feb5 | 2015-03-20 22:09:29 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | def 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 Fiselier | 6dbed46 | 2016-09-16 00:00:48 +0000 | [diff] [blame] | 27 | '--removed-only', dest='removed_only', |
| 28 | help='Only print removed symbols', |
| 29 | action='store_true', default=False) |
Eric Fiselier | 0d69418 | 2016-11-18 01:40:20 +0000 | [diff] [blame] | 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) |
Eric Fiselier | 08305aa | 2017-01-18 00:05:01 +0000 | [diff] [blame] | 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) |
Eric Fiselier | 6dbed46 | 2016-09-16 00:00:48 +0000 | [diff] [blame] | 37 | parser.add_argument( |
Eric Fiselier | 2d9feb5 | 2015-03-20 22:09:29 +0000 | [diff] [blame] | 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 | |
Eric Fiselier | 0d69418 | 2016-11-18 01:40:20 +0000 | [diff] [blame] | 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 | |
Eric Fiselier | 2d9feb5 | 2015-03-20 22:09:29 +0000 | [diff] [blame] | 58 | added, removed, changed = diff.diff(old_syms_list, new_syms_list) |
Eric Fiselier | 6dbed46 | 2016-09-16 00:00:48 +0000 | [diff] [blame] | 59 | if args.removed_only: |
| 60 | added = {} |
Eric Fiselier | 08305aa | 2017-01-18 00:05:01 +0000 | [diff] [blame] | 61 | report, is_break, is_different = diff.report_diff( |
| 62 | added, removed, changed, names_only=args.names_only, |
| 63 | demangle=args.demangle) |
Eric Fiselier | 2d9feb5 | 2015-03-20 22:09:29 +0000 | [diff] [blame] | 64 | if args.output is None: |
| 65 | print(report) |
| 66 | else: |
| 67 | with open(args.output, 'w') as f: |
| 68 | f.write(report + '\n') |
Eric Fiselier | 08305aa | 2017-01-18 00:05:01 +0000 | [diff] [blame] | 69 | exit_code = 1 if is_break or (args.strict and is_different) else 0 |
| 70 | sys.exit(exit_code) |
Eric Fiselier | 2d9feb5 | 2015-03-20 22:09:29 +0000 | [diff] [blame] | 71 | |
| 72 | if __name__ == '__main__': |
| 73 | main() |