Structuring cross-platform libs for ESP32 (espidf)

I’m interested in porting some (mostly C) libs that I have to also support the esp32 through the espidf framework. These libraries all have a library.json and were written for platforms like the STM32. In practice, the libs depend on other libs also containing a library.json and so on.

There will be additional FreeRTOS integration work I will need to do to make them compatible with espidf, I know, but I am unable to work out a good way to manage libraries in a cross platform way.

For example. Readout KConfig from ESP-IDF libraries added via lib_deps, too · Issue #453 · platformio/platform-espressif32 · GitHub and many other references suggest maintaining a copy of the library in the project’s lib/ folder, with the necessary CMakeLists.txt added there. This is something I would really like to avoid, since this is essentially going to mean maintaining a separate copy of the lib, either per-project or atleast for espidf. Note that the CMakeLists.txt for espidf projects are different from vanilla cmake, with a lot of espressif tooling involved.

Is there a good and clean way to achieve a cross-platform library which can be published as used using platformIO libdeps? If not this, is there a recommended way to have a CMakeLists.txt in each library an alternate build instruction for espidf contexts, and telling platformIO to make these libraries available to espressif’s cmake build system, ideally using something in library.json or otherwise library-local.

If you’re building an ESP-IDF component, just make it compatible with the ESP-IDF build system and upload it to their component registry.

PlatformIO projects that use framework = espidf can then include it normally, not via lib_deps but via the IDF component manager’s idf_component.yml, like example. Don’t create extra files for PlatformIO.