From 6a737bcee23d5ce933dd8595c1e1fb71b4516823 Mon Sep 17 00:00:00 2001 From: Ashley Strahle Date: Tue, 30 Jun 2026 07:15:14 +0000 Subject: [PATCH] fix: patch nimconfig.h with #ifndef guards for ROLE defines to eliminate redefinition warnings --- pre_build.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/pre_build.py b/pre_build.py index 47bc48e..9b383e1 100644 --- a/pre_build.py +++ b/pre_build.py @@ -100,5 +100,66 @@ def patch_sdkconfig_bt(): print("BLE/NimBLE already configured in sdkconfig") +def patch_nimconfig(): + # Find nimconfig.h in the NimBLE-Arduino library + search_dirs = [ + os.path.join(os.getcwd(), ".pio", "libdeps"), + os.path.expanduser("~/.platformio/lib"), + ] + for env_dir in ["esp32s3", ""]: + candidate = os.path.join( + os.getcwd(), ".pio", "libdeps", + env_dir, "NimBLE-Arduino", "src", "nimconfig.h" + ) if env_dir else "" + if candidate and os.path.exists(candidate): + nimconfig_path = candidate + break + else: + # Walk search dirs as fallback + nimconfig_path = None + for base in search_dirs: + if not os.path.exists(base): + continue + for root, _dirs, files in os.walk(base): + if "nimconfig.h" in files: + nimconfig_path = os.path.join(root, "nimconfig.h") + break + if nimconfig_path: + break + + if not nimconfig_path: + print("WARNING: nimconfig.h not found, skipping ROLE define patch") + return + + print(f"Patching {nimconfig_path} with #ifndef guards for ROLE defines") + + with open(nimconfig_path, 'r') as f: + content = f.read() + + for role in ["CENTRAL", "OBSERVER", "PERIPHERAL", "BROADCASTER"]: + pattern = re.compile( + '#ifndef CONFIG_BT_NIMBLE_ROLE_' + role + '_DISABLED\n' + '#define CONFIG_BT_NIMBLE_ROLE_' + role + '\n' + '#endif' + ) + replacement = ( + '#ifndef CONFIG_BT_NIMBLE_ROLE_' + role + '_DISABLED\n' + '#ifndef CONFIG_BT_NIMBLE_ROLE_' + role + '\n' + '#define CONFIG_BT_NIMBLE_ROLE_' + role + '\n' + '#endif\n' + '#endif' + ) + if pattern.search(content): + content = pattern.sub(replacement, content) + print(f" Guarded CONFIG_BT_NIMBLE_ROLE_{role}") + else: + print(f" NOTE: CONFIG_BT_NIMBLE_ROLE_{role} pattern not found (may already be guarded)") + + with open(nimconfig_path, 'w') as f: + f.write(content) + print("nimconfig.h patched successfully") + + patch_usb_ids() -patch_sdkconfig_bt() \ No newline at end of file +patch_sdkconfig_bt() +patch_nimconfig() \ No newline at end of file