Search code examples
c++cmakecmakelists-options

How can I remove CMakeCache.txt during CMake configuration if it has outdated info?


My CMakeLists.txt uses CheckSymbolExists to determine which definitions for __NR_xyz syscall numbers are missing in asm/unistd.h. And previously it looked in unistd.h instead, which resulted in inaccurate HAVE_NR_XYZ definitions being stored in CMakeCache.txt.

I can clean the old incorrect HAVE_NR_XYZ definitions manually by removing CMakeCache.txt, but others will still do incremental builds using the updated CMakeLists.txt that checks asm/unistd.h, and use the old existing CMakeCache.txt.

Can I remove CMakeCache.txt programmatically from CMakeLists.txt if it doesn't contain some definition (like HAVE_NR_XYZ, or something else defined for this purpose)?

Or is there another way I can specify in CMakeLists.txt to delete old CMakeCache.txt files, but only if they were generated before the asm/unistd.h fix went in?


Solution

  • I don't think it makes sense to want to delete the entire cache file in a CMake project configuration file. The whole point of the cache... is to be a cache. And if I understand correctly, the cache is loaded near the start of the configuration phase, so even if you programmatically deleted it with a command in the CMake configuration, you'd still need to run configuration again to see the effect. I.e. It's not something you can "chop off" during configuration and then the variables it set will all magically be unset. CMake reads that file and sets the variables, and if you want them unset, you need to manually unset each one.

    If you want to run with a fresh cache, you can and should do so from the invocation of the configure command. In CMake 3.24 and up, you can run with a fresh cache using --fresh in the configuration command. From its docs:

    Perform a fresh configuration of the build tree. This removes any existing CMakeCache.txt file and associated CMakeFiles/ directory, and recreates them from scratch.

    If you want to do fine-grained editing of the variable cache, you can use -U <globbing expr>, or use an interactive cache-editing applications. CMake comes with two such interactive dialogs: ccmake, and cmake-gui. Make sure to run configuration again after editing the cache. The interactive dialogs have buttons / keybindings to do so, and cmake --build may actually be smart enough to detect that re-configuration needs to happen. Your IDE might also provide a CMake cache-editing GUI (though the quality may not be as good as the CMake-provided ones).