[cmake-developers] find_dependency with optional dependencies
Ruslan Baratov
ruslan_baratov at yahoo.com
Tue Apr 21 06:16:53 EDT 2015
Hi,
I'm trying to figure out what is the best way to use 'find_dependency'
if dependent package is optional (e.g. can be controlled by some build
option):
# CMakeLists.txt
option(BOO_WITH_FOO "Build Boo with Foo" ON)
if(BOO_WITH_FOO)
find_package(Foo CONFIG REQUIRED)
target_link_libraries(... PUBLIC Foo::foo)
endif()
1. Use option BOO_WITH_FOO in configure BooConfig.cmake first:
# BooConfig.cmake.in
if(@BOO_WITH_FOO@)
find_dependency(Foo)
endif()
this approach works fine but it obviously violates "do not repeat
yourself" principle and can look quite messy if we have deal with non
trivial chain of "if"s in multiple subdirectories.
2. Use regular 'find_package' with option QUIET
# BooConfig.cmake
find_package(Foo QUIET)
this works fine too, but lead to "strange" compile time errors instead
of simple CMake: "FooConfig.cmake not found"
Since the fact that target "Foo::foo" is linked is published
(IMPORTED_LINK_INTERFACE_*) in case BOO_WITH_FOO=ON and not published
when BOO_WITH_FOO=OFF it seems that we have enough information about
dependency to figure out that 'find_package(Foo)' not needed if
BOO_WITH_FOO is OFF. So may be there is a way to improve find_dependency
behaviour so it will work fine with optional packages? Does it sounds
reasonable?
Thanks, Ruslo
More information about the cmake-developers
mailing list