1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 19:08:07 +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") { } else if (argsIn[j] == "ENV") {
if (j + 1 < size) { if (j + 1 < size) {
j++; 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 { } else {
args.push_back(argsIn[j]); args.push_back(argsIn[j]);
@@ -403,9 +405,11 @@ void cmFindBase::FillCMakeSystemVariablePath()
cmList expanded{ *prefix_paths }; cmList expanded{ *prefix_paths };
install_entry.remove_self(expanded); install_entry.remove_self(expanded);
staging_entry.remove_self(expanded); staging_entry.remove_self(expanded);
for (std::string& p : expanded) {
paths.AddPrefixPaths(expanded, p = cmSystemTools::CollapseFullPath(
this->Makefile->GetCurrentSourceDirectory().c_str()); p, this->Makefile->GetCurrentSourceDirectory());
}
paths.AddPrefixPaths(expanded);
} else if (add_install_prefix && !install_prefix_in_list) { } else if (add_install_prefix && !install_prefix_in_list) {
paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX"); paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX");
paths.AddCMakePrefixPath("CMAKE_STAGING_PREFIX"); paths.AddCMakePrefixPath("CMAKE_STAGING_PREFIX");
@@ -493,7 +497,6 @@ void cmFindBase::NormalizeFindResult()
this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory())) this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory()))
.Normal() .Normal()
.GenericString(); .GenericString();
// value = cmSystemTools::CollapseFullPath(*existingValue);
if (!cmSystemTools::FileExists(value, false)) { if (!cmSystemTools::FileExists(value, false)) {
value = *existingValue; value = *existingValue;
} }

View File

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

View File

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

View File

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

View File

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

View File

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