1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-18 17:31:57 +08:00

cmFindCommonDebugState: adopt event writing logic

This lays the groundwork for `find_package` also using the same
triggering logic.
This commit is contained in:
Ben Boeckel
2025-04-09 15:21:45 +02:00
parent 716dfd3b1e
commit fdccb8846c
6 changed files with 105 additions and 36 deletions

View File

@@ -7,6 +7,7 @@
#include <deque>
#include <iterator>
#include <map>
#include <memory>
#include <utility>
#include <cm/optional>
@@ -37,6 +38,13 @@ cmFindBase::cmFindBase(std::string findCommandName, cmExecutionStatus& status)
{
}
cmFindBase::~cmFindBase()
{
if (this->DebugState) {
this->DebugState->Write();
}
}
bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
{
if (argsIn.size() < 2) {
@@ -640,25 +648,22 @@ cmFindBaseDebugState::cmFindBaseDebugState(cmFindBase const* findBase)
{
}
cmFindBaseDebugState::~cmFindBaseDebugState()
cmFindBaseDebugState::~cmFindBaseDebugState() = default;
void cmFindBaseDebugState::FoundAtImpl(std::string const& path,
std::string regexName)
{
bool found = !this->FoundSearchLocation.path.empty();
this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
}
#ifndef CMAKE_BOOTSTRAP
// Write find event to the configure log if the log exists
if (cmConfigureLog* log =
this->FindCommand->Makefile->GetCMakeInstance()->GetConfigureLog()) {
// Write event if any of:
// - the variable was not defined (first run)
// - the variable found state does not match the new found state (state
// transition)
if (!this->FindBaseCommand->IsDefined() ||
this->FindBaseCommand->IsFound() != found) {
this->WriteFindEvent(*log, *this->FindCommand->Makefile);
}
}
#endif
void cmFindBaseDebugState::FailedAtImpl(std::string const& path,
std::string regexName)
{
this->FailedSearchLocations.emplace_back(std::move(regexName), path);
}
void cmFindBaseDebugState::WriteDebug() const
{
// clang-format off
auto buffer =
cmStrCat(
@@ -713,21 +718,9 @@ cmFindBaseDebugState::~cmFindBaseDebugState()
this->FindCommand->DebugMessage(buffer);
}
void cmFindBaseDebugState::FoundAtImpl(std::string const& path,
std::string regexName)
{
this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
}
void cmFindBaseDebugState::FailedAtImpl(std::string const& path,
std::string regexName)
{
this->FailedSearchLocations.emplace_back(std::move(regexName), path);
}
#ifndef CMAKE_BOOTSTRAP
void cmFindBaseDebugState::WriteFindEvent(cmConfigureLog& log,
cmMakefile const& mf) const
void cmFindBaseDebugState::WriteEvent(cmConfigureLog& log,
cmMakefile const& mf) const
{
log.BeginEvent("find-v1", mf);

View File

@@ -25,7 +25,7 @@ class cmFindBase : public cmFindCommon
{
public:
cmFindBase(std::string findCommandName, cmExecutionStatus& status);
virtual ~cmFindBase() = default;
~cmFindBase() override;
/**
* This is called when the command is first encountered in
@@ -42,8 +42,8 @@ protected:
friend class cmFindBaseDebugState;
void ExpandPaths();
bool IsFound() const;
bool IsDefined() const;
bool IsFound() const override;
bool IsDefined() const override;
void NormalizeFindResult();
void StoreFindResult(std::string const& value);
@@ -114,8 +114,9 @@ private:
std::string path;
};
void WriteDebug() const override;
#ifndef CMAKE_BOOTSTRAP
void WriteFindEvent(cmConfigureLog& log, cmMakefile const& mf) const;
void WriteEvent(cmConfigureLog& log, cmMakefile const& mf) const override;
#endif
cmFindBase const* const FindBaseCommand;

View File

@@ -71,6 +71,8 @@ cmFindCommon::cmFindCommon(cmExecutionStatus& status)
}
}
cmFindCommon::~cmFindCommon() = default;
void cmFindCommon::SetError(std::string const& e)
{
this->Status.SetError(e);
@@ -492,6 +494,26 @@ void cmFindCommonDebugState::FailedAt(std::string const& path,
this->FailedAtImpl(path, regexName);
}
void cmFindCommonDebugState::Write()
{
#ifndef CMAKE_BOOTSTRAP
// Write find event to the configure log if the log exists
if (cmConfigureLog* log =
this->FindCommand->Makefile->GetCMakeInstance()->GetConfigureLog()) {
// Write event if any of:
// - the variable was not defined (first run)
// - the variable found state does not match the new found state (state
// transition)
if (!this->FindCommand->IsDefined() ||
this->FindCommand->IsFound() != this->IsFound) {
this->WriteEvent(*log, *this->FindCommand->Makefile);
}
}
#endif
this->WriteDebug();
}
bool cmFindCommonDebugState::TrackSearchProgress() const
{
// Track search progress if debugging or logging the configure.

View File

@@ -14,6 +14,7 @@
#include "cmSearchPath.h"
#include "cmWindowsRegistry.h"
class cmConfigureLog;
class cmFindCommonDebugState;
class cmExecutionStatus;
class cmMakefile;
@@ -29,6 +30,7 @@ class cmFindCommon
{
public:
cmFindCommon(cmExecutionStatus& status);
virtual ~cmFindCommon();
void SetError(std::string const& e);
@@ -80,6 +82,9 @@ protected:
RootPathModeBoth
};
virtual bool IsFound() const = 0;
virtual bool IsDefined() const = 0;
/** Construct the various path groups and labels */
void InitializeSearchPathGroups();
@@ -171,11 +176,18 @@ public:
void FailedAt(std::string const& path,
std::string regexName = std::string());
void Write();
protected:
virtual void FoundAtImpl(std::string const& path, std::string regexName) = 0;
virtual void FailedAtImpl(std::string const& path,
std::string regexName) = 0;
virtual void WriteDebug() const = 0;
#ifndef CMAKE_BOOTSTRAP
virtual void WriteEvent(cmConfigureLog& log, cmMakefile const& mf) const = 0;
#endif
cmFindCommon const* const FindCommand;
std::string const CommandName;
std::string const Mode;

View File

@@ -60,6 +60,8 @@
# endif
#endif
class cmConfigureLog;
namespace {
using pdt = cmFindPackageCommand::PackageDescriptionType;
@@ -600,6 +602,20 @@ void cmFindPackageCommand::InheritOptions(cmFindPackageCommand* other)
this->Quiet = other->Quiet;
}
bool cmFindPackageCommand::IsFound() const
{
// TODO: track the actual found state.
return false;
}
bool cmFindPackageCommand::IsDefined() const
{
// A `find_package` always needs to be rerun because it could create
// variables, provide commands, or targets. Therefore it is never
// "predefined" whether it is found or not.
return false;
}
bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
{
if (args.empty()) {
@@ -3365,7 +3381,7 @@ bool cmFindPackage(std::vector<std::string> const& args,
cmFindPackageDebugState::cmFindPackageDebugState(
cmFindPackageCommand const* findPackage)
: cmFindCommonDebugState("find_package", findPackage)
, FindPackageCommand(findPackage)
// , FindPackageCommand(findPackage)
{
}
@@ -3384,3 +3400,18 @@ void cmFindPackageDebugState::FailedAtImpl(std::string const& path,
(void)path;
(void)regexName;
}
void cmFindPackageDebugState::WriteDebug() const
{
}
#ifndef CMAKE_BOOTSTRAP
void cmFindPackageDebugState::WriteEvent(cmConfigureLog& log,
cmMakefile const& mf) const
{
(void)log;
(void)mf;
// TODO
}
#endif

View File

@@ -32,7 +32,9 @@ namespace std {
/* clang-format on */
#endif
class cmConfigureLog;
class cmExecutionStatus;
class cmMakefile;
class cmPackageState;
class cmSearchPath;
@@ -95,6 +97,9 @@ private:
void InheritOptions(cmFindPackageCommand* other);
bool IsFound() const override;
bool IsDefined() const override;
// Try to find a package, assuming most state has already been set up. This
// is used for recursive dependency solving, particularly when importing
// packages via CPS. Bypasses providers if argsForProvider is empty.
@@ -353,5 +358,10 @@ private:
void FoundAtImpl(std::string const& path, std::string regexName) override;
void FailedAtImpl(std::string const& path, std::string regexName) override;
cmFindPackageCommand const* const FindPackageCommand;
void WriteDebug() const override;
#ifndef CMAKE_BOOTSTRAP
void WriteEvent(cmConfigureLog& log, cmMakefile const& mf) const override;
#endif
// cmFindPackageCommand const* const FindPackageCommand;
};