From 9d44a77454484eb7b590a6a7c4d09b62dcc97a52 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 17 Oct 2024 13:08:19 -0400 Subject: [PATCH] find_*: Explicitly normalize found paths as they exist on disk --- Source/cmFindBase.cxx | 13 +++++--- Source/cmFindLibraryCommand.cxx | 21 ++++++------- Source/cmFindPackageCommand.cxx | 6 ++-- Source/cmFindPathCommand.cxx | 2 +- Source/cmFindProgramCommand.cxx | 2 ++ Source/cmSearchPath.cxx | 56 +++++++++++++++------------------ Source/cmSearchPath.h | 6 ++-- 7 files changed, 52 insertions(+), 54 deletions(-) diff --git a/Source/cmFindBase.cxx b/Source/cmFindBase.cxx index 2abf341ffd..ed29b828a4 100644 --- a/Source/cmFindBase.cxx +++ b/Source/cmFindBase.cxx @@ -56,7 +56,9 @@ bool cmFindBase::ParseArguments(std::vector const& argsIn) } else if (argsIn[j] == "ENV") { if (j + 1 < size) { j++; - cmSystemTools::GetPath(args, argsIn[j].c_str()); + std::vector 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; } diff --git a/Source/cmFindLibraryCommand.cxx b/Source/cmFindLibraryCommand.cxx index ecd1130b78..c5a60383f8 100644 --- a/Source/cmFindLibraryCommand.cxx +++ b/Source/cmFindLibraryCommand.cxx @@ -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 const& files = this->GG->GetDirectoryContent(dir); + std::set 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; } diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 03b95eab07..67b42a3980 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -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 tmp; - cmSystemTools::GetPath(tmp); - for (std::string const& i : tmp) { + std::vector 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)); diff --git a/Source/cmFindPathCommand.cxx b/Source/cmFindPathCommand.cxx index 74a69d80aa..b6a7834c81 100644 --- a/Source/cmFindPathCommand.cxx +++ b/Source/cmFindPathCommand.cxx @@ -106,7 +106,7 @@ std::string cmFindPathCommand::FindHeaderInFramework( globIt.FindFiles(glob); std::vector 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; diff --git a/Source/cmFindProgramCommand.cxx b/Source/cmFindProgramCommand.cxx index fc251fc808..51be6e08b9 100644 --- a/Source/cmFindProgramCommand.cxx +++ b/Source/cmFindProgramCommand.cxx @@ -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); diff --git a/Source/cmSearchPath.cxx b/Source/cmSearchPath.cxx index ec70c05563..bf3480ffb6 100644 --- a/Source/cmSearchPath.cxx +++ b/Source/cmSearchPath.cxx @@ -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 expanded; - cmSystemTools::GetPath(expanded, variable.c_str()); + std::vector 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 expanded; - cmSystemTools::GetPath(expanded, variable.c_str()); + std::vector 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& suffixes) } } -void cmSearchPath::AddPrefixPaths(const std::vector& paths, - const char* base) +void cmSearchPath::AddPrefixPaths(const std::vector& paths) { assert(this->FC); @@ -192,52 +197,43 @@ void cmSearchPath::AddPrefixPaths(const std::vector& 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)); } diff --git a/Source/cmSearchPath.h b/Source/cmSearchPath.h index 4c0cabbf3f..895f4a3c73 100644 --- a/Source/cmSearchPath.h +++ b/Source/cmSearchPath.h @@ -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& suffixes); - void AddPrefixPaths(const std::vector& paths, - const char* base = nullptr); + void AddPrefixPaths(const std::vector& 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 Paths;