1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-22 16:07:49 +08:00

cmExecuteProcessCommand: Port to cmArgumentParser

This commit is contained in:
Regina Pfeifer
2019-03-25 07:54:30 +01:00
committed by Kyle Edwards
parent 9bddb03f31
commit b783e62533

View File

@@ -2,12 +2,14 @@
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 "cm_static_string_view.hxx"
#include "cmsys/Process.h" #include "cmsys/Process.h"
#include <algorithm>
#include <ctype.h> /* isspace */ #include <ctype.h> /* isspace */
#include <sstream>
#include <stdio.h> #include <stdio.h>
#include "cmAlgorithms.h" #include "cmAlgorithms.h"
#include "cmArgumentParser.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmProcessOutput.h" #include "cmProcessOutput.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
@@ -32,157 +34,85 @@ bool cmExecuteProcessCommand::InitialPass(std::vector<std::string> const& args,
this->SetError("called with incorrect number of arguments"); this->SetError("called with incorrect number of arguments");
return false; return false;
} }
std::vector<std::vector<const char*>> cmds;
std::string arguments; struct Arguments
bool doing_command = false; {
size_t command_index = 0; std::vector<std::vector<std::string>> Commands;
bool output_quiet = false; std::string OutputVariable;
bool error_quiet = false; std::string ErrorVariable;
bool output_strip_trailing_whitespace = false; std::string ResultVariable;
bool error_strip_trailing_whitespace = false; std::string ResultsVariable;
std::string timeout_string; std::string WorkingDirectory;
std::string input_file; std::string InputFile;
std::string output_file; std::string OutputFile;
std::string error_file; std::string ErrorFile;
std::string output_variable; std::string Timeout;
std::string error_variable; bool OutputQuiet = false;
std::string result_variable; bool ErrorQuiet = false;
std::string results_variable; bool OutputStripTrailingWhitespace = false;
std::string working_directory; bool ErrorStripTrailingWhitespace = false;
cmProcessOutput::Encoding encoding = cmProcessOutput::None; std::string Encoding;
for (size_t i = 0; i < args.size(); ++i) { };
if (args[i] == "COMMAND") {
doing_command = true; static auto const parser =
command_index = cmds.size(); cmArgumentParser<Arguments>{}
cmds.emplace_back(); .Bind("COMMAND"_s, &Arguments::Commands)
} else if (args[i] == "OUTPUT_VARIABLE") { .Bind("OUTPUT_VARIABLE"_s, &Arguments::OutputVariable)
doing_command = false; .Bind("ERROR_VARIABLE"_s, &Arguments::ErrorVariable)
if (++i < args.size()) { .Bind("RESULT_VARIABLE"_s, &Arguments::ResultVariable)
output_variable = args[i]; .Bind("RESULTS_VARIABLE"_s, &Arguments::ResultsVariable)
} else { .Bind("WORKING_DIRECTORY"_s, &Arguments::WorkingDirectory)
this->SetError(" called with no value for OUTPUT_VARIABLE."); .Bind("INPUT_FILE"_s, &Arguments::InputFile)
.Bind("OUTPUT_FILE"_s, &Arguments::OutputFile)
.Bind("ERROR_FILE"_s, &Arguments::ErrorFile)
.Bind("TIMEOUT"_s, &Arguments::Timeout)
.Bind("OUTPUT_QUIET"_s, &Arguments::OutputQuiet)
.Bind("ERROR_QUIET"_s, &Arguments::ErrorQuiet)
.Bind("OUTPUT_STRIP_TRAILING_WHITESPACE"_s,
&Arguments::OutputStripTrailingWhitespace)
.Bind("ERROR_STRIP_TRAILING_WHITESPACE"_s,
&Arguments::ErrorStripTrailingWhitespace)
.Bind("ENCODING"_s, &Arguments::Encoding);
std::vector<std::string> unparsedArguments;
std::vector<std::string> keywordsMissingValue;
Arguments const arguments =
parser.Parse(args, &unparsedArguments, &keywordsMissingValue);
if (!keywordsMissingValue.empty()) {
this->SetError(" called with no value for " +
keywordsMissingValue.front() + ".");
return false; return false;
} }
} else if (args[i] == "ERROR_VARIABLE") { if (!unparsedArguments.empty()) {
doing_command = false; this->SetError(" given unknown argument \"" + unparsedArguments.front() +
if (++i < args.size()) { "\".");
error_variable = args[i];
} else {
this->SetError(" called with no value for ERROR_VARIABLE.");
return false; return false;
} }
} else if (args[i] == "RESULT_VARIABLE") {
doing_command = false;
if (++i < args.size()) {
result_variable = args[i];
} else {
this->SetError(" called with no value for RESULT_VARIABLE.");
return false;
}
} else if (args[i] == "RESULTS_VARIABLE") {
doing_command = false;
if (++i < args.size()) {
results_variable = args[i];
} else {
this->SetError(" called with no value for RESULTS_VARIABLE.");
return false;
}
} else if (args[i] == "WORKING_DIRECTORY") {
doing_command = false;
if (++i < args.size()) {
working_directory = args[i];
} else {
this->SetError(" called with no value for WORKING_DIRECTORY.");
return false;
}
} else if (args[i] == "INPUT_FILE") {
doing_command = false;
if (++i < args.size()) {
input_file = args[i];
} else {
this->SetError(" called with no value for INPUT_FILE.");
return false;
}
} else if (args[i] == "OUTPUT_FILE") {
doing_command = false;
if (++i < args.size()) {
output_file = args[i];
} else {
this->SetError(" called with no value for OUTPUT_FILE.");
return false;
}
} else if (args[i] == "ERROR_FILE") {
doing_command = false;
if (++i < args.size()) {
error_file = args[i];
} else {
this->SetError(" called with no value for ERROR_FILE.");
return false;
}
} else if (args[i] == "TIMEOUT") {
doing_command = false;
if (++i < args.size()) {
timeout_string = args[i];
} else {
this->SetError(" called with no value for TIMEOUT.");
return false;
}
} else if (args[i] == "OUTPUT_QUIET") {
doing_command = false;
output_quiet = true;
} else if (args[i] == "ERROR_QUIET") {
doing_command = false;
error_quiet = true;
} else if (args[i] == "OUTPUT_STRIP_TRAILING_WHITESPACE") {
doing_command = false;
output_strip_trailing_whitespace = true;
} else if (args[i] == "ERROR_STRIP_TRAILING_WHITESPACE") {
doing_command = false;
error_strip_trailing_whitespace = true;
} else if (args[i] == "ENCODING") {
doing_command = false;
if (++i < args.size()) {
encoding = cmProcessOutput::FindEncoding(args[i]);
} else {
this->SetError(" called with no value for ENCODING.");
return false;
}
} else if (doing_command) {
cmds[command_index].push_back(args[i].c_str());
} else {
std::ostringstream e;
e << " given unknown argument \"" << args[i] << "\".";
this->SetError(e.str());
return false;
}
}
if (!this->Makefile->CanIWriteThisFile(output_file)) { if (!this->Makefile->CanIWriteThisFile(arguments.OutputFile)) {
std::string e = "attempted to output into a file: " + output_file + this->SetError("attempted to output into a file: " + arguments.OutputFile +
" into a source directory."; " into a source directory.");
this->SetError(e);
cmSystemTools::SetFatalErrorOccured(); cmSystemTools::SetFatalErrorOccured();
return false; return false;
} }
// Check for commands given. // Check for commands given.
if (cmds.empty()) { if (arguments.Commands.empty()) {
this->SetError(" called with no COMMAND argument."); this->SetError(" called with no COMMAND argument.");
return false; return false;
} }
for (auto& cmd : cmds) { for (std::vector<std::string> const& cmd : arguments.Commands) {
if (cmd.empty()) { if (cmd.empty()) {
this->SetError(" given COMMAND argument with no value."); this->SetError(" given COMMAND argument with no value.");
return false; return false;
} }
// Add the null terminating pointer to the command argument list.
cmd.push_back(nullptr);
} }
// Parse the timeout string. // Parse the timeout string.
double timeout = -1; double timeout = -1;
if (!timeout_string.empty()) { if (!arguments.Timeout.empty()) {
if (sscanf(timeout_string.c_str(), "%lg", &timeout) != 1) { if (sscanf(arguments.Timeout.c_str(), "%lg", &timeout) != 1) {
this->SetError(" called with TIMEOUT value that could not be parsed."); this->SetError(" called with TIMEOUT value that could not be parsed.");
return false; return false;
} }
@@ -192,13 +122,17 @@ bool cmExecuteProcessCommand::InitialPass(std::vector<std::string> const& args,
cmsysProcess* cp = cmsysProcess_New(); cmsysProcess* cp = cmsysProcess_New();
// Set the command sequence. // Set the command sequence.
for (auto const& cmd : cmds) { for (std::vector<std::string> const& cmd : arguments.Commands) {
cmsysProcess_AddCommand(cp, cmd.data()); std::vector<const char*> argv(cmd.size() + 1);
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 (!working_directory.empty()) { if (!arguments.WorkingDirectory.empty()) {
cmsysProcess_SetWorkingDirectory(cp, working_directory.c_str()); cmsysProcess_SetWorkingDirectory(cp, arguments.WorkingDirectory.c_str());
} }
// Always hide the process window. // Always hide the process window.
@@ -206,22 +140,24 @@ bool cmExecuteProcessCommand::InitialPass(std::vector<std::string> const& args,
// Check the output variables. // Check the output variables.
bool merge_output = false; bool merge_output = false;
if (!input_file.empty()) { if (!arguments.InputFile.empty()) {
cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDIN, input_file.c_str()); cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDIN,
arguments.InputFile.c_str());
} }
if (!output_file.empty()) { if (!arguments.OutputFile.empty()) {
cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDOUT, cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDOUT,
output_file.c_str()); arguments.OutputFile.c_str());
} }
if (!error_file.empty()) { if (!arguments.ErrorFile.empty()) {
if (error_file == output_file) { if (arguments.ErrorFile == arguments.OutputFile) {
merge_output = true; merge_output = true;
} else { } else {
cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDERR, cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDERR,
error_file.c_str()); arguments.ErrorFile.c_str());
} }
} }
if (!output_variable.empty() && output_variable == error_variable) { if (!arguments.OutputVariable.empty() &&
arguments.OutputVariable == arguments.ErrorVariable) {
merge_output = true; merge_output = true;
} }
if (merge_output) { if (merge_output) {
@@ -242,19 +178,20 @@ bool cmExecuteProcessCommand::InitialPass(std::vector<std::string> const& args,
int length; int length;
char* data; char* data;
int p; int p;
cmProcessOutput processOutput(encoding); cmProcessOutput processOutput(
cmProcessOutput::FindEncoding(arguments.Encoding));
std::string strdata; std::string strdata;
while ((p = cmsysProcess_WaitForData(cp, &data, &length, nullptr))) { while ((p = cmsysProcess_WaitForData(cp, &data, &length, nullptr))) {
// Put the output in the right place. // Put the output in the right place.
if (p == cmsysProcess_Pipe_STDOUT && !output_quiet) { if (p == cmsysProcess_Pipe_STDOUT && !arguments.OutputQuiet) {
if (output_variable.empty()) { if (arguments.OutputVariable.empty()) {
processOutput.DecodeText(data, length, strdata, 1); processOutput.DecodeText(data, length, strdata, 1);
cmSystemTools::Stdout(strdata); cmSystemTools::Stdout(strdata);
} else { } else {
cmExecuteProcessCommandAppend(tempOutput, data, length); cmExecuteProcessCommandAppend(tempOutput, data, length);
} }
} else if (p == cmsysProcess_Pipe_STDERR && !error_quiet) { } else if (p == cmsysProcess_Pipe_STDERR && !arguments.ErrorQuiet) {
if (error_variable.empty()) { if (arguments.ErrorVariable.empty()) {
processOutput.DecodeText(data, length, strdata, 2); processOutput.DecodeText(data, length, strdata, 2);
cmSystemTools::Stderr(strdata); cmSystemTools::Stderr(strdata);
} else { } else {
@@ -262,13 +199,13 @@ bool cmExecuteProcessCommand::InitialPass(std::vector<std::string> const& args,
} }
} }
} }
if (!output_quiet && output_variable.empty()) { if (!arguments.OutputQuiet && arguments.OutputVariable.empty()) {
processOutput.DecodeText(std::string(), strdata, 1); processOutput.DecodeText(std::string(), strdata, 1);
if (!strdata.empty()) { if (!strdata.empty()) {
cmSystemTools::Stdout(strdata); cmSystemTools::Stdout(strdata);
} }
} }
if (!error_quiet && error_variable.empty()) { if (!arguments.ErrorQuiet && arguments.ErrorVariable.empty()) {
processOutput.DecodeText(std::string(), strdata, 2); processOutput.DecodeText(std::string(), strdata, 2);
if (!strdata.empty()) { if (!strdata.empty()) {
cmSystemTools::Stderr(strdata); cmSystemTools::Stderr(strdata);
@@ -281,46 +218,49 @@ bool cmExecuteProcessCommand::InitialPass(std::vector<std::string> const& args,
processOutput.DecodeText(tempError, tempError); processOutput.DecodeText(tempError, tempError);
// Fix the text in the output strings. // Fix the text in the output strings.
cmExecuteProcessCommandFixText(tempOutput, output_strip_trailing_whitespace); cmExecuteProcessCommandFixText(tempOutput,
cmExecuteProcessCommandFixText(tempError, error_strip_trailing_whitespace); arguments.OutputStripTrailingWhitespace);
cmExecuteProcessCommandFixText(tempError,
arguments.ErrorStripTrailingWhitespace);
// Store the output obtained. // Store the output obtained.
if (!output_variable.empty() && !tempOutput.empty()) { if (!arguments.OutputVariable.empty() && !tempOutput.empty()) {
this->Makefile->AddDefinition(output_variable, tempOutput.data()); this->Makefile->AddDefinition(arguments.OutputVariable, tempOutput.data());
} }
if (!merge_output && !error_variable.empty() && !tempError.empty()) { if (!merge_output && !arguments.ErrorVariable.empty() &&
this->Makefile->AddDefinition(error_variable, tempError.data()); !tempError.empty()) {
this->Makefile->AddDefinition(arguments.ErrorVariable, tempError.data());
} }
// Store the result of running the process. // Store the result of running the process.
if (!result_variable.empty()) { if (!arguments.ResultVariable.empty()) {
switch (cmsysProcess_GetState(cp)) { switch (cmsysProcess_GetState(cp)) {
case cmsysProcess_State_Exited: { case cmsysProcess_State_Exited: {
int v = cmsysProcess_GetExitValue(cp); int v = cmsysProcess_GetExitValue(cp);
char buf[16]; char buf[16];
sprintf(buf, "%d", v); sprintf(buf, "%d", v);
this->Makefile->AddDefinition(result_variable, buf); this->Makefile->AddDefinition(arguments.ResultVariable, buf);
} break; } break;
case cmsysProcess_State_Exception: case cmsysProcess_State_Exception:
this->Makefile->AddDefinition(result_variable, this->Makefile->AddDefinition(arguments.ResultVariable,
cmsysProcess_GetExceptionString(cp)); cmsysProcess_GetExceptionString(cp));
break; break;
case cmsysProcess_State_Error: case cmsysProcess_State_Error:
this->Makefile->AddDefinition(result_variable, this->Makefile->AddDefinition(arguments.ResultVariable,
cmsysProcess_GetErrorString(cp)); cmsysProcess_GetErrorString(cp));
break; break;
case cmsysProcess_State_Expired: case cmsysProcess_State_Expired:
this->Makefile->AddDefinition(result_variable, this->Makefile->AddDefinition(arguments.ResultVariable,
"Process terminated due to timeout"); "Process terminated due to timeout");
break; break;
} }
} }
// Store the result of running the processes. // Store the result of running the processes.
if (!results_variable.empty()) { if (!arguments.ResultsVariable.empty()) {
switch (cmsysProcess_GetState(cp)) { switch (cmsysProcess_GetState(cp)) {
case cmsysProcess_State_Exited: { case cmsysProcess_State_Exited: {
std::vector<std::string> res; std::vector<std::string> res;
for (size_t i = 0; i < cmds.size(); ++i) { for (size_t i = 0; i < arguments.Commands.size(); ++i) {
switch (cmsysProcess_GetStateByIndex(cp, static_cast<int>(i))) { switch (cmsysProcess_GetStateByIndex(cp, static_cast<int>(i))) {
case kwsysProcess_StateByIndex_Exited: { case kwsysProcess_StateByIndex_Exited: {
int exitCode = int exitCode =
@@ -339,19 +279,19 @@ bool cmExecuteProcessCommand::InitialPass(std::vector<std::string> const& args,
break; break;
} }
} }
this->Makefile->AddDefinition(results_variable, this->Makefile->AddDefinition(arguments.ResultsVariable,
cmJoin(res, ";").c_str()); cmJoin(res, ";").c_str());
} break; } break;
case cmsysProcess_State_Exception: case cmsysProcess_State_Exception:
this->Makefile->AddDefinition(results_variable, this->Makefile->AddDefinition(arguments.ResultsVariable,
cmsysProcess_GetExceptionString(cp)); cmsysProcess_GetExceptionString(cp));
break; break;
case cmsysProcess_State_Error: case cmsysProcess_State_Error:
this->Makefile->AddDefinition(results_variable, this->Makefile->AddDefinition(arguments.ResultsVariable,
cmsysProcess_GetErrorString(cp)); cmsysProcess_GetErrorString(cp));
break; break;
case cmsysProcess_State_Expired: case cmsysProcess_State_Expired:
this->Makefile->AddDefinition(results_variable, this->Makefile->AddDefinition(arguments.ResultsVariable,
"Process terminated due to timeout"); "Process terminated due to timeout");
break; break;
} }