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.
This commit is contained in:
Ivan Grokhotkov 2021-12-01 23:09:12 +01:00
parent 2934010950
commit 71a884571c

View File

@ -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
#