Installing CMake library: can you also find modules for dependencies?
My CMake library MyLibrary
has a dependency for OtherLibrary
which I import with non-standard FindOtherLibrary.cmake
.
My library depends on OtherLibrary
publicly:
target_link_libraries(MyLibrary PUBLIC OtherLibrary::OtherLibrary)
When I install MyLibrary
(along with MyLibraryConfig.cmake
) and users want to link to it, they need to import OtherLibrary
.
Is there a good practice for spreading FindOtherLibrary.cmake
along MyLibrary
?
Ideally, MyLibrary
it could be made easier for users by importing OtherLibrary
automatically from an installed config file MyLibraryConfig.cmake
if it contains something like
include(CMakeFindDependencyMacro)
find_dependency(OtherLibrary)
and knows where FindOtherLibrary.cmake
.
Is it possible?
source to share
I ended up finding a solution to my question.
Basically, it does what @utopia suggested, but in an automatic way: the end user of my library doesn't need to configure (or even know) FindOtherLibrary.cmake
. It will be automatically imported with MyLibraryConfig.cmake
.
For this I install FindOtherLibrary.cmake
along MyLibraryConfig.cmake
:
install(FILES
/path/to/MyLibraryConfig.cmake
DESTINATION
lib/cmake/MyLibrary
)
install(FILES
/path/to/FindOtherLibrary.cmake
DESTINATION
lib/cmake/MyLibrary/Modules
)
And in MyLibraryConfig.cmake
I installed how to import it:
include(CMakeFindDependencyMacro)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/Modules/")
find_dependency(OtherLibrary REQUIRED)
Note that I am setting the variable CMAKE_MODULE_PATH
because it is not possible to specify the location of the search modules in find_package
or find_dependency
(only works for config mode).
source to share
"Transient" behavior for modular mode is find_package()
not supported.
In fact, I don't think it is even possible, as it requires changing the path to the downstream CMake module with information that you would not have for you. This is one of the reasons for the presence of the configuration mode find_package()
(see here ).
To be clear, a user of your library who has a dependency on the FindModule library has no choice but to know how to get a copy of the FindModule script and add it to their CMake module path. This is usually done through documentation. You, as the author of the library using FindModule, cannot complete or partially complete this process for the end user. So there is no "good practice" for such a process.
Otherwise, it is good practice to use FindModules for non-CMake projects only, and use Config.cmake for CMake projects. If the dependent CMake library does not have Config.cmake then you are out of luck (tell them they need CMake support in the bug / issue report).
source to share