Skip to content

Commit 129e228

Browse files
Add script to generate a code size report
This script runs the specified size command (e.g. arm-none-eabi-size) on the specified library (e.g. libtfpsacrypto.a). Signed-off-by: David Horstmann <david.horstmann@arm.com>
1 parent 5ef7e74 commit 129e228

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python3
2+
"""Generate a code size report for the supplied library file, using the given
3+
size tool, and write it in JSON format to the given output file.
4+
"""
5+
6+
# Copyright The Mbed TLS Contributors
7+
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
8+
9+
import argparse
10+
import json
11+
import subprocess
12+
import sys
13+
14+
def get_size_output(size_cmd: str, library_file: str):
15+
"""Run the size command on the library and get its output"""
16+
output = subprocess.check_output([size_cmd, library_file])
17+
return str(output, 'UTF-8')
18+
19+
def size_breakdown(size_output: str):
20+
"""Convert the output of the size command to a dictionary like the following:
21+
{'filename.c.obj' : {'text': 123, 'data': 456, 'bss': 789}}"""
22+
23+
sizes = {}
24+
25+
for line in size_output.splitlines()[1:]:
26+
row = line.split()
27+
obj_file_name = row[5]
28+
text_size = int(row[0])
29+
data_size = int(row[1])
30+
bss_size = int(row[2])
31+
32+
sizes[obj_file_name] = {'text': text_size, 'data': data_size, 'bss': bss_size}
33+
34+
return sizes
35+
36+
def write_out_results(sizes: dict, output_file: str):
37+
"""Write out the sizes in JSON to the output file"""
38+
sizes_json = json.dumps(sizes, indent=4)
39+
40+
with open(output_file, "w") as out:
41+
out.write(sizes_json)
42+
43+
def main():
44+
parser = argparse.ArgumentParser(
45+
description='Generate a code size report in JSON format')
46+
47+
parser.add_argument("-o", "--output-file",
48+
help="Filename of the report to generate",
49+
default='code_size.json')
50+
51+
parser.add_argument("-s", "--size-cmd",
52+
help="Size command to use (e.g. arm-none-eabi-size)",
53+
required=True)
54+
55+
parser.add_argument("-l", "--library-file",
56+
help="Library file to generate report from",
57+
required=True)
58+
59+
args = parser.parse_args()
60+
61+
sizes = size_breakdown(get_size_output(args.size_cmd, args.library_file))
62+
63+
write_out_results(sizes, args.output_file)
64+
65+
if __name__ == "__main__":
66+
main()
67+

0 commit comments

Comments
 (0)