Where can a project install its CMake modules

If some project provides its own CMake modules, where would be the best place to install them on the system so other projects can simply use them via include() and find_package()?

Does CMake have any such additional path out of the box somehow?

I’d also be curios to know what is the best/common practice for this.

In our project we have our own CMake modules too, and we have been installing them into ${CMAKE_INSTALL_PREFIX}/share/OurPackage/modules/ as a part of a relocatable package, so the resulting folder structure is this:

β”œβ”€β”€ debug
β”‚   └── lib
β”‚       └── [ ... debug binaries ... ]
β”œβ”€β”€ include
β”‚   └── our-package
β”‚       └── [ ... public headers ... ]
β”œβ”€β”€ lib
β”‚   └── [ ... release binaries ... ]
└── share
    └── OurPackage
        β”œβ”€β”€ modules # here are the modules
        β”‚    β”œβ”€β”€ some.cmake
        β”‚    β”œβ”€β”€ thing.cmake
        β”‚    └── another.cmake
        β”œβ”€β”€ OurPackageConfig.cmake
        β”œβ”€β”€ OurPackageConfigVersion.cmake
        β”œβ”€β”€ OurPackageTargets-debug.cmake
        β”œβ”€β”€ OurPackageTargets-release.cmake
        └── OurPackageTargets.cmake

And then of course our users aside from providing path to our package in CMAKE_PREFIX_PATH (or in OurPackage_DIR) also need to add path to our modules (/path/to/our/package/prefix/share/OurPackage/modules) into their project’s CMAKE_MODULE_PATH.

Not sure if this is how it should be done, but it seems to be working fine so far.

1 Like

I’d just make a package. If you want users to then include things piece-by-piece, offer a variable for where they live:

find_package(YourCoolPackage)
list(INSERT CMAKE_MODULE_PATH 0
  ${YourCoolPackage_CMAKE_MODULE_PATH})
include(YourCoolPackage_API)

Then you provide a YourCoolPackageConfig.cmake in its normal search locations (<prefix>/share/cmake/YourCoolPackage-1.0/YourCoolPackageConfig.cmake).

If piece-by-piece isn’t necessary, just include() the API bits in the package itself.

2 Likes