From 71a884571c94fc32c8e20379172334d58b35b557 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 1 Dec 2021 23:09:12 +0100 Subject: [PATCH] cmake: add idf_component_optional_requires utility function It can be used in component CMakeLists.txt files when adding "weak" dependencies between component. A "weak" dependency from A to B is similar to target_link_libraries(A PRIVATE B), but it gets added only if B is included in the build. Use it instead of specifying B as part of PRIV_REQUIRES when the component can be built (even if with reduced functionality) without component B being available. --- tools/cmake/component.cmake | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tools/cmake/component.cmake b/tools/cmake/component.cmake index 5e3053dba5..e28a52f4ca 100644 --- a/tools/cmake/component.cmake +++ b/tools/cmake/component.cmake @@ -576,6 +576,28 @@ function(idf_component_mock) ) endfunction() +# idf_component_optional_requires +# +# @brief Add a dependency on a given component only if it is included in the build. +# +# Calling idf_component_optional_requires(PRIVATE dependency_name) has the similar effect to +# target_link_libraries(${COMPONENT_LIB} PRIVATE idf::dependency_name), only if 'dependency_name' +# component is part of the build. Otherwise, no dependency gets added. Multiple names may be given. +# +# @param[in] type of the dependency, one of: PRIVATE, PUBLIC, INTERFACE +# @param[in, multivalue] list of component names which should be added as dependencies +# +function(idf_component_optional_requires req_type) + set(optional_reqs ${ARGN}) + idf_build_get_property(build_components BUILD_COMPONENTS) + foreach(req ${optional_reqs}) + if(req IN_LIST build_components) + idf_component_get_property(req_lib ${req} COMPONENT_LIB) + target_link_libraries(${COMPONENT_LIB} ${req_type} ${req_lib}) + endif() + endforeach() +endfunction() + # # Deprecated functions #