From 286f3f730dfa6cf54e5809c8a46ab05ffb615a8b Mon Sep 17 00:00:00 2001 From: mikecarper Date: Fri, 23 Oct 2026 03:38:19 -0700 Subject: [PATCH] Resolve HIL flag helper from project root in SCons context --- scripts/nrf52_hil_irq_observer.py | 10 +++++++--- test/test_nrf52_hil_irq_binding.py | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/scripts/nrf52_hil_irq_observer.py b/scripts/nrf52_hil_irq_observer.py index 536684ca0..d20a4a1d1 100644 --- a/scripts/nrf52_hil_irq_observer.py +++ b/scripts/nrf52_hil_irq_observer.py @@ -80,10 +80,12 @@ def replace_port(build_env, node): return _private_copy(build_env, node, patched_port, "port.c") -def observer_enabled(flags=(), cppdefines=()): +def observer_enabled(flags=(), cppdefines=(), parser_path=None): # Reuse the repository's effective -D/-U parser, including split -D values. + if parser_path is None: + parser_path = Path(__file__).with_name("nrf52_flash_trim.py") spec = importlib.util.spec_from_file_location( - "mesh_nrf52_flash_trim_irq_flags", Path(__file__).with_name("nrf52_flash_trim.py")) + "mesh_nrf52_flash_trim_irq_flags", parser_path) parser = importlib.util.module_from_spec(spec) spec.loader.exec_module(parser) values = parser.flag_defines(flags, cppdefines) @@ -97,7 +99,9 @@ def observer_enabled(flags=(), cppdefines=()): def install(build_env): - if not observer_enabled(build_env.get("BUILD_FLAGS", ()), build_env.get("CPPDEFINES", ())): + # SCons executes extra scripts without defining __file__. + parser_path = Path(build_env.subst("$PROJECT_DIR")) / "scripts/nrf52_flash_trim.py" + if not observer_enabled(build_env.get("BUILD_FLAGS", ()), build_env.get("CPPDEFINES", ()), parser_path): return if (build_env.PioPlatform().name != "nordicnrf52" or build_env.BoardConfig().get("build.mcu", "").lower() != "nrf52840"): diff --git a/test/test_nrf52_hil_irq_binding.py b/test/test_nrf52_hil_irq_binding.py index 39209dd57..31b0aba4b 100644 --- a/test/test_nrf52_hil_irq_binding.py +++ b/test/test_nrf52_hil_irq_binding.py @@ -82,6 +82,8 @@ class BuildEnv(dict): self.middleware.append((callback, pattern)) def subst(self, value): + if value == "$PROJECT_DIR": + return str(ROOT) assert value == "$BUILD_DIR" return str(self.build_dir) @@ -90,6 +92,17 @@ class BuildEnv(dict): class Nrf52HilIrqBindingTest(unittest.TestCase): + def test_actual_scons_entry_without_file_uses_project_root(self): + source = (ROOT / "scripts/nrf52_hil_irq_observer.py").read_text(encoding="ascii") + with tempfile.TemporaryDirectory() as directory: + for flag, expected in (("0", 0), ("1", 2)): + env = BuildEnv(Path(directory), "-DMESH_HIL_IRQ_TIMING=" + flag) + scope = {} + scope["Import"] = lambda name: scope.update({name: env}) + exec(compile(source, "nrf52_hil_irq_observer.py", "exec"), scope) + self.assertNotIn("__file__", scope) + self.assertEqual(len(env.middleware), expected) + def test_startup_changes_only_six_vectors_and_preserves_all_real_aliases(self): patched = PATCH.patched_startup(STARTUP.replace("\n", "\r\n")) self.assertEqual(PATCH.patched_startup(patched), patched)