From 1221581aa8de2b4bb06e09894940dba6ff90caa1 Mon Sep 17 00:00:00 2001
From: Todd Gamblin <tgamblin@llnl.gov>
Date: Thu, 12 Aug 2010 15:20:47 -0700
Subject: Teach find_* commands to ignore some paths

Add platform configuration variable CMAKE_SYSTEM_IGNORE_PATH and user
configuration variable CMAKE_IGNORE_PATH.  These specify a set of
directories that will be ignored by all the find commands.  Update
FindPackageTest so that several cases will fail without a functioning
CMAKE_IGNORE_PATH.
---
 Source/cmDocumentVariables.cxx                     | 34 ++++++++++++-
 Source/cmFindBase.cxx                              |  5 ++
 Source/cmFindCommon.cxx                            | 57 ++++++++++++++++++++++
 Source/cmFindCommon.h                              |  8 +++
 Source/cmFindPackageCommand.cxx                    | 14 ++++++
 Source/cmFindPackageCommand.h                      |  1 +
 Tests/FindPackageTest/CMakeLists.txt               |  4 ++
 .../lib/cmake/zot-3.1/zot-config-version.cmake     |  4 ++
 .../lib/cmake/zot-3.1/zot-config.cmake             |  2 +
 9 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 Tests/FindPackageTest/lib/cmake/zot-3.1/zot-config-version.cmake
 create mode 100644 Tests/FindPackageTest/lib/cmake/zot-3.1/zot-config.cmake

diff --git a/Source/cmDocumentVariables.cxx b/Source/cmDocumentVariables.cxx
index 2ed959f..9617355 100644
--- a/Source/cmDocumentVariables.cxx
+++ b/Source/cmDocumentVariables.cxx
@@ -587,7 +587,39 @@ void cmDocumentVariables::DefineVariables(cmake* cm)
      "directories for the current system. It is NOT intended "
      "to be modified by the project, use CMAKE_PREFIX_PATH for this. See also "
      "CMAKE_SYSTEM_INCLUDE_PATH, CMAKE_SYSTEM_LIBRARY_PATH, "
-     "CMAKE_SYSTEM_PROGRAM_PATH.", false,
+     "CMAKE_SYSTEM_PROGRAM_PATH, and CMAKE_SYSTEM_IGNORE_PATH.", false,
+     "Variables That Change Behavior");
+
+  cm->DefineProperty
+    ("CMAKE_SYSTEM_IGNORE_PATH", cmProperty::VARIABLE,
+     "Path to be ignored by FIND_XXX() commands.",
+     "Specifies directories to be ignored by searches in FIND_XXX() commands "
+     "This is useful in cross-compiled environments where some system "
+     "directories contain incompatible but possibly linkable libraries. For "
+     "example, on cross-compiled cluster environments, this allows a user to "
+     "ignore directories containing libraries meant for the front-end "
+     "machine that modules like FindX11 (and others) would normally search. "
+     "By default this contains a list of directories containing incompatible "
+     "binaries for the host system. "
+     "See also CMAKE_SYSTEM_PREFIX_PATH, CMAKE_SYSTEM_LIBRARY_PATH, "
+     "CMAKE_SYSTEM_INCLUDE_PATH, and CMAKE_SYSTEM_PROGRAM_PATH.", false,
+     "Variables That Change Behavior");
+
+  cm->DefineProperty
+    ("CMAKE_IGNORE_PATH", cmProperty::VARIABLE,
+     "Path to be ignored by FIND_XXX() commands.",
+     "Specifies directories to be ignored by searches in FIND_XXX() commands "
+     "This is useful in cross-compiled environments where some system "
+     "directories contain incompatible but possibly linkable libraries. For "
+     "example, on cross-compiled cluster environments, this allows a user to "
+     "ignore directories containing libraries meant for the front-end "
+     "machine that modules like FindX11 (and others) would normally search. "
+     "By default this is empty; it is intended to be set by the project. "
+     "Note that CMAKE_IGNORE_PATH takes a list of directory names, NOT a "
+     "list of prefixes. If you want to ignore paths under prefixes (bin, "
+     "include, lib, etc.), you'll need to specify them explicitly. "
+     "See also CMAKE_PREFIX_PATH, CMAKE_LIBRARY_PATH, CMAKE_INCLUDE_PATH, "
+     "CMAKE_PROGRAM_PATH.", false,
      "Variables That Change Behavior");
 
   cm->DefineProperty
