Easily incorporate 3rd party CMake modules

Incorporating 3rd party cmake modules is a bit of a pain. I have experimented with multiple approaches (FetchContent, git-submodules, etc.).

But ultimately I’d like a very simple solution that doesn’t require too much CMake knowledge and is very obvious.

cmake_module_manager(
    GIT_URL "https://github.com/cpm-cmake/CPM.cmake"
    TAG "0.34.0"
)

This will download the repo and cache it into a CMake module cache, so it only needs to be downloaded once. And it will automatically add the module the to CMAKE_MODULE_PATH.

To clear the downloads it would be something like:

cmake --clean-modules

Thoughts?

What about

CPMAddPackage(
  NAME          # The unique name of the dependency (should be the exported 
target's name)
  VERSION       # The minimum version of the dependency (optional, defaults to 
0)
  OPTIONS       # Configuration options passed to the dependency (optional)
  DOWNLOAD_ONLY # If set, the project is downloaded, but not configured 
(optional)
  [...]         # Origin parameters forwarded to FetchContent_Declare, see 
below
)

?

See GitHub - cpm-cmake/CPM.cmake: 📦 CMake's missing package manager. A small CMake script for setup-free, cross-platform, reproducible dependency management.
IMHO it is the best dependency manager for cmake; I wish it would be shipped
with cmake.

/Martin

@15knots I’m talking about a system for just CMake code. Not CMake projects. IE someone wrote useful CMake code and I want to use it in my project. CPM is an example of CMake code I’d like to easily incorporate into my project. The question is how to incorporate 3rd party CMake code in the first place.

I’ve had trouble getting CMAKE_MODULE_PATH to propogate outwards to containing directories, even when setting it in the cache or trying PARENT_SCOPE. I eventually resorted to having my cmake module package’s config script set a MY_CMAKE_MODULES_PATH variable that the user can then use like so:

find_package (my_cmake_modules)
list (APPEND CMAKE_MODULE_PATH ${MY_CMAKE_MODULES_PATH})

If I’m missing something here, or if there’s another way to handle this, I’d love to know what it is!

find_package inner code doesn’t run with a new scope, so PARENT_SCOPE shouldn’t be necessary. Given that other things are directory scoped as well, the default scoping for config.cmake files hasn’t been an issue for me (callers just need to use find_package “high enough” for it to work).

1 Like

I see. Perhaps it’s an issue related to the CPM.cmake package manger…

Does this assume the cmake related files are all in cmake/? I think i would like to have the ability to specify only the files inside this repository to be downloaded.