From 716dfd3b1ee1e77749a1da1cec1e4e928d6831d0 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Wed, 9 Apr 2025 15:15:44 +0200 Subject: [PATCH] cmFindCommon: track debug state as an object This also means that the `DebugState` instances do not need to ask if debug mode is enabled: if they exist, it is enabled. --- Source/cmFindBase.cxx | 8 +----- Source/cmFindCommon.cxx | 7 ++++-- Source/cmFindCommon.h | 6 +++-- Source/cmFindLibraryCommand.cxx | 31 +++++++++++++----------- Source/cmFindPackageCommand.cxx | 29 ++++++++++++++++++++-- Source/cmFindPackageCommand.h | 13 ++++++++++ Source/cmFindPathCommand.cxx | 43 +++++++++++++++++++++------------ Source/cmFindPathCommand.h | 7 +++--- Source/cmFindProgramCommand.cxx | 26 ++++++++++++++------ 9 files changed, 116 insertions(+), 54 deletions(-) diff --git a/Source/cmFindBase.cxx b/Source/cmFindBase.cxx index 2368655b92..8e5ea268fb 100644 --- a/Source/cmFindBase.cxx +++ b/Source/cmFindBase.cxx @@ -649,22 +649,16 @@ cmFindBaseDebugState::~cmFindBaseDebugState() if (cmConfigureLog* log = this->FindCommand->Makefile->GetCMakeInstance()->GetConfigureLog()) { // Write event if any of: - // - debug mode is enabled // - the variable was not defined (first run) // - the variable found state does not match the new found state (state // transition) - if (this->FindCommand->DebugModeEnabled() || - !this->FindBaseCommand->IsDefined() || + if (!this->FindBaseCommand->IsDefined() || this->FindBaseCommand->IsFound() != found) { this->WriteFindEvent(*log, *this->FindCommand->Makefile); } } #endif - if (!this->FindCommand->DebugModeEnabled()) { - return; - } - // clang-format off auto buffer = cmStrCat( diff --git a/Source/cmFindCommon.cxx b/Source/cmFindCommon.cxx index 644a440fa9..1e5f674d3b 100644 --- a/Source/cmFindCommon.cxx +++ b/Source/cmFindCommon.cxx @@ -59,8 +59,6 @@ cmFindCommon::cmFindCommon(cmExecutionStatus& status) this->InitializeSearchPathGroups(); - this->DebugMode = false; - // Windows Registry views // When policy CMP0134 is not NEW, rely on previous behavior: if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0134) != @@ -78,6 +76,11 @@ void cmFindCommon::SetError(std::string const& e) this->Status.SetError(e); } +bool cmFindCommon::DebugModeEnabled() const +{ + return static_cast(this->DebugState); +} + void cmFindCommon::DebugMessage(std::string const& msg) const { if (this->Makefile) { diff --git a/Source/cmFindCommon.h b/Source/cmFindCommon.h index 5cd254f138..8ff63c1591 100644 --- a/Source/cmFindCommon.h +++ b/Source/cmFindCommon.h @@ -5,6 +5,7 @@ #include "cmConfigure.h" // IWYU pragma: keep #include +#include #include #include #include @@ -13,6 +14,7 @@ #include "cmSearchPath.h" #include "cmWindowsRegistry.h" +class cmFindCommonDebugState; class cmExecutionStatus; class cmMakefile; @@ -30,7 +32,7 @@ public: void SetError(std::string const& e); - bool DebugModeEnabled() const { return this->DebugMode; } + bool DebugModeEnabled() const; protected: friend class cmSearchPath; @@ -127,7 +129,7 @@ protected: void AddPathSuffix(std::string const& arg); void DebugMessage(std::string const& msg) const; - bool DebugMode; + std::unique_ptr DebugState; bool NoDefaultPath; bool NoPackageRootPath; bool NoCMakePath; diff --git a/Source/cmFindLibraryCommand.cxx b/Source/cmFindLibraryCommand.cxx index 62fd4cb863..9e61b5390f 100644 --- a/Source/cmFindLibraryCommand.cxx +++ b/Source/cmFindLibraryCommand.cxx @@ -9,10 +9,12 @@ #include #include +#include #include #include "cmsys/RegularExpression.hxx" +#include "cmFindCommon.h" #include "cmGlobalGenerator.h" #include "cmList.h" #include "cmMakefile.h" @@ -42,7 +44,9 @@ bool cmFindLibraryCommand::InitialPass(std::vector const& argsIn) return false; } - this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName); + if (this->ComputeIfDebugModeWanted(this->VariableName)) { + this->DebugState = cm::make_unique(this); + } if (this->IsFound()) { this->NormalizeFindResult(); @@ -191,7 +195,8 @@ std::string cmFindLibraryCommand::FindLibrary() struct cmFindLibraryHelper { - cmFindLibraryHelper(cmMakefile* mf, cmFindBase const* findBase); + cmFindLibraryHelper(cmMakefile* mf, cmFindBase const* findBase, + cmFindCommonDebugState* debugState); // Context information. cmMakefile* Makefile; @@ -213,8 +218,6 @@ struct cmFindLibraryHelper // Support for OpenBSD shared library naming: lib.so.. bool IsOpenBSD; - bool DebugMode; - // Current names under consideration. struct Name { @@ -250,11 +253,11 @@ struct cmFindLibraryHelper return this->FindBase->Validate(path); } - cmFindBaseDebugState DebugSearches; + cmFindCommonDebugState* DebugState; void DebugLibraryFailed(std::string const& name, std::string const& path) { - if (this->DebugMode) { + if (this->DebugState) { // To improve readability of the debug output, if there is only one // prefix/suffix, use the plain prefix/suffix instead of the regex. auto const& prefix = (this->Prefixes.size() == 1) ? this->Prefixes[0] @@ -263,16 +266,16 @@ struct cmFindLibraryHelper : this->SuffixRegexStr; auto regexName = cmStrCat(prefix, name, suffix); - this->DebugSearches.FailedAt(path, regexName); + this->DebugState->FailedAt(path, regexName); } } void DebugLibraryFound(std::string const& name, std::string const& path) { - if (this->DebugMode) { + if (this->DebugState) { auto regexName = cmStrCat(this->PrefixRegexStr, name, this->SuffixRegexStr); - this->DebugSearches.FoundAt(path, regexName); + this->DebugState->FoundAt(path, regexName); } } }; @@ -306,11 +309,11 @@ std::string const& get_suffixes(cmMakefile* mf) } } cmFindLibraryHelper::cmFindLibraryHelper(cmMakefile* mf, - cmFindBase const* base) + cmFindBase const* base, + cmFindCommonDebugState* debugState) : Makefile(mf) , FindBase(base) - , DebugMode(base->DebugModeEnabled()) - , DebugSearches(base) + , DebugState(debugState) { this->GG = this->Makefile->GetGlobalGenerator(); @@ -539,7 +542,7 @@ std::string cmFindLibraryCommand::FindNormalLibrary() std::string cmFindLibraryCommand::FindNormalLibraryNamesPerDir() { // Search for all names in each directory. - cmFindLibraryHelper helper(this->Makefile, this); + cmFindLibraryHelper helper(this->Makefile, this, this->DebugState.get()); for (std::string const& n : this->Names) { helper.AddName(n); } @@ -556,7 +559,7 @@ std::string cmFindLibraryCommand::FindNormalLibraryNamesPerDir() std::string cmFindLibraryCommand::FindNormalLibraryDirsPerName() { // Search the entire path for each name. - cmFindLibraryHelper helper(this->Makefile, this); + cmFindLibraryHelper helper(this->Makefile, this, this->DebugState.get()); for (std::string const& n : this->Names) { // Switch to searching for this name. helper.SetName(n); diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 86a75c9f65..e8f3888ecc 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -529,7 +530,6 @@ cmFindPackageCommand::cmFindPackageCommand(cmExecutionStatus& status) , VersionRangeMax(VERSION_ENDPOINT_INCLUDED) { this->CMakePathName = "PACKAGE"; - this->DebugMode = false; this->AppendSearchPathGroups(); this->DeprecatedFindModules["Boost"] = cmPolicies::CMP0167; @@ -704,7 +704,9 @@ bool cmFindPackageCommand::InitialPass(std::vector const& args) // Process debug mode cmMakefile::DebugFindPkgRAII debugFindPkgRAII(this->Makefile, this->Name); - this->DebugMode = this->ComputeIfDebugModeWanted(); + if (this->ComputeIfDebugModeWanted()) { + this->DebugState = cm::make_unique(this); + } // Parse the arguments. enum Doing @@ -3359,3 +3361,26 @@ bool cmFindPackage(std::vector const& args, { return cmFindPackageCommand(status).InitialPass(args); } + +cmFindPackageDebugState::cmFindPackageDebugState( + cmFindPackageCommand const* findPackage) + : cmFindCommonDebugState("find_package", findPackage) + , FindPackageCommand(findPackage) +{ +} + +cmFindPackageDebugState::~cmFindPackageDebugState() = default; + +void cmFindPackageDebugState::FoundAtImpl(std::string const& path, + std::string regexName) +{ + (void)path; + (void)regexName; +} + +void cmFindPackageDebugState::FailedAtImpl(std::string const& path, + std::string regexName) +{ + (void)path; + (void)regexName; +} diff --git a/Source/cmFindPackageCommand.h b/Source/cmFindPackageCommand.h index e228cf7ae0..766d045720 100644 --- a/Source/cmFindPackageCommand.h +++ b/Source/cmFindPackageCommand.h @@ -342,3 +342,16 @@ struct hash bool cmFindPackage(std::vector const& args, cmExecutionStatus& status); + +class cmFindPackageDebugState : public cmFindCommonDebugState +{ +public: + explicit cmFindPackageDebugState(cmFindPackageCommand const* findPackage); + ~cmFindPackageDebugState() override; + +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; +}; diff --git a/Source/cmFindPathCommand.cxx b/Source/cmFindPathCommand.cxx index 1eec555bbf..74cd005264 100644 --- a/Source/cmFindPathCommand.cxx +++ b/Source/cmFindPathCommand.cxx @@ -4,8 +4,11 @@ #include +#include + #include "cmsys/Glob.hxx" +#include "cmFindCommon.h" #include "cmStateTypes.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" @@ -35,7 +38,9 @@ bool cmFindPathCommand::InitialPass(std::vector const& argsIn) return false; } - this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName); + if (this->ComputeIfDebugModeWanted(this->VariableName)) { + this->DebugState = cm::make_unique(this); + } if (this->IsFound()) { this->NormalizeFindResult(); @@ -49,24 +54,22 @@ bool cmFindPathCommand::InitialPass(std::vector const& argsIn) std::string cmFindPathCommand::FindHeader() { - cmFindBaseDebugState debug(this); std::string header; if (this->SearchFrameworkFirst || this->SearchFrameworkOnly) { - header = this->FindFrameworkHeader(debug); + header = this->FindFrameworkHeader(); } if (header.empty() && !this->SearchFrameworkOnly) { - header = this->FindNormalHeader(debug); + header = this->FindNormalHeader(); } if (header.empty() && this->SearchFrameworkLast) { - header = this->FindFrameworkHeader(debug); + header = this->FindFrameworkHeader(); } return header; } std::string cmFindPathCommand::FindHeaderInFramework( - std::string const& file, std::string const& dir, - cmFindBaseDebugState& debug) const + std::string const& file, std::string const& dir) const { std::string fileName = file; std::string frameWorkName; @@ -90,13 +93,17 @@ std::string cmFindPathCommand::FindHeaderInFramework( std::string intPath = cmStrCat(fpath, "/Headers/", fileName); if (cmSystemTools::FileExists(intPath) && this->Validate(this->IncludeFileInPath ? intPath : fpath)) { - debug.FoundAt(intPath); + if (this->DebugState) { + this->DebugState->FoundAt(intPath); + } if (this->IncludeFileInPath) { return intPath; } return fpath; } - debug.FailedAt(intPath); + if (this->DebugState) { + this->DebugState->FailedAt(intPath); + } } } // if it is not found yet or not a framework header, then do a glob search @@ -107,7 +114,9 @@ std::string cmFindPathCommand::FindHeaderInFramework( std::vector files = globIt.GetFiles(); if (!files.empty()) { std::string fheader = cmSystemTools::ToNormalizedPathOnDisk(files[0]); - debug.FoundAt(fheader); + if (this->DebugState) { + this->DebugState->FoundAt(fheader); + } if (this->IncludeFileInPath) { return fheader; } @@ -119,7 +128,7 @@ std::string cmFindPathCommand::FindHeaderInFramework( return ""; } -std::string cmFindPathCommand::FindNormalHeader(cmFindBaseDebugState& debug) +std::string cmFindPathCommand::FindNormalHeader() { std::string tryPath; for (std::string const& n : this->Names) { @@ -127,23 +136,27 @@ std::string cmFindPathCommand::FindNormalHeader(cmFindBaseDebugState& debug) tryPath = cmStrCat(sp, n); if (cmSystemTools::FileExists(tryPath) && this->Validate(this->IncludeFileInPath ? tryPath : sp)) { - debug.FoundAt(tryPath); + if (this->DebugState) { + this->DebugState->FoundAt(tryPath); + } if (this->IncludeFileInPath) { return tryPath; } return sp; } - debug.FailedAt(tryPath); + if (this->DebugState) { + this->DebugState->FailedAt(tryPath); + } } } return ""; } -std::string cmFindPathCommand::FindFrameworkHeader(cmFindBaseDebugState& debug) +std::string cmFindPathCommand::FindFrameworkHeader() { for (std::string const& n : this->Names) { for (std::string const& sp : this->SearchPaths) { - std::string fwPath = this->FindHeaderInFramework(n, sp, debug); + std::string fwPath = this->FindHeaderInFramework(n, sp); if (!fwPath.empty()) { return fwPath; } diff --git a/Source/cmFindPathCommand.h b/Source/cmFindPathCommand.h index 3870a09f53..ba4683c65a 100644 --- a/Source/cmFindPathCommand.h +++ b/Source/cmFindPathCommand.h @@ -30,11 +30,10 @@ public: private: std::string FindHeaderInFramework(std::string const& file, - std::string const& dir, - cmFindBaseDebugState& debug) const; + std::string const& dir) const; std::string FindHeader(); - std::string FindNormalHeader(cmFindBaseDebugState& debug); - std::string FindFrameworkHeader(cmFindBaseDebugState& debug); + std::string FindNormalHeader(); + std::string FindFrameworkHeader(); }; bool cmFindPath(std::vector const& args, diff --git a/Source/cmFindProgramCommand.cxx b/Source/cmFindProgramCommand.cxx index b188aabc27..fef2e31bac 100644 --- a/Source/cmFindProgramCommand.cxx +++ b/Source/cmFindProgramCommand.cxx @@ -5,6 +5,9 @@ #include #include +#include + +#include "cmFindCommon.h" #include "cmMakefile.h" #include "cmMessageType.h" #include "cmPolicies.h" @@ -24,8 +27,9 @@ class cmExecutionStatus; struct cmFindProgramHelper { - cmFindProgramHelper(cmMakefile* makefile, cmFindBase const* base) - : DebugSearches(base) + cmFindProgramHelper(cmMakefile* makefile, cmFindBase const* base, + cmFindCommonDebugState* debugState) + : DebugState(debugState) , Makefile(makefile) , FindBase(base) , PolicyCMP0109(makefile->GetPolicyStatus(cmPolicies::CMP0109)) @@ -49,7 +53,7 @@ struct cmFindProgramHelper std::vector Names; // Debug state - cmFindBaseDebugState DebugSearches; + cmFindCommonDebugState* DebugState; cmMakefile* Makefile; cmFindBase const* FindBase; @@ -93,11 +97,15 @@ struct cmFindProgramHelper cmSystemTools::ToNormalizedPathOnDisk(testPath); if (this->FindBase->Validate(testPath)) { this->BestPath = testPath; - this->DebugSearches.FoundAt(testPath); + if (this->DebugState) { + this->DebugState->FoundAt(testPath); + } return true; } } - this->DebugSearches.FailedAt(testPath); + if (this->DebugState) { + this->DebugState->FailedAt(testPath); + } return false; }); } @@ -191,7 +199,9 @@ bool cmFindProgramCommand::InitialPass(std::vector const& argsIn) if (!this->ParseArguments(argsIn)) { return false; } - this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName); + if (this->ComputeIfDebugModeWanted(this->VariableName)) { + this->DebugState = cm::make_unique(this); + } if (this->IsFound()) { this->NormalizeFindResult(); @@ -231,7 +241,7 @@ std::string cmFindProgramCommand::FindNormalProgram() std::string cmFindProgramCommand::FindNormalProgramNamesPerDir() { // Search for all names in each directory. - cmFindProgramHelper helper(this->Makefile, this); + cmFindProgramHelper helper(this->Makefile, this, this->DebugState.get()); for (std::string const& n : this->Names) { helper.AddName(n); } @@ -254,7 +264,7 @@ std::string cmFindProgramCommand::FindNormalProgramNamesPerDir() std::string cmFindProgramCommand::FindNormalProgramDirsPerName() { // Search the entire path for each name. - cmFindProgramHelper helper(this->Makefile, this); + cmFindProgramHelper helper(this->Makefile, this, this->DebugState.get()); for (std::string const& n : this->Names) { // Switch to searching for this name. helper.SetName(n);