浏览代码

Merge topic 'cmake-trace-source'

e63151ff cmake: Add an option to control what files needs to be traced
Brad King 9 年之前
父节点
当前提交
74fde78311

+ 5 - 0
Help/manual/cmake.1.rst

@@ -108,6 +108,11 @@ Options
 
  Like ``--trace``, but with variables expanded.
 
+``--trace-source=<file>``
+ Put cmake in trace mode, but output only lines of a specified file.
+
+ Multiple options are allowed.
+
 ``--warn-uninitialized``
  Warn about uninitialized values.
 

+ 4 - 0
Help/release/dev/cmake-trace-source.rst

@@ -0,0 +1,4 @@
+cmake-trace-source
+------------------
+
+* The :manual:`cmake(1)` command learned a ``--trace-source=<file>`` option.

+ 23 - 1
Source/cmMakefile.cxx

@@ -37,6 +37,7 @@
 
 #include <cmsys/FStream.hxx>
 #include <cmsys/RegularExpression.hxx>
+#include <cmsys/SystemTools.hxx>
 #include <cmsys/auto_ptr.hxx>
 
 #include <assert.h>
@@ -175,8 +176,29 @@ cmListFileContext cmMakefile::GetExecutionContext() const
 
 void cmMakefile::PrintCommandTrace(const cmListFileFunction& lff) const
 {
+  // Check if current file in the list of requested to trace...
+  std::vector<std::string> const& trace_only_this_files =
+    this->GetCMakeInstance()->GetTraceSources();
+  std::string const& full_path = this->GetExecutionFilePath();
+  std::string const& only_filename = cmSystemTools::GetFilenameName(full_path);
+  bool trace = trace_only_this_files.empty();
+  if (!trace) {
+    for (std::vector<std::string>::const_iterator i =
+           trace_only_this_files.begin();
+         !trace && i != trace_only_this_files.end(); ++i) {
+      std::string::size_type const pos = full_path.rfind(*i);
+      trace = (pos != std::string::npos) &&
+        ((pos + i->size()) == full_path.size()) &&
+        (only_filename == cmSystemTools::GetFilenameName(*i));
+    }
+    // Do nothing if current file wasn't requested for trace...
+    if (!trace) {
+      return;
+    }
+  }
+
   std::ostringstream msg;
-  msg << this->GetExecutionFilePath() << "(" << lff.Line << "):  ";
+  msg << full_path << "(" << lff.Line << "):  ";
   msg << lff.Name << "(";
   bool expand = this->GetCMakeInstance()->GetTraceExpand();
   std::string temp;

+ 5 - 0
Source/cmake.cxx

@@ -589,6 +589,11 @@ void cmake::SetArgs(const std::vector<std::string>& args,
       std::cout << "Running with expanded trace output on.\n";
       this->SetTrace(true);
       this->SetTraceExpand(true);
+    } else if (arg.find("--trace-source=", 0) == 0) {
+      std::string file = arg.substr(strlen("--trace-source="));
+      cmSystemTools::ConvertToUnixSlashes(file);
+      this->AddTraceSource(file);
+      this->SetTrace(true);
     } else if (arg.find("--trace", 0) == 0) {
       std::cout << "Running with trace output on.\n";
       this->SetTrace(true);

+ 10 - 0
Source/cmake.h

@@ -307,6 +307,14 @@ public:
   void SetTrace(bool b) { this->Trace = b; }
   bool GetTraceExpand() { return this->TraceExpand; }
   void SetTraceExpand(bool b) { this->TraceExpand = b; }
+  void AddTraceSource(std::string const& file)
+  {
+    this->TraceOnlyThisSources.push_back(file);
+  }
+  std::vector<std::string> const& GetTraceSources() const
+  {
+    return this->TraceOnlyThisSources;
+  }
   bool GetWarnUninitialized() { return this->WarnUninitialized; }
   void SetWarnUninitialized(bool b) { this->WarnUninitialized = b; }
   bool GetWarnUnused() { return this->WarnUnused; }
@@ -481,6 +489,8 @@ private:
   cmState* State;
   cmState::Snapshot CurrentSnapshot;
 
+  std::vector<std::string> TraceOnlyThisSources;
+
   void UpdateConversionPathTable();
 
   // Print a list of valid generators to stderr.

+ 2 - 0
Source/cmakemain.cxx

@@ -73,6 +73,8 @@ static const char* cmDocumentationOptions[][2] = {
   { "--debug-output", "Put cmake in a debug mode." },
   { "--trace", "Put cmake in trace mode." },
   { "--trace-expand", "Put cmake in trace mode with variable expansion." },
+  { "--trace-source=<file>",
+    "Trace only this CMake file/module. Multiple options allowed." },
   { "--warn-uninitialized", "Warn about uninitialized values." },
   { "--warn-unused-vars", "Warn about unused variables." },
   { "--no-warn-unused-cli", "Don't warn about command line options." },

+ 4 - 0
Tests/RunCMake/CommandLine/RunCMakeTest.cmake

@@ -264,6 +264,10 @@ set(RunCMake_TEST_OPTIONS --trace-expand)
 run_cmake(trace-expand)
 unset(RunCMake_TEST_OPTIONS)
 
+set(RunCMake_TEST_OPTIONS --trace-source=trace-only-this-file.cmake)
+run_cmake(trace-source)
+unset(RunCMake_TEST_OPTIONS)
+
 set(RunCMake_TEST_OPTIONS --debug-trycompile)
 run_cmake(debug-trycompile)
 unset(RunCMake_TEST_OPTIONS)

+ 1 - 0
Tests/RunCMake/CommandLine/trace-only-this-file.cmake

@@ -0,0 +1 @@
+message(STATUS "trace particular file test passed")

+ 1 - 0
Tests/RunCMake/CommandLine/trace-source-stderr.txt

@@ -0,0 +1 @@
+^.*/trace-only-this-file.cmake\(1\):  message\(STATUS trace particular file test passed \)$

+ 3 - 0
Tests/RunCMake/CommandLine/trace-source.cmake

@@ -0,0 +1,3 @@
+message(STATUS "before include()")
+include("trace-only-this-file.cmake")
+message(STATUS "after include()")