Add infrastructure to invalidate cached file system state

Caching results of file system queries during configure requires
forgetting them whenever files may have been removed or renamed, i.e.
by file(RENAME), file(REMOVE), execute_process, exec_program,
try_compile, or try_run, and at the start of every configure in the
same process.

Add cmake::InvalidateFileSystemCaches and call it from those places.
It does nothing yet.  Later commits add individual caches to it.
This commit is contained in:
Cristian Adam committed 2026-10-14 07:33:46 +02:00
1 parent 841b2289aa
commit 947ec5e324
7 files changed
+32

No files matched your search

+3
View File
@@ -1372,6 +1372,9 @@ void cmCoreTryCompile::CleanupFiles(std::string const& binDir)
return;
}
// Cached knowledge of the removed files becomes stale.
this->Makefile->GetCMakeInstance()->InvalidateFileSystemCaches();
cmsys::Directory dir;
dir.Load(binDir);
std::set<std::string> deletedFiles;
+4
View File
@@ -12,6 +12,7 @@
#include "cmProcessOutput.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmake.h"
using Encoding = cmProcessOutput::Encoding;
@@ -98,6 +99,9 @@ bool cmExecProgramCommand(std::vector<std::string> const& args,
retVal = -1;
}
// The process may have removed or renamed files.
status.GetMakefile().GetCMakeInstance()->InvalidateFileSystemCaches();
if (!output_variable.empty()) {
std::string::size_type first = output.find_first_not_of(" \n\t\r");
std::string::size_type last = output.find_last_not_of(" \n\t\r");
+4
View File
@@ -40,6 +40,7 @@
#include "cmUVHandlePtr.h"
#include "cmUVProcessChain.h"
#include "cmUVStream.h"
#include "cmake.h"
namespace {
bool cmExecuteProcessCommandIsWhitespace(char c)
@@ -427,6 +428,9 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
if (timedOut) {
chain.Terminate();
}
// The processes may have removed or renamed files.
status.GetMakefile().GetCMakeInstance()->InvalidateFileSystemCaches();
if (!arguments.OutputQuiet &&
(arguments.OutputVariable.empty() || arguments.EchoOutputVariable)) {
processOutput.DecodeText(std::string(), strdata, 1);
+6
View File
@@ -1469,6 +1469,9 @@ bool HandleRename(std::vector<std::string> const& args,
return false;
}
// Cached knowledge of the old and new names becomes stale.
status.GetMakefile().GetCMakeInstance()->InvalidateFileSystemCaches();
std::string err;
switch (cmSystemTools::RenameFile(oldname, newname,
arguments.NoReplace
@@ -1599,6 +1602,9 @@ bool HandleCopyFile(std::vector<std::string> const& args,
bool HandleRemoveImpl(std::vector<std::string> const& args, bool recurse,
cmExecutionStatus& status)
{
// Cached knowledge of the removed files becomes stale.
status.GetMakefile().GetCMakeInstance()->InvalidateFileSystemCaches();
for (std::string const& arg :
cmMakeRange(args).advance(1)) // Get rid of subcommand
{
+2
View File
@@ -288,6 +288,8 @@ void TryRunCommandImpl::RunExecutable(std::string const& runArgs,
stdOut || stdErr ? stdErr : out, &retVal,
workDir ? workDir->c_str() : nullptr, cmSystemTools::OUTPUT_NONE,
cmDuration::zero());
// The executable may have removed or renamed files.
this->Makefile->GetCMakeInstance()->InvalidateFileSystemCaches();
// set the run var
std::string retStr = worked ? std::to_string(retVal) : "FAILED_TO_RUN";
if (this->NoCache) {
+9
View File
@@ -2575,11 +2575,20 @@ int cmake::Configure()
return ret;
}
void cmake::InvalidateFileSystemCaches()
{
// Caches of file system state are cleared here as they are added.
}
int cmake::ActualConfigure()
{
// Construct right now our path conversion table before it's too late:
this->CleanupCommandsAndMacros();
// The file system may have changed since a previous configure in the
// same process.
this->InvalidateFileSystemCaches();
// Do not keep the parse trees of a previous configure in the same
// process alive. Compiled expressions still referring to them share
// their ownership.
+4
View File
@@ -476,6 +476,10 @@ public:
return *this->GeneratorExpressionParseCache;
}
//! Forget what is known about the file system, e.g. after files may
//! have been removed or renamed.
void InvalidateFileSystemCaches();
bool WasLogLevelSetViaCLI() const { return this->LogLevelWasSetViaCLI; }
//! Get the selected log level for `message()` commands during the cmake run.