From f5ce3f8c8b5054792bd95f776c26b7f12d395903 Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Mon, 18 Aug 2025 16:52:47 +0200 Subject: [PATCH] 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 --- tools/cmakev2/compat.cmake | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tools/cmakev2/compat.cmake b/tools/cmakev2/compat.cmake index 6f5a2e469e4..3e41f72d05c 100644 --- a/tools/cmakev2/compat.cmake +++ b/tools/cmakev2/compat.cmake @@ -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[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} "$<$:${req_interface}>") + endforeach() +endfunction()