diff --git a/CMakeLists.txt b/CMakeLists.txt index d4b121e..63ada4d 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 8be0608..7808152 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)