find_*: Use debug logging infrastructure

Teach the find_package, find_library, find_program, find_path, and
find_file commands to print debug log messages when enabled by the
`--debug-find` command-line option or `CMAKE_FIND_DEBUG_MODE` variable.
This commit is contained in:
Robert Maynard
2019-12-11 13:01:09 -05:00
parent a7ea20649d
commit 204b8d9f4e
21 changed files with 404 additions and 31 deletions

View File

@@ -29,6 +29,7 @@ cmFindLibraryCommand::cmFindLibraryCommand(cmExecutionStatus& status)
// cmFindLibraryCommand
bool cmFindLibraryCommand::InitialPass(std::vector<std::string> const& argsIn)
{
this->DebugMode = ComputeIfDebugModeWanted();
this->VariableDocumentation = "Path to a library.";
this->CMakePathName = "LIBRARY";
if (!this->ParseArguments(argsIn)) {
@@ -92,6 +93,13 @@ void cmFindLibraryCommand::AddArchitecturePaths(const char* suffix)
original.swap(this->SearchPaths);
for (std::string const& o : original) {
this->AddArchitecturePath(o, 0, suffix);
if (this->DebugMode) {
std::string msg = cmStrCat(
"find_library(", this->VariableName, ") removed original suffix ", o,
" from PATH_SUFFIXES while adding architecture paths for suffix '",
suffix, "'");
this->DebugMessage(msg);
}
}
}
@@ -153,11 +161,23 @@ void cmFindLibraryCommand::AddArchitecturePath(
if (use_dirX) {
dirX += "/";
if (this->DebugMode) {
std::string msg = cmStrCat(
"find_library(", this->VariableName, ") added replacement path ",
dirX, " to PATH_SUFFIXES for architecture suffix '", suffix, "'");
this->DebugMessage(msg);
}
this->SearchPaths.push_back(std::move(dirX));
}
if (use_dir) {
this->SearchPaths.push_back(dir);
if (this->DebugMode) {
std::string msg = cmStrCat(
"find_library(", this->VariableName, ") added replacement path ",
dir, " to PATH_SUFFIXES for architecture suffix '", suffix, "'");
this->DebugMessage(msg);
}
}
}
}
@@ -179,7 +199,7 @@ std::string cmFindLibraryCommand::FindLibrary()
struct cmFindLibraryHelper
{
cmFindLibraryHelper(cmMakefile* mf);
cmFindLibraryHelper(cmMakefile* mf, cmFindBase const* findBase);
// Context information.
cmMakefile* Makefile;
@@ -198,6 +218,8 @@ struct cmFindLibraryHelper
// Support for OpenBSD shared library naming: lib<name>.so.<major>.<minor>
bool OpenBSD;
bool DebugMode;
// Current names under consideration.
struct Name
{
@@ -227,10 +249,33 @@ struct cmFindLibraryHelper
void SetName(std::string const& name);
bool CheckDirectory(std::string const& path);
bool CheckDirectoryForName(std::string const& path, Name& name);
cmFindBaseDebugState DebugSearches;
void DebugLibraryFailed(std::string const& name, std::string const& path)
{
if (this->DebugMode) {
auto regexName =
cmStrCat(this->PrefixRegexStr, name, this->SuffixRegexStr);
this->DebugSearches.FailedAt(path, regexName);
}
};
void DebugLibraryFound(std::string const& name, std::string const& path)
{
if (this->DebugMode) {
auto regexName =
cmStrCat(this->PrefixRegexStr, name, this->SuffixRegexStr);
this->DebugSearches.FoundAt(path, regexName);
}
};
};
cmFindLibraryHelper::cmFindLibraryHelper(cmMakefile* mf)
cmFindLibraryHelper::cmFindLibraryHelper(cmMakefile* mf,
cmFindBase const* base)
: Makefile(mf)
, DebugMode(base->DebugModeEnabled())
, DebugSearches("find_library", base)
{
this->GG = this->Makefile->GetGlobalGenerator();
@@ -350,7 +395,12 @@ bool cmFindLibraryHelper::CheckDirectoryForName(std::string const& path,
// library or an import library).
if (name.TryRaw) {
this->TestPath = cmStrCat(path, name.Raw);
if (cmSystemTools::FileExists(this->TestPath, true)) {
const bool exists = cmSystemTools::FileExists(this->TestPath, true);
if (!exists) {
this->DebugLibraryFailed(name.Raw, path);
} else {
this->DebugLibraryFound(name.Raw, path);
this->BestPath = cmSystemTools::CollapseFullPath(this->TestPath);
cmSystemTools::ConvertToUnixSlashes(this->BestPath);
return true;
@@ -376,6 +426,8 @@ bool cmFindLibraryHelper::CheckDirectoryForName(std::string const& path,
if (name.Regex.find(testName)) {
this->TestPath = cmStrCat(path, origName);
if (!cmSystemTools::FileIsDirectory(this->TestPath)) {
this->DebugLibraryFound(name.Raw, dir);
// This is a matching file. Check if it is better than the
// best name found so far. Earlier prefixes are preferred,
// followed by earlier suffixes. For OpenBSD, shared library
@@ -402,6 +454,12 @@ bool cmFindLibraryHelper::CheckDirectoryForName(std::string const& path,
}
}
if (this->BestPath.empty()) {
this->DebugLibraryFailed(name.Raw, dir);
} else {
this->DebugLibraryFound(name.Raw, this->BestPath);
}
// Use the best candidate found in this directory, if any.
return !this->BestPath.empty();
}
@@ -417,7 +475,7 @@ std::string cmFindLibraryCommand::FindNormalLibrary()
std::string cmFindLibraryCommand::FindNormalLibraryNamesPerDir()
{
// Search for all names in each directory.
cmFindLibraryHelper helper(this->Makefile);
cmFindLibraryHelper helper(this->Makefile, this);
for (std::string const& n : this->Names) {
helper.AddName(n);
}
@@ -434,7 +492,7 @@ std::string cmFindLibraryCommand::FindNormalLibraryNamesPerDir()
std::string cmFindLibraryCommand::FindNormalLibraryDirsPerName()
{
// Search the entire path for each name.
cmFindLibraryHelper helper(this->Makefile);
cmFindLibraryHelper helper(this->Makefile, this);
for (std::string const& n : this->Names) {
// Switch to searching for this name.
helper.SetName(n);