Using a Find*.cmake file with a header only library

Viewed 76

I am creating a header only library (Library A) that includes another library (Library B) using its FindB.cmake file.

I include the FindB.cmake file by having it in a cmake directory and doing:

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

find_package(B REQUIRED)

target_link_libraries(A INTERFACE B::B)

The tests on Library A all work fine so B is included successfully in A. To make B available to projects that want to include A, I have in my AConfig.cmake.in :

include(CMakeFindDependencyMacro)
find_dependency(B REQUIRED)

But when I try to make an application (App C) that uses Library A (with find_package(A REQUIRED)), I get an error that cmake cannot find the FindB.cmake file. The error goes away if I put the FindB.cmake file in the App C project and include it like I did with Library A, but I don't want people using Library A to need the FindB.cmake file.

Is there a way to make all of the location information from FindB.cmake carry over from Library A to App C without needing to include the actual FindB.cmake file?

1 Answers

@Tsyvarev had the answer.

I added this to install the FindB.cmake:

install(FILES "${PROJECT_SOURCE_DIR}/cmake/FindB.cmake"
        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/A/cmake
)

Then in my AConfig.cmake.in, I edited it with the following:

set(CMAKE_MODULE_PATH @CMAKE_INSTALL_DATAROOTDIR@/A/cmake @CMAKE_MODULE_PATH@)

find_dependency(B REQUIRED)

I can now add Library A to App C with find_package(A REQUIRED) and everything transfers over.

Thanks again @Tsyvarev

Related