1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00

find_*: Explicitly normalize found paths as they exist on disk

This commit is contained in:
Brad King
2024-10-17 13:08:19 -04:00
parent 967d3ea85c
commit 9d44a77454
7 changed files with 52 additions and 54 deletions

View File

@@ -56,7 +56,9 @@ bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
} else if (argsIn[j] == "ENV") {
if (j + 1 < size) {
j++;
cmSystemTools::GetPath(args, argsIn[j].c_str());
std::vector<std::string> p =
cmSystemTools::GetEnvPathNormalized(argsIn[j]);
std::move(p.begin(), p.end(), std::back_inserter(args));
}
} else {
args.push_back(argsIn[j]);
@@ -403,9 +405,11 @@ void cmFindBase::FillCMakeSystemVariablePath()
cmList expanded{ *prefix_paths };
install_entry.remove_self(expanded);
staging_entry.remove_self(expanded);
paths.AddPrefixPaths(expanded,
this->Makefile->GetCurrentSourceDirectory().c_str());
for (std::string& p : expanded) {
p = cmSystemTools::CollapseFullPath(
p, this->Makefile->GetCurrentSourceDirectory());
}
paths.AddPrefixPaths(expanded);
} else if (add_install_prefix && !install_prefix_in_list) {
paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX");
paths.AddCMakePrefixPath("CMAKE_STAGING_PREFIX");
@@ -493,7 +497,6 @@ void cmFindBase::NormalizeFindResult()
this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory()))
.Normal()
.GenericString();
// value = cmSystemTools::CollapseFullPath(*existingValue);
if (!cmSystemTools::FileExists(value, false)) {
value = *existingValue;
}

View File

