From fdccb8846c358ef0d97e753ea666538fb09f283c Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Wed, 9 Apr 2025 15:21:45 +0200 Subject: [PATCH] cmFindCommonDebugState: adopt event writing logic This lays the groundwork for `find_package` also using the same triggering logic. --- Source/cmFindBase.cxx | 53 ++++++++++++++------------------- Source/cmFindBase.h | 9 +++--- Source/cmFindCommon.cxx | 22 ++++++++++++++ Source/cmFindCommon.h | 12 ++++++++ Source/cmFindPackageCommand.cxx | 33 +++++++++++++++++++- Source/cmFindPackageCommand.h | 12 +++++++- 6 files changed, 105 insertions(+), 36 deletions(-) diff --git a/Source/cmFindBase.cxx b/Source/cmFindBase.cxx index 8e5ea268fb..34c1118e58 100644 --- a/Source/cmFindBase.cxx +++ b/Source/cmFindBase.cxx @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -37,6 +38,13 @@ cmFindBase::cmFindBase(std::string findCommandName, cmExecutionStatus& status) { } +cmFindBase::~cmFindBase() +{ + if (this->DebugState) { + this->DebugState->Write(); + } +} + bool cmFindBase::ParseArguments(std::vector 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); diff --git a/Source/cmFindBase.h b/Source/cmFindBase.h index 94c34e8cab..73f4a84a64 100644 --- a/Source/cmFindBase.h +++ b/Source/cmFindBase.h @@ -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; diff --git a/Source/cmFindCommon.cxx b/Source/cmFindCommon.cxx index 1e5f674d3b..337fb19711 100644 --- a/Source/cmFindCommon.cxx +++ b/Source/cmFindCommon.cxx @@ -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. diff --git a/Source/cmFindCommon.h b/Source/cmFindCommon.h index 8ff63c1591..b8e62135a9 100644 --- a/Source/cmFindCommon.h +++ b/Source/cmFindCommon.h @@ -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; diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index e8f3888ecc..244fbd2d41 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -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 const& args) { if (args.empty()) { @@ -3365,7 +3381,7 @@ bool cmFindPackage(std::vector 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 diff --git a/Source/cmFindPackageCommand.h b/Source/cmFindPackageCommand.h index 766d045720..18a49e3dfe 100644 --- a/Source/cmFindPackageCommand.h +++ b/Source/cmFindPackageCommand.h @@ -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; };