circuitpython/tools/build_memory_info.py

79 lines
2.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2020-06-03 18:40:05 -04:00
# SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
#
2020-06-03 18:40:05 -04:00
# SPDX-License-Identifier: MIT
import re
import sys
2023-03-04 03:37:38 -05:00
import json
# Handle size constants with K or M suffixes (allowed in .ld but not in Python).
2021-03-15 09:57:36 -04:00
K_PATTERN = re.compile(r"([0-9]+)[kK]")
K_REPLACE = r"(\1*1024)"
2021-03-15 09:57:36 -04:00
M_PATTERN = re.compile(r"([0-9]+)[mM]")
M_REPLACE = r"(\1*1024*1024)"
text = 0
data = 0
bss = 0
2023-03-04 03:37:38 -05:00
# stdin is the linker output.
for line in sys.stdin:
2019-12-10 17:57:17 -05:00
# Uncomment to see linker output.
# print(line)
line = line.strip()
if not line.startswith("text"):
text, data, bss = map(int, line.split()[:3])
regions = {}
2023-03-04 03:37:38 -05:00
# This file is the linker script.
with open(sys.argv[1], "r") as f:
for line in f:
line = line.strip()
if line.startswith(("FLASH_FIRMWARE", "RAM")):
regions[line.split()[0]] = line.split("=")[-1]
for region in regions:
space = regions[region]
if "/*" in space:
space = space.split("/*")[0]
space = K_PATTERN.sub(K_REPLACE, space)
space = M_PATTERN.sub(M_REPLACE, space)
2019-12-10 22:39:44 -05:00
regions[region] = int(eval(space))
firmware_region = regions["FLASH_FIRMWARE"]
2019-12-10 17:57:17 -05:00
ram_region = regions["RAM"]
used_flash = data + text
free_flash = firmware_region - used_flash
used_ram = data + bss
free_ram = ram_region - used_ram
2023-03-04 03:37:38 -05:00
with open(f"{sys.argv[2]}/firmware.size.json", "w") as f:
json.dump({"used_flash": used_flash, "firmware_region": firmware_region}, f)
print()
2021-03-15 09:57:36 -04:00
print(
"{} bytes used, {} bytes free in flash firmware space out of {} bytes ({}kB).".format(
used_flash, free_flash, firmware_region, firmware_region / 1024
)
)
print(
"{} bytes used, {} bytes free in ram for stack and heap out of {} bytes ({}kB).".format(
used_ram, free_ram, ram_region, ram_region / 1024
)
)
print()
# Check that we have free flash space. GCC doesn't fail when the text + data
# sections don't fit in FLASH. It only counts data in RAM.
if free_flash < 0:
print("Too little flash!!!")
print()
sys.exit(-1)