blob: 987c207c1e41257e3f319ec445bffd428d0f29bc [file] [log] [blame]
Eric Fiselierfdea4b42015-03-20 22:09:29 +00001#!/usr/bin/env python
Eric Fiselier120ec472016-01-19 21:58:49 +00002#===----------------------------------------------------------------------===##
3#
Chandler Carruthd2012102019-01-19 10:56:40 +00004# 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 Fiselier120ec472016-01-19 21:58:49 +00007#
8#===----------------------------------------------------------------------===##
Eric Fiselierfdea4b42015-03-20 22:09:29 +00009"""
10sym_extract - Extract and output a list of symbols from a shared library.
11"""
12from argparse import ArgumentParser
Eric Fiselier569ea5b2017-02-09 22:53:14 +000013from libcxx.sym_check import extract, util
Eric Fiselierfdea4b42015-03-20 22:09:29 +000014
15
16def main():
17 parser = ArgumentParser(
18 description='Extract a list of symbols from a shared library.')
19 parser.add_argument('library', metavar='shared-lib', type=str,
20 help='The library to extract symbols from')
21 parser.add_argument('-o', '--output', dest='output',
22 help='The output file. stdout is used if not given',
23 type=str, action='store', default=None)
24 parser.add_argument('--names-only', dest='names_only',
25 help='Output only the name of the symbol',
26 action='store_true', default=False)
Eric Fiselierc10332f2016-11-18 01:40:20 +000027 parser.add_argument('--only-stdlib-symbols', dest='only_stdlib',
28 help="Filter all symbols not related to the stdlib",
29 action='store_true', default=False)
Eric Fiselier9a4994b2019-02-12 00:00:43 +000030 parser.add_argument('--defined-only', dest='defined_only',
31 help="Filter all symbols that are not defined",
32 action='store_true', default=False)
33 parser.add_argument('--undefined-only', dest='undefined_only',
34 help="Filter all symbols that are defined",
35 action='store_true', default=False)
36
Eric Fiselierfdea4b42015-03-20 22:09:29 +000037 args = parser.parse_args()
Eric Fiselier9a4994b2019-02-12 00:00:43 +000038 assert not (args.undefined_only and args.defined_only)
Eric Fiselierfdea4b42015-03-20 22:09:29 +000039 if args.output is not None:
40 print('Extracting symbols from %s to %s.'
41 % (args.library, args.output))
42 syms = extract.extract_symbols(args.library)
Eric Fiselierc10332f2016-11-18 01:40:20 +000043 if args.only_stdlib:
44 syms, other_syms = util.filter_stdlib_symbols(syms)
Eric Fiselier9a4994b2019-02-12 00:00:43 +000045 filter = lambda x: x
46 if args.defined_only:
47 filter = lambda l: list([x for x in l if x['is_defined']])
48 if args.undefined_only:
49 filter = lambda l: list([x for x in l if not x['is_defined']])
50 util.write_syms(syms, out=args.output, names_only=args.names_only, filter=filter)
Eric Fiselierfdea4b42015-03-20 22:09:29 +000051
52
53if __name__ == '__main__':
54 main()