From b6e4e4babcdc6b44a1d494c4e9c07634c2b3bcd6 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 24 Jan 2024 10:07:54 -0500 Subject: cmUVProcessChain: Simplify SetExternalStream usage It is commonly called with the `fileno()` of a `FILE*` stream, so accept the latter directly. --- Source/CTest/cmCTestCoverageHandler.cxx | 6 ++---- Source/CTest/cmCTestLaunch.cxx | 9 ++------- Source/cmExecuteProcessCommand.cxx | 13 +++++-------- Source/cmSystemTools.cxx | 12 +++--------- Source/cmUVProcessChain.cxx | 8 ++++++++ Source/cmUVProcessChain.h | 2 ++ Source/cmake.cxx | 7 ++----- Source/cmcmd.cxx | 9 ++------- Tests/CMakeLib/testUVProcessChain.cxx | 5 +---- 9 files changed, 27 insertions(+), 44 deletions(-) diff --git a/Source/CTest/cmCTestCoverageHandler.cxx b/Source/CTest/cmCTestCoverageHandler.cxx index 1aa49cf..f9f9add 100644 --- a/Source/CTest/cmCTestCoverageHandler.cxx +++ b/Source/CTest/cmCTestCoverageHandler.cxx @@ -21,8 +21,6 @@ #include "cmsys/Glob.hxx" #include "cmsys/RegularExpression.hxx" -#include "cm_fileno.hxx" - #include "cmCTest.h" #include "cmDuration.h" #include "cmGeneratedFileStream.h" @@ -1887,9 +1885,9 @@ int cmCTestCoverageHandler::RunBullseyeCommand( cmsys::SystemTools::Fopen(stderrFile, "w"), fclose); builder.AddCommand(args) .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, - cm_fileno(stdoutHandle.get())) + stdoutHandle.get()) .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, - cm_fileno(stderrHandle.get())); + stderrHandle.get()); // since we set the output file names wait for it to end auto chain = builder.Start(); chain.Wait(); diff --git a/Source/CTest/cmCTestLaunch.cxx b/Source/CTest/cmCTestLaunch.cxx index 6b13ad1..9669d76 100644 --- a/Source/CTest/cmCTestLaunch.cxx +++ b/Source/CTest/cmCTestLaunch.cxx @@ -13,8 +13,6 @@ #include "cmsys/FStream.hxx" #include "cmsys/RegularExpression.hxx" -#include "cm_fileno.hxx" - #include "cmCTestLaunchReporter.h" #include "cmGlobalGenerator.h" #include "cmMakefile.h" @@ -158,11 +156,8 @@ void cmCTestLaunch::RunChild() cmsys::ofstream ferr; if (this->Reporter.Passthru) { // In passthru mode we just share the output pipes. - builder - .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, - cm_fileno(stdout)) - .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, - cm_fileno(stderr)); + builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, stdout) + .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, stderr); } else { // In full mode we record the child output pipes to log files. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT) diff --git a/Source/cmExecuteProcessCommand.cxx b/Source/cmExecuteProcessCommand.cxx index e764545..26b9424 100644 --- a/Source/cmExecuteProcessCommand.cxx +++ b/Source/cmExecuteProcessCommand.cxx @@ -18,8 +18,6 @@ #include -#include "cm_fileno.hxx" - #include "cmArgumentParser.h" #include "cmExecutionStatus.h" #include "cmList.h" @@ -184,11 +182,10 @@ bool cmExecuteProcessCommand(std::vector const& args, inputFile.reset(cmsys::SystemTools::Fopen(inputFilename, "rb")); if (inputFile) { builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, - cm_fileno(inputFile.get())); + inputFile.get()); } } else { - builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, - cm_fileno(stdin)); + builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, stdin); } std::unique_ptr outputFile(nullptr, fclose); @@ -196,7 +193,7 @@ bool cmExecuteProcessCommand(std::vector const& args, outputFile.reset(cmsys::SystemTools::Fopen(outputFilename, "wb")); if (outputFile) { builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, - cm_fileno(outputFile.get())); + outputFile.get()); } } else { if (arguments.OutputVariable == arguments.ErrorVariable && @@ -212,13 +209,13 @@ bool cmExecuteProcessCommand(std::vector const& args, if (errorFilename == outputFilename) { if (outputFile) { builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, - cm_fileno(outputFile.get())); + outputFile.get()); } } else { errorFile.reset(cmsys::SystemTools::Fopen(errorFilename, "wb")); if (errorFile) { builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, - cm_fileno(errorFile.get())); + errorFile.get()); } } } else if (arguments.ErrorVariable.empty() || diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index f606c22..1f5333f 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -25,8 +25,6 @@ #include -#include "cm_fileno.hxx" - #include "cmDuration.h" #include "cmELF.h" #include "cmMessageMetadata.h" @@ -576,8 +574,7 @@ bool cmSystemTools::RunSingleCommand(std::vector const& command, cmDuration timeout, Encoding encoding) { cmUVProcessChainBuilder builder; - builder - .SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, cm_fileno(stdin)) + builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, stdin) .AddCommand(command); if (dir) { builder.SetWorkingDirectory(dir); @@ -586,11 +583,8 @@ bool cmSystemTools::RunSingleCommand(std::vector const& command, if (outputflag == OUTPUT_PASSTHROUGH) { captureStdOut = nullptr; captureStdErr = nullptr; - builder - .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, - cm_fileno(stdout)) - .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, - cm_fileno(stderr)); + builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, stdout) + .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, stderr); } else if (outputflag == OUTPUT_MERGE || (captureStdErr && captureStdErr == captureStdOut)) { builder.SetMergedBuiltinStreams(); diff --git a/Source/cmUVProcessChain.cxx b/Source/cmUVProcessChain.cxx index 1e826c1..338abba 100644 --- a/Source/cmUVProcessChain.cxx +++ b/Source/cmUVProcessChain.cxx @@ -12,6 +12,8 @@ #include +#include "cm_fileno.hxx" + #include "cmGetPipes.h" #include "cmUVHandlePtr.h" @@ -117,6 +119,12 @@ cmUVProcessChainBuilder& cmUVProcessChainBuilder::SetExternalStream( return *this; } +cmUVProcessChainBuilder& cmUVProcessChainBuilder::SetExternalStream( + Stream stdio, FILE* stream) +{ + return this->SetExternalStream(stdio, cm_fileno(stream)); +} + cmUVProcessChainBuilder& cmUVProcessChainBuilder::SetMergedBuiltinStreams() { this->MergedBuiltinStreams = true; diff --git a/Source/cmUVProcessChain.h b/Source/cmUVProcessChain.h index 83af639..aa63ba1 100644 --- a/Source/cmUVProcessChain.h +++ b/Source/cmUVProcessChain.h @@ -7,6 +7,7 @@ #include #include // IWYU pragma: keep #include +#include #include #include #include @@ -34,6 +35,7 @@ public: cmUVProcessChainBuilder& SetBuiltinStream(Stream stdio); cmUVProcessChainBuilder& SetMergedBuiltinStreams(); cmUVProcessChainBuilder& SetExternalStream(Stream stdio, int fd); + cmUVProcessChainBuilder& SetExternalStream(Stream stdio, FILE* stream); cmUVProcessChainBuilder& SetWorkingDirectory(std::string dir); cmUVProcessChain Start() const; diff --git a/Source/cmake.cxx b/Source/cmake.cxx index f54196b..636a0da 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -27,7 +27,6 @@ #include "cmsys/Glob.hxx" #include "cmsys/RegularExpression.hxx" -#include "cm_fileno.hxx" #include "cm_sys_stat.h" #include "cmBuildOptions.h" @@ -3916,10 +3915,8 @@ std::function cmake::BuildWorkflowStep( { cmUVProcessChainBuilder builder; builder.AddCommand(args) - .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, - cm_fileno(stdout)) - .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, - cm_fileno(stderr)); + .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, stdout) + .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, stderr); return [builder]() -> int { auto chain = builder.Start(); chain.Wait(); diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 43a945f..5bf8edc 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -11,8 +11,6 @@ #include #include -#include "cm_fileno.hxx" - #include "cmCommandLineArgument.h" #include "cmConsoleBuf.h" #include "cmCryptoHash.h" @@ -1903,11 +1901,8 @@ int cmcmd::ExecuteLinkScript(std::vector const& args) cmUVProcessChainBuilder builder; // Children should share stdout and stderr with this process. - builder - .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, - cm_fileno(stdout)) - .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, - cm_fileno(stderr)); + builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, stdout) + .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, stderr); // Setup this command line. std::vector args2; diff --git a/Tests/CMakeLib/testUVProcessChain.cxx b/Tests/CMakeLib/testUVProcessChain.cxx index aab084b..c6404ac 100644 --- a/Tests/CMakeLib/testUVProcessChain.cxx +++ b/Tests/CMakeLib/testUVProcessChain.cxx @@ -12,8 +12,6 @@ #include -#include "cm_fileno.hxx" - #include "cmGetPipes.h" #include "cmStringAlgorithms.h" #include "cmUVHandlePtr.h" @@ -632,8 +630,7 @@ bool testUVProcessChainInputFile(const char* helperCommand) cmUVProcessChainBuilder builder; builder.AddCommand({ helperCommand, "dedup" }) - .SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, - cm_fileno(f.get())) + .SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, f.get()) .SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT); auto chain = builder.Start(); -- cgit v0.12