Resolve HIL flag helper from project root in SCons context

This commit is contained in:
mikecarper committed 2026-10-23 03:38:19 -07:00
1 parent 7a6a118775
commit 286f3f730d
2 files changed
+20 -3

No files matched your search

+7 -3
View File
@@ -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"):
+13
View File
@@ -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)