From 71dfcdeab7f7aeabe75f1281d6d546a2c365c8ae Mon Sep 17 00:00:00 2001 From: Petrus Date: Mon, 30 Nov 2026 19:41:20 +0100 Subject: [PATCH] builder: explain builds that do not fit in the board's memory When the selected features make the firmware too big, the build failed with only a linker error in the log (region `flash' overflowed by N bytes). After the build step, look for that error and append a plain message to the build log: how many bytes too large, and that deselecting features fixes it. Fixes #114 --- builder/builder.py | 45 ++++++++++++++++ tests/builder/test_memory_overflow_hint.py | 63 ++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 tests/builder/test_memory_overflow_hint.py diff --git a/builder/builder.py b/builder/builder.py index 9c9edf34..49269cd3 100644 --- a/builder/builder.py +++ b/builder/builder.py @@ -4,6 +4,7 @@ ) import subprocess import os +import re import shutil import logging import tarfile @@ -43,6 +44,44 @@ def resolve_feature_defines(selected_labels, all_features): return enabled_defines, disabled_defines, all_defines, unknown_labels +# GNU ld reports a firmware that is too big for a memory region as: +# region `flash' overflowed by 1234 bytes +REGION_OVERFLOW_RE = re.compile( + r"region [`'\"]?([\w.]+)['`\"]? overflowed by (\d+) bytes" +) + + +def memory_overflow_hint(build_log): + """ + Explain a build that failed because the firmware does not fit in the + board's memory, which otherwise only shows up as a linker error. + + Parameters: + build_log (str): Contents of the build log. + + Returns: + str or None: A message for the user, or None if the log contains + no memory overflow. + """ + overflows = {} + for region, size in REGION_OVERFLOW_RE.findall(build_log): + overflows[region] = max(overflows.get(region, 0), int(size)) + if not overflows: + return None + + flash = {r: n for r, n in overflows.items() if "flash" in r.lower()} + if flash: + what = "the board's flash memory" + size = max(flash.values()) + else: + region, size = max(overflows.items(), key=lambda item: item[1]) + what = f"the board's '{region}' memory region" + return ( + f"BUILD FAILED: the firmware is {size} bytes too large for {what}. " + "Deselect some features and build again." + ) + + class Builder: """ Processes build requests, perform builds and ship build artifacts @@ -469,6 +508,12 @@ def __build(self, build_id: str) -> None: shell=False, timeout=CBS_BUILD_TIMEOUT_SEC, ) + build_log.flush() + with open(logpath, encoding="utf-8", errors="replace") as f: + hint = memory_overflow_hint(f.read()) + if hint: + self.logger.info(f"Build {build_id}: {hint}") + build_log.write(f"\n{hint}\n\n") build_log.write("done build\n") build_log.flush() except subprocess.TimeoutExpired: diff --git a/tests/builder/test_memory_overflow_hint.py b/tests/builder/test_memory_overflow_hint.py new file mode 100644 index 00000000..477b1823 --- /dev/null +++ b/tests/builder/test_memory_overflow_hint.py @@ -0,0 +1,63 @@ +"""Tests for the build log hint shown when firmware does not fit in memory.""" +from builder.builder import memory_overflow_hint + +LINKER_FLASH_OVERFLOW = """\ +[1234/1240] Linking build/CubeOrangePlus/bin/arducopter +/usr/lib/gcc/arm-none-eabi/10.2.1/../../../arm-none-eabi/bin/ld: \ +build/CubeOrangePlus/bin/arducopter section `.text' will not fit in region `flash' +/usr/lib/gcc/arm-none-eabi/10.2.1/../../../arm-none-eabi/bin/ld: \ +region `flash' overflowed by 4232 bytes +collect2: error: ld returned 1 exit status +""" + + +def test_no_hint_for_successful_build(): + log = "[1240/1240] Linking build/CubeOrangePlus/bin/arducopter\nTotal Flash Used: 1923456\n" + assert memory_overflow_hint(log) is None + + +def test_no_hint_for_unrelated_failure(): + log = "libraries/AP_Foo/AP_Foo.cpp:12:5: error: 'bar' was not declared in this scope\n" + assert memory_overflow_hint(log) is None + + +def test_flash_overflow_reports_size_and_what_to_do(): + hint = memory_overflow_hint(LINKER_FLASH_OVERFLOW) + assert hint is not None + assert "4232 bytes too large" in hint + assert "flash memory" in hint + assert "Deselect some features" in hint + + +def test_hint_does_not_look_like_a_successful_build(): + # The progress updater treats "Total Flash Used" in the log as success. + assert "Total Flash Used" not in memory_overflow_hint(LINKER_FLASH_OVERFLOW) + + +def test_largest_flash_overflow_wins(): + log = ( + "ld: region `flash' overflowed by 100 bytes\n" + "ld: region `flash' overflowed by 2500 bytes\n" + ) + assert "2500 bytes too large" in memory_overflow_hint(log) + + +def test_flash_preferred_over_ram_region(): + log = ( + "ld: region `ram0' overflowed by 9000 bytes\n" + "ld: region `flash' overflowed by 300 bytes\n" + ) + hint = memory_overflow_hint(log) + assert "300 bytes too large" in hint + assert "flash memory" in hint + + +def test_other_region_is_named(): + hint = memory_overflow_hint("ld: region `ram0' overflowed by 512 bytes\n") + assert "'ram0' memory region" in hint + assert "512 bytes too large" in hint + + +def test_straight_quotes_are_accepted(): + hint = memory_overflow_hint("ld: region 'flash' overflowed by 64 bytes\n") + assert "64 bytes too large" in hint