From 0480f43984f51a3a0d60831f0d6830ca3d981e91 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 10 Apr 2026 16:30:22 -1000 Subject: [PATCH] [ota] Use ESPHOME_USE_OTA build flag to avoid USE_OTA redefinition warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Emitting -DUSE_OTA alongside the defines.h #define USE_OTA entry caused 'USE_OTA redefined' warnings in every TU that includes defines.h. Use a distinct ESPHOME_USE_OTA name for the compiler -D flag — only the .c files that cannot include defines.h (lwip_fast_select.c) reference the ESPHOME_-prefixed name, and everyone else continues to use USE_OTA via defines.h. --- esphome/components/ota/__init__.py | 12 ++++++------ esphome/core/lwip_fast_select.c | 18 ++++++++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py index e7a362ca10..3abfd2c69e 100644 --- a/esphome/components/ota/__init__.py +++ b/esphome/components/ota/__init__.py @@ -102,13 +102,13 @@ BASE_OTA_SCHEMA = cv.Schema( @coroutine_with_priority(CoroPriority.OTA_UPDATES) async def to_code(config): - # Both: add_define keeps defines.h in sync for static analyzers / IDEs that read it, - # while add_build_flag passes -DUSE_OTA as a compiler flag so USE_OTA is visible in .c - # translation units that cannot include defines.h (lwip_fast_select.c in particular — - # including defines.h would drag in macros.h → Arduino.h under Arduino builds and - # break the C compile). Needed for the fast-select OTA wake hook. cg.add_define("USE_OTA") - cg.add_build_flag("-DUSE_OTA") + # Separate compiler -D flag using an ESPHOME_-prefixed name so .c translation units + # (which cannot include defines.h because macros.h → Arduino.h breaks the C compile + # under Arduino builds) can still tell OTA is compiled in. Needed by the fast-select + # OTA wake hook in lwip_fast_select.c. A distinct name avoids the "USE_OTA redefined" + # warning that would fire if we also emitted -DUSE_OTA — defines.h already has it. + cg.add_build_flag("-DESPHOME_USE_OTA") CORE.add_job(final_step) if CORE.is_rp2040 and CORE.using_arduino: diff --git a/esphome/core/lwip_fast_select.c b/esphome/core/lwip_fast_select.c index ab20bdd29e..613d1f29ec 100644 --- a/esphome/core/lwip_fast_select.c +++ b/esphome/core/lwip_fast_select.c @@ -157,15 +157,16 @@ _Static_assert(offsetof(struct lwip_sock, rcvevent) == ESPHOME_LWIP_SOCK_RCVEVEN // Saved original event_callback pointer — written once in first hook_socket(), read from TCP/IP task. static netconn_callback s_original_callback = NULL; -#ifdef USE_OTA +#ifdef ESPHOME_USE_OTA // Extern wake hook for the OTA component (implemented in application.cpp). Called from the // TCP/IP task on every NETCONN_EVT_RCVPLUS — not just OTA's listener, so this can be a false // wake from an unrelated monitored socket. OTA::loop() handles that by disabling itself again // when there is no pending work. The hook only marks the OTA component as pending loop-enable; // it does not itself wake the main task (the caller below already does that). -// NOTE: USE_OTA reaches this file only because ota/__init__.py adds it as a build flag -// (not a cg.add_define). defines.h cannot be included from this .c file (it pulls in -// macros.h → Arduino.h under Arduino builds). +// NOTE: ESPHOME_USE_OTA (not USE_OTA) because USE_OTA only lives in defines.h, and this .c +// file cannot include defines.h — macros.h → Arduino.h would break the C compile under +// Arduino builds. ota/__init__.py emits -DESPHOME_USE_OTA as a build flag specifically so +// this file can see it without a name collision with the defines.h USE_OTA entry. extern void esphome_wake_ota_component_any_context(void); #endif @@ -183,11 +184,12 @@ static void esphome_socket_event_callback(struct netconn *conn, enum netconn_evt // (rcvevent++ with a NULL pbuf or error in recvmbox), so error conditions // already wake the main loop through the RCVPLUS path. if (evt == NETCONN_EVT_RCVPLUS) { - // Invoke the OTA wake hook BEFORE xTaskNotifyGive — if OTA is compiled in, this marks - // its component pending-enable, and those flags must be visible before we wake the - // main task. Otherwise the main loop could run a full iteration without seeing the - // pending-enable request. When OTA is not compiled in, this function's body is empty. +#ifdef ESPHOME_USE_OTA + // Mark the OTA component pending-enable BEFORE xTaskNotifyGive — the flags must be + // visible before we wake the main task, otherwise the main loop could run a full + // iteration without seeing the pending-enable request. esphome_wake_ota_component_any_context(); +#endif TaskHandle_t task = esphome_main_task_handle; if (task != NULL) { xTaskNotifyGive(task);