[cmake-developers] [CMake] Should configuration package files define module package variables?

Robert Dailey rcdailey.lists at gmail.com
Tue Sep 5 10:38:38 EDT 2017


On Sat, Sep 2, 2017 at 3:08 AM, P F <pfultz2 at yahoo.com> wrote:
> In general, if the library does not have any dependencies, you can just export the targets directly to the config.cmake file:
>
> install(TARGETS foo EXPORT foo-config)
> install(EXPORT foo-config DESTINATION lib/cmake/foo)
>
> However, if the library has dependencies, you will need to iterate over them in the config.cmake file. That is if `foo` uses zlib, like this:
>
> find_package(ZLIB)
> target_link_libraries(foo ZLIB::ZLIB)
>
> Then the foo-config.cmake needs to do this(assuming the export targets are written to foo-targets):
>
> include(CMakeFindDependencyMacro)
> find_dependency(ZLIB)
> include("${CMAKE_CURRENT_LIST_DIR}/foo-targets.cmake”)
>
> The reason for this is that the imported foo target will link against ZLIB::ZLIB, but it will not define it. Actually, the generated foo-targets.cmake will create an error if ZLIB::ZLIB doesn’t exists, which is why `find_dependency(ZLIB)`(which is really just `find_package` underneath) needs to be called before including the foo-targets.cmake.

When you throw components in the mix, where you have 1 targets.cmake
exported per component, each may have its own find_dependency()
requirements. It would be nice if that was self-contained in the
targets.cmake script itself, but there's no examples I could find on
how to manage per-component find dependencies like this, especially if
those components are put into `OPTIONAL_COMPONENTS`.

Thanks for the information, it is very helpful!


More information about the cmake-developers mailing list