@@ -423,7 +423,7 @@ bool cmFindLibraryHelper::CheckDirectoryForName(std::string const& path,
std::string testPath = cmStrCat(path, name.Raw);
if (cmSystemTools::FileExists(testPath, true)) {
testPath = cmSystemTools::CollapseFullPath(testPath);
testPath = cmSystemTools::ToNormalizedPathOnDisk(testPath);
if (this->Validate(testPath)) {
this->DebugLibraryFound(name.Raw, path);
this->BestPath = testPath;
@@ -440,9 +440,7 @@ bool cmFindLibraryHelper::CheckDirectoryForName(std::string const& path,
unsigned int bestMinor = 0;
// Search for a file matching the library name regex.
std::string dir = path;
cmSystemTools::ConvertToUnixSlashes(dir);
std::set<std::string> const& files = this->GG->GetDirectoryContent(dir);
std::set<std::string> const& files = this->GG->GetDirectoryContent(path);
for (std::string const& origName : files) {
#if defined(_WIN32) || defined(__APPLE__)
std::string testName = cmSystemTools::LowerCase(origName);
@@ -453,11 +451,12 @@ bool cmFindLibraryHelper::CheckDirectoryForName(std::string const& path,
std::string testPath = cmStrCat(path, origName);
// Make sure the path is readable and is not a directory.
if (cmSystemTools::FileExists(testPath, true)) {
if (!this->Validate(cmSystemTools::CollapseFullPath(testPath))) {
testPath = cmSystemTools::ToNormalizedPathOnDisk(testPath);
if (!this->Validate(testPath)) {
continue;
}
this->DebugLibraryFound(name.Raw, dir);
this->DebugLibraryFound(name.Raw, path);
// 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
@@ -485,7 +484,7 @@ bool cmFindLibraryHelper::CheckDirectoryForName(std::string const& path,
}
if (this->BestPath.empty()) {
this->DebugLibraryFailed(name.Raw, dir);
this->DebugLibraryFailed(name.Raw, path);
} else {
this->DebugLibraryFound(name.Raw, this->BestPath);
}
@@ -554,7 +553,7 @@ std::string cmFindLibraryCommand::FindFrameworkLibraryNamesPerDir()
for (std::string const& n : this->Names) {
fwPath = cmStrCat(d, n, ".xcframework");
if (cmSystemTools::FileIsDirectory(fwPath)) {
auto finalPath = cmSystemTools::CollapseFullPath(fwPath);
auto finalPath = cmSystemTools::ToNormalizedPathOnDisk(fwPath);
if (this->Validate(finalPath)) {
return finalPath;
}
@@ -562,7 +561,7 @@ std::string cmFindLibraryCommand::FindFrameworkLibraryNamesPerDir()
fwPath = cmStrCat(d, n, ".framework");
if (cmSystemTools::FileIsDirectory(fwPath)) {
auto finalPath = cmSystemTools::CollapseFullPath(fwPath);
auto finalPath = cmSystemTools::ToNormalizedPathOnDisk(fwPath);
if (this->Validate(finalPath)) {
return finalPath;
}
@@ -582,7 +581,7 @@ std::string cmFindLibraryCommand::FindFrameworkLibraryDirsPerName()
for (std::string const& d : this->SearchPaths) {
fwPath = cmStrCat(d, n, ".xcframework");
if (cmSystemTools::FileIsDirectory(fwPath)) {
auto finalPath = cmSystemTools::CollapseFullPath(fwPath);
auto finalPath = cmSystemTools::ToNormalizedPathOnDisk(fwPath);
if (this->Validate(finalPath)) {
return finalPath;
}
@@ -590,7 +589,7 @@ std::string cmFindLibraryCommand::FindFrameworkLibraryDirsPerName()
fwPath = cmStrCat(d, n, ".framework");
if (cmSystemTools::FileIsDirectory(fwPath)) {
auto finalPath = cmSystemTools::CollapseFullPath(fwPath);
auto finalPath = cmSystemTools::ToNormalizedPathOnDisk(fwPath);
if (this->Validate(finalPath)) {
return finalPath;
}

View File

@@ -2118,9 +2118,9 @@ void cmFindPackageCommand::FillPrefixesSystemEnvironment()
// Use the system search path to generate prefixes.
// Relative paths are interpreted with respect to the current
// working directory.
std::vector<std::string> tmp;
cmSystemTools::GetPath(tmp);
for (std::string const& i : tmp) {
std::vector<std::string> envPATH =
cmSystemTools::GetEnvPathNormalized("PATH");
for (std::string const& i : envPATH) {
// If the path is a PREFIX/bin case then add its parent instead.
if ((cmHasLiteralSuffix(i, "/bin")) || (cmHasLiteralSuffix(i, "/sbin"))) {
paths.AddPath(cmSystemTools::GetFilenamePath(i));

View File

@@ -106,7 +106,7 @@ std::string cmFindPathCommand::FindHeaderInFramework(
globIt.FindFiles(glob);
std::vector<std::string> files = globIt.GetFiles();
if (!files.empty()) {
std::string fheader = cmSystemTools::CollapseFullPath(files[0]);
std::string fheader = cmSystemTools::ToNormalizedPathOnDisk(files[0]);
debug.FoundAt(fheader);
if (this->IncludeFileInPath) {
return fheader;

View File

@@ -89,6 +89,8 @@ struct cmFindProgramHelper
std::string testPath =
cmSystemTools::CollapseFullPath(testNameExt, path);
if (this->FileIsExecutable(testPath)) {
testPath =
cmSystemTools::ToNormalizedPathOnDisk(testPath);
if (this->FindBase->Validate(testPath)) {
this->BestPath = testPath;
this->DebugSearches.FoundAt(testPath);

View File

@@ -62,7 +62,9 @@ void cmSearchPath::AddUserPath(const std::string& path)
// Process them all from the current directory
for (std::string const& p : outPaths) {
this->AddPathInternal(
p, "", this->FC->Makefile->GetCurrentSourceDirectory().c_str());
cmSystemTools::CollapseFullPath(
p, this->FC->Makefile->GetCurrentSourceDirectory()),
"");
}
}
@@ -76,15 +78,17 @@ void cmSearchPath::AddCMakePath(const std::string& variable)
for (std::string const& p : expanded) {
this->AddPathInternal(
p, "", this->FC->Makefile->GetCurrentSourceDirectory().c_str());
cmSystemTools::CollapseFullPath(
p, this->FC->Makefile->GetCurrentSourceDirectory()),
"");
}
}
}
void cmSearchPath::AddEnvPath(const std::string& variable)
{
std::vector<std::string> expanded;
cmSystemTools::GetPath(expanded, variable.c_str());
std::vector<std::string> expanded =
cmSystemTools::GetEnvPathNormalized(variable);
for (std::string const& p : expanded) {
this->AddPathInternal(p, "");
}
@@ -97,9 +101,11 @@ void cmSearchPath::AddCMakePrefixPath(const std::string& variable)
// Get a path from a CMake variable.
if (cmValue value = this->FC->Makefile->GetDefinition(variable)) {
cmList expanded{ *value };
this->AddPrefixPaths(
expanded, this->FC->Makefile->GetCurrentSourceDirectory().c_str());
for (std::string& p : expanded) {
p = cmSystemTools::CollapseFullPath(
p, this->FC->Makefile->GetCurrentSourceDirectory());
}
this->AddPrefixPaths(expanded);
}
}
@@ -114,8 +120,8 @@ static std::string cmSearchPathStripBin(std::string const& s)
void cmSearchPath::AddEnvPrefixPath(const std::string& variable, bool stripBin)
{
std::vector<std::string> expanded;
cmSystemTools::GetPath(expanded, variable.c_str());
std::vector<std::string> expanded =
cmSystemTools::GetEnvPathNormalized(variable);
if (stripBin) {
std::transform(expanded.begin(), expanded.end(), expanded.begin(),
cmSearchPathStripBin);
@@ -151,8 +157,7 @@ void cmSearchPath::AddSuffixes(const std::vector<std::string>& suffixes)
}
}
void cmSearchPath::AddPrefixPaths(const std::vector<std::string>& paths,
const char* base)
void cmSearchPath::AddPrefixPaths(const std::vector<std::string>& paths)
{
assert(this->FC);
@@ -192,52 +197,43 @@ void cmSearchPath::AddPrefixPaths(const std::vector<std::string>& paths,
"CMAKE_PREFIX_LIBRARY_ARCHITECTURE")) {
if (foundUnknown) {
this->AddPathInternal(cmStrCat('/', archNoUnknown, dir, subdir),
cmStrCat('/', archNoUnknown, prefix), base);
cmStrCat('/', archNoUnknown, prefix));
}
this->AddPathInternal(cmStrCat('/', *arch, dir, subdir),
cmStrCat('/', *arch, prefix), base);
cmStrCat('/', *arch, prefix));
} else {
if (foundUnknown) {
this->AddPathInternal(cmStrCat(dir, subdir, '/', archNoUnknown),
prefix, base);
prefix);
}
this->AddPathInternal(cmStrCat(dir, subdir, '/', *arch), prefix,
base);
this->AddPathInternal(cmStrCat(dir, subdir, '/', *arch), prefix);
}
}
}
std::string add = dir + subdir;
if (add != "/") {
this->AddPathInternal(add, prefix, base);
this->AddPathInternal(add, prefix);
}
if (subdir == "bin") {
this->AddPathInternal(dir + "sbin", prefix, base);
this->AddPathInternal(dir + "sbin", prefix);
}
if (!subdir.empty() && path != "/") {
this->AddPathInternal(path, prefix, base);
this->AddPathInternal(path, prefix);
}
}
}
void cmSearchPath::AddPathInternal(const std::string& path,
const std::string& prefix, const char* base)
const std::string& prefix)
{
assert(this->FC);
std::string collapsedPath = cmSystemTools::CollapseFullPath(path, base);
if (collapsedPath.empty()) {
if (path.empty()) {
return;
}
std::string collapsedPrefix;
if (!prefix.empty()) {
collapsedPrefix = cmSystemTools::CollapseFullPath(prefix, base);
}
// Insert the path if has not already been emitted.
PathWithPrefix pathWithPrefix{ std::move(collapsedPath),
std::move(collapsedPrefix) };
PathWithPrefix pathWithPrefix{ path, prefix };
if (this->FC->SearchPathsEmitted.insert(pathWithPrefix).second) {
this->Paths.emplace_back(std::move(pathWithPrefix));
}

View File

@@ -55,12 +55,10 @@ public:
void AddCMakePrefixPath(const std::string& variable);
void AddEnvPrefixPath(const std::string& variable, bool stripBin = false);
void AddSuffixes(const std::vector<std::string>& suffixes);
void AddPrefixPaths(const std::vector<std::string>& paths,
const char* base = nullptr);
void AddPrefixPaths(const std::vector<std::string>& paths);
protected:
void AddPathInternal(const std::string& path, const std::string& prefix,
const char* base = nullptr);
void AddPathInternal(const std::string& path, const std::string& prefix);
cmFindCommon* FC;
std::vector<PathWithPrefix> Paths;