Browse Source

cmFindCommon: log search path variables

No pruning of variables which are not relevant is performed as it would
involve duplication of determining which paths to manage.
Ben Boeckel 5 months ago
parent
commit
3ac505f79d

+ 28 - 0
Help/manual/cmake-configure-log.7.rst

@@ -442,6 +442,20 @@ The keys specific to ``find-v1`` mappings are:
   Either a string representing the found value or ``false`` if it was not
   found.
 
+``search_context``
+  A mapping of variable names to search paths specified by them (either a
+  string or an array of strings depending on the variable). Environment
+  variables are wrapped with ``ENV{`` and ``}``, otherwise CMake variables are
+  used. Only variables with any paths specified are used.
+
+  ``package_stack``
+    An array of objects with paths which come from the stack of paths made
+    available by :command:`find_package` calls.
+
+    ``package_paths``
+      The paths made available by :command:`find_package` commands in the call
+      stack.
+
 .. _`find_package configure-log event`:
 
 Event Kind ``find_package``
@@ -681,3 +695,17 @@ The keys specific to ``find_package-v1`` mappings are:
 
   ``version``
     The reported version of the package.
+
+``search_context``
+  A mapping of variable names to search paths specified by them (either a
+  string or an array of strings depending on the variable). Environment
+  variables are wrapped with ``ENV{`` and ``}``, otherwise CMake variables are
+  used. Only variables with any paths specified are used.
+
+  ``package_stack``
+    An array of objects with paths which come from the stack of paths made
+    available by :command:`find_package` calls.
+
+    ``package_paths``
+      The paths made available by :command:`find_package` commands in the call
+      stack.

+ 15 - 0
Source/cmFindBase.cxx

@@ -778,6 +778,21 @@ void cmFindBaseDebugState::WriteEvent(cmConfigureLog& log,
   } else {
     log.WriteValue("found"_s, false);
   }
+
+  this->WriteSearchVariables(log, mf);
+
   log.EndEvent();
 }
+
+std::vector<std::pair<cmFindCommonDebugState::VariableSource, std::string>>
+cmFindBaseDebugState::ExtraSearchVariables() const
+{
+  std::vector<std::pair<cmFindCommonDebugState::VariableSource, std::string>>
+    extraSearches;
+  if (!this->FindBaseCommand->EnvironmentPath.empty()) {
+    extraSearches.emplace_back(VariableSource::EnvironmentList,
+                               this->FindBaseCommand->EnvironmentPath);
+  }
+  return extraSearches;
+}
 #endif

+ 2 - 0
Source/cmFindBase.h

@@ -117,6 +117,8 @@ private:
   void WriteDebug() const override;
 #ifndef CMAKE_BOOTSTRAP
   void WriteEvent(cmConfigureLog& log, cmMakefile const& mf) const override;
+  std::vector<std::pair<VariableSource, std::string>> ExtraSearchVariables()
+    const override;
 #endif
 
   cmFindBase const* const FindBaseCommand;

+ 120 - 0
Source/cmFindCommon.cxx

@@ -18,6 +18,18 @@
 #include "cmValue.h"
 #include "cmake.h"
 
+#ifndef CMAKE_BOOTSTRAP
+#  include <deque>
+#  include <iterator>
+
+#  include <cm/optional>
+#  include <cm/string_view>
+#  include <cmext/string_view>
+
+#  include "cmConfigureLog.h"
+#  include "cmRange.h"
+#endif
+
 cmFindCommon::PathGroup cmFindCommon::PathGroup::All("ALL");
 cmFindCommon::PathLabel cmFindCommon::PathLabel::PackageRoot(
   "PackageName_ROOT");
@@ -514,6 +526,114 @@ void cmFindCommonDebugState::Write()
   this->WriteDebug();
 }
 
