From e5c3d30ba08f28fe3fbd3985faa55810b9476bb3 Mon Sep 17 00:00:00 2001 From: ainyan03 <205502311+ainyan03@users.noreply.github.com> Date: Thu, 27 Aug 2026 07:35:39 +0000 Subject: [PATCH] Link the Arduino component automatically when built as an ESP-IDF component arduino-esp32 publishes its -DARDUINO... definitions as PUBLIC compile options, so they only reach components that link against it. The public headers of this library change class layout with ARDUINO (LGFXBase derives from Print only under ARDUINO, for example), so an application built with arduino-esp32 as a component read the same objects with a different layout than this library was compiled with: getPanel() returned null and drawing faulted. Pick the Arduino component that is already part of the build (arduino / arduino-esp32 / espressif__arduino-esp32, overridable with _ARDUINO_COMPONENT, OFF to disable) after register_component() and link it publicly. Only BUILD_COMPONENTS is consulted, so a build that excludes Arduino is unaffected, and only APIs that exist since ESP-IDF 4.x are used. --- CMakeLists.txt | 35 +++++++++++++++++++++++++++++++++-- README.md | 11 +++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4b121e2..63ada4df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,10 +41,41 @@ else() set(COMPONENT_REQUIRES nvs_flash efuse) endif() -### If you use arduino-esp32 components, please activate next comment line. -# list(APPEND COMPONENT_REQUIRES arduino-esp32) message(STATUS "M5GFX use components = ${COMPONENT_REQUIRES}") register_component() + +# Arduino as an ESP-IDF component: arduino-esp32 publishes -DARDUINO... as PUBLIC compile options, so +# they only reach components that link against it, while the public headers of this library change +# class layout with ARDUINO. Link the Arduino component publicly whenever it is part of the build so +# this library is compiled in the same mode as the application. Only components already selected +# for the build are considered; a build without Arduino is unaffected. +# M5GFX_ARDUINO_COMPONENT= use a differently named Arduino component +# M5GFX_ARDUINO_COMPONENT=OFF disable the automatic dependency +set(_m5gfx_arduino_candidates arduino arduino-esp32 espressif__arduino-esp32) +set(_m5gfx_arduino_enabled ON) +if(DEFINED M5GFX_ARDUINO_COMPONENT AND NOT "${M5GFX_ARDUINO_COMPONENT}" STREQUAL "") + if(M5GFX_ARDUINO_COMPONENT) + set(_m5gfx_arduino_candidates ${M5GFX_ARDUINO_COMPONENT}) + else() + set(_m5gfx_arduino_enabled OFF) + endif() +endif() +if(_m5gfx_arduino_enabled) + idf_build_get_property(_m5gfx_arduino_build_components BUILD_COMPONENTS) + set(_m5gfx_arduino_hits) + foreach(_m5gfx_arduino_name ${_m5gfx_arduino_candidates}) + if(_m5gfx_arduino_name IN_LIST _m5gfx_arduino_build_components) + list(APPEND _m5gfx_arduino_hits ${_m5gfx_arduino_name}) + endif() + endforeach() + list(LENGTH _m5gfx_arduino_hits _m5gfx_arduino_count) + if(_m5gfx_arduino_count GREATER 1) + message(FATAL_ERROR "M5GFX: several Arduino components are in the build (${_m5gfx_arduino_hits}). Set M5GFX_ARDUINO_COMPONENT to the one to use.") + elseif(_m5gfx_arduino_count EQUAL 1) + idf_component_get_property(_m5gfx_arduino_lib ${_m5gfx_arduino_hits} COMPONENT_LIB) + target_link_libraries(${COMPONENT_LIB} PUBLIC ${_m5gfx_arduino_lib}) + endif() +endif() diff --git a/README.md b/README.md index 8be0608e..7808152e 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Supported framework - Arduino for ESP32 + Supported device ---------------- - M5Stack ( Basic / Gray / GO / Fire ) @@ -38,6 +39,16 @@ Supported device - [AtomDisplay](docs/ATOMDisplay.md) / ModuleDisplay +Notes +---------------- +### Arduino as an ESP-IDF component +When arduino-esp32 is used as an ESP-IDF component together with this library, the library links +the Arduino component automatically (it looks for a component named `arduino`, `arduino-esp32` or +`espressif__arduino-esp32` in the build) so that it is compiled with the same `ARDUINO` definitions +as the application. Without that, the application and the library would see different layouts of the +same classes. Set `M5GFX_ARDUINO_COMPONENT=` (CMake cache variable) if your Arduino component +has another name, or `M5GFX_ARDUINO_COMPONENT=OFF` to disable this. + License ---------------- M5GFX : [MIT](LICENSE)