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

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake-developers

Reply via email to