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.
This commit is contained in:
Ben Boeckel
2025-04-09 15:15:44 +02:00
parent c6d097135d
commit 716dfd3b1e
9 changed files with 116 additions and 54 deletions

View File

@@ -649,22 +649,16 @@ cmFindBaseDebugState::~cmFindBaseDebugState()
if (cmConfigureLog* log = if (cmConfigureLog* log =
this->FindCommand->Makefile->GetCMakeInstance()->GetConfigureLog()) { this->FindCommand->Makefile->GetCMakeInstance()->GetConfigureLog()) {
// Write event if any of: // Write event if any of:
// - debug mode is enabled
// - the variable was not defined (first run) // - the variable was not defined (first run)
// - the variable found state does not match the new found state (state // - the variable found state does not match the new found state (state
// transition) // transition)
if (this->FindCommand->DebugModeEnabled() || if (!this->FindBaseCommand->IsDefined() ||
!this->FindBaseCommand->IsDefined() ||
this->FindBaseCommand->IsFound() != found) { this->FindBaseCommand->IsFound() != found) {
this->WriteFindEvent(*log, *this->FindCommand->Makefile); this->WriteFindEvent(*log, *this->FindCommand->Makefile);
} }
} }
#endif #endif
if (!this->FindCommand->DebugModeEnabled()) {
return;
}
// clang-format off // clang-format off
auto buffer = auto buffer =
cmStrCat( cmStrCat(

View File

@@ -59,8 +59,6 @@ cmFindCommon::cmFindCommon(cmExecutionStatus& status)
this->InitializeSearchPathGroups(); this->InitializeSearchPathGroups();
this->DebugMode = false;
// Windows Registry views // Windows Registry views
// When policy CMP0134 is not NEW, rely on previous behavior: // When policy CMP0134 is not NEW, rely on previous behavior:
if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0134) != if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0134) !=
@@ -78,6 +76,11 @@ void cmFindCommon::SetError(std::string const& e)
this->Status.SetError(e); this->Status.SetError(e);
} }
bool cmFindCommon::DebugModeEnabled() const
{
return static_cast<bool>(this->DebugState);
}
void cmFindCommon::DebugMessage(std::string const& msg) const void cmFindCommon::DebugMessage(std::string const& msg) const
{ {
if (this->Makefile) { if (this->Makefile) {

View File

@@ -5,6 +5,7 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <map> #include <map>
#include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -13,6 +14,7 @@
#include "cmSearchPath.h" #include "cmSearchPath.h"
#include "cmWindowsRegistry.h" #include "cmWindowsRegistry.h"
class cmFindCommonDebugState;
class cmExecutionStatus; class cmExecutionStatus;
class cmMakefile; class cmMakefile;
@@ -30,7 +32,7 @@ public:
void SetError(std::string const& e); void SetError(std::string const& e);
bool DebugModeEnabled() const { return this->DebugMode; } bool DebugModeEnabled() const;
protected: protected:
friend class cmSearchPath; friend class cmSearchPath;
@@ -127,7 +129,7 @@ protected:
void AddPathSuffix(std::string const& arg); void AddPathSuffix(std::string const& arg);
void DebugMessage(std::string const& msg) const; void DebugMessage(std::string const& msg) const;
bool DebugMode; std::unique_ptr<cmFindCommonDebugState> DebugState;
bool NoDefaultPath; bool NoDefaultPath;
bool NoPackageRootPath; bool NoPackageRootPath;
bool NoCMakePath; bool NoCMakePath;

View File

@@ -9,10 +9,12 @@
#include <set> #include <set>
#include <utility> #include <utility>
#include <cm/memory>
#include <cm/optional> #include <cm/optional>
#include "cmsys/RegularExpression.hxx" #include "cmsys/RegularExpression.hxx"
#include "cmFindCommon.h"
#include "cmGlobalGenerator.h" #include "cmGlobalGenerator.h"
#include "cmList.h" #include "cmList.h"
#include "cmMakefile.h" #include "cmMakefile.h"
@@ -42,7 +44,9 @@ bool cmFindLibraryCommand::InitialPass(std::vector<std::string> const& argsIn)
return false; return false;
} }
this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName); if (this->ComputeIfDebugModeWanted(this->VariableName)) {
this->DebugState = cm::make_unique<cmFindBaseDebugState>(this);
}
if (this->IsFound()) { if (this->IsFound()) {
this->NormalizeFindResult(); this->NormalizeFindResult();
@@ -191,7 +195,8 @@ std::string cmFindLibraryCommand::FindLibrary()
struct cmFindLibraryHelper struct cmFindLibraryHelper
{ {
cmFindLibraryHelper(cmMakefile* mf, cmFindBase const* findBase); cmFindLibraryHelper(cmMakefile* mf, cmFindBase const* findBase,
cmFindCommonDebugState* debugState);
// Context information. // Context information.
cmMakefile* Makefile; cmMakefile* Makefile;
@@ -213,8 +218,6 @@ struct cmFindLibraryHelper
// Support for OpenBSD shared library naming: lib<name>.so.<major>.<minor> // Support for OpenBSD shared library naming: lib<name>.so.<major>.<minor>
bool IsOpenBSD; bool IsOpenBSD;
bool DebugMode;
// Current names under consideration. // Current names under consideration.
struct Name struct Name
{ {
@@ -250,11 +253,11 @@ struct cmFindLibraryHelper
return this->FindBase->Validate(path); return this->FindBase->Validate(path);
} }
cmFindBaseDebugState DebugSearches; cmFindCommonDebugState* DebugState;
void DebugLibraryFailed(std::string const& name, std::string const& path) 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 // To improve readability of the debug output, if there is only one
// prefix/suffix, use the plain prefix/suffix instead of the regex. // prefix/suffix, use the plain prefix/suffix instead of the regex.
auto const& prefix = (this->Prefixes.size() == 1) ? this->Prefixes[0] auto const& prefix = (this->Prefixes.size() == 1) ? this->Prefixes[0]
@@ -263,16 +266,16 @@ struct cmFindLibraryHelper
: this->SuffixRegexStr; : this->SuffixRegexStr;
auto regexName = cmStrCat(prefix, name, suffix); 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) void DebugLibraryFound(std::string const& name, std::string const& path)
{ {
if (this->DebugMode) { if (this->DebugState) {
auto regexName = auto regexName =
cmStrCat(this->PrefixRegexStr, name, this->SuffixRegexStr); 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, cmFindLibraryHelper::cmFindLibraryHelper(cmMakefile* mf,
cmFindBase const* base) cmFindBase const* base,
cmFindCommonDebugState* debugState)
: Makefile(mf) : Makefile(mf)
, FindBase(base) , FindBase(base)
, DebugMode(base->DebugModeEnabled()) , DebugState(debugState)
, DebugSearches(base)
{ {
this->GG = this->Makefile->GetGlobalGenerator(); this->GG = this->Makefile->GetGlobalGenerator();
@@ -539,7 +542,7 @@ std::string cmFindLibraryCommand::FindNormalLibrary()
std::string cmFindLibraryCommand::FindNormalLibraryNamesPerDir() std::string cmFindLibraryCommand::FindNormalLibraryNamesPerDir()
{ {
// Search for all names in each directory. // 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) { for (std::string const& n : this->Names) {
helper.AddName(n); helper.AddName(n);
} }
@@ -556,7 +559,7 @@ std::string cmFindLibraryCommand::FindNormalLibraryNamesPerDir()
std::string cmFindLibraryCommand::FindNormalLibraryDirsPerName() std::string cmFindLibraryCommand::FindNormalLibraryDirsPerName()
{ {
// Search the entire path for each name. // 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) { for (std::string const& n : this->Names) {
// Switch to searching for this name. // Switch to searching for this name.
helper.SetName(n); helper.SetName(n);

View File

@@ -12,6 +12,7 @@
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
#include <cm/memory>
#include <cm/optional> #include <cm/optional>
#include <cmext/algorithm> #include <cmext/algorithm>
#include <cmext/string_view> #include <cmext/string_view>
@@ -529,7 +530,6 @@ cmFindPackageCommand::cmFindPackageCommand(cmExecutionStatus& status)
, VersionRangeMax(VERSION_ENDPOINT_INCLUDED) , VersionRangeMax(VERSION_ENDPOINT_INCLUDED)
{ {
this->CMakePathName = "PACKAGE"; this->CMakePathName = "PACKAGE";
this->DebugMode = false;
this->AppendSearchPathGroups(); this->AppendSearchPathGroups();
this->DeprecatedFindModules["Boost"] = cmPolicies::CMP0167; this->DeprecatedFindModules["Boost"] = cmPolicies::CMP0167;
@@ -704,7 +704,9 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
// Process debug mode // Process debug mode
cmMakefile::DebugFindPkgRAII debugFindPkgRAII(this->Makefile, this->Name); cmMakefile::DebugFindPkgRAII debugFindPkgRAII(this->Makefile, this->Name);
this->DebugMode = this->ComputeIfDebugModeWanted(); if (this->ComputeIfDebugModeWanted()) {
this->DebugState = cm::make_unique<cmFindPackageDebugState>(this);
}
// Parse the arguments. // Parse the arguments.
enum Doing enum Doing
@@ -3359,3 +3361,26 @@ bool cmFindPackage(std::vector<std::string> const& args,
{ {
return cmFindPackageCommand(status).InitialPass(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;
}

View File

@@ -342,3 +342,16 @@ struct hash<cmFindPackageCommand::ConfigFileInfo>
bool cmFindPackage(std::vector<std::string> const& args, bool cmFindPackage(std::vector<std::string> const& args,
cmExecutionStatus& status); 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;
};

View File

@@ -4,8 +4,11 @@
#include <utility> #include <utility>
#include <cm/memory>
#include "cmsys/Glob.hxx" #include "cmsys/Glob.hxx"
#include "cmFindCommon.h"
#include "cmStateTypes.h" #include "cmStateTypes.h"
#include "cmStringAlgorithms.h" #include "cmStringAlgorithms.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
@@ -35,7 +38,9 @@ bool cmFindPathCommand::InitialPass(std::vector<std::string> const& argsIn)
return false; return false;
} }
this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName); if (this->ComputeIfDebugModeWanted(this->VariableName)) {
this->DebugState = cm::make_unique<cmFindBaseDebugState>(this);
}
if (this->IsFound()) { if (this->IsFound()) {
this->NormalizeFindResult(); this->NormalizeFindResult();
@@ -49,24 +54,22 @@ bool cmFindPathCommand::InitialPass(std::vector<std::string> const& argsIn)
std::string cmFindPathCommand::FindHeader() std::string cmFindPathCommand::FindHeader()
{ {
cmFindBaseDebugState debug(this);
std::string header; std::string header;
if (this->SearchFrameworkFirst || this->SearchFrameworkOnly) { if (this->SearchFrameworkFirst || this->SearchFrameworkOnly) {
header = this->FindFrameworkHeader(debug); header = this->FindFrameworkHeader();
} }
if (header.empty() && !this->SearchFrameworkOnly) { if (header.empty() && !this->SearchFrameworkOnly) {
header = this->FindNormalHeader(debug); header = this->FindNormalHeader();
} }
if (header.empty() && this->SearchFrameworkLast) { if (header.empty() && this->SearchFrameworkLast) {
header = this->FindFrameworkHeader(debug); header = this->FindFrameworkHeader();
} }
return header; return header;
} }
std::string cmFindPathCommand::FindHeaderInFramework( std::string cmFindPathCommand::FindHeaderInFramework(
std::string const& file, std::string const& dir, std::string const& file, std::string const& dir) const
cmFindBaseDebugState& debug) const
{ {
std::string fileName = file; std::string fileName = file;
std::string frameWorkName; std::string frameWorkName;
@@ -90,13 +93,17 @@ std::string cmFindPathCommand::FindHeaderInFramework(
std::string intPath = cmStrCat(fpath, "/Headers/", fileName); std::string intPath = cmStrCat(fpath, "/Headers/", fileName);
if (cmSystemTools::FileExists(intPath) && if (cmSystemTools::FileExists(intPath) &&
this->Validate(this->IncludeFileInPath ? intPath : fpath)) { this->Validate(this->IncludeFileInPath ? intPath : fpath)) {
debug.FoundAt(intPath); if (this->DebugState) {
this->DebugState->FoundAt(intPath);
}
if (this->IncludeFileInPath) { if (this->IncludeFileInPath) {
return intPath; return intPath;
} }
return fpath; 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 // 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<std::string> files = globIt.GetFiles(); std::vector<std::string> files = globIt.GetFiles();
if (!files.empty()) { if (!files.empty()) {
std::string fheader = cmSystemTools::ToNormalizedPathOnDisk(files[0]); std::string fheader = cmSystemTools::ToNormalizedPathOnDisk(files[0]);
debug.FoundAt(fheader); if (this->DebugState) {
this->DebugState->FoundAt(fheader);
}
if (this->IncludeFileInPath) { if (this->IncludeFileInPath) {
return fheader; return fheader;
} }
@@ -119,7 +128,7 @@ std::string cmFindPathCommand::FindHeaderInFramework(
return ""; return "";
} }
std::string cmFindPathCommand::FindNormalHeader(cmFindBaseDebugState& debug) std::string cmFindPathCommand::FindNormalHeader()
{ {
std::string tryPath; std::string tryPath;
for (std::string const& n : this->Names) { for (std::string const& n : this->Names) {
@@ -127,23 +136,27 @@ std::string cmFindPathCommand::FindNormalHeader(cmFindBaseDebugState& debug)
tryPath = cmStrCat(sp, n); tryPath = cmStrCat(sp, n);
if (cmSystemTools::FileExists(tryPath) && if (cmSystemTools::FileExists(tryPath) &&
this->Validate(this->IncludeFileInPath ? tryPath : sp)) { this->Validate(this->IncludeFileInPath ? tryPath : sp)) {
debug.FoundAt(tryPath); if (this->DebugState) {
this->DebugState->FoundAt(tryPath);
}
if (this->IncludeFileInPath) { if (this->IncludeFileInPath) {
return tryPath; return tryPath;
} }
return sp; return sp;
} }
debug.FailedAt(tryPath); if (this->DebugState) {
this->DebugState->FailedAt(tryPath);
}
} }
} }
return ""; return "";
} }
std::string cmFindPathCommand::FindFrameworkHeader(cmFindBaseDebugState& debug) std::string cmFindPathCommand::FindFrameworkHeader()
{ {
for (std::string const& n : this->Names) { for (std::string const& n : this->Names) {
for (std::string const& sp : this->SearchPaths) { 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()) { if (!fwPath.empty()) {
return fwPath; return fwPath;
} }

View File

@@ -30,11 +30,10 @@ public:
private: private:
std::string FindHeaderInFramework(std::string const& file, std::string FindHeaderInFramework(std::string const& file,
std::string const& dir, std::string const& dir) const;
cmFindBaseDebugState& debug) const;
std::string FindHeader(); std::string FindHeader();
std::string FindNormalHeader(cmFindBaseDebugState& debug); std::string FindNormalHeader();
std::string FindFrameworkHeader(cmFindBaseDebugState& debug); std::string FindFrameworkHeader();
}; };
bool cmFindPath(std::vector<std::string> const& args, bool cmFindPath(std::vector<std::string> const& args,

View File

@@ -5,6 +5,9 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <cm/memory>
#include "cmFindCommon.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmMessageType.h" #include "cmMessageType.h"
#include "cmPolicies.h" #include "cmPolicies.h"
@@ -24,8 +27,9 @@ class cmExecutionStatus;
struct cmFindProgramHelper struct cmFindProgramHelper
{ {
cmFindProgramHelper(cmMakefile* makefile, cmFindBase const* base) cmFindProgramHelper(cmMakefile* makefile, cmFindBase const* base,
: DebugSearches(base) cmFindCommonDebugState* debugState)
: DebugState(debugState)
, Makefile(makefile) , Makefile(makefile)
, FindBase(base) , FindBase(base)
, PolicyCMP0109(makefile->GetPolicyStatus(cmPolicies::CMP0109)) , PolicyCMP0109(makefile->GetPolicyStatus(cmPolicies::CMP0109))
@@ -49,7 +53,7 @@ struct cmFindProgramHelper
std::vector<std::string> Names; std::vector<std::string> Names;
// Debug state // Debug state
cmFindBaseDebugState DebugSearches; cmFindCommonDebugState* DebugState;
cmMakefile* Makefile; cmMakefile* Makefile;
cmFindBase const* FindBase; cmFindBase const* FindBase;
@@ -93,11 +97,15 @@ struct cmFindProgramHelper
cmSystemTools::ToNormalizedPathOnDisk(testPath); cmSystemTools::ToNormalizedPathOnDisk(testPath);
if (this->FindBase->Validate(testPath)) { if (this->FindBase->Validate(testPath)) {
this->BestPath = testPath; this->BestPath = testPath;
this->DebugSearches.FoundAt(testPath); if (this->DebugState) {
this->DebugState->FoundAt(testPath);
}
return true; return true;
} }
} }
this->DebugSearches.FailedAt(testPath); if (this->DebugState) {
this->DebugState->FailedAt(testPath);
}
return false; return false;
}); });
} }
@@ -191,7 +199,9 @@ bool cmFindProgramCommand::InitialPass(std::vector<std::string> const& argsIn)
if (!this->ParseArguments(argsIn)) { if (!this->ParseArguments(argsIn)) {
return false; return false;
} }
this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName); if (this->ComputeIfDebugModeWanted(this->VariableName)) {
this->DebugState = cm::make_unique<cmFindBaseDebugState>(this);
}
if (this->IsFound()) { if (this->IsFound()) {
this->NormalizeFindResult(); this->NormalizeFindResult();
@@ -231,7 +241,7 @@ std::string cmFindProgramCommand::FindNormalProgram()
std::string cmFindProgramCommand::FindNormalProgramNamesPerDir() std::string cmFindProgramCommand::FindNormalProgramNamesPerDir()
{ {
// Search for all names in each directory. // 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) { for (std::string const& n : this->Names) {
helper.AddName(n); helper.AddName(n);
} }
@@ -254,7 +264,7 @@ std::string cmFindProgramCommand::FindNormalProgramNamesPerDir()
std::string cmFindProgramCommand::FindNormalProgramDirsPerName() std::string cmFindProgramCommand::FindNormalProgramDirsPerName()
{ {
// Search the entire path for each name. // 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) { for (std::string const& n : this->Names) {
// Switch to searching for this name. // Switch to searching for this name.
helper.SetName(n); helper.SetName(n);