Handle custom Findfoo.cmake in dependency

For my library bla I have a Config.cmake.in that does a find_dependency(foo) because bla depends on foo and by using find_dependency(foo) I can propogate this dependency to any project that uses bla via find_package(bla REQUIRED).

This works (the find_package(bla) part), however this foo dependency is resolved via a custom Findfoo.cmake file that is part of the bla project and not available to any downstream projects.

i.e: I install bla to /usr/local/... via sudo make install and it writes a bunch of things:

  • The library itself /usr/local/lib/libbla.so
  • The CMake config file(s) so that find_package(bla REQUIRED) is possible under /usr/local/lib/cmake/bla

But it does not write Findfoo.cmake anywhere.

How to handle this so that downstream projects also have access to this custom Findfoo.cmake file?

I was able to solve this by installing Find*.cmake files to the cmake directory:

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/"
        DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/bla/modules/"
        FILES_MATCHING
        PATTERN "*.cmake"
        )

Then in Config.cmake.in I can append modules/ to CMAKE_MODULE_PATH:

@PACKAGE_INIT@

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/modules/")

include(CMakeFindDependencyMacro)
find_dependency(foo)

include("${CMAKE_CURRENT_LIST_DIR}/blaTargets.cmake")

check_required_components(bla)