abuse / misuse of find_package?

I am reviewing at my work some code that a developer submitted and a big red flag went up for me. I wanted to make sure I’m not the one misunderstanding find_package before I really dig in with my objections to some changes. I want to be sure our project uses CMake in an idiomatic way as much as possible.

In essence this person is using CMake and find_package in the following way. Executable target A has in its CMakeLists.txt a call to find_package(B). Dependency B has a B.cmake file located in some folder that was added to the project via CMAKE_MODULE_PATH. B.cmake internally does an add_subdirectory(B). In B’s subdirectory it has a regular CMakeLists.txt which builds itself.

My understanding of idiomatic usage of find_package is stuff like this:

find_package(Qt5 COMPONENTS Core QuickCompiler REQUIRED)
or
find_package(Boost)

neither of the above two lines would attempt to build Qt or Boost. Maybe this whole question is stupid, but my understanding of find_package is that it tries to find, not build.

Am I right to object to the proposed usage?

Yes, you are right.

To achieve what you describe, you have to manage a super-build using FetchContent or ExternalProject.

Or more simply, include directly add_subdirectory(B) in your CMake project.

1 Like

Ultimately I think it might be a good idea to use a package manager like Conan or vcpkg to create recipes for building dependencies and the projects that depend on them reliably. We’re on windows, so those seem like to the two most commonly used options. Before the proposed changes, we were simply doing
add_subdirectory(B)
but there is a desire to make things more granular so B should probably be built separately.