sym_extract.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_extract - Extract and output a list of symbols from a shared library.
  11. """
  12. from argparse import ArgumentParser
  13. from libcxx.sym_check import extract, util
  14. def main():
  15. parser = ArgumentParser(
  16. description='Extract a list of symbols from a shared library.')
  17. parser.add_argument('library', metavar='shared-lib', type=str,
  18. help='The library to extract symbols from')
  19. parser.add_argument('-o', '--output', dest='output',
  20. help='The output file. stdout is used if not given',
  21. type=str, action='store', default=None)
  22. parser.add_argument('--names-only', dest='names_only',
  23. help='Output only the name of the symbol',
  24. action='store_true', default=False)
  25. parser.add_argument('--only-stdlib-symbols', dest='only_stdlib',
  26. help="Filter all symbols not related to the stdlib",
  27. action='store_true', default=False)
  28. parser.add_argument('--defined-only', dest='defined_only',
  29. help="Filter all symbols that are not defined",
  30. action='store_true', default=False)
  31. parser.add_argument('--undefined-only', dest='undefined_only',
  32. help="Filter all symbols that are defined",
  33. action='store_true', default=False)
  34. args = parser.parse_args()
  35. assert not (args.undefined_only and args.defined_only)
  36. if args.output is not None:
  37. print('Extracting symbols from %s to %s.'
  38. % (args.library, args.output))
  39. syms = extract.extract_symbols(args.library)
  40. if args.only_stdlib:
  41. syms, other_syms = util.filter_stdlib_symbols(syms)
  42. filter = lambda x: x
  43. if args.defined_only:
  44. filter = lambda l: list([x for x in l if x['is_defined']])
  45. if args.undefined_only:
  46. filter = lambda l: list([x for x in l if not x['is_defined']])
  47. util.write_syms(syms, out=args.output, names_only=args.names_only, filter=filter)
  48. if __name__ == '__main__':
  49. main()