sym_diff.py 2.7 KB

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