include vs find_package rules of thumb

Looking for what other people use as rules of thumb for using include() vs find_package().

I was reviewing a PR and a coworker wanted to use find_package() to pull in a CMake module that defined some helpful functions. The rationale was that to use include() would be tricky because most likely the module would be pulled in not only by the project but also by the project’s dependencies. In this example all projects are our own.

My objection was that find_package(), in module mode, brings in a FindXYZ.cmake file not just to define functions but to find something other than itself - i.e. an executable or library. A CMake module that exists to define functions is also a module, but if it’s not there to find something external to itself it IMO doesn’t meet the criteria for being brought in with a find_package() call.

It seems like include(repeatedly_used_module) would make more sense if we just added include_guard(GLOBAL) to the module itself. Is this the right way to think about this? Is using include_guard a code smell in any way?

1 Like

You are 100% right. Just put an include guard in the module and use include().

find_package is usually used to import targets. You could have a package of all cmake modules, but that would still imply a separate package/distribution, not simply a module distributed as part of your source tree.

3 Likes