Bläddra i källkod

try_compile: Propagate --trace through try_compile

Add ability to specify another cmake instance from which to get the
trace output target. Use this during try_compile.
Matthew Woehlke 3 år sedan
förälder
incheckning
5ba51621af

+ 5 - 0
Help/release/dev/trace-try_compile.rst

@@ -0,0 +1,5 @@
+trace-try_compile
+-----------------
+
+* The :option:`cmake --trace` option now follows :command:`try_compile` and
+  :command:`try_run` invocations.

+ 3 - 0
Source/cmMakefile.cxx

@@ -3584,6 +3584,9 @@ int cmMakefile::TryCompile(const std::string& srcdir,
   gg->RecursionDepth = this->RecursionDepth;
   cm.SetGlobalGenerator(std::move(gg));
 
+  // copy trace state
+  cm.SetTraceRedirect(this->GetCMakeInstance());
+
   // do a configure
   cm.SetHomeDirectory(srcdir);
   cm.SetHomeOutputDirectory(bindir);

+ 10 - 0
Source/cmake.cxx

@@ -1537,6 +1537,16 @@ void cmake::PrintTraceFormatVersion()
   }
 }
 
+void cmake::SetTraceRedirect(cmake* other)
+{
+  this->Trace = other->Trace;
+  this->TraceExpand = other->TraceExpand;
+  this->TraceFormatVar = other->TraceFormatVar;
+  this->TraceOnlyThisSources = other->TraceOnlyThisSources;
+
+  this->TraceRedirect = other;
+}
+
 bool cmake::SetDirectoriesFromFile(const std::string& arg)
 {
   // Check if the argument refers to a CMakeCache.txt or

+ 11 - 1
Source/cmake.h

@@ -513,10 +513,19 @@ public:
   {
     return this->TraceOnlyThisSources;
   }
-  cmGeneratedFileStream& GetTraceFile() { return this->TraceFile; }
+  cmGeneratedFileStream& GetTraceFile()
+  {
+    if (this->TraceRedirect) {
+      return this->TraceRedirect->GetTraceFile();
+    }
+    return this->TraceFile;
+  }
   void SetTraceFile(std::string const& file);
   void PrintTraceFormatVersion();
 
+  //! Use trace from another ::cmake instance.
+  void SetTraceRedirect(cmake* other);
+
   bool GetWarnUninitialized() const { return this->WarnUninitialized; }
   void SetWarnUninitialized(bool b) { this->WarnUninitialized = b; }
   bool GetWarnUnusedCli() const { return this->WarnUnusedCli; }
@@ -677,6 +686,7 @@ private:
   bool TraceExpand = false;
   TraceFormat TraceFormatVar = TRACE_HUMAN;
   cmGeneratedFileStream TraceFile;
+  cmake* TraceRedirect = nullptr;
   bool WarnUninitialized = false;
   bool WarnUnusedCli = true;
   bool CheckSystemVars = false;

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

@@ -940,6 +940,7 @@ unset(RunCMake_TEST_OPTIONS)
 
 set(RunCMake_TEST_OPTIONS --trace)
 run_cmake(trace)
+run_cmake(trace-try_compile)
 unset(RunCMake_TEST_OPTIONS)
 
 set(RunCMake_TEST_OPTIONS --trace-expand)
@@ -952,6 +953,7 @@ unset(RunCMake_TEST_OPTIONS)
 
 set(RunCMake_TEST_OPTIONS --trace-redirect=${RunCMake_BINARY_DIR}/redirected.trace)
 run_cmake(trace-redirect)
+run_cmake(trace-try_compile-redirect)
 unset(RunCMake_TEST_OPTIONS)
 
 set(RunCMake_TEST_OPTIONS --trace-redirect=/no/such/file.txt)

+ 13 - 0
Tests/RunCMake/CommandLine/trace-try_compile-redirect-check.cmake

@@ -0,0 +1,13 @@
+file(READ ${RunCMake_SOURCE_DIR}/trace-try_compile-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()

+ 2 - 0
Tests/RunCMake/CommandLine/trace-try_compile-redirect.cmake

@@ -0,0 +1,2 @@
+cmake_minimum_required(VERSION 3.24)
+project(test C)

+ 4 - 0
Tests/RunCMake/CommandLine/trace-try_compile-stderr.txt

@@ -0,0 +1,4 @@
+.*Modules/CMakeDetermineCompilerABI.cmake\([0-9]+\):  try_compile\([^)]+\)
+.*Tests/RunCMake/CommandLine/trace-try_compile(-redirect)?-build/CMakeFiles/CMakeScratch/TryCompile-[^/]+/CMakeLists.txt\([0-9]+\):  cmake_minimum_required.*
+.*Tests/RunCMake/CommandLine/trace-try_compile(-redirect)?-build/CMakeFiles/CMakeScratch/TryCompile-[^/]+/CMakeLists.txt\([0-9]+\):  project\(CMAKE_TRY_COMPILE.*
+.*Tests/RunCMake/CommandLine/trace-try_compile(-redirect)?-build/CMakeFiles/CMakeScratch/TryCompile-[^/]+/CMakeLists.txt\([0-9]+\):  add_executable\(cmTC_.*

+ 2 - 0
Tests/RunCMake/CommandLine/trace-try_compile.cmake

@@ -0,0 +1,2 @@
+cmake_minimum_required(VERSION 3.24)
+project(test C)