diff options
author | Kyle Edwards <kyle.edwards@kitware.com> | 2022-02-01 19:42:54 (GMT) |
---|---|---|
committer | Kyle Edwards <kyle.edwards@kitware.com> | 2022-02-02 16:09:00 (GMT) |
commit | bd805a51ae4b5be5be6bbadf4afab108fcf4ddb3 (patch) | |
tree | 9f487e27f9af0ef2b9c092919c1165c85bb4bc11 | |
parent | 92459258108c0036808f29f83a8503fea18ccee9 (diff) | |
download | CMake-bd805a51ae4b5be5be6bbadf4afab108fcf4ddb3.zip CMake-bd805a51ae4b5be5be6bbadf4afab108fcf4ddb3.tar.gz CMake-bd805a51ae4b5be5be6bbadf4afab108fcf4ddb3.tar.bz2 |
Refactor: Keep track of prefixes in cmSearchPath
-rw-r--r-- | Source/cmFindCommon.h | 2 | ||||
-rw-r--r-- | Source/cmFindPackageCommand.cxx | 2 | ||||
-rw-r--r-- | Source/cmSearchPath.cxx | 59 | ||||
-rw-r--r-- | Source/cmSearchPath.h | 18 |
4 files changed, 54 insertions, 27 deletions
diff --git a/Source/cmFindCommon.h b/Source/cmFindCommon.h index 49d64e4..6649c35 100644 --- a/Source/cmFindCommon.h +++ b/Source/cmFindCommon.h @@ -135,7 +135,7 @@ protected: std::map<PathLabel, cmSearchPath> LabeledPaths; std::vector<std::string> SearchPaths; - std::set<std::string> SearchPathsEmitted; + std::set<cmSearchPath::PathWithPrefix> SearchPathsEmitted; bool SearchFrameworkFirst; bool SearchFrameworkOnly; diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index c468a3c..85badf6 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -1309,7 +1309,7 @@ inline std::size_t collectPathsForDebug(std::string& buffer, return 0; } for (std::size_t i = startIndex; i < paths.size(); i++) { - buffer += " " + paths[i] + "\n"; + buffer += " " + paths[i].Path + "\n"; } return paths.size(); } diff --git a/Source/cmSearchPath.cxx b/Source/cmSearchPath.cxx index 1bb459c..ff54a8a 100644 --- a/Source/cmSearchPath.cxx +++ b/Source/cmSearchPath.cxx @@ -26,16 +26,16 @@ void cmSearchPath::ExtractWithout(const std::set<std::string>& ignore, if (clear) { outPaths.clear(); } - for (std::string const& path : this->Paths) { - if (ignore.count(path) == 0) { - outPaths.push_back(path); + for (auto const& path : this->Paths) { + if (ignore.count(path.Path) == 0) { + outPaths.push_back(path.Path); } } } void cmSearchPath::AddPath(const std::string& path) { - this->AddPathInternal(path); + this->AddPathInternal(path, ""); } void cmSearchPath::AddUserPath(const std::string& path) @@ -69,7 +69,7 @@ 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()); + p, "", this->FC->Makefile->GetCurrentSourceDirectory().c_str()); } } @@ -83,7 +83,7 @@ void cmSearchPath::AddCMakePath(const std::string& variable) for (std::string const& p : expanded) { this->AddPathInternal( - p, this->FC->Makefile->GetCurrentSourceDirectory().c_str()); + p, "", this->FC->Makefile->GetCurrentSourceDirectory().c_str()); } } } @@ -93,7 +93,7 @@ void cmSearchPath::AddEnvPath(const std::string& variable) std::vector<std::string> expanded; cmSystemTools::GetPath(expanded, variable.c_str()); for (std::string const& p : expanded) { - this->AddPathInternal(p); + this->AddPathInternal(p, ""); } } @@ -132,24 +132,25 @@ void cmSearchPath::AddEnvPrefixPath(const std::string& variable, bool stripBin) void cmSearchPath::AddSuffixes(const std::vector<std::string>& suffixes) { - std::vector<std::string> inPaths; + std::vector<PathWithPrefix> inPaths; inPaths.swap(this->Paths); this->Paths.reserve(inPaths.size() * (suffixes.size() + 1)); - for (std::string& inPath : inPaths) { - cmSystemTools::ConvertToUnixSlashes(inPath); + for (PathWithPrefix& inPath : inPaths) { + cmSystemTools::ConvertToUnixSlashes(inPath.Path); + cmSystemTools::ConvertToUnixSlashes(inPath.Prefix); // if *i is only / then do not add a // // this will get incorrectly considered a network // path on windows and cause huge delays. - std::string p = inPath; + std::string p = inPath.Path; if (!p.empty() && p.back() != '/') { p += "/"; } // Combine with all the suffixes for (std::string const& suffix : suffixes) { - this->Paths.push_back(p + suffix); + this->Paths.push_back(PathWithPrefix{ p + suffix, inPath.Prefix }); } // And now the original w/o any suffix @@ -178,6 +179,10 @@ void cmSearchPath::AddPrefixPaths(const std::vector<std::string>& paths, if (!subdir.empty() && !dir.empty() && dir.back() != '/') { dir += "/"; } + std::string prefix = dir; + if (!prefix.empty() && prefix != "/") { + prefix.erase(prefix.size() - 1); + } if (subdir == "include" || subdir == "lib") { cmValue arch = this->FC->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE"); @@ -185,37 +190,47 @@ void cmSearchPath::AddPrefixPaths(const std::vector<std::string>& paths, if (this->FC->Makefile->IsDefinitionSet("CMAKE_SYSROOT") && this->FC->Makefile->IsDefinitionSet( "CMAKE_PREFIX_LIBRARY_ARCHITECTURE")) { - this->AddPathInternal(cmStrCat('/', *arch, dir, subdir), base); + this->AddPathInternal(cmStrCat('/', *arch, dir, subdir), + cmStrCat('/', *arch, prefix), base); } else { - this->AddPathInternal(cmStrCat(dir, subdir, '/', *arch), base); + this->AddPathInternal(cmStrCat(dir, subdir, '/', *arch), prefix, + base); } } } std::string add = dir + subdir; if (add != "/") { - this->AddPathInternal(add, base); + this->AddPathInternal(add, prefix, base); } if (subdir == "bin") { - this->AddPathInternal(dir + "sbin", base); + this->AddPathInternal(dir + "sbin", prefix, base); } if (!subdir.empty() && path != "/") { - this->AddPathInternal(path, base); + this->AddPathInternal(path, prefix, base); } } } -void cmSearchPath::AddPathInternal(const std::string& path, const char* base) +void cmSearchPath::AddPathInternal(const std::string& path, + const std::string& prefix, const char* base) { assert(this->FC != nullptr); - std::string collapsed = cmSystemTools::CollapseFullPath(path, base); + std::string collapsedPath = cmSystemTools::CollapseFullPath(path, base); - if (collapsed.empty()) { + if (collapsedPath.empty()) { return; } + std::string collapsedPrefix; + if (!prefix.empty()) { + collapsedPrefix = cmSystemTools::CollapseFullPath(prefix, base); + } + // Insert the path if has not already been emitted. - if (this->FC->SearchPathsEmitted.insert(collapsed).second) { - this->Paths.push_back(std::move(collapsed)); + PathWithPrefix pathWithPrefix{ std::move(collapsedPath), + std::move(collapsedPrefix) }; + 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 09f9722..08f9cfa 100644 --- a/Source/cmSearchPath.h +++ b/Source/cmSearchPath.h @@ -29,7 +29,18 @@ public: cmSearchPath(const cmSearchPath&) = default; cmSearchPath& operator=(const cmSearchPath&) = default; - const std::vector<std::string>& GetPaths() const { return this->Paths; } + struct PathWithPrefix + { + std::string Path; + std::string Prefix; + + bool operator<(const PathWithPrefix& other) const + { + return this->Path < other.Path || + (this->Path == other.Path && this->Prefix < other.Prefix); + } + }; + const std::vector<PathWithPrefix>& GetPaths() const { return this->Paths; } std::size_t size() const { return this->Paths.size(); } void ExtractWithout(const std::set<std::string>& ignore, @@ -47,8 +58,9 @@ public: const char* base = nullptr); protected: - void AddPathInternal(const std::string& path, const char* base = nullptr); + void AddPathInternal(const std::string& path, const std::string& prefix, + const char* base = nullptr); cmFindCommon* FC; - std::vector<std::string> Paths; + std::vector<PathWithPrefix> Paths; }; |