Post-configuration task

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

I believe there is now support for cmake_language(DEFER) which you can use to do this. It is new in CMake 3.19.

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

Ah. Then you might be able to do this:

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.

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.