diff --git a/Source/cmFindBase.cxx b/Source/cmFindBase.cxx
index a54ad7a..e1188d5 100644
--- a/Source/cmFindBase.cxx
+++ b/Source/cmFindBase.cxx
@@ -269,6 +269,11 @@ bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
     }
   this->ExpandPaths();
 
+  // Filter out ignored paths from the prefix list
+  std::set<std::string> ignored;
+  this->GetIgnoredPaths(ignored);
+  this->FilterPaths(this->SearchPaths, ignored);
+
   // Handle search root stuff.
   this->RerootPaths(this->SearchPaths);
 
diff --git a/Source/cmFindCommon.cxx b/Source/cmFindCommon.cxx
index f352172..b7d3e52 100644
--- a/Source/cmFindCommon.cxx
+++ b/Source/cmFindCommon.cxx
@@ -241,6 +241,63 @@ void cmFindCommon::RerootPaths(std::vector<std::string>& paths)
 }
 
 //----------------------------------------------------------------------------
+void cmFindCommon::FilterPaths(std::vector<std::string>& paths,
+                               const std::set<std::string>& ignore)
+{
+  // Now filter out anything that's in the ignore set.
+  std::vector<std::string> unfiltered;
+  unfiltered.swap(paths);
+
+  for(std::vector<std::string>::iterator pi = unfiltered.begin();
+      pi != unfiltered.end(); ++pi)
+    {
+    if (ignore.count(*pi) == 0)
+      {
+      paths.push_back(*pi);
+      }
+    }
+}
+
+
+//----------------------------------------------------------------------------
+void cmFindCommon::GetIgnoredPaths(std::vector<std::string>& ignore)
+{
+  // null-terminated list of paths.
+  static const char *paths[] =
+    { "CMAKE_SYSTEM_IGNORE_PATH", "CMAKE_IGNORE_PATH", 0 };
+
+  // Construct the list of path roots with no trailing slashes.
+  for(const char **pathName = paths; *pathName; ++pathName)
+    {
+    // Get the list of paths to ignore from the variable.
+    const char* ignorePath = this->Makefile->GetDefinition(*pathName);
+    if((ignorePath == 0) || (strlen(ignorePath) == 0))
+      {
+      continue;
+      }
+
+    cmSystemTools::ExpandListArgument(ignorePath, ignore);
+    }
+
+  for(std::vector<std::string>::iterator i = ignore.begin();
+      i != ignore.end(); ++i)
+    {
+    cmSystemTools::ConvertToUnixSlashes(*i);
+    }
+}
+
+
+//----------------------------------------------------------------------------
+void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore)
+{
+  std::vector<std::string> ignoreVec;
+  GetIgnoredPaths(ignoreVec);
+  ignore.insert(ignoreVec.begin(), ignoreVec.end());
+}
+
+
+
+//----------------------------------------------------------------------------
 bool cmFindCommon::CheckCommonArgument(std::string const& arg)
 {
   if(arg == "NO_DEFAULT_PATH")
diff --git a/Source/cmFindCommon.h b/Source/cmFindCommon.h
index 2ffbd00..a4866ba 100644
--- a/Source/cmFindCommon.h
+++ b/Source/cmFindCommon.h
@@ -39,6 +39,14 @@ protected:
   /** Place a set of search paths under the search roots.  */
   void RerootPaths(std::vector<std::string>& paths);
 
+  /** Get ignored paths from CMAKE_[SYSTEM_]IGNORE_path variables.  */
+  void GetIgnoredPaths(std::vector<std::string>& ignore);
+  void GetIgnoredPaths(std::set<std::string>& ignore);
+
+  /** Remove paths in the ignore set from the supplied vector.  */
+  void FilterPaths(std::vector<std::string>& paths,
+                   const std::set<std::string>& ignore);
+
   /** Add trailing slashes to all search paths.  */
   void AddTrailingSlashes(std::vector<std::string>& paths);
 
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx
index bd58f56..eb86014 100644
--- a/Source/cmFindPackageCommand.cxx
+++ b/Source/cmFindPackageCommand.cxx
@@ -596,6 +596,15 @@ bool cmFindPackageCommand
       }
     }
 
