1#!/usr/bin/python 2# Copyright 2018 gRPC authors. 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 16# This script analyzes link map file generated by Xcode. It calculates and 17# prints out the sizes of each dependent library and the total sizes of the 18# symbols. 19# The script takes one parameter, which is the path to the link map file. 20 21import sys 22import re 23 24 25def parse_link_map(filename): 26 table_tag = {} 27 state = "start" 28 29 table_stats_symbol = {} 30 table_stats_dead = {} 31 section_total_size = 0 32 symbol_total_size = 0 33 34 boringssl_size = 0 35 core_size = 0 36 objc_size = 0 37 protobuf_size = 0 38 39 lines = list(open(filename)) 40 for line in lines: 41 line_stripped = line[:-1] 42 if "# Object files:" == line_stripped: 43 state = "object" 44 continue 45 elif "# Sections:" == line_stripped: 46 state = "section" 47 continue 48 elif "# Symbols:" == line_stripped: 49 state = "symbol" 50 continue 51 elif "# Dead Stripped Symbols:" == line_stripped: 52 state = "dead" 53 continue 54 55 if state == "object": 56 segs = re.search('(\[ *[0-9]*\]) (.*)', line_stripped) 57 table_tag[segs.group(1)] = segs.group(2) 58 59 if state == "section": 60 if len(line_stripped) == 0 or line_stripped[0] == '#': 61 continue 62 segs = re.search('^(.+?)\s+(.+?)\s+.*', line_stripped) 63 section_total_size += int(segs.group(2), 16) 64 65 if state == "symbol": 66 if len(line_stripped) == 0 or line_stripped[0] == '#': 67 continue 68 segs = re.search('^.+?\s+(.+?)\s+(\[.+?\]).*', line_stripped) 69 target = table_tag[segs.group(2)] 70 target_stripped = re.search('^(.*?)(\(.+?\))?$', target).group(1) 71 size = int(segs.group(1), 16) 72 if not target_stripped in table_stats_symbol: 73 table_stats_symbol[target_stripped] = 0 74 table_stats_symbol[target_stripped] += size 75 if 'BoringSSL' in target_stripped: 76 boringssl_size += size 77 elif 'libgRPC-Core' in target_stripped: 78 core_size += size 79 elif 'libgRPC-RxLibrary' in target_stripped or \ 80 'libgRPC' in target_stripped or \ 81 'libgRPC-ProtoLibrary' in target_stripped: 82 objc_size += size 83 elif 'libProtobuf' in target_stripped: 84 protobuf_size += size 85 86 for target in table_stats_symbol: 87 symbol_total_size += table_stats_symbol[target] 88 89 return core_size, objc_size, boringssl_size, protobuf_size, symbol_total_size 90 91 92def main(): 93 filename = sys.argv[1] 94 core_size, objc_size, boringssl_size, protobuf_size, total_size = parse_link_map( 95 filename) 96 print('Core size:{:,}'.format(core_size)) 97 print('ObjC size:{:,}'.format(objc_size)) 98 print('BoringSSL size:{:,}'.format(boringssl_size)) 99 print('Protobuf size:{:,}\n'.format(protobuf_size)) 100 print('Total size:{:,}'.format(total_size)) 101 102 103if __name__ == "__main__": 104 main() 105