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