diff --git a/scripts/nrf52_flash_trim.py b/scripts/nrf52_flash_trim.py index 9efa478bd..104243055 100644 --- a/scripts/nrf52_flash_trim.py +++ b/scripts/nrf52_flash_trim.py @@ -259,10 +259,9 @@ def compile_packed_help(build_env, node): return build_env.Object(node, **flags) -def compile_shared_cli_replies(build_env, node): - """Keep constant reply copies out of line in the qualified CLI object only.""" +def _compile_shared_reply_copies(build_env, node, relative_path): source = Path(node.srcnode().get_abspath()).resolve() - expected = Path(build_env.subst("$PROJECT_DIR/src/helpers/CommonCLI.cpp")).resolve() + expected = Path(build_env.subst("$PROJECT_DIR/" + relative_path)).resolve() if source != expected: return node # GCC otherwise expands many constant strcpy replies into repeated stores. @@ -273,6 +272,25 @@ def compile_shared_cli_replies(build_env, node): return build_env.Object(node, CCFLAGS=flags) +def compile_shared_cli_replies(build_env, node): + """Keep constant reply copies out of line in the qualified CLI object only.""" + return _compile_shared_reply_copies(build_env, node, "src/helpers/CommonCLI.cpp") + + +def compile_shared_companion_replies(build_env, node): + return _compile_shared_reply_copies(build_env, node, "examples/companion_radio/MyMesh.cpp") + + +def companion_shared_replies_policy(platform, mcu, env_name, src_filter, + flags=(), cppdefines=()): + if env_name != "RAK_4631_companion_radio_full" or str(mcu).lower() != "nrf52840": + return False + values = flag_defines(flags, cppdefines) + return (bool(companion_trim_policy(platform, mcu, src_filter, flags, cppdefines)) + and ("MESH_CLI_SHARE_REPLIES" not in values + or _enabled(values["MESH_CLI_SHARE_REPLIES"]))) + + def install(build_env): platform = build_env.PioPlatform() mcu = build_env.BoardConfig().get("build.mcu", "") @@ -304,6 +322,10 @@ def install(build_env): if rak_repeater and _enabled(values.get("MESH_CLI_SHARE_REPLIES", definitions.get("MESH_CLI_SHARE_REPLIES"))): build_env.AddBuildMiddleware(compile_shared_cli_replies, "*CommonCLI.cpp") + if not infrastructure and companion_shared_replies_policy( + platform.name, mcu, build_env["PIOENV"], build_env.get("SRC_FILTER", ()), + flags, cppdefines): + build_env.AddBuildMiddleware(compile_shared_companion_replies, "*MyMesh.cpp") if infrastructure: names = ("local OTA target name only" if definitions.get("OTA_TARGET_NAME_TABLE") == 0 or flag_defines(flags, cppdefines).get("OTA_TARGET_NAME_TABLE") == "0" diff --git a/test/test_nrf52_flash_trim.py b/test/test_nrf52_flash_trim.py index 9b4788840..74dad3fbc 100644 --- a/test/test_nrf52_flash_trim.py +++ b/test/test_nrf52_flash_trim.py @@ -124,6 +124,33 @@ class Nrf52FlashTrimTest(unittest.TestCase): other = Node(ROOT / "test/fixtures/CommonCLI.cpp") self.assertIs(TRIM.compile_shared_cli_replies(env, other), other) + companion = Node(ROOT / "examples/companion_radio/MyMesh.cpp") + result, flags = TRIM.compile_shared_companion_replies(env, companion) + self.assertIs(result, companion) + self.assertEqual(flags["CCFLAGS"], ["-Os", "-flto", "-fno-builtin-strcpy"]) + self.assertEqual(env["CCFLAGS"], ["-Os", "-flto"]) + for path in ("examples/simple_repeater/MyMesh.cpp", "test/fixtures/MyMesh.cpp", + "src/helpers/CommonCLI.cpp", "src/helpers/RadioProfileCLI.cpp"): + other = Node(ROOT / path) + self.assertIs(TRIM.compile_shared_companion_replies(env, other), other) + + def test_companion_shared_replies_are_limited_to_the_qualified_rak_full_image(self): + defaults = dict(platform="nordicnrf52", mcu="nrf52840", + env_name="RAK_4631_companion_radio_full", + src_filter="+<../examples/companion_radio/*.cpp>") + self.assertTrue(TRIM.companion_shared_replies_policy(**defaults)) + for changes in (dict(env_name="RAK_3401_companion_radio_full"), + dict(env_name="RAK_4631_companion_radio_usb"), + dict(env_name="custom_companion_radio_full"), + dict(mcu="nrf52832"), dict(platform="ststm32"), + dict(src_filter="+<../examples/simple_repeater>"), + dict(src_filter="+<../examples/companion_radio> +<../examples/simple_sensor>"), + dict(flags="-DMESH_CLI_SHARE_REPLIES=0"), + dict(flags="-UMESH_CLI_SHARE_REPLIES"), + dict(flags="-DMESH_NRF52_COMPANION_TRIM=0")): + with self.subTest(changes=changes): + self.assertFalse(TRIM.companion_shared_replies_policy(**(defaults | changes))) + def test_local_names_do_not_change_companion_or_unqualified_roles(self): for options in (dict(name="RAK_4631_companion_radio_full"), dict(role="custom"), dict(platform="espressif32", mcu="esp32")):