cmake: Add an option to control what files needs to be traced

Even in relatively small projects using `--trace` (and `--trace-expand`)
may produce a lot of output.  When developing a custom module usually
one is interested in output of only a few particular modules.

Add a `--trace-source=<file>` option to enable tracing only a subset of
source files.  The final output would be only from requested modules,
ignoring anything else not matched to given filename(s).
This commit is contained in:
Alex Turbov 2016-06-14 02:27:58 +07:00 committed by Brad King
parent 9e47255604
commit e63151ff54
10 changed files with 58 additions and 1 deletions

View File

@ -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.

View File

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

View File

@ -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;

View File

@ -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);

View File

@ -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.

View File

@ -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." },

View File

@ -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)

View File

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

View File

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

View File

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