1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 03:48:02 +08:00

cmFindBaseDebugState: factor out a base class

This will be used to share logic with `find_package` writing events.
This commit is contained in:
Ben Boeckel
2025-04-09 14:22:01 +02:00
parent bd5cb1f8e6
commit c6d097135d
4 changed files with 99 additions and 46 deletions

View File

@@ -635,8 +635,8 @@ void cmFindBase::StoreFindResult(std::string const& value)
}
cmFindBaseDebugState::cmFindBaseDebugState(cmFindBase const* findBase)
: FindCommand(findBase)
, CommandName(findBase->FindCommandName)
: cmFindCommonDebugState(findBase->FindCommandName, findBase)
, FindBaseCommand(findBase)
{
}
@@ -653,8 +653,9 @@ cmFindBaseDebugState::~cmFindBaseDebugState()
// - the variable was not defined (first run)
// - the variable found state does not match the new found state (state
// transition)
if (this->FindCommand->DebugMode || !this->FindCommand->IsDefined() ||
this->FindCommand->IsFound() != found) {
if (this->FindCommand->DebugModeEnabled() ||
!this->FindBaseCommand->IsDefined() ||
this->FindBaseCommand->IsFound() != found) {
this->WriteFindEvent(*log, *this->FindCommand->Makefile);
}
}
@@ -668,17 +669,17 @@ cmFindBaseDebugState::~cmFindBaseDebugState()
auto buffer =
cmStrCat(
this->CommandName, " called with the following settings:"
"\n VAR: ", this->FindCommand->VariableName,
"\n NAMES: ", cmWrap('"', this->FindCommand->Names, '"', "\n "),
"\n Documentation: ", this->FindCommand->VariableDocumentation,
"\n VAR: ", this->FindBaseCommand->VariableName,
"\n NAMES: ", cmWrap('"', this->FindBaseCommand->Names, '"', "\n "),
"\n Documentation: ", this->FindBaseCommand->VariableDocumentation,
"\n Framework"
"\n Only Search Frameworks: ", this->FindCommand->SearchFrameworkOnly,
"\n Search Frameworks Last: ", this->FindCommand->SearchFrameworkLast,
"\n Search Frameworks First: ", this->FindCommand->SearchFrameworkFirst,
"\n Only Search Frameworks: ", this->FindBaseCommand->SearchFrameworkOnly,
"\n Search Frameworks Last: ", this->FindBaseCommand->SearchFrameworkLast,
"\n Search Frameworks First: ", this->FindBaseCommand->SearchFrameworkFirst,
"\n AppBundle"
"\n Only Search AppBundle: ", this->FindCommand->SearchAppBundleOnly,
"\n Search AppBundle Last: ", this->FindCommand->SearchAppBundleLast,
"\n Search AppBundle First: ", this->FindCommand->SearchAppBundleFirst,
"\n Only Search AppBundle: ", this->FindBaseCommand->SearchAppBundleOnly,
"\n Search AppBundle Last: ", this->FindBaseCommand->SearchAppBundleLast,
"\n Search AppBundle First: ", this->FindBaseCommand->SearchAppBundleFirst,
"\n"
);
// clang-format on
@@ -708,7 +709,7 @@ cmFindBaseDebugState::~cmFindBaseDebugState()
buffer += cmStrCat(path, '\n');
}
if (found) {
if (this->HasBeenFound()) {
buffer += cmStrCat("The item was found at\n ",
this->FoundSearchLocation.path, '\n');
} else {
@@ -718,20 +719,16 @@ cmFindBaseDebugState::~cmFindBaseDebugState()
this->FindCommand->DebugMessage(buffer);
}
void cmFindBaseDebugState::FoundAt(std::string const& path,
std::string regexName)
void cmFindBaseDebugState::FoundAtImpl(std::string const& path,
std::string regexName)
{
if (this->TrackSearchProgress()) {
this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
}
this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
}
void cmFindBaseDebugState::FailedAt(std::string const& path,
std::string regexName)
void cmFindBaseDebugState::FailedAtImpl(std::string const& path,
std::string regexName)
{
if (this->TrackSearchProgress()) {
this->FailedSearchLocations.emplace_back(std::move(regexName), path);
}
this->FailedSearchLocations.emplace_back(std::move(regexName), path);
}
#ifndef CMAKE_BOOTSTRAP
@@ -742,8 +739,9 @@ void cmFindBaseDebugState::WriteFindEvent(cmConfigureLog& log,
// Mode is the Command name without the "find_" prefix
log.WriteValue("mode"_s, this->CommandName.substr(5));
log.WriteValue("variable"_s, this->FindCommand->VariableName);
log.WriteValue("description"_s, this->FindCommand->VariableDocumentation);
log.WriteValue("variable"_s, this->FindBaseCommand->VariableName);
log.WriteValue("description"_s,
this->FindBaseCommand->VariableDocumentation);
// Yes, this needs to return a `std::string`. If it returns a `const char*`,
// the `WriteValue` method prefers the `bool` overload. There's no overload
@@ -774,7 +772,7 @@ void cmFindBaseDebugState::WriteFindEvent(cmConfigureLog& log,
!this->FindCommand->NoCMakeInstallPath);
log.EndObject();
log.WriteValue("names"_s, this->FindCommand->Names);
log.WriteValue("names"_s, this->FindBaseCommand->Names);
std::vector<std::string> directories;
directories.reserve(this->FailedSearchLocations.size());
for (auto const& location : this->FailedSearchLocations) {
@@ -790,13 +788,3 @@ void cmFindBaseDebugState::WriteFindEvent(cmConfigureLog& log,
log.EndEvent();
}
#endif
bool cmFindBaseDebugState::TrackSearchProgress() const
{
// Track search progress if debugging or logging the configure.
return this->FindCommand->DebugMode
#ifndef CMAKE_BOOTSTRAP
|| this->FindCommand->Makefile->GetCMakeInstance()->GetConfigureLog()
#endif
;
}

View File

@@ -92,17 +92,16 @@ private:
void FillUserGuessPath();
};
class cmFindBaseDebugState
class cmFindBaseDebugState : public cmFindCommonDebugState
{
public:
explicit cmFindBaseDebugState(cmFindBase const* findBase);
~cmFindBaseDebugState();
void FoundAt(std::string const& path, std::string regexName = std::string());
void FailedAt(std::string const& path,
std::string regexName = std::string());
~cmFindBaseDebugState() override;
private:
void FoundAtImpl(std::string const& path, std::string regexName) override;
void FailedAtImpl(std::string const& path, std::string regexName) override;
struct DebugLibState
{
DebugLibState() = default;
@@ -119,9 +118,7 @@ private:
void WriteFindEvent(cmConfigureLog& log, cmMakefile const& mf) const;
#endif
cmFindBase const* FindCommand;
std::string CommandName;
bool TrackSearchProgress() const;
cmFindBase const* const FindBaseCommand;
std::vector<DebugLibState> FailedSearchLocations;
DebugLibState FoundSearchLocation;
};

View File

@@ -457,3 +457,44 @@ void cmFindCommon::ComputeFinalPaths(IgnorePaths ignorePaths,
}
});
}
cmFindCommonDebugState::cmFindCommonDebugState(std::string name,
cmFindCommon const* findCommand)
: FindCommand(findCommand)
, CommandName(std::move(name))
// Strip the `find_` prefix.
, Mode(this->CommandName.substr(5))
{
}
void cmFindCommonDebugState::FoundAt(std::string const& path,
std::string regexName)
{
this->IsFound = true;
if (!this->TrackSearchProgress()) {
return;
}
this->FoundAtImpl(path, regexName);
}
void cmFindCommonDebugState::FailedAt(std::string const& path,
std::string regexName)
{
if (!this->TrackSearchProgress()) {
return;
}
this->FailedAtImpl(path, regexName);
}
bool cmFindCommonDebugState::TrackSearchProgress() const
{
// Track search progress if debugging or logging the configure.
return this->FindCommand->DebugModeEnabled()
#ifndef CMAKE_BOOTSTRAP
|| this->FindCommand->Makefile->GetCMakeInstance()->GetConfigureLog()
#endif
;
}

View File

@@ -35,6 +35,7 @@ public:
protected:
friend class cmSearchPath;
friend class cmFindBaseDebugState;
friend class cmFindCommonDebugState;
/** Used to define groups of path labels */
class PathGroup : public cmPathLabel
@@ -157,3 +158,29 @@ protected:
cmMakefile* Makefile;
cmExecutionStatus& Status;
};
class cmFindCommonDebugState
{
public:
cmFindCommonDebugState(std::string name, cmFindCommon const* findCommand);
virtual ~cmFindCommonDebugState() = default;
void FoundAt(std::string const& path, std::string regexName = std::string());
void FailedAt(std::string const& path,
std::string regexName = std::string());
protected:
virtual void FoundAtImpl(std::string const& path, std::string regexName) = 0;
virtual void FailedAtImpl(std::string const& path,
std::string regexName) = 0;
cmFindCommon const* const FindCommand;
std::string const CommandName;
std::string const Mode;
bool HasBeenFound() const { return this->IsFound; }
private:
bool TrackSearchProgress() const;
bool IsFound{ false };
};