# Post-configuration task

**URL:** https://discourse.cmake.org/t/post-configuration-task/1949
**Category:** Usage
**Tags:** os:linux
**Created:** [September 30, 2020, 3:46pm UTC](https://discourse.cmake.org/t/post-configuration-task/1949 "2020-09-30T15:46:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tom](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://discourse.cmake.org/u/tom)
#### Post date: [September 30, 2020, 3:46pm UTC](https://discourse.cmake.org/t/post-configuration-task/1949/1 "2020-09-30T15:46:24Z")

</div>

Hi all,

I’m sure this has been asked before, but I couldn’t find any existing threads.

I’m developing a C++ platform that our clients regularly compile for themselves. They often compile multiple different configurations of the platform. I’d like to archive the CMakeCache.txt file as a fail-safe - if the client has a record of the cache file used, that goes _most_ of the way to ensuring they can replicate the build.

I’d like to copy the cache to a secondary location every time it’s re-generated. A naive approach would be to write a custom command with a file-level dependency on the cache file, but that doesn’t work. I assume this is because custom commands are all parsed before the cache file is re-generated.

Old stack overflow threads suggest this isn’t possible ([https://stackoverflow.com/questions/7091447/run-command-after-generation-step-in-cmake](https://stackoverflow.com/questions/7091447/run-command-after-generation-step-in-cmake)). However, that thread is nearly a decade old. Is there a nice way to achieve what I want with recent versions of CMake?

Thanks,  
Tom

---

<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: [November 24, 2020, 11:47pm UTC](https://discourse.cmake.org/t/post-configuration-task/1949/2 "2020-11-24T23:47:41Z")

</div>

I believe there is now support for [`cmake_language(DEFER)`](https://cmake.org/cmake/help/latest/command/cmake_language.html#defer) which you can use to do this. It is new in CMake 3.19.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [November 25, 2020, 10:50pm UTC](https://discourse.cmake.org/t/post-configuration-task/1949/3 "2020-11-25T22:50:01Z")

</div>

I would expect `cmake_language(DEFER)` to have the same problems. It will run at the end of the current scope (or a specified directory’s scope) but still during the configure phase. My understanding is that the `CMakeCache.txt` file is only written out after the whole project has completed its configure stage, so it isn’t accessible from a `cmake_language(DEFER)`.

---

<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: [November 25, 2020, 11:34pm UTC](https://discourse.cmake.org/t/post-configuration-task/1949/4 "2020-11-25T23:34:01Z")

</div>

Ah. Then you might be able to do this:

```cmake
file(GENERATE
  INPUT "${CMAKE_BINARY_DIR}/CMakeCache.txt"
  OUTPUT "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeCache-archive/${datestamp}")

```

ETA: Note that this has the benefit of saving the cache from `ccmake` or `cmake-gui` only once the `generate` step is used. Repeated configures don’t make “bogus” cache snapshots.

---

<div class="post-metadata">

### Author: ![tom](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://discourse.cmake.org/u/tom)
#### Post date: [November 30, 2020, 4:03pm UTC](https://discourse.cmake.org/t/post-configuration-task/1949/5 "2020-11-30T16:03:09Z")

</div>

Thanks very much for getting back to me - I’ll try the file(generate …) fix when I get time, and mark it as a solution accordingly.
