We have scenario where binary artifacts of a CMake project Foo
are pulled in through FetchContent. The project has some transitive dependency Bar
that is bundled within the project. The structure of the binary artifact of Foo is (partly) as follows:
lib/libfoo.a
lib/libbar.a
lib/cmake/Foo/FooConfig.cmake
lib/cmake/Foo/...etc.
lib/cmake/Bar/BarConfig.cmake
lib/cmake/Bar/...etc.
The config file of Foo
has a find_dependency(Bar)
call. Foo
is then pulled in as follows:
FetchContent_Declare(Foo ...)
FetchContent_MakeAvailable(Foo)
This results in the following CMake error:
CMake Error at /usr/local/.../CMakeFindDependencyMacro.cmake:76 (find_package):By not providing “FindBar.cmake” in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by “Bar”, but CMake did not find one.
This is solved by defining in between FetchContent_Declare
and FetchContent_MakeAvailable
the following:
list(PREPEND CMAKE_PREFIX_PATH "${foo_SOURCE_DIR}")
Is there a way to solve this in a more canonical way? Ideally, no additional configuration is necessary at the consumer side to be able to depend on Foo
.