+#ifndef CMAKE_BOOTSTRAP
+void cmFindCommonDebugState::WriteSearchVariables(cmConfigureLog& log,
+                                                  cmMakefile const& mf) const
+{
+  auto WriteString = [&log, &mf](std::string const& name) {
+    if (cmValue value = mf.GetDefinition(name)) {
+      log.WriteValue(name, *value);
+    }
+  };
+  auto WriteCMakeList = [&log, &mf](std::string const& name) {
+    if (cmValue value = mf.GetDefinition(name)) {
+      cmList values{ *value };
+      if (!values.empty()) {
+        log.WriteValue(name, values);
+      }
+    }
+  };
+  auto WriteEnvList = [&log](std::string const& name) {
+    if (auto value = cmSystemTools::GetEnvVar(name)) {
+      auto values = cmSystemTools::SplitEnvPath(*value);
+      if (!values.empty()) {
+        log.WriteValue(cmStrCat("ENV{", name, '}'), values);
+      }
+    }
+  };
+
+  auto const* fc = this->FindCommand;
+  log.BeginObject("search_context"_s);
+  auto const& packageRootStack = mf.FindPackageRootPathStack;
+  if (!packageRootStack.empty()) {
+    bool havePaths =
+      std::any_of(packageRootStack.begin(), packageRootStack.end(),
+                  [](std::vector<std::string> const& entry) -> bool {
+                    return !entry.empty();
+                  });
+    if (havePaths) {
+      log.BeginObject("package_stack");
+      log.BeginArray();
+      for (auto const& pkgPaths : cmReverseRange(packageRootStack)) {
+        if (!pkgPaths.empty()) {
+          log.NextArrayElement();
+          log.WriteValue("package_paths", pkgPaths);
+        }
+      }
+      log.EndArray();
+      log.EndObject();
+    }
+  }
+  auto cmakePathVar = cmStrCat("CMAKE_", fc->CMakePathName, "_PATH");
+  WriteCMakeList(cmakePathVar);
+  WriteCMakeList("CMAKE_PREFIX_PATH");
+  if (fc->CMakePathName == "PROGRAM"_s) {
+    WriteCMakeList("CMAKE_APPBUNDLE_PATH");
+  } else {
+    WriteCMakeList("CMAKE_FRAMEWORK_PATH");
+  }
+  // Same as above, but ask the environment instead.
+  WriteEnvList(cmakePathVar);
+  WriteEnvList("CMAKE_PREFIX_PATH");
+  if (fc->CMakePathName == "PROGRAM"_s) {
+    WriteEnvList("CMAKE_APPBUNDLE_PATH");
+  } else {
+    WriteEnvList("CMAKE_FRAMEWORK_PATH");
+  }
+  WriteEnvList("PATH");
+  WriteString("CMAKE_INSTALL_PREFIX");
+  WriteString("CMAKE_STAGING_PREFIX");
+  WriteCMakeList("CMAKE_SYSTEM_PREFIX_PATH");
+  auto systemPathVar = cmStrCat("CMAKE_SYSTEM_", fc->CMakePathName, "_PATH");
+  WriteCMakeList(systemPathVar);
+  // Sysroot paths.
+  WriteString("CMAKE_SYSROOT");
+  WriteString("CMAKE_SYSROOT_COMPILE");
+  WriteString("CMAKE_SYSROOT_LINK");
+  WriteString("CMAKE_FIND_ROOT_PATH");
+  // Write out paths which are ignored.
+  WriteCMakeList("CMAKE_IGNORE_PATH");
+  WriteCMakeList("CMAKE_IGNORE_PREFIX_PATH");
+  WriteCMakeList("CMAKE_SYSTEM_IGNORE_PATH");
+  WriteCMakeList("CMAKE_SYSTEM_IGNORE_PREFIX_PATH");
+  if (fc->CMakePathName == "PROGRAM"_s) {
+    WriteCMakeList("CMAKE_SYSTEM_APPBUNDLE_PATH");
+  } else {
+    WriteCMakeList("CMAKE_SYSTEM_FRAMEWORK_PATH");
+  }
+  for (auto const& extraVar : this->ExtraSearchVariables()) {
+    switch (extraVar.first) {
+      case VariableSource::String:
+        WriteString(extraVar.second);
+        break;
+      case VariableSource::PathList:
+        WriteCMakeList(extraVar.second);
+        break;
+      case VariableSource::EnvironmentList:
+        WriteEnvList(extraVar.second);
+        break;
+    }
+  }
+  log.EndObject();
+}
+
+std::vector<std::pair<cmFindCommonDebugState::VariableSource, std::string>>
+cmFindCommonDebugState::ExtraSearchVariables() const
+{
+  return {};
+}
+#endif
+
 bool cmFindCommonDebugState::TrackSearchProgress() const
 {
   // Track search progress if debugging or logging the configure.

+ 10 - 0
Source/cmFindCommon.h

@@ -8,6 +8,7 @@
 #include <memory>
 #include <set>
 #include <string>
+#include <utility>
 #include <vector>
 
 #include "cmPathLabel.h"
@@ -187,6 +188,15 @@ protected:
   virtual void WriteDebug() const = 0;
 #ifndef CMAKE_BOOTSTRAP
   virtual void WriteEvent(cmConfigureLog& log, cmMakefile const& mf) const = 0;
+  void WriteSearchVariables(cmConfigureLog& log, cmMakefile const& mf) const;
+  enum class VariableSource
+  {
+    String,
+    PathList,
+    EnvironmentList,
+  };
+  virtual std::vector<std::pair<VariableSource, std::string>>
+  ExtraSearchVariables() const;
 #endif
 
   cmFindCommon const* const FindCommand;

+ 13 - 0
Source/cmFindPackageCommand.cxx

@@ -3668,6 +3668,19 @@ void cmFindPackageDebugState::WriteEvent(cmConfigureLog& log,
     log.WriteValue("found"_s, nullptr);
   }
 
+  this->WriteSearchVariables(log, mf);
+
   log.EndEvent();
 }
