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 37 38def GetGnArgValueOrNone(arg): 39 if 'current' in arg: 40 return eval(arg['current']['value']) 41 if 'default' in arg: 42 return eval(arg['default']['value']) 43 return None 44 45 46def GetTargetOsForBuildDir(build_dir): 47 cmd = [GN_PATH, 'args', '--list', '--json', build_dir] 48 args = json.loads(subprocess.check_output(cmd)) 49 target_os = None 50 host_os = None 51 for arg in args: 52 if arg['name'] == 'target_os': 53 print(arg) 54 target_os = GetGnArgValueOrNone(arg) 55 if arg['name'] == 'host_os': 56 print(arg) 57 host_os = GetGnArgValueOrNone(arg) 58 return target_os or host_os or None 59 60 61def main(): 62 parser = argparse.ArgumentParser( 63 formatter_class=argparse.RawDescriptionHelpFormatter, description=__doc__) 64 parser.add_argument( 65 '-C', '--build-dir', metavar='DIR', help='build directory', required=True) 66 parser.add_argument('-o', '--output', help='output path', default=None) 67 parser.add_argument( 68 'binaries', metavar='BINARY', nargs='+', help='subjects of size report') 69 args = parser.parse_args() 70 71 if not os.path.exists(BLOATY_PATH): 72 print( 73 'Could not find bloaty at expected path "{}". Try re-running ./tools/install-build-deps' 74 .format(BLOATY_PATH)) 75 return 1 76 77 results = [] 78 79 out_directory = args.build_dir 80 target_os = GetTargetOsForBuildDir(out_directory) 81 print('target_os', target_os) 82 for binary in args.binaries: 83 binary_path = os.path.join(out_directory, binary) 84 output = '{} - {}\n'.format(binary, binary_path) 85 if target_os == 'mac': 86 subprocess.check_output(['dsymutil', binary_path]) 87 symbols = '--debug-file={}.dSYM/Contents/Resources/DWARF/{}'.format( 88 binary_path, binary) 89 cmd = [symbols, '-d', 'compileunits', '-n', '100', binary_path] 90 else: 91 cmd = ['-d', 'compileunits', '-n', '100', binary_path] 92 output += subprocess.check_output([BLOATY_PATH] + cmd).decode('utf-8') 93 results.append(output) 94 95 if args.output is None or args.output == '-': 96 out = sys.stdout 97 else: 98 out = open(args.output, 'w') 99 100 for result in results: 101 out.write(result) 102 out.write('\n') 103 return 0 104 105 106if __name__ == '__main__': 107 sys.exit(main()) 108