cmake: Retain cache from presets and command line before re-configure

Triggering a cache reset and re-configure by changing the compiler or
CMAKE_TOOLCHAIN_FILE should be a shortcut for cmake --fresh. Losing the
cache variables from the command line and especially presets can have an
unexpected effect. This commit restores the preset cache variables and
reprocesses cache-affecting variables from the immediate command line.

Fixes: #27899
This commit is contained in:
Aiden Woodruff
2026-07-03 10:00:16 -04:00
parent ee6bfdc1f3
commit ca0bf9848d
2 changed files with 14 additions and 0 deletions

View File

@@ -2192,6 +2192,9 @@ bool cmake::SetArgsFromPreset(cmCMakePresetsConfigureArgs const& args,
this->SetTraceFile(expandedPreset->TraceRedirect);
}
// Store preset variables in case of cache reset.
this->InitialPresetVariables = this->UnprocessedPresetVariables;
return true;
}
@@ -2410,6 +2413,15 @@ int cmake::HandleDeleteCacheVariables(
this->DeleteCache(this->GetHomeOutputDirectory());
// load the empty cache
this->LoadCache();
#ifndef CMAKE_BOOTSTRAP
// Restore preset cache variables.
this->UnprocessedPresetVariables = this->InitialPresetVariables;
this->ProcessPresetVariables();
#endif
// Restore command line cache variables (from this invocation cmake only).
bool resetArgsSuccess = this->SetCacheArgs(this->cmdArgs);
assert(resetArgsSuccess);
(void)resetArgsSuccess;
// restore the changed compilers
for (SaveCacheEntry const& i : saved) {
this->AddCacheEntry(i.key, i.value, i.help, i.type);

View File

@@ -806,6 +806,8 @@ private:
UnprocessedPresetVariables;
std::map<std::string, cm::optional<std::string>>
UnprocessedPresetEnvironment;
std::map<std::string, cm::optional<cmCMakePresetsGraph::CacheVariable>>
InitialPresetVariables;
#endif
#if !defined(CMAKE_BOOTSTRAP)