Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
)
import subprocess
import os
import re
import shutil
import logging
import tarfile
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
63 changes: 63 additions & 0 deletions tests/builder/test_memory_overflow_hint.py
Original file line number Diff line number Diff line change
@@ -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