From 30e5c1d92baf6e5ac0daed4e4debcdf208968a65 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Mon, 31 Jan 2022 10:33:09 -0500 Subject: find_package(): Add tests for CMAKE_IGNORE_PATH --- Tests/RunCMake/find_package/IgnorePath.cmake | 12 ++++++++++++ Tests/RunCMake/find_package/RunCMakeTest.cmake | 1 + 2 files changed, 13 insertions(+) create mode 100644 Tests/RunCMake/find_package/IgnorePath.cmake diff --git a/Tests/RunCMake/find_package/IgnorePath.cmake b/Tests/RunCMake/find_package/IgnorePath.cmake new file mode 100644 index 0000000..f40549b --- /dev/null +++ b/Tests/RunCMake/find_package/IgnorePath.cmake @@ -0,0 +1,12 @@ +set(CMAKE_PREFIX_PATH + ${CMAKE_SOURCE_DIR}/PackageRoot/foo/cmake_root + ${CMAKE_SOURCE_DIR}/PackageRoot/foo/env_root + ) +set(CMAKE_IGNORE_PATH + ${CMAKE_SOURCE_DIR}/PackageRoot//foo/cmake_root// # Test double slashes + ${CMAKE_SOURCE_DIR}/PackageRoot/foo/env_root/cmake + ) +find_package(Bar QUIET CONFIG) +if(Bar_FOUND) + message(FATAL_ERROR "Bar should not be found, was found in ${Bar_DIR}") +endif() diff --git a/Tests/RunCMake/find_package/RunCMakeTest.cmake b/Tests/RunCMake/find_package/RunCMakeTest.cmake index 2bace98..12701dc 100644 --- a/Tests/RunCMake/find_package/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_package/RunCMakeTest.cmake @@ -44,6 +44,7 @@ run_cmake(VersionRangeConfig2) run_cmake(VersionRangeConfig02) run_cmake(VersionRangeConfigStd) run_cmake(VersionRangeConfigStd2) +run_cmake(IgnorePath) if(UNIX AND NOT MSYS # FIXME: This works on CYGWIN but not on MSYS ) -- cgit v0.12 From 11f97d196880e78717211ab68138e9ff922ec826 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Fri, 28 Jan 2022 16:15:17 -0500 Subject: find_package(): Refactor CMAKE_[SYSTEM_]IGNORE_PATH In the old implementation, CMAKE_[SYSTEM_]IGNORE_PATH was handled in cmFindCommon. Move it into cmFindPackageCommand. --- Source/cmFindBase.cxx | 2 +- Source/cmFindCommon.cxx | 17 ++++++++++------- Source/cmFindCommon.h | 9 +++++++-- Source/cmFindPackageCommand.cxx | 11 ++++++++++- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Source/cmFindBase.cxx b/Source/cmFindBase.cxx index a123e44..efc4e3a 100644 --- a/Source/cmFindBase.cxx +++ b/Source/cmFindBase.cxx @@ -168,7 +168,7 @@ bool cmFindBase::ParseArguments(std::vector const& argsIn) } this->ExpandPaths(); - this->ComputeFinalPaths(); + this->ComputeFinalPaths(IgnorePaths::Yes); return true; } diff --git a/Source/cmFindCommon.cxx b/Source/cmFindCommon.cxx index 7631583..c58db1e 100644 --- a/Source/cmFindCommon.cxx +++ b/Source/cmFindCommon.cxx @@ -283,14 +283,15 @@ void cmFindCommon::RerootPaths(std::vector& paths) void cmFindCommon::GetIgnoredPaths(std::vector& ignore) { - // null-terminated list of paths. - static const char* paths[] = { "CMAKE_SYSTEM_IGNORE_PATH", - "CMAKE_IGNORE_PATH", nullptr }; + static constexpr const char* paths[] = { + "CMAKE_SYSTEM_IGNORE_PATH", + "CMAKE_IGNORE_PATH", + }; // Construct the list of path roots with no trailing slashes. - for (const char** pathName = paths; *pathName; ++pathName) { + for (const char* pathName : paths) { // Get the list of paths to ignore from the variable. - this->Makefile->GetDefExpandList(*pathName, ignore); + this->Makefile->GetDefExpandList(pathName, ignore); } for (std::string& i : ignore) { @@ -365,11 +366,13 @@ static void AddTrailingSlash(std::string& s) s += '/'; } } -void cmFindCommon::ComputeFinalPaths() +void cmFindCommon::ComputeFinalPaths(IgnorePaths ignorePaths) { // Filter out ignored paths from the prefix list std::set ignored; - this->GetIgnoredPaths(ignored); + if (ignorePaths == IgnorePaths::Yes) { + this->GetIgnoredPaths(ignored); + } // Combine the separate path types, filtering out ignores this->SearchPaths.clear(); diff --git a/Source/cmFindCommon.h b/Source/cmFindCommon.h index 1a49aff..49d64e4 100644 --- a/Source/cmFindCommon.h +++ b/Source/cmFindCommon.h @@ -82,12 +82,17 @@ protected: /** Place a set of search paths under the search roots. */ void RerootPaths(std::vector& paths); - /** Get ignored paths from CMAKE_[SYSTEM_]IGNORE_path variables. */ + /** Get ignored paths from CMAKE_[SYSTEM_]IGNORE_PATH variables. */ void GetIgnoredPaths(std::vector& ignore); void GetIgnoredPaths(std::set& ignore); /** Compute final search path list (reroot + trailing slash). */ - void ComputeFinalPaths(); + enum class IgnorePaths + { + No, + Yes, + }; + void ComputeFinalPaths(IgnorePaths ignorePaths); /** Compute the current default root path mode. */ void SelectDefaultRootPathMode(); diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 6d788e4..c468a3c 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -1346,7 +1346,7 @@ void cmFindPackageCommand::ComputePrefixes() } this->FillPrefixesUserGuess(); - this->ComputeFinalPaths(); + this->ComputeFinalPaths(IgnorePaths::No); } void cmFindPackageCommand::FillPrefixesPackageRoot() @@ -2286,6 +2286,15 @@ bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in) return false; } + // Skip this if it's in ignored paths. + std::string prefixWithoutSlash = prefix_in; + if (prefixWithoutSlash != "/" && prefixWithoutSlash.back() == '/') { + prefixWithoutSlash.erase(prefixWithoutSlash.length() - 1); + } + if (this->IgnoredPaths.count(prefixWithoutSlash)) { + return false; + } + // PREFIX/ (useful on windows or in build trees) if (this->SearchDirectory(prefix_in)) { return true; -- cgit v0.12