# Caching FetchContent source in a different directory

**URL:** https://discourse.cmake.org/t/caching-fetchcontent-source-in-a-different-directory/8326
**Category:** Code
**Created:** [June 13, 2023, 6:20pm UTC](https://discourse.cmake.org/t/caching-fetchcontent-source-in-a-different-directory/8326 "2023-06-13T18:20:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dwarwick](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dwarwick/32/2298_2.png) [@dwarwick](https://discourse.cmake.org/u/dwarwick)
#### Post date: [June 13, 2023, 6:20pm UTC](https://discourse.cmake.org/t/caching-fetchcontent-source-in-a-different-directory/8326/1 "2023-06-13T18:20:54Z")

</div>

We use FetchContent to fetch external dependencies for our project:

```auto
FetchContent_Declare(Library
  GIT_REPOSITORY https://github.corpname.com/OrgName/library
  GIT_TAG aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
)
FetchContent_MakeAvailable(Library)

```

However, since source is cached in the binary dir, this means our build agents which do a clean build every run do a git checkout for _all_ of our dependencies every run. We would like to cache the sources outside of the binary dir.

There is `FETCHCONTENT_BASE_DIR` which solves our use case, as it allows us to move the FetchContent base dir outside of the build dir, but then we run into issues with building multiple configurations because this also moves the build and subbuild dirs outside of the binary dir and shares them across all builds. There is `SUBBUILD_DIR` which allows us to move the subbuild dir back to the binary dir, but not the build dir. There is `FETCHCONTENT_SOURCE_DIR_<uppercaseName>` which allows us to move just the source dir, but this disables fetching entirely and makes us rewrite our source fetching outside of cmake.

Is there a way to only change just the FetchContent source dir cache location without touching build/subbuild dirs or disabling fetching?

---

<div class="post-metadata">

### Author: ![codeling](https://discourse.cmake.org/user_avatar/discourse.cmake.org/codeling/32/1700_2.png) [@codeling](https://discourse.cmake.org/u/codeling)
#### Post date: [June 19, 2023, 10:54am UTC](https://discourse.cmake.org/t/caching-fetchcontent-source-in-a-different-directory/8326/2 "2023-06-19T10:54:58Z")

</div>

A while back there was a related discussion on [caching git repositories for ExternalProject\_Add](https://discourse.cmake.org/t/externalproject-add-cache-for-git-repositories/4218).

The [linked proposal](https://gitlab.kitware.com/cmake/cmake/-/issues/22688) mentions that git has a solution for this (url..insteadOf).

Hope this helps.

---

<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: [June 19, 2023, 3:01pm UTC](https://discourse.cmake.org/t/caching-fetchcontent-source-in-a-different-directory/8326/3 "2023-06-19T15:01:53Z")

</div>

I think that the linked issue is no longer relevant as it focused on the ability to rewrite the URL for a repo before cloning. This and the linked thread are about storing clones…somewhere that isn’t under the build tree so that it doesn’t get nuked in CI. This really needs support in `ExternalProject` to work well IMO.

Cc: @craig.scott
