1#!/usr/bin/env python3 2# Copyright (C) 2021 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15""" 16Generate a size report for target binaries. 17 18For example: 19$ tools/ninja -C out/r traced_probes traced 20$ tools/size-report.py -C out/r traced_probes traced 21""" 22 23from __future__ import absolute_import 24from __future__ import division 25from __future__ import print_function 26 27import argparse 28import os 29import subprocess 30import sys 31import json 32 33ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 34BLOATY_PATH = os.path.join(ROOT_DIR, 'buildtools', 'bloaty', 'bloaty') 35GN_PATH = os.path.join(ROOT_DIR, 'tools', 'gn') 36 37def GetGnArgValueOrNone(arg): 38 if 'current' in arg: 39 return eval(arg['current']['value']) 40 if 'default' in arg: 41 return eval(arg['default']['value']) 42 return None 43 44 45def GetTargetOsForBuildDir(build_dir): 46 cmd = [GN_PATH, 'args', '--list', '--json', build_dir] 47 args = json.loads(subprocess.check_output(cmd)) 48 target_os = None 49 host_os = None 50 for arg in args: 51 if arg['name'] == 'target_os': 52 print(arg) 53 target_os = GetGnArgValueOrNone(arg) 54 if arg['name'] == 'host_os': 55 print(arg) 56 host_os = GetGnArgValueOrNone(arg) 57 return target_os or host_os or None 58 59 60def main(): 61 parser = argparse.ArgumentParser( 62 formatter_class=argparse.RawDescriptionHelpFormatter, description=__doc__) 63 parser.add_argument( 64 '-C', '--build-dir', metavar='DIR', help='build directory', required=True) 65 parser.add_argument('-o', '--output', help='output path', default=None) 66 parser.add_argument( 67 'binaries', metavar='BINARY', nargs='+', help='subjects of size report') 68 args = parser.parse_args() 69 70 if not os.path.exists(BLOATY_PATH): 71 print( 72 'Could not find bloaty at expected path "{}". Try re-running ./tools/install-build-deps' 73 .format(BLOATY_PATH)) 74 return 1 75 76 results = [] 77 78 out_directory = args.build_dir 79 target_os = GetTargetOsForBuildDir(out_directory) 80 print('target_os', target_os) 81 for binary in args.binaries: 82 binary_path = os.path.join(out_directory, binary) 83 output = '{} - {}\n'.format(binary, binary_path) 84 if target_os == 'mac': 85 subprocess.check_output(['dsymutil', binary_path]) 86 symbols = '--debug-file={}.dSYM/Contents/Resources/DWARF/{}'.format( 87 binary_path, binary) 88 cmd = [symbols, '-d', 'compileunits', '-n', '100', binary_path] 89 else: 90 cmd = ['-d', 'compileunits', '-n', '100', binary_path] 91 output += subprocess.check_output([BLOATY_PATH] + cmd).decode('utf-8') 92 results.append(output) 93 94 if args.output is None or args.output == '-': 95 out = sys.stdout 96 else: 97 out = open(args.output, 'w') 98 99 for result in results: 100 out.write(result) 101 out.write('\n') 102 return 0 103 104 105if __name__ == '__main__': 106 sys.exit(main()) 107