2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2001-05-04 19:34:59 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2001-05-04 19:34:59 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2001-05-08 02:11:16 +04:00
|
|
|
#include "cmExecProgramCommand.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2001-05-04 19:34:59 +04:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
|
2013-10-18 23:47:17 +04:00
|
|
|
#include <cmsys/Process.h>
|
|
|
|
|
2001-05-08 02:11:16 +04:00
|
|
|
// cmExecProgramCommand
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmExecProgramCommand::InitialPass(std::vector<std::string> const& args,
|
|
|
|
cmExecutionStatus&)
|
2001-05-04 19:34:59 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 1) {
|
2001-05-04 19:34:59 +04:00
|
|
|
this->SetError("called with incorrect number of arguments");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-04-04 01:14:06 +04:00
|
|
|
std::string arguments;
|
|
|
|
bool doingargs = false;
|
|
|
|
int count = 0;
|
2002-08-08 23:29:19 +04:00
|
|
|
std::string output_variable;
|
|
|
|
bool haveoutput_variable = false;
|
|
|
|
std::string return_variable;
|
|
|
|
bool havereturn_variable = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (size_t i = 0; i < args.size(); ++i) {
|
|
|
|
if (args[i] == "OUTPUT_VARIABLE") {
|
2002-08-08 23:13:20 +04:00
|
|
|
count++;
|
|
|
|
doingargs = false;
|
2002-08-08 23:29:19 +04:00
|
|
|
havereturn_variable = false;
|
|
|
|
haveoutput_variable = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (haveoutput_variable) {
|
|
|
|
if (!output_variable.empty()) {
|
2002-08-08 23:13:20 +04:00
|
|
|
this->SetError("called with incorrect number of arguments");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-08-08 23:29:19 +04:00
|
|
|
output_variable = args[i];
|
2002-10-24 00:57:21 +04:00
|
|
|
haveoutput_variable = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
count++;
|
|
|
|
} else if (args[i] == "RETURN_VALUE") {
|
2002-04-04 01:14:06 +04:00
|
|
|
count++;
|
2002-08-08 23:29:19 +04:00
|
|
|
doingargs = false;
|
|
|
|
haveoutput_variable = false;
|
|
|
|
havereturn_variable = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (havereturn_variable) {
|
|
|
|
if (!return_variable.empty()) {
|
2002-08-08 23:29:19 +04:00
|
|
|
this->SetError("called with incorrect number of arguments");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-08-08 23:29:19 +04:00
|
|
|
return_variable = args[i];
|
2002-10-24 00:57:21 +04:00
|
|
|
havereturn_variable = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
count++;
|
|
|
|
} else if (args[i] == "ARGS") {
|
2002-04-04 01:14:06 +04:00
|
|
|
count++;
|
2002-08-08 23:29:19 +04:00
|
|
|
havereturn_variable = false;
|
|
|
|
haveoutput_variable = false;
|
2002-04-04 01:14:06 +04:00
|
|
|
doingargs = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (doingargs) {
|
2002-08-08 23:29:19 +04:00
|
|
|
arguments += args[i];
|
|
|
|
arguments += " ";
|
|
|
|
count++;
|
2002-04-04 01:14:06 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-04-04 01:14:06 +04:00
|
|
|
|
|
|
|
std::string command;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!arguments.empty()) {
|
2004-06-24 00:34:38 +04:00
|
|
|
command = cmSystemTools::ConvertToRunCommandPath(args[0].c_str());
|
2002-04-04 01:14:06 +04:00
|
|
|
command += " ";
|
|
|
|
command += arguments;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2002-04-04 01:14:06 +04:00
|
|
|
command = args[0];
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-10-24 00:57:21 +04:00
|
|
|
bool verbose = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!output_variable.empty()) {
|
2002-10-24 00:57:21 +04:00
|
|
|
verbose = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-08-08 23:29:19 +04:00
|
|
|
int retVal = 0;
|
2001-05-04 19:34:59 +04:00
|
|
|
std::string output;
|
2004-06-24 00:34:38 +04:00
|
|
|
bool result = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() - count == 2) {
|
2001-05-08 02:11:16 +04:00
|
|
|
cmSystemTools::MakeDirectory(args[1].c_str());
|
2013-10-18 23:47:17 +04:00
|
|
|
result = cmExecProgramCommand::RunCommand(command.c_str(), output, retVal,
|
|
|
|
args[1].c_str(), verbose);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
result = cmExecProgramCommand::RunCommand(command.c_str(), output, retVal,
|
2016-06-27 23:44:16 +03:00
|
|
|
CM_NULLPTR, verbose);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!result) {
|
2004-06-24 16:57:54 +04:00
|
|
|
retVal = -1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-08-08 23:13:20 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!output_variable.empty()) {
|
2002-08-08 23:13:20 +04:00
|
|
|
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-05-16 17:34:04 +03:00
|
|
|
if (first == std::string::npos) {
|
2002-11-08 23:46:08 +03:00
|
|
|
first = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (last == std::string::npos) {
|
|
|
|
last = output.size() - 1;
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string coutput = std::string(output, first, last - first + 1);
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(output_variable, coutput.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-08-08 23:29:19 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!return_variable.empty()) {
|
2002-08-08 23:29:19 +04:00
|
|
|
char buffer[100];
|
|
|
|
sprintf(buffer, "%d", retVal);
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(return_variable, buffer);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2004-06-24 16:57:54 +04:00
|
|
|
return true;
|
2001-05-04 19:34:59 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmExecProgramCommand::RunCommand(const char* command, std::string& output,
|
|
|
|
int& retVal, const char* dir,
|
2013-10-18 23:47:17 +04:00
|
|
|
bool verbose)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::GetRunCommandOutput()) {
|
2013-10-18 23:47:17 +04:00
|
|
|
verbose = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
|
2015-06-15 16:56:52 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2013-10-18 23:47:17 +04: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
|
|
|
|
int count = 0;
|
|
|
|
std::string shortCmd;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (command[0] == '\"') {
|
2013-10-18 23:47:17 +04:00
|
|
|
// count the number of quotes
|
2016-05-16 17:34:04 +03:00
|
|
|
for (const char* s = command; *s != 0; ++s) {
|
|
|
|
if (*s == '\"') {
|
2013-10-18 23:47:17 +04:00
|
|
|
count++;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (count > 2) {
|
2013-10-18 23:47:17 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04: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-05-16 17:34:04 +03:00
|
|
|
if (count > 2) {
|
2013-10-18 23:47:17 +04:00
|
|
|
cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (quoted.find(command)) {
|
2013-10-18 23:47:17 +04:00
|
|
|
std::string cmd = quoted.match(1);
|
|
|
|
std::string args = quoted.match(2);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::FileExists(cmd.c_str())) {
|
2013-10-18 23:47:17 +04:00
|
|
|
shortCmd = cmd;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd)) {
|
|
|
|
cmSystemTools::Error("GetShortPath failed for ", cmd.c_str());
|
2013-10-18 23:47:17 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
shortCmd += " ";
|
|
|
|
shortCmd += args;
|
|
|
|
|
|
|
|
command = shortCmd.c_str();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-10-18 23:47:17 +04:00
|
|
|
cmSystemTools::Error("Could not parse command line with quotes ",
|
|
|
|
command);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Allocate a process instance.
|
|
|
|
cmsysProcess* cp = cmsysProcess_New();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cp) {
|
2013-10-18 23:47:17 +04:00
|
|
|
cmSystemTools::Error("Error allocating process instance.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
|
2015-06-15 16:56:52 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (dir) {
|
2013-10-18 23:47:17 +04:00
|
|
|
cmsysProcess_SetWorkingDirectory(cp, dir);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (cmSystemTools::GetRunCommandHideConsole()) {
|
2013-10-18 23:47:17 +04:00
|
|
|
cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* cmd[] = { command, 0 };
|
2013-10-18 23:47:17 +04:00
|
|
|
cmsysProcess_SetCommand(cp, cmd);
|
|
|
|
#else
|
|
|
|
std::string commandInDir;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (dir) {
|
2013-10-18 23:47:17 +04:00
|
|
|
commandInDir = "cd \"";
|
|
|
|
commandInDir += dir;
|
|
|
|
commandInDir += "\" && ";
|
|
|
|
commandInDir += command;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-10-18 23:47:17 +04:00
|
|
|
commandInDir = command;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
#ifndef __VMS
|
|
|
|
commandInDir += " 2>&1";
|
|
|
|
#endif
|
|
|
|
command = commandInDir.c_str();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (verbose) {
|
2013-10-18 23:47:17 +04:00
|
|
|
cmSystemTools::Stdout("running ");
|
|
|
|
cmSystemTools::Stdout(command);
|
|
|
|
cmSystemTools::Stdout("\n");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2016-06-27 23:44:16 +03:00
|
|
|
const char* cmd[] = { "/bin/sh", "-c", command, CM_NULLPTR };
|
2013-10-18 23:47:17 +04:00
|
|
|
cmsysProcess_SetCommand(cp, cmd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cmsysProcess_Execute(cp);
|
|
|
|
|
|
|
|
// Read the process output.
|
|
|
|
int length;
|
|
|
|
char* data;
|
|
|
|
int p;
|
2016-06-27 23:44:16 +03:00
|
|
|
while ((p = cmsysProcess_WaitForData(cp, &data, &length, CM_NULLPTR), p)) {
|
2016-05-16 17:34:04 +03:00
|
|
|
if (p == cmsysProcess_Pipe_STDOUT || p == cmsysProcess_Pipe_STDERR) {
|
|
|
|
if (verbose) {
|
2013-10-18 23:47:17 +04:00
|
|
|
cmSystemTools::Stdout(data, length);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
output.append(data, length);
|
2013-10-18 23:47:17 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
|
|
|
|
// All output has been read. Wait for the process to exit.
|
2016-06-27 23:44:16 +03:00
|
|
|
cmsysProcess_WaitForExit(cp, CM_NULLPTR);
|
2013-10-18 23:47:17 +04:00
|
|
|
|
|
|
|
// Check the result of running the process.
|
|
|
|
std::string msg;
|
2016-05-16 17:34:04 +03:00
|
|
|
switch (cmsysProcess_GetState(cp)) {
|
2013-10-18 23:47:17 +04: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-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!msg.empty()) {
|
2015-06-15 16:56:52 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2013-10-18 23:47:17 +04:00
|
|
|
// Old Windows process execution printed this info.
|
|
|
|
msg += "\n\nfor command: ";
|
|
|
|
msg += command;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (dir) {
|
2013-10-18 23:47:17 +04:00
|
|
|
msg += "\nin dir: ";
|
|
|
|
msg += dir;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
msg += "\n";
|
2016-05-16 17:34:04 +03:00
|
|
|
if (verbose) {
|
2013-10-18 23:47:17 +04:00
|
|
|
cmSystemTools::Stdout(msg.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
output += msg;
|
|
|
|
#else
|
|
|
|
// Old UNIX process execution only put message in output.
|
|
|
|
output += msg;
|
|
|
|
#endif
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-18 23:47:17 +04:00
|
|
|
|
|
|
|
// Delete the process instance.
|
|
|
|
cmsysProcess_Delete(cp);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|