+  // get igonored paths from vars and reroot them.
+  std::vector<std::string> ignored;
+  this->GetIgnoredPaths(ignored);
+  this->RerootPaths(ignored);
+
+  // Construct a set of ignored paths
+  this->IgnoredPaths.clear();
+  this->IgnoredPaths.insert(ignored.begin(), ignored.end());
+
   // Find and load the package.
   bool result = this->HandlePackageMode();
   this->AppendSuccessInformation();
@@ -1431,6 +1440,11 @@ bool cmFindPackageCommand::CheckDirectory(std::string const& dir)
 bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
                                           std::string& file)
 {
+  if (this->IgnoredPaths.count(dir))
+    {
+    return false;
+    }
+
   for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
       ci != this->Configs.end(); ++ci)
     {
diff --git a/Source/cmFindPackageCommand.h b/Source/cmFindPackageCommand.h
index 63f4111..53ea4fc 100644
--- a/Source/cmFindPackageCommand.h
+++ b/Source/cmFindPackageCommand.h
@@ -136,6 +136,7 @@ private:
   bool PolicyScope;
   std::vector<std::string> Names;
   std::vector<std::string> Configs;
+  std::set<std::string> IgnoredPaths;
 };
 
 #endif
diff --git a/Tests/FindPackageTest/CMakeLists.txt b/Tests/FindPackageTest/CMakeLists.txt
index 74cc115..a472bea 100644
--- a/Tests/FindPackageTest/CMakeLists.txt
+++ b/Tests/FindPackageTest/CMakeLists.txt
@@ -54,6 +54,10 @@ SET(CMAKE_FIND_APPBUNDLE FIRST)
 # Set the wrong answer for a find to make sure it re-finds.
 set(VersionedA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/cmake/zot-4.0)
 
+# Test that CMAKE_IGNORE_PATH can ignore the purposely bad package
+# files in the lib/cmake/zot-3.1 directory.
+set(CMAKE_IGNORE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib/cmake/zot-3.1)
+
 # Look for packages with new-style signatures.
 FIND_PACKAGE(foo NO_MODULE)
 FIND_PACKAGE(Foo CONFIGS FooConfig.cmake)
diff --git a/Tests/FindPackageTest/lib/cmake/zot-3.1/zot-config-version.cmake b/Tests/FindPackageTest/lib/cmake/zot-3.1/zot-config-version.cmake
new file mode 100644
index 0000000..bee2f0e
--- /dev/null
+++ b/Tests/FindPackageTest/lib/cmake/zot-3.1/zot-config-version.cmake
@@ -0,0 +1,4 @@
+# Claim to be any version to test that CMAKE_IGNORE_PATH hides us.
+SET(PACKAGE_VERSION 3.1)
+SET(PACKAGE_VERSION_COMPATIBLE 1)
+SET(PACKAGE_VERSION_EXACT 1)
diff --git a/Tests/FindPackageTest/lib/cmake/zot-3.1/zot-config.cmake b/Tests/FindPackageTest/lib/cmake/zot-3.1/zot-config.cmake
new file mode 100644
index 0000000..2fbd525
--- /dev/null
+++ b/Tests/FindPackageTest/lib/cmake/zot-3.1/zot-config.cmake
@@ -0,0 +1,2 @@
+# Test config file.
+message(WARNING "CMAKE_IGNORE_PATH failed to ignore this file!")
-- 
cgit v0.12