cmake/Source/cmExecProgramCommand.cxx

291 lines
7.9 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmExecProgramCommand.h"
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
#include <cstdio>
2017-07-20 19:35:53 +02:00
#include "cmsys/Process.h"
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmProcessOutput.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
using Encoding = cmProcessOutput::Encoding;
namespace {
bool RunCommand(std::string command, std::string& output, int& retVal,
const char* directory = nullptr, bool verbose = true,
Encoding encoding = cmProcessOutput::Auto);
}
2014-08-03 19:52:23 +02:00
// cmExecProgramCommand
2020-02-01 23:06:01 +01:00
bool cmExecProgramCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
std::string arguments;
bool doingargs = false;
int count = 0;
std::string output_variable;
bool haveoutput_variable = false;
std::string return_variable;
bool havereturn_variable = false;
2018-01-26 17:06:56 +01:00
for (std::string const& arg : args) {
if (arg == "OUTPUT_VARIABLE") {
count++;
doingargs = false;
havereturn_variable = false;
haveoutput_variable = true;
2016-07-09 11:21:54 +02:00
} else if (haveoutput_variable) {
if (!output_variable.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
output_variable = arg;
haveoutput_variable = false;
2016-07-09 11:21:54 +02:00
count++;
2018-01-26 17:06:56 +01:00
} else if (arg == "RETURN_VALUE") {
count++;
doingargs = false;
haveoutput_variable = false;
havereturn_variable = true;
2016-07-09 11:21:54 +02:00
} else if (havereturn_variable) {
if (!return_variable.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
return_variable = arg;
havereturn_variable = false;
2016-07-09 11:21:54 +02:00
count++;
2018-01-26 17:06:56 +01:00
} else if (arg == "ARGS") {
count++;
havereturn_variable = false;
haveoutput_variable = false;
doingargs = true;
2016-07-09 11:21:54 +02:00
} else if (doingargs) {
2018-01-26 17:06:56 +01:00
arguments += arg;
arguments += " ";
count++;
}
2016-07-09 11:21:54 +02:00
}
std::string command;
2016-07-09 11:21:54 +02:00
if (!arguments.empty()) {
2020-02-01 23:06:01 +01:00
command = cmStrCat(cmSystemTools::ConvertToRunCommandPath(args[0]), ' ',
arguments);
2016-07-09 11:21:54 +02:00
} else {
command = args[0];
2016-07-09 11:21:54 +02:00
}
bool verbose = true;
2016-07-09 11:21:54 +02:00
if (!output_variable.empty()) {
verbose = false;
2016-07-09 11:21:54 +02:00
}
int retVal = 0;
std::string output;
bool result = true;
2016-07-09 11:21:54 +02:00
if (args.size() - count == 2) {
2018-04-23 21:13:27 +02:00
cmSystemTools::MakeDirectory(args[1]);
2020-02-01 23:06:01 +01:00
result = RunCommand(command, output, retVal, args[1].c_str(), verbose);
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
result = RunCommand(command, output, retVal, nullptr, verbose);
2016-07-09 11:21:54 +02:00
}
if (!result) {
retVal = -1;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (!output_variable.empty()) {
std::string::size_type first = output.find_first_not_of(" \n\t\r");
std::string::size_type last = output.find_last_not_of(" \n\t\r");
2016-07-09 11:21:54 +02:00
if (first == std::string::npos) {
first = 0;
2016-07-09 11:21:54 +02:00
}
if (last == std::string::npos) {
last = output.size() - 1;
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
std::string coutput = std::string(output, first, last - first + 1);
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(output_variable, coutput);
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (!return_variable.empty()) {
char buffer[100];
sprintf(buffer, "%d", retVal);
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(return_variable, buffer);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return true;
}
2020-02-01 23:06:01 +01:00
namespace {
bool RunCommand(std::string command, std::string& output, int& retVal,
const char* dir, bool verbose, Encoding encoding)
2014-08-03 19:52:23 +02:00
{
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetRunCommandOutput()) {
2014-08-03 19:52:23 +02:00
verbose = false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2015-11-17 17:22:37 +01:00
#if defined(_WIN32) && !defined(__CYGWIN__)
2014-08-03 19:52:23 +02:00
// if the command does not start with a quote, then
// try to find the program, and if the program can not be
// found use system to run the command as it must be a built in
// shell command like echo or dir
2019-11-11 23:01:05 +01:00
if (!command.empty() && command[0] == '\"') {
2014-08-03 19:52:23 +02:00
// count the number of quotes
2019-11-11 23:01:05 +01:00
int count = 0;
for (char c : command) {
if (c == '\"') {
2014-08-03 19:52:23 +02:00
count++;
2016-07-09 11:21:54 +02:00
if (count > 2) {
2014-08-03 19:52:23 +02:00
break;
}
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// if there are more than two double quotes use
// GetShortPathName, the cmd.exe program in windows which
// is used by system fails to execute if there are more than
// one set of quotes in the arguments
2016-07-09 11:21:54 +02:00
if (count > 2) {
2014-08-03 19:52:23 +02:00
cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
2016-07-09 11:21:54 +02:00
if (quoted.find(command)) {
2019-11-11 23:01:05 +01:00
std::string shortCmd;
2014-08-03 19:52:23 +02:00
std::string cmd = quoted.match(1);
std::string args = quoted.match(2);
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileExists(cmd)) {
2014-08-03 19:52:23 +02:00
shortCmd = cmd;
2019-11-11 23:01:05 +01:00
} else if (!cmSystemTools::GetShortPath(cmd, shortCmd)) {
cmSystemTools::Error("GetShortPath failed for " + cmd);
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
shortCmd += " ";
shortCmd += args;
2019-11-11 23:01:05 +01:00
command = shortCmd;
2016-07-09 11:21:54 +02:00
} else {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Could not parse command line with quotes " +
2014-08-03 19:52:23 +02:00
command);
}
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
#endif
// Allocate a process instance.
cmsysProcess* cp = cmsysProcess_New();
2016-07-09 11:21:54 +02:00
if (!cp) {
2014-08-03 19:52:23 +02:00
cmSystemTools::Error("Error allocating process instance.");
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2015-11-17 17:22:37 +01:00
#if defined(_WIN32) && !defined(__CYGWIN__)
2016-07-09 11:21:54 +02:00
if (dir) {
2014-08-03 19:52:23 +02:00
cmsysProcess_SetWorkingDirectory(cp, dir);
2016-07-09 11:21:54 +02:00
}
if (cmSystemTools::GetRunCommandHideConsole()) {
2014-08-03 19:52:23 +02:00
cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
2019-11-11 23:01:05 +01:00
const char* cmd[] = { command.c_str(), nullptr };
2014-08-03 19:52:23 +02:00
cmsysProcess_SetCommand(cp, cmd);
#else
std::string commandInDir;
2016-07-09 11:21:54 +02:00
if (dir) {
2020-02-01 23:06:01 +01:00
commandInDir = cmStrCat("cd \"", dir, "\" && ", command);
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
commandInDir = command;
2016-07-09 11:21:54 +02:00
}
2018-08-09 18:06:22 +02:00
# ifndef __VMS
2014-08-03 19:52:23 +02:00
commandInDir += " 2>&1";
2018-08-09 18:06:22 +02:00
# endif
2019-11-11 23:01:05 +01:00
command = commandInDir;
2016-07-09 11:21:54 +02:00
if (verbose) {
2014-08-03 19:52:23 +02:00
cmSystemTools::Stdout("running ");
cmSystemTools::Stdout(command);
cmSystemTools::Stdout("\n");
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
fflush(stdout);
fflush(stderr);
2019-11-11 23:01:05 +01:00
const char* cmd[] = { "/bin/sh", "-c", command.c_str(), nullptr };
2014-08-03 19:52:23 +02:00
cmsysProcess_SetCommand(cp, cmd);
#endif
cmsysProcess_Execute(cp);
// Read the process output.
int length;
char* data;
int p;
2017-04-14 19:02:05 +02:00
cmProcessOutput processOutput(encoding);
std::string strdata;
2019-11-11 23:01:05 +01:00
while ((p = cmsysProcess_WaitForData(cp, &data, &length, nullptr))) {
2016-07-09 11:21:54 +02:00
if (p == cmsysProcess_Pipe_STDOUT || p == cmsysProcess_Pipe_STDERR) {
if (verbose) {
2017-04-14 19:02:05 +02:00
processOutput.DecodeText(data, length, strdata);
2019-11-11 23:01:05 +01:00
cmSystemTools::Stdout(strdata);
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
output.append(data, length);
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2017-04-14 19:02:05 +02:00
if (verbose) {
processOutput.DecodeText(std::string(), strdata);
if (!strdata.empty()) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Stdout(strdata);
2017-04-14 19:02:05 +02:00
}
}
2014-08-03 19:52:23 +02:00
// All output has been read. Wait for the process to exit.
2018-01-26 17:06:56 +01:00
cmsysProcess_WaitForExit(cp, nullptr);
2017-04-14 19:02:05 +02:00
processOutput.DecodeText(output, output);
2014-08-03 19:52:23 +02:00
// Check the result of running the process.
std::string msg;
2016-07-09 11:21:54 +02:00
switch (cmsysProcess_GetState(cp)) {
2014-08-03 19:52:23 +02:00
case cmsysProcess_State_Exited:
retVal = cmsysProcess_GetExitValue(cp);
break;
case cmsysProcess_State_Exception:
retVal = -1;
msg += "\nProcess terminated due to: ";
msg += cmsysProcess_GetExceptionString(cp);
break;
case cmsysProcess_State_Error:
retVal = -1;
msg += "\nProcess failed because: ";
msg += cmsysProcess_GetErrorString(cp);
break;
case cmsysProcess_State_Expired:
retVal = -1;
msg += "\nProcess terminated due to timeout.";
break;
2016-07-09 11:21:54 +02:00
}
if (!msg.empty()) {
2015-11-17 17:22:37 +01:00
#if defined(_WIN32) && !defined(__CYGWIN__)
2014-08-03 19:52:23 +02:00
// Old Windows process execution printed this info.
msg += "\n\nfor command: ";
msg += command;
2016-07-09 11:21:54 +02:00
if (dir) {
2014-08-03 19:52:23 +02:00
msg += "\nin dir: ";
msg += dir;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
msg += "\n";
2016-07-09 11:21:54 +02:00
if (verbose) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Stdout(msg);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
output += msg;
#else
// Old UNIX process execution only put message in output.
output += msg;
#endif
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Delete the process instance.
cmsysProcess_Delete(cp);
return true;
}
2020-02-01 23:06:01 +01:00
}