Caching FetchContent source in a different directory

We use FetchContent to fetch external dependencies for our project:

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?

1 Like

A while back there was a related discussion on caching git repositories for ExternalProject_Add.

The linked proposal mentions that git has a solution for this (url..insteadOf).

Hope this helps.

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

1 Like