diff --git a/esphome/components/nrf52/toolchain.py b/esphome/components/nrf52/toolchain.py index 5c9e3d4632..aaa317aa5f 100644 --- a/esphome/components/nrf52/toolchain.py +++ b/esphome/components/nrf52/toolchain.py @@ -2,6 +2,8 @@ from pathlib import Path +import esphome.config_validation as cv +from esphome.const import KEY_CORE, KEY_FRAMEWORK_VERSION from esphome.core import CORE from .framework import toolchain_tool @@ -16,8 +18,11 @@ def get_readelf_path() -> Path: def get_elf_path() -> Path: - """The linked Zephyr image; the SDK nests it one level deeper from 2.9.2.""" - nested = CORE.relative_pioenvs_path(CORE.name, "zephyr", "zephyr", "zephyr.elf") - if nested.is_file(): - return nested - return CORE.relative_pioenvs_path(CORE.name, "zephyr", "zephyr.elf") + """The linked Zephyr image, at the layout the configured SDK version writes. + + Chosen by version rather than by probing so a leftover image from another + SDK layout is never analyzed in place of the current build. + """ + if CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] < cv.Version(2, 9, 2): + return CORE.relative_pioenvs_path(CORE.name, "zephyr", "zephyr.elf") + return CORE.relative_pioenvs_path(CORE.name, "zephyr", "zephyr", "zephyr.elf") diff --git a/tests/unit_tests/test_nrf52_toolchain.py b/tests/unit_tests/test_nrf52_toolchain.py index 1a9e4bd2e2..ee8f6eaf49 100644 --- a/tests/unit_tests/test_nrf52_toolchain.py +++ b/tests/unit_tests/test_nrf52_toolchain.py @@ -7,6 +7,8 @@ import pytest from esphome.components.nrf52 import toolchain from esphome.components.nrf52.framework import TOOLCHAIN_VERSION, get_sdk_nrf_tools_path +import esphome.config_validation as cv +from esphome.const import KEY_CORE, KEY_FRAMEWORK_VERSION from esphome.core import CORE @@ -17,6 +19,10 @@ def nrf52_build(tmp_path: Path) -> Path: return CORE.build_path / ".pioenvs" / "test_device" / "zephyr" +def _set_sdk_version(version: cv.Version) -> None: + CORE.data.setdefault(KEY_CORE, {})[KEY_FRAMEWORK_VERSION] = version + + def test_binutils_come_from_the_pinned_zephyr_sdk() -> None: bin_path = ( get_sdk_nrf_tools_path() @@ -31,15 +37,18 @@ def test_binutils_come_from_the_pinned_zephyr_sdk() -> None: assert toolchain.get_readelf_path() == bin_path / f"arm-zephyr-eabi-readelf{suffix}" -def test_elf_prefers_the_nested_sdk_layout(nrf52_build: Path) -> None: +def test_elf_uses_the_nested_layout_from_sdk_2_9_2(nrf52_build: Path) -> None: + _set_sdk_version(cv.Version(2, 9, 2)) + # A flat image left by an older SDK must not be picked over the current + # layout, so the choice follows the version rather than what exists + (nrf52_build / "zephyr.elf").parent.mkdir(parents=True) + (nrf52_build / "zephyr.elf").write_text("") + assert toolchain.get_elf_path() == nrf52_build / "zephyr" / "zephyr.elf" + + +def test_elf_uses_the_flat_layout_before_sdk_2_9_2(nrf52_build: Path) -> None: + _set_sdk_version(cv.Version(2, 6, 1)) nested = nrf52_build / "zephyr" / "zephyr.elf" nested.parent.mkdir(parents=True) nested.write_text("") - (nrf52_build / "zephyr.elf").write_text("") - assert toolchain.get_elf_path() == nested - - -def test_elf_falls_back_to_the_flat_layout(nrf52_build: Path) -> None: - # SDKs before 2.9.2 wrote the image one level up; a missing ELF is - # reported by the caller under this path assert toolchain.get_elf_path() == nrf52_build / "zephyr.elf"