1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-05-08 14:29:03 +08:00

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 =
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(

View File

@ -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<bool>(this->DebugState);
}
void cmFindCommon::DebugMessage(std::string const& msg) const
{
if (this->Makefile) {

View File

@ -5,6 +5,7 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
@ -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<cmFindCommonDebugState> DebugState;
bool NoDefaultPath;
bool NoPackageRootPath;
bool NoCMakePath;

View File

@ -9,10 +9,12 @@
#include <set>
#include <utility>
#include <cm/memory>
#include <cm/optional>
#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<std::string> const& argsIn)
return false;
}
this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName);
if (this->ComputeIfDebugModeWanted(this->VariableName)) {
this->DebugState = cm::make_unique<cmFindBaseDebugState>(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<name>.so.<major>.<minor>
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);

View File

@ -12,6 +12,7 @@
#include <unordered_set>
#include <utility>
#include <cm/memory>
#include <cm/optional>
#include <cmext/algorithm>
#include <cmext/string_view>
@ -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<std::string> const& args)
// Process debug mode
cmMakefile::DebugFindPkgRAII debugFindPkgRAII(this->Makefile, this->Name);
this->DebugMode = this->ComputeIfDebugModeWanted();
if (this->ComputeIfDebugModeWanted()) {
this->DebugState = cm::make_unique<cmFindPackageDebugState>(this);
}
// Parse the arguments.
enum Doing
@ -3359,3 +3361,26 @@ bool cmFindPackage(std::vector<std::string> 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;
}

View File

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

View File

@ -4,8 +4,11 @@
#include <utility>
#include <cm/memory>
#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<std::string> const& argsIn)
return false;
}
this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName);
if (this->ComputeIfDebugModeWanted(this->VariableName)) {
this->DebugState = cm::make_unique<cmFindBaseDebugState>(this);
}
if (this->IsFound()) {
this->NormalizeFindResult();
@ -49,24 +54,22 @@ bool cmFindPathCommand::InitialPass(std::vector<std::string> 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<std::string> 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;
}

View File

@ -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<std::string> const& args,

View File

@ -5,6 +5,9 @@
#include <algorithm>
#include <string>
#include <cm/memory>
#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<std::string> 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<std::string> const& argsIn)
if (!this->ParseArguments(argsIn)) {
return false;
}
this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName);
if (this->ComputeIfDebugModeWanted(this->VariableName)) {
this->DebugState = cm::make_unique<cmFindBaseDebugState>(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);