From 3c94069660112f841df3bae66891e05bfc7bad00 Mon Sep 17 00:00:00 2001 From: Igor Ivanov Date: Sat, 10 Aug 2019 11:41:53 +0300 Subject: Add --trace-redirect parameter to redirect trace output to a file --- Help/manual/cmake.1.rst | 3 +++ Help/release/dev/trace-redirect.rst | 6 ++++++ Source/cmMakefile.cxx | 9 ++++++++- Source/cmake.cxx | 20 ++++++++++++++++++++ Source/cmake.h | 5 +++++ Source/cmakemain.cxx | 2 ++ Tests/RunCMake/CommandLine/RunCMakeTest.cmake | 8 ++++++++ .../RunCMake/CommandLine/trace-redirect-check.cmake | 13 +++++++++++++ .../CommandLine/trace-redirect-nofile-result.txt | 1 + .../CommandLine/trace-redirect-nofile-stderr.txt | 1 + .../RunCMake/CommandLine/trace-redirect-nofile.cmake | 0 Tests/RunCMake/CommandLine/trace-redirect-stdout.txt | 1 + Tests/RunCMake/CommandLine/trace-redirect.cmake | 0 13 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 Help/release/dev/trace-redirect.rst create mode 100644 Tests/RunCMake/CommandLine/trace-redirect-check.cmake create mode 100644 Tests/RunCMake/CommandLine/trace-redirect-nofile-result.txt create mode 100644 Tests/RunCMake/CommandLine/trace-redirect-nofile-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/trace-redirect-nofile.cmake create mode 100644 Tests/RunCMake/CommandLine/trace-redirect-stdout.txt create mode 100644 Tests/RunCMake/CommandLine/trace-redirect.cmake diff --git a/Help/manual/cmake.1.rst b/Help/manual/cmake.1.rst index 68d88e7..26ef904 100644 --- a/Help/manual/cmake.1.rst +++ b/Help/manual/cmake.1.rst @@ -238,6 +238,9 @@ Options Multiple options are allowed. +``--trace-redirect=`` + Put cmake in trace mode and redirect trace output to a file instead of stderr. + ``--warn-uninitialized`` Warn about uninitialized values. diff --git a/Help/release/dev/trace-redirect.rst b/Help/release/dev/trace-redirect.rst new file mode 100644 index 0000000..410021e --- /dev/null +++ b/Help/release/dev/trace-redirect.rst @@ -0,0 +1,6 @@ +trace-redirect +-------------- + +* :manual:`cmake(1)` gained a ``--trace-redirect=`` command line option + that can be used to redirect ``--trace`` output to a file instead + of ``stderr``. diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 015453a..ea42289 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -23,6 +23,7 @@ #include "cmExpandedCommandArgument.h" // IWYU pragma: keep #include "cmFileLockPool.h" #include "cmFunctionBlocker.h" +#include "cmGeneratedFileStream.h" #include "cmGeneratorExpression.h" #include "cmGeneratorExpressionEvaluationFile.h" #include "cmGlobalGenerator.h" @@ -321,7 +322,13 @@ void cmMakefile::PrintCommandTrace(const cmListFileFunction& lff) const 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. diff --git a/Source/cmake.cxx b/Source/cmake.cxx index a81b7e4..98dfd21 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -140,6 +140,7 @@ cmake::cmake(Role role, cmState::Mode mode) , State(cm::make_unique()) , Messenger(cm::make_unique()) { + this->TraceFile.close(); this->State->SetMode(mode); this->CurrentSnapshot = this->State->CreateBaseSnapshot(); @@ -740,6 +741,11 @@ void cmake::SetArgs(const std::vector& args) cmSystemTools::ConvertToUnixSlashes(file); this->AddTraceSource(file); 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) { std::cout << "Running with trace output on.\n"; this->SetTrace(true); @@ -870,6 +876,20 @@ cmake::LogLevel cmake::StringToLogLevel(const std::string& levelStr) 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) { // Check if the argument refers to a CMakeCache.txt or diff --git a/Source/cmake.h b/Source/cmake.h index 92494ae..fdcf50f 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -13,6 +13,7 @@ #include #include +#include "cmGeneratedFileStream.h" #include "cmInstalledFile.h" #include "cmListFileCache.h" #include "cmMessageType.h" @@ -401,6 +402,9 @@ public: { return this->TraceOnlyThisSources; } + cmGeneratedFileStream& GetTraceFile() { return this->TraceFile; } + void SetTraceFile(std::string const& file); + bool GetWarnUninitialized() { return this->WarnUninitialized; } void SetWarnUninitialized(bool b) { this->WarnUninitialized = b; } bool GetWarnUnused() { return this->WarnUnused; } @@ -547,6 +551,7 @@ private: bool DebugOutput = false; bool Trace = false; bool TraceExpand = false; + cmGeneratedFileStream TraceFile; bool WarnUninitialized = false; bool WarnUnused = false; bool WarnUnusedCli = true; diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index 10a6825..1fe6439 100644 --- a/Source/cmakemain.cxx +++ b/Source/cmakemain.cxx @@ -80,6 +80,8 @@ const char* cmDocumentationOptions[][2] = { { "--trace-expand", "Put cmake in trace mode with variable expansion." }, { "--trace-source=", "Trace only this CMake file/module. Multiple options allowed." }, + { "--trace-redirect=", + "Redirect trace output to a file instead of stderr." }, { "--warn-uninitialized", "Warn about uninitialized values." }, { "--warn-unused-vars", "Warn about unused variables." }, { "--no-warn-unused-cli", "Don't warn about command line options." }, diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index 4e19871..dd49423 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake @@ -484,6 +484,14 @@ set(RunCMake_TEST_OPTIONS --trace-expand --warn-uninitialized) run_cmake(trace-expand-warn-uninitialized) 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) run_cmake(warn-uninitialized) unset(RunCMake_TEST_OPTIONS) diff --git a/Tests/RunCMake/CommandLine/trace-redirect-check.cmake b/Tests/RunCMake/CommandLine/trace-redirect-check.cmake new file mode 100644 index 0000000..1ee0e0d --- /dev/null +++ b/Tests/RunCMake/CommandLine/trace-redirect-check.cmake @@ -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() diff --git a/Tests/RunCMake/CommandLine/trace-redirect-nofile-result.txt b/Tests/RunCMake/CommandLine/trace-redirect-nofile-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/trace-redirect-nofile-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/trace-redirect-nofile-stderr.txt b/Tests/RunCMake/CommandLine/trace-redirect-nofile-stderr.txt new file mode 100644 index 0000000..edb0c8e --- /dev/null +++ b/Tests/RunCMake/CommandLine/trace-redirect-nofile-stderr.txt @@ -0,0 +1 @@ +^CMake Error: Error opening trace file /no/such/file.txt: .+$ diff --git a/Tests/RunCMake/CommandLine/trace-redirect-nofile.cmake b/Tests/RunCMake/CommandLine/trace-redirect-nofile.cmake new file mode 100644 index 0000000..e69de29 diff --git a/Tests/RunCMake/CommandLine/trace-redirect-stdout.txt b/Tests/RunCMake/CommandLine/trace-redirect-stdout.txt new file mode 100644 index 0000000..775f2b5 --- /dev/null +++ b/Tests/RunCMake/CommandLine/trace-redirect-stdout.txt @@ -0,0 +1 @@ +^.*Trace will be written to .+redirected.trace.*$ diff --git a/Tests/RunCMake/CommandLine/trace-redirect.cmake b/Tests/RunCMake/CommandLine/trace-redirect.cmake new file mode 100644 index 0000000..e69de29 -- cgit v0.12