Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Serge Guelton | 1a12dd7 | 2019-03-26 14:46:15 | [diff] [blame] | 3 | import subprocess |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 4 | import optparse |
| 5 | import os |
| 6 | import os.path |
| 7 | import re |
| 8 | import sys |
| 9 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 10 | |
| 11 | def extract_exe_symbol_names(arch, exe_path, match_str): |
| 12 | command = 'dsymutil --arch %s -s "%s" | grep "%s" | colrm 1 69' % ( |
| 13 | arch, exe_path, match_str) |
Serge Guelton | 1a12dd7 | 2019-03-26 14:46:15 | [diff] [blame] | 14 | (command_exit_status, command_output) = subprocess.getstatusoutput(command) |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 15 | if command_exit_status == 0: |
| 16 | if command_output: |
| 17 | return command_output[0:-1].split("'\n") |
| 18 | else: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 19 | print('error: command returned no output') |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 20 | else: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 21 | print('error: command failed with exit status %i\n command: %s' % (command_exit_status, command)) |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 22 | return list() |
| 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 24 | |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 25 | def verify_api(all_args): |
| 26 | '''Verify the API in the specified library is valid given one or more binaries.''' |
| 27 | usage = "usage: verify_api --library <path> [ --library <path> ...] executable1 [executable2 ...]" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 28 | description = '''Verify the API in the specified library is valid given one or more binaries. |
| 29 | |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 30 | Example: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 31 | |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 32 | verify_api.py --library ~/Documents/src/lldb/build/Debug/LLDB.framework/LLDB --arch x86_64 /Applications/Xcode.app/Contents/PlugIns/DebuggerLLDB.ideplugin/Contents/MacOS/DebuggerLLDB --api-regex lldb |
| 33 | ''' |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 34 | parser = optparse.OptionParser( |
| 35 | description=description, |
| 36 | prog='verify_api', |
| 37 | usage=usage) |
| 38 | parser.add_option( |
| 39 | '-v', |
| 40 | '--verbose', |
| 41 | action='store_true', |
| 42 | dest='verbose', |
| 43 | help='display verbose debug info', |
| 44 | default=False) |
| 45 | parser.add_option( |
| 46 | '-a', |
| 47 | '--arch', |
| 48 | type='string', |
| 49 | action='append', |
| 50 | dest='archs', |
Kazuaki Ishizaki | e9264b7 | 2020-04-06 16:06:02 | [diff] [blame] | 51 | help='architecture to use when checking the api') |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 52 | parser.add_option( |
| 53 | '-r', |
| 54 | '--api-regex', |
| 55 | type='string', |
| 56 | dest='api_regex_str', |
| 57 | help='Exclude any undefined symbols that do not match this regular expression when searching for missing APIs.') |
| 58 | parser.add_option( |
| 59 | '-l', |
| 60 | '--library', |
| 61 | type='string', |
| 62 | action='append', |
| 63 | dest='libraries', |
| 64 | help='Specify one or more libraries that will contain all needed APIs for the executables.') |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 65 | (options, args) = parser.parse_args(all_args) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 66 | |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 67 | api_external_symbols = list() |
| 68 | if options.archs: |
| 69 | for arch in options.archs: |
| 70 | for library in options.libraries: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 71 | external_symbols = extract_exe_symbol_names( |
| 72 | arch, library, "( SECT EXT)") |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 73 | if external_symbols: |
| 74 | for external_symbol in external_symbols: |
| 75 | api_external_symbols.append(external_symbol) |
| 76 | else: |
| 77 | sys.exit(1) |
| 78 | else: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 79 | print('error: must specify one or more architectures with the --arch option') |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 80 | sys.exit(4) |
| 81 | if options.verbose: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 82 | print("API symbols:") |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 83 | for (i, external_symbol) in enumerate(api_external_symbols): |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 84 | print("[%u] %s" % (i, external_symbol)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 85 | |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 86 | api_regex = None |
| 87 | if options.api_regex_str: |
| 88 | api_regex = re.compile(options.api_regex_str) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 89 | |
| 90 | for arch in options.archs: |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 91 | for exe_path in args: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 92 | print('Verifying (%s) "%s"...' % (arch, exe_path)) |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 93 | exe_errors = 0 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 94 | undefined_symbols = extract_exe_symbol_names( |
| 95 | arch, exe_path, "( UNDF EXT)") |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 96 | for undefined_symbol in undefined_symbols: |
| 97 | if api_regex: |
| 98 | match = api_regex.search(undefined_symbol) |
| 99 | if not match: |
| 100 | if options.verbose: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 101 | print('ignoring symbol: %s' % (undefined_symbol)) |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 102 | continue |
| 103 | if undefined_symbol in api_external_symbols: |
| 104 | if options.verbose: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 105 | print('verified symbol: %s' % (undefined_symbol)) |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 106 | else: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 107 | print('missing symbol: %s' % (undefined_symbol)) |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 108 | exe_errors += 1 |
| 109 | if exe_errors: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 110 | print('error: missing %u API symbols from %s' % (exe_errors, options.libraries)) |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 111 | else: |
Serge Guelton | 525cd59 | 2019-03-21 18:27:40 | [diff] [blame] | 112 | print('success') |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 113 | |
Greg Clayton | 0274766 | 2012-08-30 21:21:24 | [diff] [blame] | 114 | if __name__ == '__main__': |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 115 | verify_api(sys.argv[1:]) |