idf_size.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. #!/usr/bin/env python
  2. #
  3. # esp-idf alternative to "size" to print ELF file sizes, also analyzes
  4. # the linker map file to dump higher resolution details.
  5. #
  6. # Includes information which is not shown in "xtensa-esp32-elf-size",
  7. # or easy to parse from "xtensa-esp32-elf-objdump" or raw map files.
  8. #
  9. # SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  10. # SPDX-License-Identifier: Apache-2.0
  11. #
  12. from __future__ import division, print_function, unicode_literals
  13. import argparse
  14. import collections
  15. import json
  16. import os.path
  17. import re
  18. import sys
  19. from typing import Any, Callable, Collection, Dict, Iterable, List, Optional, TextIO, Tuple, Union
  20. import yaml
  21. from future.utils import iteritems
  22. Section = Dict[str, Union[str, int]]
  23. SectionDict = Dict[str, Section]
  24. try:
  25. basestring
  26. except NameError:
  27. basestring = str
  28. GLOBAL_JSON_INDENT = 4
  29. GLOBAL_JSON_SEPARATORS = (',', ': ')
  30. class MemRegions(object):
  31. """
  32. Regions determined by the chip target.
  33. """
  34. # DIRAM is not added here. The DIRAM is indicated by the `secondary_addr` of each MemRegDef
  35. (DRAM_ID, IRAM_ID, CACHE_D_ID, CACHE_I_ID, RTC_FAST_D_ID, RTC_FAST_I_ID, RTC_SLOW_D_ID) = range(7)
  36. # The order of variables in the tuple is the same as in the soc_memory_layout.c files
  37. MemRegDef = collections.namedtuple('MemRegDef', ['primary_addr', 'length', 'type', 'secondary_addr'])
  38. class Region(object):
  39. # Helper class to store region information
  40. def __init__(self, start: int, length: int, region: 'MemRegions.MemRegDef', section: Optional[str]=None) -> None:
  41. self.start = start
  42. self.len = length
  43. self.region = region
  44. self.section = section
  45. @staticmethod
  46. def get_mem_regions(target: str) -> List:
  47. """
  48. Get memory regions for specific target
  49. """
  50. # The target specific memory structure is deduced from soc_memory_types defined in
  51. # $IDF_PATH/components/soc/**/soc_memory_layout.c files.
  52. MemRegDef = MemRegions.MemRegDef
  53. def change_to_proper_format(length: Union[str, bytes]) -> Any:
  54. '''
  55. Change `length` if it is string like `'0x8000 + 6 * 0x10000'` to resolve of this math equation
  56. or if `length` is number function return it without changing.
  57. '''
  58. try:
  59. return eval(length)
  60. except TypeError:
  61. return length
  62. def get_mem_reg_def(chip_info: Dict, memory_reg: str) -> Tuple:
  63. chip_info[memory_reg]['secondary_address'] = chip_info[memory_reg].get('secondary_address') or 0
  64. return MemRegDef(chip_info[memory_reg]['primary_address'], change_to_proper_format(chip_info[memory_reg]['length']),
  65. getattr(MemRegions, memory_reg.strip('_12') + '_ID'), chip_info[memory_reg]['secondary_address'])
  66. try:
  67. with open(os.path.join(os.path.dirname(__file__), 'idf_size_yaml', target + '_data_info.yaml'), 'r') as stream:
  68. chip_info = (yaml.safe_load(stream))
  69. except FileNotFoundError:
  70. raise RuntimeError('Target not detected.')
  71. return sorted([get_mem_reg_def(chip_info, item) for item in chip_info])
  72. def __init__(self, target: str) -> None:
  73. self.chip_mem_regions = self.get_mem_regions(target)
  74. if not self.chip_mem_regions:
  75. raise RuntimeError('Target {} is not implemented in idf_size'.format(target))
  76. def _get_first_region(self, start: int, length: int) -> Tuple[Union['MemRegions.MemRegDef', None], int]:
  77. for region in self.chip_mem_regions: # type: ignore
  78. if region.primary_addr <= start < region.primary_addr + region.length:
  79. return (region, length)
  80. if region.secondary_addr and region.secondary_addr <= start < region.secondary_addr + region.length:
  81. return (region, length)
  82. print('WARNING: Given section not found in any memory region.')
  83. print('Check whether the LD file is compatible with the definitions in get_mem_regions in idf_size.py')
  84. return (None, length)
  85. def _get_regions(self, start: int, length: int, name: Optional[str]=None) -> List:
  86. ret = []
  87. while length > 0:
  88. (region, cur_len) = self._get_first_region(start, length)
  89. if region is None:
  90. # skip regions that not in given section
  91. length -= cur_len
  92. start += cur_len
  93. continue
  94. ret.append(MemRegions.Region(start, cur_len, region, name))
  95. length -= cur_len
  96. start += cur_len
  97. return ret
  98. def fit_segments_into_regions(self, segments: Dict) -> List:
  99. region_list = []
  100. for segment in segments.values():
  101. sorted_segments = self._get_regions(segment['origin'], segment['length'])
  102. region_list.extend(sorted_segments)
  103. return region_list
  104. def fit_sections_into_regions(self, sections: Dict) -> List:
  105. region_list = []
  106. for section in sections.values():
  107. sorted_sections = self._get_regions(section['address'], section['size'], section['name'])
  108. region_list.extend(sorted_sections)
  109. return region_list
  110. class LinkingSections(object):
  111. _section_type_dict = {key: re.compile(value) for key, value in {
  112. 'text': r'.*\.text',
  113. 'data': r'.*\.data',
  114. 'bss': r'.*\.bss',
  115. 'rodata': r'.*\.rodata',
  116. 'noinit': r'.*noinit',
  117. 'vectors': r'.*\.vectors',
  118. 'flash': r'.*flash.*',
  119. }.items()}
  120. @staticmethod
  121. def in_section(section: str, section_name_or_list: Union[str, Iterable]) -> bool:
  122. """
  123. Check if section in section_name_or_list
  124. """
  125. if isinstance(section_name_or_list, basestring):
  126. section_name_or_list = [section_name_or_list]
  127. for section_name in section_name_or_list:
  128. if LinkingSections._section_type_dict[section_name].match(section):
  129. return True
  130. return False
  131. @staticmethod
  132. def filter_sections(sections: Dict) -> Dict:
  133. return {key: v for key, v in sections.items()
  134. if LinkingSections.in_section(key, LinkingSections._section_type_dict.keys())}
  135. @staticmethod
  136. def get_display_name_order(section_name_list: List[str]) -> Tuple[List[str], List[str]]:
  137. '''
  138. Return two lists, in the suggested display order.
  139. First list is the reordered section_name_list, second list is the suggested display name, corresponding to the first list
  140. '''
  141. def get_memory_name(split_name: List) -> Tuple[str, str]:
  142. memory_name = f'.{split_name[1]}'
  143. display_name = section
  144. for seg_name in ['iram','dram','flash']:
  145. if seg_name in split_name[1]:
  146. memory_name = f'.{seg_name}'
  147. seg_name = seg_name.upper() if seg_name != 'flash' else seg_name.capitalize()
  148. display_name = seg_name + ('' if seg_name != 'IRAM' else split_name[1].replace('iram', '')) + f' .{split_name[2]}'
  149. return memory_name, display_name
  150. ordered_name_list = sorted(section_name_list)
  151. display_name_list = ordered_name_list.copy()
  152. memory_name = ''
  153. ordered_name_list = sort_dict(ordered_name_list)
  154. for i, section in enumerate(ordered_name_list):
  155. if memory_name and section.startswith(memory_name):
  156. # If the section has same memory type with the previous one, use shorter name
  157. display_name_list[i] = section.replace(memory_name, '& ')
  158. continue
  159. memory_name = ''
  160. split_name = section.split('.')
  161. if len(split_name) > 1:
  162. # If the section has a memory type, update the type and try to display the type properly
  163. assert len(split_name) == 3 and split_name[0] == '', 'Unexpected section name'
  164. memory_name, display_name_list[i] = get_memory_name(split_name)
  165. continue
  166. # Otherwise use its original name
  167. display_name_list[i] = section
  168. return ordered_name_list, display_name_list
  169. def scan_to_header(file: Iterable, header_line: str) -> None:
  170. """ Scan forward in a file until you reach 'header_line', then return """
  171. for line in file:
  172. if line.strip() == header_line:
  173. return
  174. raise RuntimeError("Didn't find line '%s' in file" % header_line)
  175. def format_json(json_object: Dict) -> str:
  176. return json.dumps(json_object,
  177. allow_nan=True,
  178. indent=GLOBAL_JSON_INDENT,
  179. separators=GLOBAL_JSON_SEPARATORS) + os.linesep
  180. def load_map_data(map_file: TextIO) -> Tuple[str, Dict, Dict]:
  181. segments = load_segments(map_file)
  182. detected_chip = detect_target_chip(map_file)
  183. sections = load_sections(map_file)
  184. # Exclude the .dummy section, which usually means shared region among I/D buses
  185. dummy_keys = [key for key in sections if key.endswith(('.dummy'))]
  186. if dummy_keys:
  187. sections.pop(*dummy_keys)
  188. return detected_chip, segments, sections
  189. def load_segments(map_file: TextIO) -> Dict:
  190. """ Memory Configuration section is the total size of each segment """
  191. result = {} # type: Dict[Any, Dict]
  192. scan_to_header(map_file, 'Memory Configuration')
  193. RE_MEMORY_SECTION = re.compile(r'(?P<name>[^ ]+) +0x(?P<origin>[\da-f]+) +0x(?P<length>[\da-f]+)')
  194. for line in map_file:
  195. match_section = RE_MEMORY_SECTION.match(line)
  196. if match_section is None:
  197. if len(result) == 0:
  198. continue # whitespace or a header, before the content we want
  199. else:
  200. return result # we're at the end of the Memory Configuration
  201. segment = {
  202. 'name': match_section.group('name'),
  203. 'origin': int(match_section.group('origin'), 16),
  204. 'length': int(match_section.group('length'), 16),
  205. }
  206. if segment['name'] != '*default*':
  207. result[segment['name']] = segment
  208. raise RuntimeError('End of file while scanning memory configuration?')
  209. def detect_target_chip(map_file: Iterable) -> str:
  210. ''' Detect target chip based on the target archive name in the linker script part of the MAP file '''
  211. scan_to_header(map_file, 'Linker script and memory map')
  212. RE_TARGET = re.compile(r'project_elf_src_(.*)\.c.obj')
  213. # For back-compatible with make
  214. RE_TARGET_MAKE = re.compile(r'^LOAD .*?/xtensa-([^-]+)-elf/')
  215. for line in map_file:
  216. match_target = RE_TARGET.search(line)
  217. if match_target:
  218. return match_target.group(1)
  219. match_target = RE_TARGET_MAKE.search(line)
  220. if match_target:
  221. return match_target.group(1)
  222. line = line.strip()
  223. # There could be empty line(s) between the "Linker script and memory map" header and "LOAD lines". Therefore,
  224. # line stripping and length is checked as well. The "LOAD lines" are between START GROUP and END GROUP for
  225. # older MAP files.
  226. if not line.startswith(('LOAD', 'START GROUP', 'END GROUP')) and len(line) > 0:
  227. # This break is a failsafe to not process anything load_sections() might want to analyze.
  228. break
  229. raise RuntimeError('Target not detected')
  230. def load_sections(map_file: TextIO) -> Dict:
  231. """ Load section size information from the MAP file.
  232. Returns a dict of 'sections', where each key is a section name and the value
  233. is a dict with details about this section, including a "sources" key which holds a list of source file line
  234. information for each symbol linked into the section.
  235. There are two kinds of lines:
  236. - symbol_only: [optional space]<sym_name>
  237. - full line: [optional space][optional sym_name] <address> <size> [optional file_info]
  238. If <sym_name> doesn't exist, ues the symbol name from the symbol_only line above
  239. If the line is the starting of a section, the <file> should be empty, otherwise if the line is for a source
  240. line, the <file> must exist, or the <sym_name> should be is no *fill*. This rule is used to tell sections from
  241. source lines.
  242. """
  243. # Check for lines which only contain the sym name (and rest is on following lines)
  244. RE_SYMBOL_ONLY_LINE = re.compile(r'^\s*(?P<sym_name>\S*)$')
  245. # Fast check to see if line is a potential source line before running the slower full regex against it
  246. RE_PRE_FILTER = re.compile(r'.*0x[\da-f]+\s*0x[\da-f]+.*')
  247. # source file line, ie
  248. # 0x0000000040080400 0xa4 /home/gus/esp/32/idf/examples/get-started/hello_world/build/esp32/libesp32.a(cpu_start.o)
  249. # cmake build system links some object files directly, not part of any archive, so make that part optional
  250. # .xtensa.info 0x0000000000000000 0x38 CMakeFiles/hello_world.elf.dir/project_elf_src.c.obj
  251. # *fill* 0x00000000400e2967 0x1
  252. RE_FULL_LINE = re.compile(r'\s*(?P<sym_name>\S*) +0x(?P<address>[\da-f]+) +0x(?P<size>[\da-f]+)\s*(?P<file>.*)$')
  253. # Extract archive and object_file from the file_info field
  254. RE_FILE = re.compile(r'((?P<archive>[^ ]+\.a)?\(?(?P<object_file>[^ ]+\.(o|obj))\)?)')
  255. def dump_src_line(src: Dict) -> str:
  256. return '%s(%s) addr: 0x%08x, size: 0x%x+%d' % (src['sym_name'], src['file'], src['address'], src['size'], src['fill'])
  257. sections = {} # type: Dict[Any, Dict]
  258. section = {} # type: Dict[str, Any]
  259. sym_backup = ''
  260. for line in map_file:
  261. if line.strip() == 'Cross Reference Table':
  262. # Stop processing lines because we are at the next section in the map file
  263. break
  264. match_line = RE_SYMBOL_ONLY_LINE.match(line)
  265. if match_line:
  266. # In some cases the section name appears on the previous line, back it up in here
  267. sym_backup = match_line.group('sym_name')
  268. continue
  269. if not RE_PRE_FILTER.match(line):
  270. # Line does not match our quick check, so skip to next line
  271. continue
  272. match_line = RE_FULL_LINE.match(line)
  273. if not match_line:
  274. assert not sym_backup, 'Symbol only line must be followed by a line with address and size'
  275. continue
  276. name = match_line.group('sym_name') if match_line.group('sym_name') else sym_backup
  277. sym_backup = ''
  278. is_section = not match_line.group('file') and name != '*fill*'
  279. if is_section:
  280. # section
  281. section = {
  282. 'name': name,
  283. 'address': int(match_line.group('address'), 16),
  284. 'size': int(match_line.group('size'), 16),
  285. 'sources': [],
  286. }
  287. sections[name] = section
  288. else:
  289. # symbol
  290. if not section:
  291. continue
  292. # There are some source lines in rodata section doesn't actually take any space, but have size
  293. # Make size of those sections zero
  294. srcs = section['sources'] # type: List[Dict]
  295. if srcs:
  296. last_src = srcs[-1]
  297. if last_src['size'] > 0 and last_src['address'] == int(match_line.group('address'), 16):
  298. if '.comment' != section['name'] and '.debug_str' != section['name'] and\
  299. 'rodata' not in last_src['sym_name']:
  300. raise RuntimeError('Due to overlap with following lines, size of the line set to 0:\n %s' % dump_src_line(last_src))
  301. last_src['size'] = 0
  302. # Count the padding size into the last valid (size > 0) source in the section
  303. if name == '*fill*':
  304. for src in reversed(srcs):
  305. if src['size'] > 0:
  306. src['fill'] += int(match_line.group('size'), 16)
  307. break
  308. continue
  309. # Extract archive and file information
  310. match_arch_and_file = RE_FILE.match(match_line.group('file'))
  311. assert match_arch_and_file
  312. archive = match_arch_and_file.group('archive')
  313. if archive is None:
  314. # optional named group "archive" was not matched, so assign a value to it
  315. archive = '(exe)'
  316. file = match_arch_and_file.group('object_file')
  317. assert name
  318. source = {
  319. 'size': int(match_line.group('size'), 16),
  320. 'address': int(match_line.group('address'), 16),
  321. 'archive': os.path.basename(archive),
  322. 'object_file': os.path.basename(file),
  323. 'sym_name': name,
  324. 'fill': 0, # padding size ofter the source
  325. }
  326. source['file'] = '%s:%s' % (source['archive'], source['object_file'])
  327. section['sources'].append(source) # type: ignore
  328. # Validate the map file
  329. for section in sections.values():
  330. src_curr = {} # type: Dict[str, Any]
  331. for src in section['sources']:
  332. if src['size'] == 0:
  333. continue
  334. expected_addr = src_curr['address'] + src_curr['size'] + src_curr['fill'] if src_curr else section['sources'][0]['address']
  335. if src['address'] != expected_addr:
  336. print('Warning: source line overlap:')
  337. print(' ' + dump_src_line(src_curr))
  338. print(' ' + dump_src_line(src))
  339. src_curr = src
  340. return sections
  341. def check_target(target: str, map_file: TextIO) -> None:
  342. if target is None:
  343. raise RuntimeError('The target chip cannot be detected for {}. '
  344. 'Please report the issue.'.format(map_file.name))
  345. def main() -> None:
  346. parser = argparse.ArgumentParser(description='idf_size - a tool to print size information from an IDF MAP file')
  347. parser.add_argument(
  348. '--json',
  349. help='Output results as JSON',
  350. action='store_true')
  351. parser.add_argument(
  352. 'map_file', help='MAP file produced by linker',
  353. type=argparse.FileType('r'))
  354. parser.add_argument(
  355. '--archives', help='Print per-archive sizes', action='store_true')
  356. parser.add_argument(
  357. '--archive_details', help='Print detailed symbols per archive')
  358. parser.add_argument(
  359. '--files', help='Print per-file sizes', action='store_true')
  360. parser.add_argument(
  361. '--target', help='Set target chip', default=None)
  362. parser.add_argument(
  363. '--diff', help='Show the differences in comparison with another MAP file',
  364. metavar='ANOTHER_MAP_FILE',
  365. default=None,
  366. dest='another_map_file')
  367. parser.add_argument(
  368. '-o',
  369. '--output-file',
  370. type=argparse.FileType('w'),
  371. default=sys.stdout,
  372. help='Print output to the specified file instead of stdout')
  373. args = parser.parse_args()
  374. detected_target, segments, sections = load_map_data(args.map_file)
  375. args.map_file.close()
  376. check_target(detected_target, args.map_file)
  377. if args.another_map_file:
  378. with open(args.another_map_file, 'r') as f:
  379. detected_target_diff, segments_diff, sections_diff = load_map_data(f)
  380. check_target(detected_target_diff, f)
  381. if detected_target_diff != detected_target:
  382. print('WARNING: The target of the reference and other MAP files is {} and {}, respectively.'
  383. ''.format(detected_target, detected_target_diff))
  384. else:
  385. segments_diff, sections_diff, detected_target_diff = {}, {}, ''
  386. if args.target is not None:
  387. if args.target != detected_target or (detected_target_diff and args.target != detected_target_diff):
  388. print('WARNING: The detected chip target overwritten to {} by command line argument!'.format(args.target))
  389. detected_target = args.target
  390. detected_target_diff = args.target
  391. output = ''
  392. if not args.json or not (args.archives or args.files or args.archive_details):
  393. output += get_summary(args.map_file.name, segments, sections, detected_target,
  394. args.json,
  395. args.another_map_file, segments_diff, sections_diff, detected_target_diff, not (args.archives or args.files))
  396. if args.archives:
  397. output += get_detailed_sizes(sections, 'archive', 'Archive File', args.json, sections_diff)
  398. if args.files:
  399. output += get_detailed_sizes(sections, 'file', 'Object File', args.json, sections_diff)
  400. if args.archive_details:
  401. output += get_archive_symbols(sections, args.archive_details, args.json, sections_diff)
  402. args.output_file.write(output)
  403. args.output_file.close()
  404. class StructureForSummary(object):
  405. used_dram_data, used_dram_bss, used_dram_rodata, used_dram_other, used_dram, dram_total, dram_remain = (0, ) * 7
  406. used_dram_ratio = 0.
  407. used_iram_vectors, used_iram_text, used_iram_other, used_iram, iram_total, iram_remain = (0, ) * 6
  408. used_iram_ratio = 0.
  409. used_diram_data, used_diram_bss, used_diram_text, used_diram_vectors, used_diram_rodata, used_diram_other, diram_total, used_diram, diram_remain = (0, ) * 9
  410. used_diram_ratio = 0.
  411. used_flash_text, used_flash_rodata, used_flash_other, used_flash, total_size = (0, ) * 5
  412. def __sub__(self, rhs: 'StructureForSummary') -> 'StructureForSummary':
  413. assert isinstance(rhs, StructureForSummary)
  414. ret = self
  415. for key in StructureForSummary.get_required_items():
  416. setattr(ret, key, getattr(self, key) - getattr(rhs, key))
  417. return ret
  418. def get_dram_overflowed(self) -> bool:
  419. return self.used_dram_ratio > 1.0
  420. def get_iram_overflowed(self) -> bool:
  421. return self.used_iram_ratio > 1.0
  422. def get_diram_overflowed(self) -> bool:
  423. return self.used_diram_ratio > 1.0
  424. @classmethod
  425. def get_required_items(cls: Any) -> List:
  426. whole_list = list(filter(lambda x: not (x.startswith('__') or x.endswith('__') or callable(getattr(cls, x))), dir(cls)))
  427. return whole_list
  428. @staticmethod
  429. def get(segments: List, sections: List) -> 'StructureForSummary':
  430. def get_size(sections: Iterable) -> int:
  431. return sum([x.len for x in sections])
  432. def in_diram(x: MemRegions.Region) -> bool:
  433. return x.region.type in (MemRegions.DRAM_ID, MemRegions.IRAM_ID) and x.region.secondary_addr > 0
  434. def in_dram(x: MemRegions.Region) -> bool:
  435. return x.region.type == MemRegions.DRAM_ID and x.region.secondary_addr == 0 # type: ignore
  436. def in_iram(x: MemRegions.Region) -> bool:
  437. return x.region.type == MemRegions.IRAM_ID and x.region.secondary_addr == 0 # type: ignore
  438. r = StructureForSummary()
  439. diram_filter = filter(in_diram, segments)
  440. r.diram_total = int(get_size(diram_filter) / 2)
  441. dram_filter = filter(in_dram, segments)
  442. r.dram_total = get_size(dram_filter)
  443. iram_filter = filter(in_iram, segments)
  444. r.iram_total = get_size(iram_filter)
  445. if r.diram_total == 0:
  446. r.diram_total = r.dram_total + r.iram_total
  447. def filter_in_section(sections: Iterable[MemRegions.Region], section_to_check: str) -> List[MemRegions.Region]:
  448. return list(filter(lambda x: LinkingSections.in_section(x.section, section_to_check), sections)) # type: ignore
  449. dram_sections = list(filter(in_dram, sections))
  450. iram_sections = list(filter(in_iram, sections))
  451. diram_sections = list(filter(in_diram, sections))
  452. if not diram_sections:
  453. diram_sections = dram_sections + iram_sections
  454. flash_sections = filter_in_section(sections, 'flash')
  455. dram_data_list = filter_in_section(dram_sections, 'data')
  456. dram_bss_list = filter_in_section(dram_sections, 'bss')
  457. dram_rodata_list = filter_in_section(dram_sections, 'rodata')
  458. dram_other_list = [x for x in dram_sections if x not in dram_data_list + dram_bss_list + dram_rodata_list]
  459. iram_vectors_list = filter_in_section(iram_sections, 'vectors')
  460. iram_text_list = filter_in_section(iram_sections, 'text')
  461. iram_other_list = [x for x in iram_sections if x not in iram_vectors_list + iram_text_list]
  462. diram_vectors_list = filter_in_section(diram_sections, 'vectors')
  463. diram_data_list = filter_in_section(diram_sections, 'data')
  464. diram_bss_list = filter_in_section(diram_sections, 'bss')
  465. diram_text_list = filter_in_section(diram_sections, 'text')
  466. diram_rodata_list = filter_in_section(diram_sections, 'rodata')
  467. diram_other_list = [x for x in diram_sections if x not in diram_data_list + diram_bss_list + diram_text_list + diram_vectors_list + diram_rodata_list]
  468. flash_text_list = filter_in_section(flash_sections, 'text')
  469. flash_rodata_list = filter_in_section(flash_sections, 'rodata')
  470. flash_other_list = [x for x in flash_sections if x not in flash_text_list + flash_rodata_list]
  471. r.used_dram_data = get_size(dram_data_list)
  472. r.used_dram_bss = get_size(dram_bss_list)
  473. r.used_dram_rodata = get_size(dram_rodata_list)
  474. r.used_dram_other = get_size(dram_other_list)
  475. r.used_dram = r.used_dram_data + r.used_dram_bss + r.used_dram_other + r.used_dram_rodata
  476. try:
  477. r.used_dram_ratio = r.used_dram / r.dram_total
  478. except ZeroDivisionError:
  479. r.used_dram_ratio = float('nan') if r.used_dram != 0 else 0
  480. r.dram_remain = r.dram_total - r.used_dram
  481. r.used_iram_vectors = get_size((iram_vectors_list))
  482. r.used_iram_text = get_size((iram_text_list))
  483. r.used_iram_other = get_size((iram_other_list))
  484. r.used_iram = r.used_iram_vectors + r.used_iram_text + r.used_iram_other
  485. try:
  486. r.used_iram_ratio = r.used_iram / r.iram_total
  487. except ZeroDivisionError:
  488. r.used_iram_ratio = float('nan') if r.used_iram != 0 else 0
  489. r.iram_remain = r.iram_total - r.used_iram
  490. r.used_diram_data = get_size(diram_data_list)
  491. r.used_diram_bss = get_size(diram_bss_list)
  492. r.used_diram_text = get_size(diram_text_list)
  493. r.used_diram_vectors = get_size(diram_vectors_list)
  494. r.used_diram_rodata = get_size(diram_rodata_list)
  495. r.used_diram_other = get_size(diram_other_list)
  496. r.used_diram = r.used_diram_data + r.used_diram_bss + r.used_diram_text + r.used_diram_vectors + r.used_diram_other + r.used_diram_rodata
  497. try:
  498. r.used_diram_ratio = r.used_diram / r.diram_total
  499. except ZeroDivisionError:
  500. r.used_diram_ratio = float('nan') if r.used_diram != 0 else 0
  501. r.diram_remain = r.diram_total - r.used_diram
  502. r.used_flash_text = get_size(flash_text_list)
  503. r.used_flash_rodata = get_size(flash_rodata_list)
  504. r.used_flash_other = get_size(flash_other_list)
  505. r.used_flash = r.used_flash_text + r.used_flash_rodata + r.used_flash_other
  506. # The used DRAM BSS is counted into the "Used static DRAM" but not into the "Total image size"
  507. r.total_size = r.used_dram - r.used_dram_bss + r.used_iram + r.used_diram - r.used_diram_bss + r.used_flash
  508. return r
  509. def get_json_dic(self) -> collections.OrderedDict:
  510. ret = collections.OrderedDict([
  511. ('dram_data', self.used_dram_data),
  512. ('dram_bss', self.used_dram_bss),
  513. ('dram_rodata', self.used_dram_rodata),
  514. ('dram_other', self.used_dram_other),
  515. ('used_dram', self.used_dram),
  516. ('dram_total', self.dram_total),
  517. ('used_dram_ratio', self.used_dram_ratio if self.used_dram_ratio is not float('nan') else 0),
  518. ('dram_remain', self.dram_remain),
  519. ('iram_vectors', self.used_iram_vectors),
  520. ('iram_text', self.used_iram_text),
  521. ('iram_other', self.used_iram_other),
  522. ('used_iram', self.used_iram),
  523. ('iram_total', self.iram_total),
  524. ('used_iram_ratio', self.used_iram_ratio),
  525. ('iram_remain', self.iram_remain),
  526. ('diram_data', self.used_diram_data),
  527. ('diram_bss', self.used_diram_bss),
  528. ('diram_text', self.used_diram_text),
  529. ('diram_vectors', self.used_diram_vectors),
  530. ('diram_rodata', self.used_diram_rodata),
  531. ('diram_other', self.used_diram_other),
  532. ('diram_total', self.diram_total),
  533. ('used_diram', self.used_diram),
  534. ('used_diram_ratio', self.used_diram_ratio),
  535. ('diram_remain', self.diram_remain),
  536. ('flash_code', self.used_flash_text),
  537. ('flash_rodata', self.used_flash_rodata),
  538. ('flash_other', self.used_flash_other),
  539. ('used_flash_non_ram', self.used_flash), # text/data in D/I RAM not included
  540. ('total_size', self.total_size) # bss not included
  541. ])
  542. assert len(ret) == len(StructureForSummary.get_required_items())
  543. return ret
  544. def get_structure_for_target(segments: Dict, sections: Dict, target: str) -> StructureForSummary:
  545. """
  546. Return StructureForSummary for specific target
  547. """
  548. mem_regions = MemRegions(target)
  549. segment_layout = mem_regions.fit_segments_into_regions(segments)
  550. section_layout = mem_regions.fit_sections_into_regions(LinkingSections.filter_sections(sections))
  551. current = StructureForSummary.get(segment_layout, section_layout)
  552. return current
  553. def get_summary(path: str, segments: Dict, sections: Dict, target: str,
  554. as_json: bool=False,
  555. path_diff: str='', segments_diff: Optional[Dict]=None, sections_diff: Optional[Dict]=None,
  556. target_diff: str='', print_suggestions: bool=True) -> str:
  557. segments_diff = segments_diff or {}
  558. sections_diff = sections_diff or {}
  559. current = get_structure_for_target(segments, sections, target)
  560. if path_diff:
  561. diff_en = True
  562. mem_regions_diff = MemRegions(target_diff)
  563. segment_layout_diff = mem_regions_diff.fit_segments_into_regions(segments_diff)
  564. section_layout_diff = mem_regions_diff.fit_sections_into_regions(LinkingSections.filter_sections(sections_diff))
  565. reference = StructureForSummary.get(segment_layout_diff, section_layout_diff)
  566. else:
  567. diff_en = False
  568. reference = StructureForSummary()
  569. if as_json:
  570. current_json_dic = current.get_json_dic()
  571. if diff_en:
  572. reference_json_dic = reference.get_json_dic()
  573. diff_json_dic = collections.OrderedDict([
  574. (k, v - reference_json_dic[k]) for k, v in iteritems(current_json_dic)])
  575. output = format_json(collections.OrderedDict([('current', current_json_dic),
  576. ('reference', reference_json_dic),
  577. ('diff', diff_json_dic),
  578. ]))
  579. else:
  580. output = format_json(current_json_dic)
  581. else:
  582. class LineDef(object):
  583. title = ''
  584. name = ''
  585. def __init__(self, title: str, name: str) -> None:
  586. self.title = title
  587. self.name = name
  588. def format_line(self) -> Tuple[str, str, str, str]:
  589. return (self.title + ': {%s:>7} bytes' % self.name,
  590. '{%s:>7}' % self.name,
  591. '{%s:+}' % self.name,
  592. '')
  593. class HeadLineDef(LineDef):
  594. remain = ''
  595. ratio = ''
  596. total = ''
  597. warning_message = ''
  598. def __init__(self, title: str, name: str, remain: str, ratio: str, total: str, warning_message: str) -> None:
  599. super(HeadLineDef, self).__init__(title, name)
  600. self.remain = remain
  601. self.ratio = ratio
  602. self.total = total
  603. self.warning_message = warning_message
  604. def format_line(self) -> Tuple[str, str, str, str]:
  605. return ('%s: {%s:>7} bytes ({%s:>7} remain, {%s:.1%%} used)%s' % (self.title, self.name, self.remain, self.ratio, self.warning_message),
  606. '{%s:>7}' % self.name,
  607. '{%s:+}' % self.name,
  608. '({%s:>+7} remain, {%s:>+7} total)' % (self.remain, self.total))
  609. class TotalLineDef(LineDef):
  610. def format_line(self) -> Tuple[str, str, str, str]:
  611. return (self.title + ': {%s:>7} bytes (.bin may be padded larger)' % self.name,
  612. '{%s:>7}' % self.name,
  613. '{%s:+}' % self.name,
  614. '')
  615. warning_message = ' Overflow detected!' + (' You can run idf.py size-files for more information.' if print_suggestions else '')
  616. format_list = [
  617. HeadLineDef('Used static DRAM', 'used_dram', remain='dram_remain', ratio='used_dram_ratio', total='dram_total',
  618. warning_message=warning_message if current.get_dram_overflowed() else ''),
  619. LineDef(' .data size', 'used_dram_data'),
  620. LineDef(' .bss size', 'used_dram_bss'),
  621. LineDef(' .rodata size', 'used_dram_rodata'),
  622. LineDef(' DRAM other size', 'used_dram_other'),
  623. HeadLineDef('Used static IRAM', 'used_iram', remain='iram_remain', ratio='used_iram_ratio', total='iram_total',
  624. warning_message=warning_message if current.get_iram_overflowed() else ''),
  625. LineDef(' .text size', 'used_iram_text'),
  626. LineDef(' .vectors size', 'used_iram_vectors'),
  627. HeadLineDef('Used stat D/IRAM', 'used_diram', remain='diram_remain', ratio='used_diram_ratio', total='diram_total',
  628. warning_message=warning_message if current.get_diram_overflowed() else ''),
  629. LineDef(' .data size', 'used_diram_data'),
  630. LineDef(' .bss size', 'used_diram_bss'),
  631. LineDef(' .text size', 'used_diram_text'),
  632. LineDef(' .vectors size', 'used_diram_vectors'),
  633. LineDef(' .rodata size', 'used_diram_rodata'),
  634. LineDef(' other ', 'used_diram_other'),
  635. LineDef('Used Flash size ', 'used_flash'),
  636. LineDef(' .text ', 'used_flash_text'),
  637. LineDef(' .rodata ', 'used_flash_rodata'),
  638. TotalLineDef('Total image size', 'total_size')
  639. ]
  640. def convert_to_fmt_dict(summary: StructureForSummary, suffix: str='') -> Dict:
  641. required_items = StructureForSummary.get_required_items()
  642. return dict([(key + suffix, getattr(summary, key)) for key in required_items])
  643. f_dic1 = convert_to_fmt_dict(current)
  644. if diff_en:
  645. f_dic2 = convert_to_fmt_dict(reference)
  646. f_dic_diff = convert_to_fmt_dict(current - reference)
  647. lf = '{:60}{:>15}{:>15} {}' # Width for a, b, c, d columns
  648. def print_in_columns(a: str, b: Optional[str]='', c: Optional[str]='', d: Optional[str]='') -> str:
  649. return lf.format(a, b, c, d).rstrip() + os.linesep
  650. output = ''
  651. if diff_en:
  652. output += print_in_columns('<CURRENT> MAP file: ' + path)
  653. output += print_in_columns('<REFERENCE> MAP file: ' + path_diff)
  654. output += print_in_columns('Difference is counted as <CURRENT> - <REFERENCE>, ',
  655. 'i.e. a positive number means that <CURRENT> is larger.')
  656. output += print_in_columns('Total sizes of <CURRENT>:', '<REFERENCE>', 'Difference', '')
  657. for line in format_list:
  658. if getattr(current, line.name) > 0 or getattr(reference, line.name) > 0 or line.name == 'total_size':
  659. main_string_format, reference_format, sign_format, main_diff_format = line.format_line()
  660. output += print_in_columns(
  661. main_string_format.format(**f_dic1),
  662. reference_format.format(**f_dic2),
  663. sign_format.format(**f_dic_diff) if not sign_format.format(**f_dic_diff).startswith('+0') else '',
  664. main_diff_format.format(**f_dic_diff))
  665. else:
  666. output += print_in_columns('Total sizes:')
  667. for line in format_list:
  668. if getattr(current, line.name) > 0 or line.name == 'total_size':
  669. main_string_format, reference_format, sign_format, main_diff_format = line.format_line()
  670. output += print_in_columns(main_string_format.format(**f_dic1))
  671. return output
  672. def sort_dict(non_sort_list: List) -> List:
  673. '''
  674. sort with keeping the order data, bss, other, iram, diram, ram_st_total, flash_text, flash_rodata, flash_total
  675. '''
  676. start_of_other = 0
  677. props_sort = [] # type: List
  678. props_elem = ['.data', '.bss', 'other', 'iram', 'diram', 'ram_st_total', 'flash.text', 'flash.rodata', 'flash', 'flash_total']
  679. for i in props_elem:
  680. for j in non_sort_list:
  681. if i == 'other':
  682. # remembering where 'other' will start
  683. start_of_other = len(props_sort)
  684. elif i in j and j not in props_sort:
  685. props_sort.append(j)
  686. for j in non_sort_list:
  687. if j not in props_sort:
  688. # add all item that fit in other in dict
  689. props_sort.insert(start_of_other, j)
  690. return props_sort
  691. class StructureForDetailedSizes(object):
  692. @staticmethod
  693. def sizes_by_key(sections: SectionDict, key: str, include_padding: Optional[bool]=False) -> Dict[str, Dict[str, int]]:
  694. """ Takes a dict of sections (from load_sections) and returns
  695. a dict keyed by 'key' with aggregate output size information.
  696. Key can be either "archive" (for per-archive data) or "file" (for per-file data) in the result.
  697. """
  698. result = {} # type: Dict[str, Dict[str, int]]
  699. for _, section in iteritems(sections):
  700. for s in section['sources']:
  701. if not s[key] in result:
  702. result[s[key]] = {}
  703. archive = result[s[key]]
  704. if not section['name'] in archive:
  705. archive[section['name']] = 0
  706. archive[section['name']] += s['size']
  707. if include_padding:
  708. archive[section['name']] += s['fill']
  709. return result
  710. @staticmethod
  711. def get(sections: SectionDict, by_key: str) -> collections.OrderedDict:
  712. """
  713. Get the detailed structure before using the filter to remove undesired sections,
  714. to show entries without desired sections
  715. """
  716. sizes = StructureForDetailedSizes.sizes_by_key(sections, by_key)
  717. for key_name in sizes:
  718. sizes[key_name] = LinkingSections.filter_sections(sizes[key_name])
  719. s = []
  720. for key, section_dict in sizes.items():
  721. ram_st_total = sum([x[1] for x in section_dict.items() if not LinkingSections.in_section(x[0], 'flash')])
  722. flash_total = sum([x[1] for x in section_dict.items() if not LinkingSections.in_section(x[0], 'bss')]) # type: int
  723. section_dict['ram_st_total'] = ram_st_total
  724. section_dict['flash_total'] = flash_total
  725. sorted_dict = sorted(section_dict.items(), key=lambda elem: elem[0])
  726. s.append((key, collections.OrderedDict(sorted_dict)))
  727. s = sorted(s, key=lambda elem: elem[0])
  728. # do a secondary sort in order to have consistent order (for diff-ing the output)
  729. s = sorted(s, key=lambda elem: elem[1]['flash_total'], reverse=True)
  730. return collections.OrderedDict(s)
  731. def get_detailed_sizes(sections: Dict, key: str, header: str, as_json: bool=False, sections_diff: Dict=None) -> str:
  732. key_name_set = set()
  733. current = StructureForDetailedSizes.get(sections, key)
  734. for section_dict in current.values():
  735. key_name_set.update(section_dict.keys())
  736. if sections_diff:
  737. reference = StructureForDetailedSizes.get(sections_diff, key)
  738. for section_dict in reference.values():
  739. key_name_set.update(section_dict.keys())
  740. diff_en = True
  741. else:
  742. diff_en = False
  743. key_name_list = list(key_name_set)
  744. ordered_key_list, display_name_list = LinkingSections.get_display_name_order(key_name_list)
  745. if as_json:
  746. if diff_en:
  747. diff_json_dic = collections.OrderedDict()
  748. for name in sorted(list(frozenset(current.keys()) | frozenset(reference.keys()))):
  749. cur_name_dic = current.get(name, {})
  750. ref_name_dic = reference.get(name, {})
  751. all_keys = sorted(list(frozenset(cur_name_dic.keys()) | frozenset(ref_name_dic.keys())))
  752. diff_json_dic[name] = collections.OrderedDict([(k,
  753. cur_name_dic.get(k, 0) -
  754. ref_name_dic.get(k, 0)) for k in all_keys])
  755. output = format_json(collections.OrderedDict([('current', current),
  756. ('reference', reference),
  757. ('diff', diff_json_dic),
  758. ]))
  759. else:
  760. output = format_json(current)
  761. else:
  762. def _get_header_format(disp_list: List=display_name_list) -> str:
  763. len_list = [len(x) for x in disp_list]
  764. len_list.insert(0, 24)
  765. return ' '.join(['{:>%d}' % x for x in len_list]) + os.linesep
  766. def _get_output(data: Dict[str, Dict[str, int]], selection: Collection, key_list: List=ordered_key_list, disp_list: List=display_name_list) -> str:
  767. header_format = _get_header_format(disp_list)
  768. output = header_format.format(header, *disp_list)
  769. for key, data_info in iteritems(data):
  770. if key not in selection:
  771. continue
  772. try:
  773. _, key = key.split(':', 1)
  774. # print subheadings for key of format archive:file
  775. except ValueError:
  776. # k remains the same
  777. pass
  778. def get_section_size(section_dict: Dict) -> Callable[[str], int]:
  779. return lambda x: section_dict.get(x, 0)
  780. section_size_list = map(get_section_size(section_dict=data_info), key_list)
  781. output += header_format.format(key[:24], *(section_size_list))
  782. return output
  783. def _get_header_format_diff(disp_list: List=display_name_list, columns: bool=False) -> str:
  784. if columns:
  785. len_list = (24, ) + (7, ) * 3 * len(disp_list)
  786. return '|'.join(['{:>%d}' % x for x in len_list]) + os.linesep
  787. len_list = (24, ) + (23, ) * len(disp_list)
  788. return ' '.join(['{:>%d}' % x for x in len_list]) + os.linesep
  789. def _get_output_diff(curr: Dict, ref: Dict, key_list: List=ordered_key_list, disp_list: List=display_name_list) -> str:
  790. # First header without Current/Ref/Diff columns
  791. header_format = _get_header_format_diff(columns=False)
  792. output = header_format.format(header, *disp_list)
  793. f_print = ('-' * 23, '') * len(key_list)
  794. f_print = f_print[0:len(key_list)]
  795. header_line = header_format.format('', *f_print)
  796. header_format = _get_header_format_diff(columns=True)
  797. f_print = ('<C>', '<R>', '<C>-<R>') * len(key_list)
  798. output += header_format.format('', *f_print)
  799. output += header_line
  800. for key, data_info in iteritems(curr):
  801. try:
  802. v2 = ref[key]
  803. except KeyError:
  804. continue
  805. try:
  806. _, key = key.split(':', 1)
  807. # print subheadings for key of format archive:file
  808. except ValueError:
  809. # k remains the same
  810. pass
  811. def _get_items(name: str, section_dict: Dict=data_info, section_dict_ref: Dict=v2) -> Tuple[str, str, str]:
  812. a = section_dict.get(name, 0)
  813. b = section_dict_ref.get(name, 0)
  814. diff = a - b
  815. # the sign is added here and not in header_format in order to be able to print empty strings
  816. return (a or '', b or '', '' if diff == 0 else '{:+}'.format(diff))
  817. x = [] # type: List[str]
  818. for section in key_list:
  819. x.extend(_get_items(section))
  820. output += header_format.format(key[:24], *(x))
  821. return output
  822. output = 'Per-{} contributions to ELF file:{}'.format(key, os.linesep)
  823. if diff_en:
  824. output += _get_output_diff(current, reference)
  825. in_current = frozenset(current.keys())
  826. in_reference = frozenset(reference.keys())
  827. only_in_current = in_current - in_reference
  828. only_in_reference = in_reference - in_current
  829. if len(only_in_current) > 0:
  830. output += 'The following entries are present in <CURRENT> only:{}'.format(os.linesep)
  831. output += _get_output(current, only_in_current)
  832. if len(only_in_reference) > 0:
  833. output += 'The following entries are present in <REFERENCE> only:{}'.format(os.linesep)
  834. output += _get_output(reference, only_in_reference)
  835. else:
  836. output += _get_output(current, current)
  837. return output
  838. class StructureForArchiveSymbols(object):
  839. @staticmethod
  840. def get(archive: str, sections: Dict) -> Dict:
  841. interested_sections = LinkingSections.filter_sections(sections)
  842. result = dict([(t, {}) for t in interested_sections]) # type: Dict[str, Dict[str, int]]
  843. for _, section in iteritems(sections):
  844. section_name = section['name']
  845. if section_name not in interested_sections:
  846. continue
  847. for s in section['sources']:
  848. if archive != s['archive']:
  849. continue
  850. s['sym_name'] = re.sub('(.text.|.literal.|.data.|.bss.|.rodata.)', '', s['sym_name'])
  851. result[section_name][s['sym_name']] = result[section_name].get(s['sym_name'], 0) + s['size']
  852. # build a new ordered dict of each section, where each entry is an ordereddict of symbols to sizes
  853. section_symbols = collections.OrderedDict()
  854. for t in sorted(list(interested_sections)):
  855. s = sorted(result[t].items(), key=lambda k_v: str(k_v[0]))
  856. # do a secondary sort in order to have consistent order (for diff-ing the output)
  857. s = sorted(s, key=lambda k_v: int(k_v[1]), reverse=True)
  858. section_symbols[t] = collections.OrderedDict(s)
  859. return section_symbols
  860. def get_archive_symbols(sections: Dict, archive: str, as_json: bool=False, sections_diff: Dict=None) -> str:
  861. diff_en = bool(sections_diff)
  862. current = StructureForArchiveSymbols.get(archive, sections)
  863. reference = StructureForArchiveSymbols.get(archive, sections_diff) if sections_diff else {}
  864. if as_json:
  865. if diff_en:
  866. diff_json_dic = collections.OrderedDict()
  867. for name in sorted(list(frozenset(current.keys()) | frozenset(reference.keys()))):
  868. cur_name_dic = current.get(name, {})
  869. ref_name_dic = reference.get(name, {})
  870. all_keys = sorted(list(frozenset(cur_name_dic.keys()) | frozenset(ref_name_dic.keys())))
  871. diff_json_dic[name] = collections.OrderedDict([(key,
  872. cur_name_dic.get(key, 0) -
  873. ref_name_dic.get(key, 0)) for key in all_keys])
  874. output = format_json(collections.OrderedDict([('current', current),
  875. ('reference', reference),
  876. ('diff', diff_json_dic),
  877. ]))
  878. else:
  879. output = format_json(current)
  880. else:
  881. def _get_item_pairs(name: str, section: collections.OrderedDict) -> collections.OrderedDict:
  882. return collections.OrderedDict([(key.replace(name + '.', ''), val) for key, val in iteritems(section)])
  883. def _get_max_len(symbols_dict: Dict) -> Tuple[int, int]:
  884. # the lists have 0 in them because max() doesn't work with empty lists
  885. names_max_len = 0
  886. numbers_max_len = 0
  887. for t, s in iteritems(symbols_dict):
  888. numbers_max_len = max([numbers_max_len, *[len(str(x)) for _, x in iteritems(s)]])
  889. names_max_len = max([names_max_len, *[len(x) for x in _get_item_pairs(t, s)]])
  890. return names_max_len, numbers_max_len
  891. def _get_output(section_symbols: Dict) -> str:
  892. output = ''
  893. names_max_len, numbers_max_len = _get_max_len(section_symbols)
  894. for t, s in iteritems(section_symbols):
  895. output += '{}Symbols from section: {}{}'.format(os.linesep, t, os.linesep)
  896. item_pairs = _get_item_pairs(t, s)
  897. for key, val in iteritems(item_pairs):
  898. output += ' '.join([('\t{:<%d} : {:>%d}\n' % (names_max_len,numbers_max_len)).format(key, val)])
  899. section_total = sum([val for _, val in iteritems(item_pairs)])
  900. output += 'Section total: {}{}'.format(section_total, os.linesep)
  901. return output
  902. output = '{}Symbols within the archive: {} (Not all symbols may be reported){}'.format(os.linesep, archive, os.linesep)
  903. if diff_en:
  904. def _generate_line_tuple(curr: collections.OrderedDict, ref: collections.OrderedDict, name: str) -> Tuple[str, int, int, str]:
  905. cur_val = curr.get(name, 0)
  906. ref_val = ref.get(name, 0)
  907. diff_val = cur_val - ref_val
  908. # string slicing is used just to make sure it will fit into the first column of line_format
  909. return ((' ' * 4 + name)[:40], cur_val, ref_val, '' if diff_val == 0 else '{:+}'.format(diff_val))
  910. line_format = '{:40} {:>12} {:>12} {:>25}'
  911. all_section_names = sorted(list(frozenset(current.keys()) | frozenset(reference.keys())))
  912. for section_name in all_section_names:
  913. current_item_pairs = _get_item_pairs(section_name, current.get(section_name, {}))
  914. reference_item_pairs = _get_item_pairs(section_name, reference.get(section_name, {}))
  915. output += os.linesep + line_format.format(section_name[:40],
  916. '<CURRENT>',
  917. '<REFERENCE>',
  918. '<CURRENT> - <REFERENCE>') + os.linesep
  919. current_section_total = sum([val for _, val in iteritems(current_item_pairs)])
  920. reference_section_total = sum([val for _, val in iteritems(reference_item_pairs)])
  921. diff_section_total = current_section_total - reference_section_total
  922. all_item_names = sorted(list(frozenset(current_item_pairs.keys()) |
  923. frozenset(reference_item_pairs.keys())))
  924. output += os.linesep.join([line_format.format(*_generate_line_tuple(current_item_pairs,
  925. reference_item_pairs,
  926. n)
  927. ).rstrip() for n in all_item_names])
  928. output += os.linesep if current_section_total > 0 or reference_section_total > 0 else ''
  929. output += line_format.format('Section total:',
  930. current_section_total,
  931. reference_section_total,
  932. '' if diff_section_total == 0 else '{:+}'.format(diff_section_total)
  933. ).rstrip() + os.linesep
  934. else:
  935. output += _get_output(current)
  936. return output
  937. if __name__ == '__main__':
  938. main()