mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-19 11:18:40 +08:00
Add --trace-redirect parameter to redirect trace output to a file
This commit is contained in:
@@ -238,6 +238,9 @@ Options
|
|||||||
|
|
||||||
Multiple options are allowed.
|
Multiple options are allowed.
|
||||||
|
|
||||||
|
``--trace-redirect=<file>``
|
||||||
|
Put cmake in trace mode and redirect trace output to a file instead of stderr.
|
||||||
|
|
||||||
``--warn-uninitialized``
|
``--warn-uninitialized``
|
||||||
Warn about uninitialized values.
|
Warn about uninitialized values.
|
||||||
|
|
||||||
|
6
Help/release/dev/trace-redirect.rst
Normal file
6
Help/release/dev/trace-redirect.rst
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
trace-redirect
|
||||||
|
--------------
|
||||||
|
|
||||||
|
* :manual:`cmake(1)` gained a ``--trace-redirect=<file>`` command line option
|
||||||
|
that can be used to redirect ``--trace`` output to a file instead
|
||||||
|
of ``stderr``.
|
@@ -23,6 +23,7 @@
|
|||||||
#include "cmExpandedCommandArgument.h" // IWYU pragma: keep
|
#include "cmExpandedCommandArgument.h" // IWYU pragma: keep
|
||||||
#include "cmFileLockPool.h"
|
#include "cmFileLockPool.h"
|
||||||
#include "cmFunctionBlocker.h"
|
#include "cmFunctionBlocker.h"
|
||||||
|
#include "cmGeneratedFileStream.h"
|
||||||
#include "cmGeneratorExpression.h"
|
#include "cmGeneratorExpression.h"
|
||||||
#include "cmGeneratorExpressionEvaluationFile.h"
|
#include "cmGeneratorExpressionEvaluationFile.h"
|
||||||
#include "cmGlobalGenerator.h"
|
#include "cmGlobalGenerator.h"
|
||||||
@@ -321,7 +322,13 @@ void cmMakefile::PrintCommandTrace(const cmListFileFunction& lff) const
|
|||||||
msg << " ";
|
msg << " ";
|
||||||
}
|
}
|
||||||
msg << ")";
|
msg << ")";
|
||||||
cmSystemTools::Message(msg.str());
|
|
||||||
|
auto& f = this->GetCMakeInstance()->GetTraceFile();
|
||||||
|
if (f) {
|
||||||
|
f << msg.str() << '\n';
|
||||||
|
} else {
|
||||||
|
cmSystemTools::Message(msg.str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper class to make sure the call stack is valid.
|
// Helper class to make sure the call stack is valid.
|
||||||
|
@@ -140,6 +140,7 @@ cmake::cmake(Role role, cmState::Mode mode)
|
|||||||
, State(cm::make_unique<cmState>())
|
, State(cm::make_unique<cmState>())
|
||||||
, Messenger(cm::make_unique<cmMessenger>())
|
, Messenger(cm::make_unique<cmMessenger>())
|
||||||
{
|
{
|
||||||
|
this->TraceFile.close();
|
||||||
this->State->SetMode(mode);
|
this->State->SetMode(mode);
|
||||||
this->CurrentSnapshot = this->State->CreateBaseSnapshot();
|
this->CurrentSnapshot = this->State->CreateBaseSnapshot();
|
||||||
|
|
||||||
@@ -740,6 +741,11 @@ void cmake::SetArgs(const std::vector<std::string>& args)
|
|||||||
cmSystemTools::ConvertToUnixSlashes(file);
|
cmSystemTools::ConvertToUnixSlashes(file);
|
||||||
this->AddTraceSource(file);
|
this->AddTraceSource(file);
|
||||||
this->SetTrace(true);
|
this->SetTrace(true);
|
||||||
|
} else if (arg.find("--trace-redirect=", 0) == 0) {
|
||||||
|
std::string file = arg.substr(strlen("--trace-redirect="));
|
||||||
|
cmSystemTools::ConvertToUnixSlashes(file);
|
||||||
|
this->SetTraceFile(file);
|
||||||
|
this->SetTrace(true);
|
||||||
} else if (arg.find("--trace", 0) == 0) {
|
} else if (arg.find("--trace", 0) == 0) {
|
||||||
std::cout << "Running with trace output on.\n";
|
std::cout << "Running with trace output on.\n";
|
||||||
this->SetTrace(true);
|
this->SetTrace(true);
|
||||||
@@ -870,6 +876,20 @@ cmake::LogLevel cmake::StringToLogLevel(const std::string& levelStr)
|
|||||||
return (it != levels.cend()) ? it->second : LogLevel::LOG_UNDEFINED;
|
return (it != levels.cend()) ? it->second : LogLevel::LOG_UNDEFINED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cmake::SetTraceFile(const std::string& file)
|
||||||
|
{
|
||||||
|
this->TraceFile.close();
|
||||||
|
this->TraceFile.open(file.c_str());
|
||||||
|
if (!this->TraceFile) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Error opening trace file " << file << ": "
|
||||||
|
<< cmSystemTools::GetLastSystemError();
|
||||||
|
cmSystemTools::Error(ss.str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::cout << "Trace will be written to " << file << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
void cmake::SetDirectoriesFromFile(const std::string& arg)
|
void cmake::SetDirectoriesFromFile(const std::string& arg)
|
||||||
{
|
{
|
||||||
// Check if the argument refers to a CMakeCache.txt or
|
// Check if the argument refers to a CMakeCache.txt or
|
||||||
|
@@ -13,6 +13,7 @@
|
|||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "cmGeneratedFileStream.h"
|
||||||
#include "cmInstalledFile.h"
|
#include "cmInstalledFile.h"
|
||||||
#include "cmListFileCache.h"
|
#include "cmListFileCache.h"
|
||||||
#include "cmMessageType.h"
|
#include "cmMessageType.h"
|
||||||
@@ -401,6 +402,9 @@ public:
|
|||||||
{
|
{
|
||||||
return this->TraceOnlyThisSources;
|
return this->TraceOnlyThisSources;
|
||||||
}
|
}
|
||||||
|
cmGeneratedFileStream& GetTraceFile() { return this->TraceFile; }
|
||||||
|
void SetTraceFile(std::string const& file);
|
||||||
|
|
||||||
bool GetWarnUninitialized() { return this->WarnUninitialized; }
|
bool GetWarnUninitialized() { return this->WarnUninitialized; }
|
||||||
void SetWarnUninitialized(bool b) { this->WarnUninitialized = b; }
|
void SetWarnUninitialized(bool b) { this->WarnUninitialized = b; }
|
||||||
bool GetWarnUnused() { return this->WarnUnused; }
|
bool GetWarnUnused() { return this->WarnUnused; }
|
||||||
@@ -547,6 +551,7 @@ private:
|
|||||||
bool DebugOutput = false;
|
bool DebugOutput = false;
|
||||||
bool Trace = false;
|
bool Trace = false;
|
||||||
bool TraceExpand = false;
|
bool TraceExpand = false;
|
||||||
|
cmGeneratedFileStream TraceFile;
|
||||||
bool WarnUninitialized = false;
|
bool WarnUninitialized = false;
|
||||||
bool WarnUnused = false;
|
bool WarnUnused = false;
|
||||||
bool WarnUnusedCli = true;
|
bool WarnUnusedCli = true;
|
||||||
|
@@ -80,6 +80,8 @@ const char* cmDocumentationOptions[][2] = {
|
|||||||
{ "--trace-expand", "Put cmake in trace mode with variable expansion." },
|
{ "--trace-expand", "Put cmake in trace mode with variable expansion." },
|
||||||
{ "--trace-source=<file>",
|
{ "--trace-source=<file>",
|
||||||
"Trace only this CMake file/module. Multiple options allowed." },
|
"Trace only this CMake file/module. Multiple options allowed." },
|
||||||
|
{ "--trace-redirect=<file>",
|
||||||
|
"Redirect trace output to a file instead of stderr." },
|
||||||
{ "--warn-uninitialized", "Warn about uninitialized values." },
|
{ "--warn-uninitialized", "Warn about uninitialized values." },
|
||||||
{ "--warn-unused-vars", "Warn about unused variables." },
|
{ "--warn-unused-vars", "Warn about unused variables." },
|
||||||
{ "--no-warn-unused-cli", "Don't warn about command line options." },
|
{ "--no-warn-unused-cli", "Don't warn about command line options." },
|
||||||
|
@@ -484,6 +484,14 @@ set(RunCMake_TEST_OPTIONS --trace-expand --warn-uninitialized)
|
|||||||
run_cmake(trace-expand-warn-uninitialized)
|
run_cmake(trace-expand-warn-uninitialized)
|
||||||
unset(RunCMake_TEST_OPTIONS)
|
unset(RunCMake_TEST_OPTIONS)
|
||||||
|
|
||||||
|
set(RunCMake_TEST_OPTIONS --trace-redirect=${RunCMake_BINARY_DIR}/redirected.trace)
|
||||||
|
run_cmake(trace-redirect)
|
||||||
|
unset(RunCMake_TEST_OPTIONS)
|
||||||
|
|
||||||
|
set(RunCMake_TEST_OPTIONS --trace-redirect=/no/such/file.txt)
|
||||||
|
run_cmake(trace-redirect-nofile)
|
||||||
|
unset(RunCMake_TEST_OPTIONS)
|
||||||
|
|
||||||
set(RunCMake_TEST_OPTIONS -Wno-deprecated --warn-uninitialized)
|
set(RunCMake_TEST_OPTIONS -Wno-deprecated --warn-uninitialized)
|
||||||
run_cmake(warn-uninitialized)
|
run_cmake(warn-uninitialized)
|
||||||
unset(RunCMake_TEST_OPTIONS)
|
unset(RunCMake_TEST_OPTIONS)
|
||||||
|
13
Tests/RunCMake/CommandLine/trace-redirect-check.cmake
Normal file
13
Tests/RunCMake/CommandLine/trace-redirect-check.cmake
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
file(READ ${RunCMake_SOURCE_DIR}/trace-stderr.txt expected_content)
|
||||||
|
string(REGEX REPLACE "\n+$" "" expected_content "${expected_content}")
|
||||||
|
|
||||||
|
file(READ ${RunCMake_BINARY_DIR}/redirected.trace actual_content)
|
||||||
|
string(REGEX REPLACE "\r\n" "\n" actual_content "${actual_content}")
|
||||||
|
string(REGEX REPLACE "\n+$" "" actual_content "${actual_content}")
|
||||||
|
if(NOT "${actual_content}" MATCHES "${expected_content}")
|
||||||
|
set(RunCMake_TEST_FAILED
|
||||||
|
"Trace file content does not match that expected."
|
||||||
|
"Expected to match:\n${expected_content}\n"
|
||||||
|
"Actual content:\n${actual_content}\n"
|
||||||
|
)
|
||||||
|
endif()
|
@@ -0,0 +1 @@
|
|||||||
|
1
|
@@ -0,0 +1 @@
|
|||||||
|
^CMake Error: Error opening trace file /no/such/file.txt: .+$
|
1
Tests/RunCMake/CommandLine/trace-redirect-stdout.txt
Normal file
1
Tests/RunCMake/CommandLine/trace-redirect-stdout.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
^.*Trace will be written to .+redirected.trace.*$
|
0
Tests/RunCMake/CommandLine/trace-redirect.cmake
Normal file
0
Tests/RunCMake/CommandLine/trace-redirect.cmake
Normal file
Reference in New Issue
Block a user