# Easily incorporate 3rd party CMake modules

**URL:** <https://discourse.cmake.org/t/easily-incorporate-3rd-party-cmake-modules/4875>\
**Category:** Development\
**Created:** [January 19, 2022, 4:51pm UTC](https://discourse.cmake.org/t/easily-incorporate-3rd-party-cmake-modules/4875 "2022-01-19T16:51:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)\
**Post date:** [January 19, 2022, 4:51pm UTC](https://discourse.cmake.org/t/easily-incorporate-3rd-party-cmake-modules/4875/1 "2022-01-19T16:51:34Z")

</div>

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.

```auto
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](https://cmake.org/cmake/help/latest/variable/CMAKE_MODULE_PATH.html?highlight=cmake_module_path).

To clear the downloads it would be something like:

```auto
cmake --clean-modules

```

Thoughts?

---

<div class="post-metadata">

**Author:** ![15knots](https://discourse.cmake.org/user_avatar/discourse.cmake.org/15knots/32/188_2.png) [@15knots](https://discourse.cmake.org/u/15knots)\
**Post date:** [January 19, 2022, 8:10pm UTC](https://discourse.cmake.org/t/easily-incorporate-3rd-party-cmake-modules/4875/2 "2022-01-19T20:10:04Z")

</div>

What about

```auto
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.](https://github.com/cpm-cmake/CPM.cmake#usage)  
IMHO it is the best dependency manager for cmake; I wish it would be shipped  
with cmake.

/Martin

---

<div class="post-metadata">

**Author:** ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)\
**Post date:** [January 19, 2022, 9:49pm UTC](https://discourse.cmake.org/t/easily-incorporate-3rd-party-cmake-modules/4875/3 "2022-01-19T21:49:04Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [January 20, 2022, 6:43pm UTC](https://discourse.cmake.org/t/easily-incorporate-3rd-party-cmake-modules/4875/4 "2022-01-20T18:43:41Z")

</div>

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:

```cmake
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!

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [January 21, 2022, 1:45pm UTC](https://discourse.cmake.org/t/easily-incorporate-3rd-party-cmake-modules/4875/5 "2022-01-21T13:45:15Z")

</div>

> [@benthevining](#):
>
> 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`.

`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).

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [January 21, 2022, 7:40pm UTC](https://discourse.cmake.org/t/easily-incorporate-3rd-party-cmake-modules/4875/6 "2022-01-21T19:40:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hachanuy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hachanuy/32/902_2.png) [@hachanuy](https://discourse.cmake.org/u/hachanuy)\
**Post date:** [January 26, 2022, 1:14pm UTC](https://discourse.cmake.org/t/easily-incorporate-3rd-party-cmake-modules/4875/7 "2022-01-26T13:14:13Z")

</div>

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.
