This is a bit an exotic situation which should be clearer with the example of using OpenSSL.
I’m making a first library, let say A that uses only one OpenSSL components, for instance OpenSSL::SSL.
I’m then making a second library B that uses possibly all OpenSSL components (Crypto, SSL,…). But what I’m doing in this case is that I automatically create an OpenSSL::OpenSSL target that interfaces all the components (see Difficulties using a third-party cmake projects: missing <package>_INCLUDE_DIR(S) (and other) - #9 by adaldev):
get_property(ImportedTargets DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY IMPORTED_TARGETS)
list(FILTER ImportedTargets INCLUDE REGEX "^${PackName}::")
if(ImportedTargets)
# Creating the main component
add_library(${PackName}::${PackName} INTERFACE IMPORTED)
# Adding the found components as dependencies
if(ImportedTargets)
target_link_libraries(${PackName}::${PackName} INTERFACE ${ImportedTargets})
endif()
endif()
For each library, a configuration file is automatically created using this template:
include(CMakeFindDependencyMacro)
foreach(_lib_ IN ITEMS @REQUIRED_DEPENDENCIES@)
if(_lib_ MATCHES "::")
# Dependency to a single component
string(REPLACE "::" ";" _parts "${_lib_}")
list(GET _parts 0 Package)
list(GET _parts 1 Component)
# Check if the component was not already imported
if(NOT TARGET ${_lib_})
# Import the specified component
find_dependency(${Package} REQUIRED COMPONENTS ${Component})
endif()
else()
# Full package
find_dependency(${_lib_} REQUIRED)
endif()
endforeach()
# include the generated target file
include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_NAME@Targets.cmake")
Where REQUIRED_DEPENDENCIES is the list of dependencies for the library being created:
| library | REQUIRED_DEPENDENCIES |
|---|---|
| A | OpenSSL::SSL |
| B | OpenSSL |
Thus, if I create an application App linking both A and B, I think that OpenSSL::SSL is imported twice. How can I avoid to import again OpenSSL::SSL in App, when OpenSSL::OpenSSL has already been imported?
NB maybe it’s just a misconception on my part as the find_dependency are used to set paths right and the actual binary import is done elsewhere. Thus, maybe I’m only doing unnecessary find_package calls but no multiple “addition” of the OpenSSL::SSL binaries to App
Regards,
A.