+
+std::vector<std::pair<cmFindCommonDebugState::VariableSource, std::string>>
+cmFindPackageDebugState::ExtraSearchVariables() const
+{
+  std::vector<std::pair<cmFindCommonDebugState::VariableSource, std::string>>
+    extraSearches;
+  if (this->FindPackageCommand->UseFindModules) {
+    extraSearches.emplace_back(VariableSource::PathList, "CMAKE_MODULE_PATH");
+  }
+  return extraSearches;
+}
 #endif

+ 2 - 0
Source/cmFindPackageCommand.h

@@ -399,6 +399,8 @@ private:
   void WriteDebug() const override;
 #ifndef CMAKE_BOOTSTRAP
   void WriteEvent(cmConfigureLog& log, cmMakefile const& mf) const override;
+  std::vector<std::pair<VariableSource, std::string>> ExtraSearchVariables()
+    const override;
 #endif
 
   cmFindPackageCommand const* const FindPackageCommand;

+ 15 - 0
Tests/RunCMake/find_package/ConfigureLog-config.txt

@@ -71,6 +71,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/lib/cmake/ViaConfig/ViaConfigConfig.cmake"
       mode: "config"
       version: "1\.0"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -100,6 +103,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindViaModule.cmake"
       mode: "module"
       version: "1.0"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -163,6 +169,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/lib/cmake/Inner/InnerConfig.cmake"
       mode: "config"
       version: "1.1"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -192,6 +201,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindWithInner.cmake"
       mode: "module"
       version: "1.1"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -265,4 +277,7 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/lib/cmake/VersionCheck-2.5/VersionCheckConfig.cmake"
       mode: "config"
       version: "2.5"
+    search_context:(
+      [^
+]*)+
 \.\.\.$

+ 30 - 0
Tests/RunCMake/find_package/ConfigureLogParameters1-config.txt

@@ -42,6 +42,9 @@ events:
         reason: "not_found"
         message: "Not an EXACT version match"
     found: null
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -75,6 +78,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindParameterCheck.cmake"
       mode: "module"
       version: "1.2"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -106,6 +112,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindParameterCheck.cmake"
       mode: "module"
       version: "1.2"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -135,6 +144,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindParameterCheck.cmake"
       mode: "module"
       version: "1.2"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -164,6 +176,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindParameterCheck.cmake"
       mode: "module"
       version: "1.2"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -202,6 +217,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindParameterCheck.cmake"
       mode: "module"
       version: "1.2"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -232,6 +250,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindParameterCheck.cmake"
       mode: "module"
       version: "1.2"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -261,6 +282,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindParameterCheck.cmake"
       mode: "module"
       version: "1.2"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -290,6 +314,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindParameterCheck.cmake"
       mode: "module"
       version: "1.2"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -319,4 +346,7 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/cmake/FindParameterCheck.cmake"
       mode: "module"
       version: "1.2"
+    search_context:(
+      [^
+]*)+
 \.\.\.$

+ 21 - 0
Tests/RunCMake/find_package/ConfigureLogParameters2-config.txt

@@ -73,6 +73,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/lib/cmake/ParameterCheckConfig/ParameterCheckConfigConfig.cmake"
       mode: "config"
       version: "1.6"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -186,6 +189,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/lib/cmake/ParameterCheckConfig/ParameterCheckConfigConfig.cmake"
       mode: "config"
       version: "1.6"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -230,6 +236,9 @@ events:
         mode: "config"
         reason: "no_exist"
     found: null
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -293,6 +302,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/lib/cmake/ParameterCheckConfig/ParameterCheckConfigConfig.cmake"
       mode: "config"
       version: "1.6"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -356,6 +368,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/lib/cmake/ParameterCheckConfig/ParameterCheckConfigConfig.cmake"
       mode: "config"
       version: "1.6"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -419,6 +434,9 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/lib/cmake/ParameterCheckConfig/ParameterCheckConfigConfig.cmake"
       mode: "config"
       version: "1.6"
+    search_context:(
+      [^
+]*)+
   -
     kind: "find_package-v1"
     backtrace:(
@@ -521,4 +539,7 @@ events:
       path: ".*/Tests/RunCMake/find_package/ConfigureLog/lib/cmake/ParameterCheckConfig/ParameterCheckConfigConfig.cmake"
       mode: "config"
       version: "1.6"
+    search_context:(
+      [^
+]*)+
 \.\.\.$