|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# This file is part of the MicroPython project, http://micropython.org/ |
| 4 | +# |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +# of this software and associated documentation files (the "Software"), to deal |
| 11 | +# in the Software without restriction, including without limitation the rights |
| 12 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +# copies of the Software, and to permit persons to whom the Software is |
| 14 | +# furnished to do so, subject to the following conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be included in |
| 17 | +# all copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +# THE SOFTWARE. |
| 26 | + |
| 27 | +import sys |
| 28 | + |
| 29 | +print() |
| 30 | + |
| 31 | +text = 0 |
| 32 | +data = 0 |
| 33 | +bss = 0 |
| 34 | +# stdin is the linker output. |
| 35 | +for line in sys.stdin: |
| 36 | + line = line.strip() |
| 37 | + if not line.startswith("text"): |
| 38 | + text, data, bss = map(int, line.split()[:3]) |
| 39 | + |
| 40 | +regions = {} |
| 41 | +# This file is the linker script. |
| 42 | +with open(sys.argv[1], "r") as f: |
| 43 | + for line in f: |
| 44 | + line = line.strip() |
| 45 | + if line.startswith(("FLASH", "RAM")): |
| 46 | + regions[line.split()[0]] = line.split("=")[-1] |
| 47 | + |
| 48 | +for region in regions: |
| 49 | + space = regions[region] |
| 50 | + if "/*" in space: |
| 51 | + space = space.split("/*")[0] |
| 52 | + regions[region] = eval(space) |
| 53 | + |
| 54 | +free_flash = regions["FLASH"] - text - data |
| 55 | +free_ram = regions["RAM"] - data - bss |
| 56 | +print(free_flash, "bytes free in flash out of", regions["FLASH"], "bytes (", regions["FLASH"] / 1024, "kb ).") |
| 57 | +print(free_ram, "bytes free in ram for stack out of", regions["RAM"], "bytes (", regions["RAM"] / 1024, "kb ).") |
| 58 | +print() |
| 59 | + |
| 60 | +# Check that we have free flash space. GCC doesn't fail when the text + data |
| 61 | +# sections don't fit in FLASH. It only counts data in RAM. |
| 62 | +if free_flash < 0: |
| 63 | + print("Too little flash!!!") |
| 64 | + print() |
| 65 | + sys.exit(-1) |
0 commit comments