From 89899c3ad256145d966562d3df121052158b92c2 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 17 Apr 2025 16:50:49 -0400 Subject: cmcmd: Simplify vs_link_{exe,dll} output encoding logic Revise commit 7e359823c9 (MSVC: Preserve linker output encoding, 2018-08-03, v3.13.0-rc1~223^2) using the approach from commit f7a5f28318 (cmake: Fix '-E cat' command for binary files on Windows, 2020-10-13, v3.18.5~5^2). --- Source/cmcmd.cxx | 28 +++++++++++++++------------- Source/cmcmd.h | 3 ++- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 326718e..8d15b26 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -19,6 +19,7 @@ #include "cmList.h" #include "cmLocalGenerator.h" #include "cmMakefile.h" +#include "cmProcessOutput.h" #include "cmQtAutoMocUic.h" #include "cmQtAutoRcc.h" #include "cmRange.h" @@ -1463,11 +1464,11 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args, } if (args[1] == "vs_link_exe") { - return cmcmd::VisualStudioLink(args, 1); + return cmcmd::VisualStudioLink(args, 1, std::move(consoleBuf)); } if (args[1] == "vs_link_dll") { - return cmcmd::VisualStudioLink(args, 2); + return cmcmd::VisualStudioLink(args, 2, std::move(consoleBuf)); } if (args[1] == "cmake_llvm_rc") { @@ -2219,19 +2220,16 @@ private: // For visual studio 2005 and newer manifest files need to be embedded into // exe and dll's. This code does that in such a way that incremental linking // still works. -int cmcmd::VisualStudioLink(std::vector const& args, int type) +int cmcmd::VisualStudioLink(std::vector const& args, int type, + std::unique_ptr consoleBuf) { // MSVC tools print output in the language specified by the VSLANG // environment variable, and encoded in the console output code page. - // RunCommand captures and converts it to our internal UTF-8 encoding. - // Then we print it as output through cmConsoleBuf: - // - NMake: Our output goes to a real console. cmConsoleBuf writes - // with WriteConsoleW, so no narrow encoding code page is needed. - // - Ninja: Our output goes to a pipe that ninja buffers and prints again. - // It does not convert encoding, so we must print in the console output - // code page even though it goes to a pipe. - // Both cases can be handled using a cmConsoleBuf without SetUTF8Pipes. - cmConsoleBuf consoleBuf; + // Since vs_link_{exe,dll} just wraps these, pass through that encoding. + // RunCommand tells RunSingleCommand to *not* convert encoding, so + // we buffer the output in its original encoding instead of UTF-8. + // Drop our output encoding conversion so we print with original encoding. + consoleBuf.reset(); if (args.size() < 2) { return -1; @@ -2291,6 +2289,9 @@ static bool RunCommand(char const* comment, NumberFormat exitFormat, int* retCodeOut = nullptr, bool (*retCodeOkay)(int) = nullptr) { + // See comment in VisualStudioLink for why we suppress encoding conversion. + cmProcessOutput::Encoding const encoding = cmProcessOutput::None; + if (verbose) { std::cout << comment << ":\n"; std::cout << cmJoin(command, " ") << '\n'; @@ -2298,7 +2299,8 @@ static bool RunCommand(char const* comment, std::string output; int retCode = 0; bool commandResult = cmSystemTools::RunSingleCommand( - command, &output, &output, &retCode, nullptr, cmSystemTools::OUTPUT_NONE); + command, &output, &output, &retCode, nullptr, cmSystemTools::OUTPUT_NONE, + cmDuration::zero(), encoding); bool const retCodeSuccess = retCode == 0 || (retCodeOkay && retCodeOkay(retCode)); bool const success = commandResult && retCodeSuccess; diff --git a/Source/cmcmd.h b/Source/cmcmd.h index f3eeba4..2435c35 100644 --- a/Source/cmcmd.h +++ b/Source/cmcmd.h @@ -39,5 +39,6 @@ protected: static int RunPreprocessor(std::vector const& command, std::string const& intermediate_file); static int RunLLVMRC(std::vector const& args); - static int VisualStudioLink(std::vector const& args, int type); + static int VisualStudioLink(std::vector const& args, int type, + std::unique_ptr consoleBuf); }; -- cgit v0.12