blob: 8eaf8b7a57591b95bf1b5b82cab14a6b35d294ad [file] [log] [blame]
Eric Fiselier9bf753c2015-03-20 22:09:291#!/usr/bin/env python
Tobias Hieta7bfaa0f2023-05-17 09:09:292# ===----------------------------------------------------------------------===##
Eric Fiseliereb2ff932016-01-19 21:58:493#
Chandler Carruth57b08b02019-01-19 10:56:404# 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 Fiseliereb2ff932016-01-19 21:58:497#
Tobias Hieta7bfaa0f2023-05-17 09:09:298# ===----------------------------------------------------------------------===##
Eric Fiselier9bf753c2015-03-20 22:09:299"""
10sym_diff - Compare two symbol lists and output the differences.
11"""
Eric Fiseliereb2ff932016-01-19 21:58:4912
Eric Fiselier9bf753c2015-03-20 22:09:2913from argparse import ArgumentParser
14import sys
Eric Fiselier0b37f202017-02-09 22:53:1415from libcxx.sym_check import diff, util
Eric Fiselier9bf753c2015-03-20 22:09:2916
17
18def main():
19 parser = ArgumentParser(
Tobias Hieta7bfaa0f2023-05-17 09:09:2920 description="Extract a list of symbols from a shared library."
21 )
Eric Fiselier9bf753c2015-03-20 22:09:2922 parser.add_argument(
Tobias Hieta7bfaa0f2023-05-17 09:09:2923 "--names-only",
24 dest="names_only",
25 help="Only print symbol names",
26 action="store_true",
27 default=False,
28 )
Eric Fiselier9bf753c2015-03-20 22:09:2929 parser.add_argument(
Tobias Hieta7bfaa0f2023-05-17 09:09:2930 "--removed-only",
31 dest="removed_only",
32 help="Only print removed symbols",
33 action="store_true",
34 default=False,
35 )
Eric Fiselierf8f31c42016-09-16 00:00:4836 parser.add_argument(
Tobias Hieta7bfaa0f2023-05-17 09:09:2937 "--only-stdlib-symbols",
38 dest="only_stdlib",
39 help="Filter all symbols not related to the stdlib",
40 action="store_true",
41 default=False,
42 )
Eric Fiselier9bf753c2015-03-20 22:09:2943 parser.add_argument(
Tobias Hieta7bfaa0f2023-05-17 09:09:2944 "--strict",
45 dest="strict",
46 help="Exit with a non-zero status if any symbols " "differ",
47 action="store_true",
48 default=False,
49 )
Eric Fiselier9bf753c2015-03-20 22:09:2950 parser.add_argument(
Tobias Hieta7bfaa0f2023-05-17 09:09:2951 "-o",
52 "--output",
53 dest="output",
54 help="The output file. stdout is used if not given",
55 type=str,
56 action="store",
57 default=None,
58 )
Eric Fiselier9bf753c2015-03-20 22:09:2959 parser.add_argument(
Tobias Hieta7bfaa0f2023-05-17 09:09:2960 "--demangle", dest="demangle", action="store_true", default=False
61 )
62 parser.add_argument(
63 "old_syms",
64 metavar="old-syms",
65 type=str,
66 help="The file containing the old symbol list or a library",
67 )
68 parser.add_argument(
69 "new_syms",
70 metavar="new-syms",
71 type=str,
72 help="The file containing the new symbol list or a library",
73 )
Eric Fiselier9bf753c2015-03-20 22:09:2974 args = parser.parse_args()
75
76 old_syms_list = util.extract_or_load(args.old_syms)
77 new_syms_list = util.extract_or_load(args.new_syms)
78
Eric Fiselier4b10f482016-11-18 01:40:2079 if args.only_stdlib:
80 old_syms_list, _ = util.filter_stdlib_symbols(old_syms_list)
81 new_syms_list, _ = util.filter_stdlib_symbols(new_syms_list)
82
Eric Fiselier9bf753c2015-03-20 22:09:2983 added, removed, changed = diff.diff(old_syms_list, new_syms_list)
Eric Fiselierf8f31c42016-09-16 00:00:4884 if args.removed_only:
85 added = {}
Eric Fiselier1da55f52017-01-18 00:05:0186 report, is_break, is_different = diff.report_diff(
Tobias Hieta7bfaa0f2023-05-17 09:09:2987 added, removed, changed, names_only=args.names_only, demangle=args.demangle
88 )
Eric Fiselier9bf753c2015-03-20 22:09:2989 if args.output is None:
90 print(report)
91 else:
Tobias Hieta7bfaa0f2023-05-17 09:09:2992 with open(args.output, "w") as f:
93 f.write(report + "\n")
Eric Fiselier1da55f52017-01-18 00:05:0194 exit_code = 1 if is_break or (args.strict and is_different) else 0
95 sys.exit(exit_code)
Eric Fiselier9bf753c2015-03-20 22:09:2996
Tobias Hieta7bfaa0f2023-05-17 09:09:2997
98if __name__ == "__main__":
Eric Fiselier9bf753c2015-03-20 22:09:2999 main()