feat(cmakev2/compat): add idf_component_optional_requires function

Add the specified component as a dependency only if it is included in
the build. This functions the same way as in cmakev1, but it uses
generator expressions because cmake2 does not maintain the
BUILD_COMPONENTS build property, which would list all components
included in the build.

Signed-off-by: Frantisek Hrbata <frantisek.hrbata@espressif.com>
This commit is contained in:
Frantisek Hrbata
2025-08-18 16:52:47 +02:00
parent 8d2ec65c5a
commit f5ce3f8c8b

View File

@@ -137,3 +137,24 @@ endfunction()
function(target_linker_script target deptype scriptfiles)
# FIXME: This is just a placeholder without implementation.
endfunction()
#[[api
.. cmakev2:function:: idf_component_optional_requires
.. code-block:: cmake
idf_component_optional_requires(<type> <component>...)
:type[in]: Type of dependency: PRIVATE, PUBLIC or INTERFACE
:component[in]: The component name that should be added as a dependency to
the evaluated component. It may be provided multiple times.
Add a dependency on a specific component only if it is included in the build.
#]]
function(idf_component_optional_requires req_type)
set(optional_reqs ${ARGN})
foreach(req ${optional_reqs})
__get_component_interface(COMPONENT "${req}" OUTPUT req_interface)
target_link_libraries(${COMPONENT_TARGET} ${req_type} "$<$<TARGET_EXISTS:${req_interface}>:${req_interface}>")
endforeach()
endfunction()