diff options
author | Brad King <brad.king@kitware.com> | 2022-02-03 12:35:16 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2022-02-03 12:35:23 (GMT) |
commit | 7077916781ea0997d65db949498754debe43a5e6 (patch) | |
tree | 020a6a5d13e951c31f828ef62f2ff0bd578f7b5f /Source | |
parent | 0c0d4e1c7a26fccd873a1053772d7989e18a871a (diff) | |
parent | 201d8c429843e9e57f833fcca8794f532e6d3028 (diff) | |
download | CMake-7077916781ea0997d65db949498754debe43a5e6.zip CMake-7077916781ea0997d65db949498754debe43a5e6.tar.gz CMake-7077916781ea0997d65db949498754debe43a5e6.tar.bz2 |
Merge topic 'cmake-ignore-prefix-path'
201d8c4298 find_*(): Add CMAKE_IGNORE_PREFIX_PATH variable
bd805a51ae Refactor: Keep track of prefixes in cmSearchPath
Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !6880
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmFindCommon.cxx | 46 | ||||
-rw-r--r-- | Source/cmFindCommon.h | 6 | ||||
-rw-r--r-- | Source/cmFindPackageCommand.cxx | 15 | ||||
-rw-r--r-- | Source/cmFindPackageCommand.h | 1 | ||||
-rw-r--r-- | Source/cmSearchPath.cxx | 63 | ||||
-rw-r--r-- | Source/cmSearchPath.h | 21 |
6 files changed, 114 insertions, 38 deletions
diff --git a/Source/cmFindCommon.cxx b/Source/cmFindCommon.cxx index c58db1e..1bb98ea 100644 --- a/Source/cmFindCommon.cxx +++ b/Source/cmFindCommon.cxx @@ -262,11 +262,13 @@ void cmFindCommon::RerootPaths(std::vector<std::string>& paths) (stagePrefix && isSameDirectoryOrSubDirectory(up, *stagePrefix))) { rootedDir = up; } else if (!up.empty() && up[0] != '~') { - // Start with the new root. - rootedDir = cmStrCat(r, '/'); - - // Append the original path with its old root removed. - rootedDir += cmSystemTools::SplitPathRootComponent(up); + auto const* split = cmSystemTools::SplitPathRootComponent(up); + if (split && *split) { + // Start with the new root. + rootedDir = cmStrCat(r, '/', split); + } else { + rootedDir = r; + } } // Store the new path. @@ -306,6 +308,31 @@ void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore) ignore.insert(ignoreVec.begin(), ignoreVec.end()); } +void cmFindCommon::GetIgnoredPrefixPaths(std::vector<std::string>& ignore) +{ + static constexpr const char* paths[] = { + "CMAKE_SYSTEM_IGNORE_PREFIX_PATH", + "CMAKE_IGNORE_PREFIX_PATH", + }; + + // Construct the list of path roots with no trailing slashes. + for (const char* pathName : paths) { + // Get the list of paths to ignore from the variable. + this->Makefile->GetDefExpandList(pathName, ignore); + } + + for (std::string& i : ignore) { + cmSystemTools::ConvertToUnixSlashes(i); + } +} + +void cmFindCommon::GetIgnoredPrefixPaths(std::set<std::string>& ignore) +{ + std::vector<std::string> ignoreVec; + this->GetIgnoredPrefixPaths(ignoreVec); + ignore.insert(ignoreVec.begin(), ignoreVec.end()); +} + bool cmFindCommon::CheckCommonArgument(std::string const& arg) { if (arg == "NO_DEFAULT_PATH") { @@ -369,16 +396,19 @@ static void AddTrailingSlash(std::string& s) void cmFindCommon::ComputeFinalPaths(IgnorePaths ignorePaths) { // Filter out ignored paths from the prefix list - std::set<std::string> ignored; + std::set<std::string> ignoredPaths; + std::set<std::string> ignoredPrefixes; if (ignorePaths == IgnorePaths::Yes) { - this->GetIgnoredPaths(ignored); + this->GetIgnoredPaths(ignoredPaths); + this->GetIgnoredPrefixPaths(ignoredPrefixes); } // Combine the separate path types, filtering out ignores this->SearchPaths.clear(); std::vector<PathLabel>& allLabels = this->PathGroupLabelMap[PathGroup::All]; for (PathLabel const& l : allLabels) { - this->LabeledPaths[l].ExtractWithout(ignored, this->SearchPaths); + this->LabeledPaths[l].ExtractWithout(ignoredPaths, ignoredPrefixes, + this->SearchPaths); } // Expand list of paths inside all search roots. diff --git a/Source/cmFindCommon.h b/Source/cmFindCommon.h index 49d64e4..5d9b3e1 100644 --- a/Source/cmFindCommon.h +++ b/Source/cmFindCommon.h @@ -86,6 +86,10 @@ protected: void GetIgnoredPaths(std::vector<std::string>& ignore); void GetIgnoredPaths(std::set<std::string>& ignore); + /** Get ignored paths from CMAKE_[SYSTEM_]IGNORE_PREFIX_PATH variables. */ + void GetIgnoredPrefixPaths(std::vector<std::string>& ignore); + void GetIgnoredPrefixPaths(std::set<std::string>& ignore); + /** Compute final search path list (reroot + trailing slash). */ enum class IgnorePaths { @@ -135,7 +139,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..fac0e08 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -665,6 +665,16 @@ bool cmFindPackageCommand::FindPackageUsingConfigMode() this->IgnoredPaths.clear(); this->IgnoredPaths.insert(ignored.begin(), ignored.end()); + // get igonored prefix paths from vars and reroot them. + std::vector<std::string> ignoredPrefixes; + this->GetIgnoredPrefixPaths(ignoredPrefixes); + this->RerootPaths(ignoredPrefixes); + + // Construct a set of ignored prefix paths + this->IgnoredPrefixPaths.clear(); + this->IgnoredPrefixPaths.insert(ignoredPrefixes.begin(), + ignoredPrefixes.end()); + // Find and load the package. return this->HandlePackageMode(HandlePackageModeType::Config); } @@ -1309,7 +1319,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(); } @@ -2291,7 +2301,8 @@ bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in) if (prefixWithoutSlash != "/" && prefixWithoutSlash.back() == '/') { prefixWithoutSlash.erase(prefixWithoutSlash.length() - 1); } - if (this->IgnoredPaths.count(prefixWithoutSlash)) { + if (this->IgnoredPaths.count(prefixWithoutSlash) || + this->IgnoredPrefixPaths.count(prefixWithoutSlash)) { return false; } diff --git a/Source/cmFindPackageCommand.h b/Source/cmFindPackageCommand.h index 9d6eddf..439d934 100644 --- a/Source/cmFindPackageCommand.h +++ b/Source/cmFindPackageCommand.h @@ -205,6 +205,7 @@ private: std::vector<std::string> Names; std::vector<std::string> Configs; std::set<std::string> IgnoredPaths; + std::set<std::string> IgnoredPrefixPaths; std::string DebugBuffer; /*! the selected sortOrder (None by default)*/ diff --git a/Source/cmSearchPath.cxx b/Source/cmSearchPath.cxx index 1bb459c..bfee64c 100644 --- a/Source/cmSearchPath.cxx +++ b/Source/cmSearchPath.cxx @@ -19,23 +19,25 @@ cmSearchPath::cmSearchPath(cmFindCommon* findCmd) cmSearchPath::~cmSearchPath() = default; -void cmSearchPath::ExtractWithout(const std::set<std::string>& ignore, +void cmSearchPath::ExtractWithout(const std::set<std::string>& ignorePaths, + const std::set<std::string>& ignorePrefixes, std::vector<std::string>& outPaths, bool clear) const { 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 (ignorePaths.count(path.Path) == 0 && + ignorePrefixes.count(path.Prefix) == 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 +71,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 +85,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 +95,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 +134,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 +181,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 +192,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..4c0cabb 100644 --- a/Source/cmSearchPath.h +++ b/Source/cmSearchPath.h @@ -29,10 +29,22 @@ 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, + void ExtractWithout(const std::set<std::string>& ignorePaths, + const std::set<std::string>& ignorePrefixes, std::vector<std::string>& outPaths, bool clear = false) const; @@ -47,8 +59,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; }; |