mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-16 05:26:58 +08:00
cmExecuteProcessCommand: Replace cmsysProcess with cmUVProcessChain
This commit is contained in:
@@ -2,8 +2,8 @@
|
|||||||
file Copyright.txt or https://cmake.org/licensing for details. */
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
||||||
#include "cmExecuteProcessCommand.h"
|
#include "cmExecuteProcessCommand.h"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cctype> /* isspace */
|
#include <cctype> /* isspace */
|
||||||
|
#include <cstdint>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
@@ -16,7 +16,9 @@
|
|||||||
#include <cmext/algorithm>
|
#include <cmext/algorithm>
|
||||||
#include <cmext/string_view>
|
#include <cmext/string_view>
|
||||||
|
|
||||||
#include "cmsys/Process.h"
|
#include <cm3p/uv.h>
|
||||||
|
|
||||||
|
#include "cm_fileno.hxx"
|
||||||
|
|
||||||
#include "cmArgumentParser.h"
|
#include "cmArgumentParser.h"
|
||||||
#include "cmExecutionStatus.h"
|
#include "cmExecutionStatus.h"
|
||||||
@@ -26,6 +28,9 @@
|
|||||||
#include "cmProcessOutput.h"
|
#include "cmProcessOutput.h"
|
||||||
#include "cmStringAlgorithms.h"
|
#include "cmStringAlgorithms.h"
|
||||||
#include "cmSystemTools.h"
|
#include "cmSystemTools.h"
|
||||||
|
#include "cmUVHandlePtr.h"
|
||||||
|
#include "cmUVProcessChain.h"
|
||||||
|
#include "cmUVStream.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
bool cmExecuteProcessCommandIsWhitespace(char c)
|
bool cmExecuteProcessCommandIsWhitespace(char c)
|
||||||
@@ -36,7 +41,7 @@ bool cmExecuteProcessCommandIsWhitespace(char c)
|
|||||||
void cmExecuteProcessCommandFixText(std::vector<char>& output,
|
void cmExecuteProcessCommandFixText(std::vector<char>& output,
|
||||||
bool strip_trailing_whitespace);
|
bool strip_trailing_whitespace);
|
||||||
void cmExecuteProcessCommandAppend(std::vector<char>& output, const char* data,
|
void cmExecuteProcessCommandAppend(std::vector<char>& output, const char* data,
|
||||||
int length);
|
std::size_t length);
|
||||||
}
|
}
|
||||||
|
|
||||||
// cmExecuteProcessCommand
|
// cmExecuteProcessCommand
|
||||||
@@ -143,57 +148,61 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Create a process instance.
|
// Create a process instance.
|
||||||
std::unique_ptr<cmsysProcess, void (*)(cmsysProcess*)> cp_ptr(
|
cmUVProcessChainBuilder builder;
|
||||||
cmsysProcess_New(), cmsysProcess_Delete);
|
|
||||||
cmsysProcess* cp = cp_ptr.get();
|
|
||||||
|
|
||||||
// Set the command sequence.
|
// Set the command sequence.
|
||||||
for (std::vector<std::string> const& cmd : arguments.Commands) {
|
for (std::vector<std::string> const& cmd : arguments.Commands) {
|
||||||
std::vector<const char*> argv(cmd.size() + 1);
|
builder.AddCommand(cmd);
|
||||||
std::transform(cmd.begin(), cmd.end(), argv.begin(),
|
|
||||||
[](std::string const& s) { return s.c_str(); });
|
|
||||||
argv.back() = nullptr;
|
|
||||||
cmsysProcess_AddCommand(cp, argv.data());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the process working directory.
|
// Set the process working directory.
|
||||||
if (!arguments.WorkingDirectory.empty()) {
|
if (!arguments.WorkingDirectory.empty()) {
|
||||||
cmsysProcess_SetWorkingDirectory(cp, arguments.WorkingDirectory.c_str());
|
builder.SetWorkingDirectory(arguments.WorkingDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always hide the process window.
|
|
||||||
cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
|
|
||||||
|
|
||||||
// Check the output variables.
|
// Check the output variables.
|
||||||
bool merge_output = false;
|
std::unique_ptr<FILE, int (*)(FILE*)> inputFile(nullptr, fclose);
|
||||||
if (!arguments.InputFile.empty()) {
|
if (!arguments.InputFile.empty()) {
|
||||||
cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDIN,
|
inputFile.reset(cmsys::SystemTools::Fopen(arguments.InputFile, "rb"));
|
||||||
arguments.InputFile.c_str());
|
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT,
|
||||||
|
cm_fileno(inputFile.get()));
|
||||||
|
} else {
|
||||||
|
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT,
|
||||||
|
cm_fileno(stdin));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<FILE, int (*)(FILE*)> outputFile(nullptr, fclose);
|
||||||
if (!arguments.OutputFile.empty()) {
|
if (!arguments.OutputFile.empty()) {
|
||||||
cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDOUT,
|
outputFile.reset(cmsys::SystemTools::Fopen(arguments.OutputFile, "wb"));
|
||||||
arguments.OutputFile.c_str());
|
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT,
|
||||||
}
|
cm_fileno(outputFile.get()));
|
||||||
if (!arguments.ErrorFile.empty()) {
|
} else {
|
||||||
if (arguments.ErrorFile == arguments.OutputFile) {
|
if (arguments.OutputVariable == arguments.ErrorVariable &&
|
||||||
merge_output = true;
|
!arguments.ErrorVariable.empty()) {
|
||||||
|
builder.SetMergedBuiltinStreams();
|
||||||
} else {
|
} else {
|
||||||
cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDERR,
|
builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT);
|
||||||
arguments.ErrorFile.c_str());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!arguments.OutputVariable.empty() &&
|
|
||||||
arguments.OutputVariable == arguments.ErrorVariable) {
|
std::unique_ptr<FILE, int (*)(FILE*)> errorFile(nullptr, fclose);
|
||||||
merge_output = true;
|
if (!arguments.ErrorFile.empty()) {
|
||||||
}
|
if (arguments.ErrorFile == arguments.OutputFile) {
|
||||||
if (merge_output) {
|
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR,
|
||||||
cmsysProcess_SetOption(cp, cmsysProcess_Option_MergeOutput, 1);
|
cm_fileno(outputFile.get()));
|
||||||
|
} else {
|
||||||
|
errorFile.reset(cmsys::SystemTools::Fopen(arguments.ErrorFile, "wb"));
|
||||||
|
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR,
|
||||||
|
cm_fileno(errorFile.get()));
|
||||||
|
}
|
||||||
|
} else if (arguments.ErrorVariable.empty() ||
|
||||||
|
(!arguments.ErrorVariable.empty() &&
|
||||||
|
arguments.OutputVariable != arguments.ErrorVariable)) {
|
||||||
|
builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the timeout if any.
|
// Set the timeout if any.
|
||||||
if (timeout >= 0) {
|
int64_t timeoutMillis = static_cast<int64_t>(timeout * 1000.0);
|
||||||
cmsysProcess_SetTimeout(cp, timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool echo_stdout = false;
|
bool echo_stdout = false;
|
||||||
bool echo_stderr = false;
|
bool echo_stderr = false;
|
||||||
@@ -241,36 +250,86 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Start the process.
|
// Start the process.
|
||||||
cmsysProcess_Execute(cp);
|
auto chain = builder.Start();
|
||||||
|
|
||||||
|
bool timedOut = false;
|
||||||
|
cm::uv_timer_ptr timer;
|
||||||
|
|
||||||
|
if (timeoutMillis >= 0) {
|
||||||
|
timer.init(chain.GetLoop(), &timedOut);
|
||||||
|
timer.start(
|
||||||
|
[](uv_timer_t* handle) {
|
||||||
|
auto* timeoutPtr = static_cast<bool*>(handle->data);
|
||||||
|
*timeoutPtr = true;
|
||||||
|
},
|
||||||
|
timeoutMillis, 0);
|
||||||
|
}
|
||||||
|
|
||||||
// Read the process output.
|
// Read the process output.
|
||||||
std::vector<char> tempOutput;
|
struct ReadData
|
||||||
std::vector<char> tempError;
|
{
|
||||||
int length;
|
bool Finished = false;
|
||||||
char* data;
|
std::vector<char> Output;
|
||||||
int p;
|
cm::uv_pipe_ptr Stream;
|
||||||
|
};
|
||||||
|
ReadData outputData;
|
||||||
|
ReadData errorData;
|
||||||
cmProcessOutput processOutput(
|
cmProcessOutput processOutput(
|
||||||
cmProcessOutput::FindEncoding(arguments.Encoding));
|
cmProcessOutput::FindEncoding(arguments.Encoding));
|
||||||
std::string strdata;
|
std::string strdata;
|
||||||
while ((p = cmsysProcess_WaitForData(cp, &data, &length, nullptr))) {
|
|
||||||
// Put the output in the right place.
|
std::unique_ptr<cmUVStreamReadHandle> outputHandle;
|
||||||
if (p == cmsysProcess_Pipe_STDOUT && !arguments.OutputQuiet) {
|
if (chain.OutputStream() >= 0) {
|
||||||
if (arguments.OutputVariable.empty() || arguments.EchoOutputVariable) {
|
outputData.Stream.init(chain.GetLoop(), 0);
|
||||||
processOutput.DecodeText(data, length, strdata, 1);
|
uv_pipe_open(outputData.Stream, chain.OutputStream());
|
||||||
cmSystemTools::Stdout(strdata);
|
outputHandle = cmUVStreamRead(
|
||||||
}
|
outputData.Stream,
|
||||||
if (!arguments.OutputVariable.empty()) {
|
[&arguments, &processOutput, &outputData,
|
||||||
cmExecuteProcessCommandAppend(tempOutput, data, length);
|
&strdata](std::vector<char> data) {
|
||||||
}
|
if (!arguments.OutputQuiet) {
|
||||||
} else if (p == cmsysProcess_Pipe_STDERR && !arguments.ErrorQuiet) {
|
if (arguments.OutputVariable.empty() ||
|
||||||
if (arguments.ErrorVariable.empty() || arguments.EchoErrorVariable) {
|
arguments.EchoOutputVariable) {
|
||||||
processOutput.DecodeText(data, length, strdata, 2);
|
processOutput.DecodeText(data.data(), data.size(), strdata, 1);
|
||||||
cmSystemTools::Stderr(strdata);
|
cmSystemTools::Stdout(strdata);
|
||||||
}
|
}
|
||||||
if (!arguments.ErrorVariable.empty()) {
|
if (!arguments.OutputVariable.empty()) {
|
||||||
cmExecuteProcessCommandAppend(tempError, data, length);
|
cmExecuteProcessCommandAppend(outputData.Output, data.data(),
|
||||||
}
|
data.size());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[&outputData]() { outputData.Finished = true; });
|
||||||
|
} else {
|
||||||
|
outputData.Finished = true;
|
||||||
|
}
|
||||||
|
std::unique_ptr<cmUVStreamReadHandle> errorHandle;
|
||||||
|
if (chain.ErrorStream() >= 0 &&
|
||||||
|
chain.ErrorStream() != chain.OutputStream()) {
|
||||||
|
errorData.Stream.init(chain.GetLoop(), 0);
|
||||||
|
uv_pipe_open(errorData.Stream, chain.ErrorStream());
|
||||||
|
errorHandle = cmUVStreamRead(
|
||||||
|
errorData.Stream,
|
||||||
|
[&arguments, &processOutput, &errorData,
|
||||||
|
&strdata](std::vector<char> data) {
|
||||||
|
if (!arguments.ErrorQuiet) {
|
||||||
|
if (arguments.ErrorVariable.empty() || arguments.EchoErrorVariable) {
|
||||||
|
processOutput.DecodeText(data.data(), data.size(), strdata, 2);
|
||||||
|
cmSystemTools::Stderr(strdata);
|
||||||
|
}
|
||||||
|
if (!arguments.ErrorVariable.empty()) {
|
||||||
|
cmExecuteProcessCommandAppend(errorData.Output, data.data(),
|
||||||
|
data.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[&errorData]() { errorData.Finished = true; });
|
||||||
|
} else {
|
||||||
|
errorData.Finished = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (chain.Valid() && !timedOut &&
|
||||||
|
!(chain.Finished() && outputData.Finished && errorData.Finished)) {
|
||||||
|
uv_run(&chain.GetLoop(), UV_RUN_ONCE);
|
||||||
}
|
}
|
||||||
if (!arguments.OutputQuiet &&
|
if (!arguments.OutputQuiet &&
|
||||||
(arguments.OutputVariable.empty() || arguments.EchoOutputVariable)) {
|
(arguments.OutputVariable.empty() || arguments.EchoOutputVariable)) {
|
||||||
@@ -287,151 +346,102 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// All output has been read. Wait for the process to exit.
|
// All output has been read.
|
||||||
cmsysProcess_WaitForExit(cp, nullptr);
|
processOutput.DecodeText(outputData.Output, outputData.Output);
|
||||||
processOutput.DecodeText(tempOutput, tempOutput);
|
processOutput.DecodeText(errorData.Output, errorData.Output);
|
||||||
processOutput.DecodeText(tempError, tempError);
|
|
||||||
|
|
||||||
// Fix the text in the output strings.
|
// Fix the text in the output strings.
|
||||||
cmExecuteProcessCommandFixText(tempOutput,
|
cmExecuteProcessCommandFixText(outputData.Output,
|
||||||
arguments.OutputStripTrailingWhitespace);
|
arguments.OutputStripTrailingWhitespace);
|
||||||
cmExecuteProcessCommandFixText(tempError,
|
cmExecuteProcessCommandFixText(errorData.Output,
|
||||||
arguments.ErrorStripTrailingWhitespace);
|
arguments.ErrorStripTrailingWhitespace);
|
||||||
|
|
||||||
// Store the output obtained.
|
// Store the output obtained.
|
||||||
if (!arguments.OutputVariable.empty() && !tempOutput.empty()) {
|
if (!arguments.OutputVariable.empty() && !outputData.Output.empty()) {
|
||||||
status.GetMakefile().AddDefinition(arguments.OutputVariable,
|
status.GetMakefile().AddDefinition(arguments.OutputVariable,
|
||||||
tempOutput.data());
|
outputData.Output.data());
|
||||||
}
|
}
|
||||||
if (!merge_output && !arguments.ErrorVariable.empty() &&
|
if (arguments.ErrorVariable != arguments.OutputVariable &&
|
||||||
!tempError.empty()) {
|
!arguments.ErrorVariable.empty() && !errorData.Output.empty()) {
|
||||||
status.GetMakefile().AddDefinition(arguments.ErrorVariable,
|
status.GetMakefile().AddDefinition(arguments.ErrorVariable,
|
||||||
tempError.data());
|
errorData.Output.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the result of running the process.
|
// Store the result of running the process.
|
||||||
if (!arguments.ResultVariable.empty()) {
|
if (!arguments.ResultVariable.empty()) {
|
||||||
switch (cmsysProcess_GetState(cp)) {
|
if (timedOut) {
|
||||||
case cmsysProcess_State_Exited: {
|
status.GetMakefile().AddDefinition(arguments.ResultVariable,
|
||||||
int v = cmsysProcess_GetExitValue(cp);
|
"Process terminated due to timeout");
|
||||||
char buf[16];
|
} else {
|
||||||
snprintf(buf, sizeof(buf), "%d", v);
|
auto const* lastStatus = chain.GetStatus().back();
|
||||||
status.GetMakefile().AddDefinition(arguments.ResultVariable, buf);
|
auto exception = lastStatus->GetException();
|
||||||
} break;
|
if (exception.first == cmUVProcessChain::ExceptionCode::None) {
|
||||||
case cmsysProcess_State_Exception:
|
|
||||||
status.GetMakefile().AddDefinition(
|
status.GetMakefile().AddDefinition(
|
||||||
arguments.ResultVariable, cmsysProcess_GetExceptionString(cp));
|
arguments.ResultVariable,
|
||||||
break;
|
std::to_string(static_cast<int>(lastStatus->ExitStatus)));
|
||||||
case cmsysProcess_State_Error:
|
} else {
|
||||||
status.GetMakefile().AddDefinition(arguments.ResultVariable,
|
status.GetMakefile().AddDefinition(arguments.ResultVariable,
|
||||||
cmsysProcess_GetErrorString(cp));
|
exception.second);
|
||||||
break;
|
}
|
||||||
case cmsysProcess_State_Expired:
|
|
||||||
status.GetMakefile().AddDefinition(
|
|
||||||
arguments.ResultVariable, "Process terminated due to timeout");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Store the result of running the processes.
|
// Store the result of running the processes.
|
||||||
if (!arguments.ResultsVariable.empty()) {
|
if (!arguments.ResultsVariable.empty()) {
|
||||||
switch (cmsysProcess_GetState(cp)) {
|
if (timedOut) {
|
||||||
case cmsysProcess_State_Exited: {
|
status.GetMakefile().AddDefinition(arguments.ResultsVariable,
|
||||||
std::vector<std::string> res;
|
"Process terminated due to timeout");
|
||||||
for (size_t i = 0; i < arguments.Commands.size(); ++i) {
|
} else {
|
||||||
switch (cmsysProcess_GetStateByIndex(cp, static_cast<int>(i))) {
|
std::vector<std::string> res;
|
||||||
case kwsysProcess_StateByIndex_Exited: {
|
for (auto const* processStatus : chain.GetStatus()) {
|
||||||
int exitCode =
|
auto exception = processStatus->GetException();
|
||||||
cmsysProcess_GetExitValueByIndex(cp, static_cast<int>(i));
|
if (exception.first == cmUVProcessChain::ExceptionCode::None) {
|
||||||
char buf[16];
|
res.emplace_back(
|
||||||
snprintf(buf, sizeof(buf), "%d", exitCode);
|
std::to_string(static_cast<int>(processStatus->ExitStatus)));
|
||||||
res.emplace_back(buf);
|
} else {
|
||||||
} break;
|
res.emplace_back(exception.second);
|
||||||
case kwsysProcess_StateByIndex_Exception:
|
|
||||||
res.emplace_back(cmsysProcess_GetExceptionStringByIndex(
|
|
||||||
cp, static_cast<int>(i)));
|
|
||||||
break;
|
|
||||||
case kwsysProcess_StateByIndex_Error:
|
|
||||||
default:
|
|
||||||
res.emplace_back("Error getting the child return code");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
status.GetMakefile().AddDefinition(arguments.ResultsVariable,
|
}
|
||||||
cmList::to_string(res));
|
status.GetMakefile().AddDefinition(arguments.ResultsVariable,
|
||||||
} break;
|
cmList::to_string(res));
|
||||||
case cmsysProcess_State_Exception:
|
|
||||||
status.GetMakefile().AddDefinition(
|
|
||||||
arguments.ResultsVariable, cmsysProcess_GetExceptionString(cp));
|
|
||||||
break;
|
|
||||||
case cmsysProcess_State_Error:
|
|
||||||
status.GetMakefile().AddDefinition(arguments.ResultsVariable,
|
|
||||||
cmsysProcess_GetErrorString(cp));
|
|
||||||
break;
|
|
||||||
case cmsysProcess_State_Expired:
|
|
||||||
status.GetMakefile().AddDefinition(
|
|
||||||
arguments.ResultsVariable, "Process terminated due to timeout");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto queryProcessStatusByIndex = [&cp](int index) -> std::string {
|
auto queryProcessStatusByIndex = [&chain](std::size_t index) -> std::string {
|
||||||
std::string processStatus;
|
auto const& processStatus = chain.GetStatus(index);
|
||||||
switch (cmsysProcess_GetStateByIndex(cp, static_cast<int>(index))) {
|
auto exception = processStatus.GetException();
|
||||||
case kwsysProcess_StateByIndex_Exited: {
|
if (exception.first == cmUVProcessChain::ExceptionCode::None) {
|
||||||
int exitCode = cmsysProcess_GetExitValueByIndex(cp, index);
|
if (processStatus.ExitStatus) {
|
||||||
if (exitCode) {
|
return cmStrCat("Child return code: ", processStatus.ExitStatus);
|
||||||
processStatus = "Child return code: " + std::to_string(exitCode);
|
|
||||||
}
|
|
||||||
} break;
|
|
||||||
case kwsysProcess_StateByIndex_Exception: {
|
|
||||||
processStatus = cmStrCat(
|
|
||||||
"Abnormal exit with child return code: ",
|
|
||||||
cmsysProcess_GetExceptionStringByIndex(cp, static_cast<int>(index)));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case kwsysProcess_StateByIndex_Error:
|
return "";
|
||||||
default:
|
|
||||||
processStatus = "Error getting the child return code";
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return processStatus;
|
return cmStrCat("Abnormal exit with child return code: ",
|
||||||
|
exception.second);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (arguments.CommandErrorIsFatal == "ANY"_s) {
|
if (arguments.CommandErrorIsFatal == "ANY"_s) {
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
switch (cmsysProcess_GetState(cp)) {
|
if (timedOut) {
|
||||||
case cmsysProcess_State_Exited: {
|
status.SetError("Process terminated due to timeout");
|
||||||
std::map<int, std::string> failureIndices;
|
ret = false;
|
||||||
for (int i = 0; i < static_cast<int>(arguments.Commands.size()); ++i) {
|
} else {
|
||||||
std::string processStatus = queryProcessStatusByIndex(i);
|
std::map<std::size_t, std::string> failureIndices;
|
||||||
if (!processStatus.empty()) {
|
auto statuses = chain.GetStatus();
|
||||||
failureIndices[i] = processStatus;
|
for (std::size_t i = 0; i < statuses.size(); ++i) {
|
||||||
}
|
std::string processStatus = queryProcessStatusByIndex(i);
|
||||||
if (!failureIndices.empty()) {
|
if (!processStatus.empty()) {
|
||||||
std::ostringstream oss;
|
failureIndices[i] = processStatus;
|
||||||
oss << "failed command indexes:\n";
|
|
||||||
for (auto const& e : failureIndices) {
|
|
||||||
oss << " " << e.first + 1 << ": \"" << e.second << "\"\n";
|
|
||||||
}
|
|
||||||
status.SetError(oss.str());
|
|
||||||
ret = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} break;
|
}
|
||||||
case cmsysProcess_State_Exception:
|
if (!failureIndices.empty()) {
|
||||||
status.SetError(
|
std::ostringstream oss;
|
||||||
cmStrCat("abnormal exit: ", cmsysProcess_GetExceptionString(cp)));
|
oss << "failed command indexes:\n";
|
||||||
|
for (auto const& e : failureIndices) {
|
||||||
|
oss << " " << e.first + 1 << ": \"" << e.second << "\"\n";
|
||||||
|
}
|
||||||
|
status.SetError(oss.str());
|
||||||
ret = false;
|
ret = false;
|
||||||
break;
|
}
|
||||||
case cmsysProcess_State_Error:
|
|
||||||
status.SetError(cmStrCat("error getting child return code: ",
|
|
||||||
cmsysProcess_GetErrorString(cp)));
|
|
||||||
ret = false;
|
|
||||||
break;
|
|
||||||
case cmsysProcess_State_Expired:
|
|
||||||
status.SetError("Process terminated due to timeout");
|
|
||||||
ret = false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
@@ -442,29 +452,23 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
|
|||||||
|
|
||||||
if (arguments.CommandErrorIsFatal == "LAST"_s) {
|
if (arguments.CommandErrorIsFatal == "LAST"_s) {
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
switch (cmsysProcess_GetState(cp)) {
|
if (timedOut) {
|
||||||
case cmsysProcess_State_Exited: {
|
status.SetError("Process terminated due to timeout");
|
||||||
|
ret = false;
|
||||||
|
} else {
|
||||||
|
auto const& lastStatus = chain.GetStatus(arguments.Commands.size() - 1);
|
||||||
|
auto exception = lastStatus.GetException();
|
||||||
|
if (exception.first != cmUVProcessChain::ExceptionCode::None) {
|
||||||
|
status.SetError(cmStrCat("Abnormal exit: ", exception.second));
|
||||||
|
ret = false;
|
||||||
|
} else {
|
||||||
int lastIndex = static_cast<int>(arguments.Commands.size() - 1);
|
int lastIndex = static_cast<int>(arguments.Commands.size() - 1);
|
||||||
const std::string processStatus = queryProcessStatusByIndex(lastIndex);
|
const std::string processStatus = queryProcessStatusByIndex(lastIndex);
|
||||||
if (!processStatus.empty()) {
|
if (!processStatus.empty()) {
|
||||||
status.SetError("last command failed");
|
status.SetError("last command failed");
|
||||||
ret = false;
|
ret = false;
|
||||||
}
|
}
|
||||||
} break;
|
}
|
||||||
case cmsysProcess_State_Exception:
|
|
||||||
status.SetError(
|
|
||||||
cmStrCat("Abnormal exit: ", cmsysProcess_GetExceptionString(cp)));
|
|
||||||
ret = false;
|
|
||||||
break;
|
|
||||||
case cmsysProcess_State_Error:
|
|
||||||
status.SetError(cmStrCat("Error getting child return code: ",
|
|
||||||
cmsysProcess_GetErrorString(cp)));
|
|
||||||
ret = false;
|
|
||||||
break;
|
|
||||||
case cmsysProcess_State_Expired:
|
|
||||||
status.SetError("Process terminated due to timeout");
|
|
||||||
ret = false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
cmSystemTools::SetFatalErrorOccurred();
|
cmSystemTools::SetFatalErrorOccurred();
|
||||||
@@ -507,7 +511,7 @@ void cmExecuteProcessCommandFixText(std::vector<char>& output,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cmExecuteProcessCommandAppend(std::vector<char>& output, const char* data,
|
void cmExecuteProcessCommandAppend(std::vector<char>& output, const char* data,
|
||||||
int length)
|
std::size_t length)
|
||||||
{
|
{
|
||||||
#if defined(__APPLE__)
|
#if defined(__APPLE__)
|
||||||
// HACK on Apple to work around bug with inserting at the
|
// HACK on Apple to work around bug with inserting at the
|
||||||
|
@@ -466,6 +466,7 @@ add_RunCMake_test(build_command)
|
|||||||
add_executable(exit_code exit_code.c)
|
add_executable(exit_code exit_code.c)
|
||||||
set(execute_process_ARGS
|
set(execute_process_ARGS
|
||||||
-DEXIT_CODE_EXE=$<TARGET_FILE:exit_code>
|
-DEXIT_CODE_EXE=$<TARGET_FILE:exit_code>
|
||||||
|
-DPRINT_STDIN_EXE=$<TARGET_FILE:print_stdin>
|
||||||
-DPython_EXECUTABLE=${Python_EXECUTABLE}
|
-DPython_EXECUTABLE=${Python_EXECUTABLE}
|
||||||
)
|
)
|
||||||
if(NOT CMake_TEST_EXTERNAL_CMAKE)
|
if(NOT CMake_TEST_EXTERNAL_CMAKE)
|
||||||
|
@@ -34,6 +34,7 @@ run_cmake_command(AnyCommandGood ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/AnyC
|
|||||||
run_cmake_command(LastCommandError ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/LastCommandError.cmake)
|
run_cmake_command(LastCommandError ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/LastCommandError.cmake)
|
||||||
run_cmake_command(LastCommandTimeout ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/LastCommandTimeout.cmake)
|
run_cmake_command(LastCommandTimeout ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/LastCommandTimeout.cmake)
|
||||||
run_cmake_command(LastCommandGood ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/LastCommandGood.cmake)
|
run_cmake_command(LastCommandGood ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/LastCommandGood.cmake)
|
||||||
|
run_cmake_command(Stdin ${CMAKE_COMMAND} -DPRINT_STDIN_EXE=${PRINT_STDIN_EXE} -P ${RunCMake_SOURCE_DIR}/Stdin.cmake)
|
||||||
|
|
||||||
if(UNIX AND Python_EXECUTABLE)
|
if(UNIX AND Python_EXECUTABLE)
|
||||||
run_cmake_command(AnyCommandAbnormalExit ${CMAKE_COMMAND} -DPython_EXECUTABLE=${Python_EXECUTABLE} -P ${RunCMake_SOURCE_DIR}/AnyCommandAbnormalExit.cmake)
|
run_cmake_command(AnyCommandAbnormalExit ${CMAKE_COMMAND} -DPython_EXECUTABLE=${Python_EXECUTABLE} -P ${RunCMake_SOURCE_DIR}/AnyCommandAbnormalExit.cmake)
|
||||||
|
1
Tests/RunCMake/execute_process/Stdin-stdin.txt
Normal file
1
Tests/RunCMake/execute_process/Stdin-stdin.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Hello world!
|
1
Tests/RunCMake/execute_process/Stdin-stdout.txt
Normal file
1
Tests/RunCMake/execute_process/Stdin-stdout.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
^Hello world!$
|
1
Tests/RunCMake/execute_process/Stdin.cmake
Normal file
1
Tests/RunCMake/execute_process/Stdin.cmake
Normal file
@@ -0,0 +1 @@
|
|||||||
|
execute_process(COMMAND ${PRINT_STDIN_EXE})
|
Reference in New Issue
Block a user