cmSystemTools: Teach RunSingleCommand to separate stdout and stderr

Extend the RunSingleCommand signature to capture stdout and stderr
separately.  Allow both to be captured to the same std::string
to preserve existing behavior.  Update all call sites to do this
so that this refactoring does not introduce functional changes.
This commit is contained in:
Brad King 2015-04-20 15:36:57 -04:00
parent f438cd3731
commit 356c26ebdf
23 changed files with 110 additions and 69 deletions

View File

@ -92,7 +92,8 @@ int cmCPackIFWGenerator::PackageFiles()
cmCPackLogger(cmCPackLog::LOG_OUTPUT,
"- Generate repository" << std::endl);
bool res = cmSystemTools::RunSingleCommand(
ifwCmd.c_str(), &output, &retVal, 0, this->GeneratorVerbose, 0);
ifwCmd.c_str(), &output, &output,
&retVal, 0, this->GeneratorVerbose, 0);
if ( !res || retVal )
{
cmGeneratedFileStream ofs(ifwTmpFile.c_str());
@ -176,7 +177,8 @@ int cmCPackIFWGenerator::PackageFiles()
int retVal = 1;
cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Generate package" << std::endl);
bool res = cmSystemTools::RunSingleCommand(
ifwCmd.c_str(), &output, &retVal, 0, this->GeneratorVerbose, 0);
ifwCmd.c_str(), &output, &output,
&retVal, 0, this->GeneratorVerbose, 0);
if ( !res || retVal )
{
cmGeneratedFileStream ofs(ifwTmpFile.c_str());

View File

@ -64,7 +64,8 @@ bool cmCPackWIXGenerator::RunWiXCommand(std::string const& command)
std::string output;
int returnValue = 0;
bool status = cmSystemTools::RunSingleCommand(command.c_str(), &output,
bool status = cmSystemTools::RunSingleCommand(
command.c_str(), &output, &output,
&returnValue, 0, cmSystemTools::OUTPUT_NONE);
cmsys::ofstream logFile(logFileName.c_str(), std::ios::app);

View File

@ -466,7 +466,7 @@ int cmCPackDebGenerator::createDeb()
std::string output;
int retval = -1;
int res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
int res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output, &output,
&retval, this->GetOption("WDIR"), this->GeneratorVerbose, 0);
if ( !res || retval )
@ -505,7 +505,7 @@ int cmCPackDebGenerator::createDeb()
cmd += "\"";
//std::string output;
//int retVal = -1;
res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output, &output,
&retval, toplevel.c_str(), this->GeneratorVerbose, 0);
if ( !res || retval )
{
@ -553,7 +553,7 @@ int cmCPackDebGenerator::createDeb()
}
}
}
res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output, &output,
&retval, this->GetOption("WDIR"), this->GeneratorVerbose, 0);
if ( !res || retval )

View File

@ -197,7 +197,7 @@ bool cmCPackDragNDropGenerator::RunCommand(std::ostringstream& command,
bool result = cmSystemTools::RunSingleCommand(
command.str().c_str(),
output,
output, output,
&exit_code,
0,
this->GeneratorVerbose,

View File

@ -290,7 +290,8 @@ int cmCPackGenerator::InstallProjectViaInstallCommands(
<< std::endl);
std::string output;
int retVal = 1;
bool resB = cmSystemTools::RunSingleCommand(it->c_str(), &output,
bool resB = cmSystemTools::RunSingleCommand(
it->c_str(), &output, &output,
&retVal, 0, this->GeneratorVerbose, 0);
if ( !resB || retVal )
{
@ -668,7 +669,7 @@ int cmCPackGenerator::InstallProjectViaInstallCMakeProjects(
int retVal = 1;
bool resB =
cmSystemTools::RunSingleCommand(buildCommand.c_str(),
&output,
&output, &output,
&retVal,
installDirectory.c_str(),
this->GeneratorVerbose, 0);

View File

@ -324,7 +324,7 @@ int cmCPackNSISGenerator::PackageFiles()
<< std::endl);
std::string output;
int retVal = 1;
bool res = cmSystemTools::RunSingleCommand(nsisCmd.c_str(), &output,
bool res = cmSystemTools::RunSingleCommand(nsisCmd.c_str(), &output, &output,
&retVal, 0, this->GeneratorVerbose, 0);
if ( !res || retVal )
{
@ -430,8 +430,8 @@ int cmCPackNSISGenerator::InitializeInternal()
<< nsisCmd << std::endl);
std::string output;
int retVal = 1;
bool resS = cmSystemTools::RunSingleCommand(nsisCmd.c_str(),
&output, &retVal, 0, this->GeneratorVerbose, 0);
bool resS = cmSystemTools::RunSingleCommand(
nsisCmd.c_str(), &output, &output, &retVal, 0, this->GeneratorVerbose, 0);
cmsys::RegularExpression versionRex("v([0-9]+.[0-9]+)");
cmsys::RegularExpression versionRexCVS("v(.*)\\.cvs");
if ( !resS || retVal ||
@ -836,9 +836,9 @@ CreateComponentDescription(cmCPackComponent *component,
zipListFileName.c_str());
std::string output;
int retVal = -1;
int res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output, &retVal,
dirName.c_str(),
cmSystemTools::OUTPUT_NONE, 0);
int res = cmSystemTools::RunSingleCommand(
cmd.c_str(), &output, &output,
&retVal, dirName.c_str(), cmSystemTools::OUTPUT_NONE, 0);
if ( !res || retVal )
{
std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");

View File

@ -180,9 +180,9 @@ int cmCPackOSXX11Generator::PackageFiles()
bool res = false;
while(numTries > 0)
{
res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
&retVal, 0,
this->GeneratorVerbose, 0);
res = cmSystemTools::RunSingleCommand(
dmgCmd.str().c_str(), &output, &output,
&retVal, 0, this->GeneratorVerbose, 0);
if ( res && !retVal )
{
numTries = -1;

View File

@ -378,9 +378,9 @@ int cmCPackPackageMakerGenerator::PackageFiles()
bool res = false;
while(numTries > 0)
{
res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
&retVal, 0, this->GeneratorVerbose,
0);
res = cmSystemTools::RunSingleCommand(
dmgCmd.str().c_str(), &output, &output,
&retVal, 0, this->GeneratorVerbose, 0);
if ( res && !retVal )
{
numTries = -1;
@ -657,8 +657,9 @@ bool cmCPackPackageMakerGenerator::RunPackageMaker(const char *command,
cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << command << std::endl);
std::string output;
int retVal = 1;
bool res = cmSystemTools::RunSingleCommand(command, &output, &retVal, 0,
this->GeneratorVerbose, 0);
bool res = cmSystemTools::RunSingleCommand(
command, &output, &output,
&retVal, 0, this->GeneratorVerbose, 0);
cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running package maker"
<< std::endl);
if ( !res || retVal )

View File

@ -709,7 +709,8 @@ int cmCTestScriptHandler::CheckOutSourceDir()
output = "";
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"Run cvs: " << this->CVSCheckOut << std::endl);
res = cmSystemTools::RunSingleCommand(this->CVSCheckOut.c_str(), &output,
res = cmSystemTools::RunSingleCommand(
this->CVSCheckOut.c_str(), &output, &output,
&retVal, this->CTestRoot.c_str(), this->HandlerVerbose,
0 /*this->TimeOut*/);
if (!res || retVal != 0)
@ -789,7 +790,8 @@ int cmCTestScriptHandler::PerformExtraUpdates()
retVal = 0;
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run Update: "
<< fullCommand << std::endl);
res = cmSystemTools::RunSingleCommand(fullCommand.c_str(), &output,
res = cmSystemTools::RunSingleCommand(
fullCommand.c_str(), &output, &output,
&retVal, cvsArgs[0].c_str(),
this->HandlerVerbose, 0 /*this->TimeOut*/);
if (!res || retVal != 0)
@ -910,7 +912,8 @@ int cmCTestScriptHandler::RunConfigurationDashboard()
retVal = 0;
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run cmake command: "
<< command << std::endl);
res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
res = cmSystemTools::RunSingleCommand(
command.c_str(), &output, &output,
&retVal, this->BinaryDir.c_str(),
this->HandlerVerbose, 0 /*this->TimeOut*/);
@ -956,7 +959,8 @@ int cmCTestScriptHandler::RunConfigurationDashboard()
retVal = 0;
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run ctest command: "
<< command << std::endl);
res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
res = cmSystemTools::RunSingleCommand(
command.c_str(), &output, &output,
&retVal, this->BinaryDir.c_str(), this->HandlerVerbose,
0 /*this->TimeOut*/);

View File

@ -1334,7 +1334,7 @@ int cmCTestTestHandler::ExecuteCommands(std::vector<std::string>& vec)
int retVal = 0;
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run command: " <<
*it << std::endl, this->Quiet);
if ( !cmSystemTools::RunSingleCommand(it->c_str(), 0, &retVal, 0,
if ( !cmSystemTools::RunSingleCommand(it->c_str(), 0, 0, &retVal, 0,
cmSystemTools::OUTPUT_MERGE
/*this->Verbose*/) || retVal != 0 )
{

View File

@ -49,7 +49,7 @@ bool cmBuildNameCommand
if(this->Makefile->GetDefinition("UNIX"))
{
buildname = "";
cmSystemTools::RunSingleCommand("uname -a", &buildname);
cmSystemTools::RunSingleCommand("uname -a", &buildname, &buildname);
if(!buildname.empty())
{
std::string RegExp = "([^ ]*) [^ ]* ([^ ]*) ";

View File

@ -1163,7 +1163,7 @@ cmExtraEclipseCDT4Generator::GetEclipsePath(const std::string& path)
#if defined(__CYGWIN__)
std::string cmd = "cygpath -m " + path;
std::string out;
if (!cmSystemTools::RunSingleCommand(cmd.c_str(), &out))
if (!cmSystemTools::RunSingleCommand(cmd.c_str(), &out, &out))
{
return path;
}

View File

@ -1737,7 +1737,7 @@ int cmGlobalGenerator::Build(
output += cmSystemTools::PrintSingleCommand(cleanCommand);
output += "\n";
if (!cmSystemTools::RunSingleCommand(cleanCommand, outputPtr,
if (!cmSystemTools::RunSingleCommand(cleanCommand, outputPtr, outputPtr,
&retVal, 0, outputflag, timeout))
{
cmSystemTools::SetRunCommandHideConsole(hideconsole);
@ -1758,7 +1758,7 @@ int cmGlobalGenerator::Build(
output += makeCommandStr;
output += "\n";
if (!cmSystemTools::RunSingleCommand(makeCommand, outputPtr,
if (!cmSystemTools::RunSingleCommand(makeCommand, outputPtr, outputPtr,
&retVal, 0, outputflag, timeout))
{
cmSystemTools::SetRunCommandHideConsole(hideconsole);

View File

@ -1233,7 +1233,7 @@ std::string cmGlobalNinjaGenerator::ninjaVersion() const
std::string version;
std::string command = ninjaCmd() + " --version";
cmSystemTools::RunSingleCommand(command.c_str(),
&version, 0, 0,
&version, 0, 0, 0,
cmSystemTools::OUTPUT_NONE);
return version;

View File

@ -164,8 +164,8 @@ cmGlobalGenerator* cmGlobalXCodeGenerator::Factory
{
std::string out;
std::string::size_type pos;
if(cmSystemTools::RunSingleCommand("xcode-select --print-path", &out, 0, 0,
cmSystemTools::OUTPUT_NONE) &&
if(cmSystemTools::RunSingleCommand("xcode-select --print-path", &out, 0,
0, 0, cmSystemTools::OUTPUT_NONE) &&
(pos = out.find(".app/"), pos != out.npos))
{
versionFile = out.substr(0, pos+5)+"Contents/version.plist";

View File

@ -188,7 +188,7 @@ std::string cmQtAutoGenerators::ListQt5RccInputs(cmSourceFile* sf,
std::string output;
int retVal = 0;
bool result = cmSystemTools::RunSingleCommand(command, &output,
bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
&retVal, 0,
cmSystemTools::OUTPUT_NONE);
if (!result || retVal)
@ -2196,7 +2196,8 @@ bool cmQtAutoGenerators::GenerateMoc(const std::string& sourceFile,
std::string output;
int retVal = 0;
bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
&retVal);
if (!result || retVal)
{
std::cerr << "AUTOGEN: error: process for " << mocFilePath <<" failed:\n"
@ -2265,7 +2266,8 @@ bool cmQtAutoGenerators::GenerateUi(const std::string& realName,
}
std::string output;
int retVal = 0;
bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
&retVal);
if (!result || retVal)
{
std::cerr << "AUTOUIC: error: process for " << ui_output_file <<
@ -2355,7 +2357,8 @@ bool cmQtAutoGenerators::GenerateQrc()
}
std::string output;
int retVal = 0;
bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
&retVal);
if (!result || retVal)
{
std::cerr << "AUTORCC: error: process for " << rcc_output_file <<

View File

@ -63,7 +63,7 @@ bool cmSiteNameCommand
{
std::string host;
cmSystemTools::RunSingleCommand(hostname_cmd.c_str(),
&host, 0, 0, cmSystemTools::OUTPUT_NONE);
&host, 0, 0, 0, cmSystemTools::OUTPUT_NONE);
// got the hostname
if (!host.empty())

View File

@ -659,7 +659,8 @@ std::vector<std::string> cmSystemTools::ParseArguments(const char* command)
bool cmSystemTools::RunSingleCommand(std::vector<std::string>const& command,
std::string* output ,
std::string* captureStdOut,
std::string* captureStdErr,
int* retVal , const char* dir ,
OutputOption outputflag ,
double timeout )
@ -671,9 +672,13 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string>const& command,
argv.push_back(a->c_str());
}
argv.push_back(0);
if ( output )
if ( captureStdOut )
{
*output = "";
*captureStdOut = "";
}
if (captureStdErr && captureStdErr != captureStdOut)
{
*captureStdErr = "";
}
cmsysProcess* cp = cmsysProcess_New();
@ -693,15 +698,17 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string>const& command,
cmsysProcess_SetTimeout(cp, timeout);
cmsysProcess_Execute(cp);
std::vector<char> tempOutput;
std::vector<char> tempStdOut;
std::vector<char> tempStdErr;
char* data;
int length;
int pipe;
if(outputflag != OUTPUT_PASSTHROUGH && (output || outputflag != OUTPUT_NONE))
if(outputflag != OUTPUT_PASSTHROUGH &&
(captureStdOut || captureStdErr || outputflag != OUTPUT_NONE))
{
while((pipe = cmsysProcess_WaitForData(cp, &data, &length, 0)) > 0)
{
if(output || outputflag != OUTPUT_NONE)
if(captureStdOut || captureStdErr || outputflag != OUTPUT_NONE)
{
// Translate NULL characters in the output into valid text.
// Visual Studio 7 puts these characters in the output of its
@ -714,9 +721,21 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string>const& command,
}
}
}
if ( output )
if(pipe == cmsysProcess_Pipe_STDOUT ||
(pipe == cmsysProcess_Pipe_STDERR &&
captureStdOut == captureStdErr))
{
tempOutput.insert(tempOutput.end(), data, data+length);
if (captureStdOut)
{
tempStdOut.insert(tempStdOut.end(), data, data+length);
}
}
else if(pipe == cmsysProcess_Pipe_STDERR)
{
if (captureStdErr)
{
tempStdErr.insert(tempStdErr.end(), data, data+length);
}
}
if(outputflag != OUTPUT_NONE)
{
@ -740,9 +759,14 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string>const& command,
}
cmsysProcess_WaitForExit(cp, 0);
if ( output && tempOutput.begin() != tempOutput.end())
if ( captureStdOut && tempStdOut.begin() != tempStdOut.end())
{
output->append(&*tempOutput.begin(), tempOutput.size());
captureStdOut->append(&*tempStdOut.begin(), tempStdOut.size());
}
if ( captureStdErr && captureStdErr != captureStdOut &&
tempStdErr.begin() != tempStdErr.end())
{
captureStdErr->append(&*tempStdErr.begin(), tempStdErr.size());
}
bool result = true;
@ -767,9 +791,9 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string>const& command,
{
std::cerr << exception_str << std::endl;
}
if ( output )
if ( captureStdErr )
{
output->append(exception_str, strlen(exception_str));
captureStdErr->append(exception_str, strlen(exception_str));
}
result = false;
}
@ -780,9 +804,9 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string>const& command,
{
std::cerr << error_str << std::endl;
}
if ( output )
if ( captureStdErr )
{
output->append(error_str, strlen(error_str));
captureStdErr->append(error_str, strlen(error_str));
}
result = false;
}
@ -793,9 +817,9 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string>const& command,
{
std::cerr << error_str << std::endl;
}
if ( output )
if ( captureStdErr )
{
output->append(error_str, strlen(error_str));
captureStdErr->append(error_str, strlen(error_str));
}
result = false;
}
@ -806,7 +830,8 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string>const& command,
bool cmSystemTools::RunSingleCommand(
const char* command,
std::string* output,
std::string* captureStdOut,
std::string* captureStdErr,
int *retVal,
const char* dir,
OutputOption outputflag,
@ -823,8 +848,8 @@ bool cmSystemTools::RunSingleCommand(
{
return false;
}
return cmSystemTools::RunSingleCommand(args, output,retVal,
dir, outputflag, timeout);
return cmSystemTools::RunSingleCommand(args, captureStdOut, captureStdErr,
retVal, dir, outputflag, timeout);
}
std::string

View File

@ -223,7 +223,9 @@ public:
OUTPUT_NORMAL,
OUTPUT_PASSTHROUGH
};
static bool RunSingleCommand(const char* command, std::string* output = 0,
static bool RunSingleCommand(const char* command,
std::string* captureStdOut = 0,
std::string* captureStdErr = 0,
int* retVal = 0, const char* dir = 0,
OutputOption outputflag = OUTPUT_MERGE,
double timeout = 0.0);
@ -233,7 +235,8 @@ public:
* be in comand[1]...command[command.size()]
*/
static bool RunSingleCommand(std::vector<std::string> const& command,
std::string* output = 0,
std::string* captureStdOut = 0,
std::string* captureStdErr = 0,
int* retVal = 0, const char* dir = 0,
OutputOption outputflag = OUTPUT_MERGE,
double timeout = 0.0);

View File

@ -224,7 +224,7 @@ void cmTryRunCommand::RunExecutable(const std::string& runArgs,
}
int timeout = 0;
bool worked = cmSystemTools::RunSingleCommand(finalCommand.c_str(),
out, &retVal,
out, out, &retVal,
0, cmSystemTools::OUTPUT_NONE, timeout);
// set the run var
char retChar[1000];

View File

@ -206,8 +206,9 @@ static int process( const std::string& srcfilename,
}
// run the command
int exit_code = 0;
bool run = cmSystemTools::RunSingleCommand(command, &output, &exit_code,
dir.c_str(), cmSystemTools::OUTPUT_NONE);
bool run = cmSystemTools::RunSingleCommand(command, &output, &output,
&exit_code, dir.c_str(),
cmSystemTools::OUTPUT_NONE);
// process the include directives and output everything else
std::stringstream ss(output);

View File

@ -267,7 +267,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
std::vector<std::string> cmd(ai, ae);
int retval;
if(cmSystemTools::RunSingleCommand(
cmd, 0, &retval, NULL, cmSystemTools::OUTPUT_PASSTHROUGH))
cmd, 0, 0, &retval, NULL, cmSystemTools::OUTPUT_PASSTHROUGH))
{
return retval;
}
@ -398,7 +398,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
time(&time_start);
clock_start = clock();
int ret =0;
cmSystemTools::RunSingleCommand(command.c_str(), 0, &ret);
cmSystemTools::RunSingleCommand(command.c_str(), 0, 0, &ret);
clock_finish = clock();
time(&time_finish);
@ -454,7 +454,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
std::string command = cmWrap('"', cmRange(args).advance(3), '"', " ");
int retval = 0;
int timeout = 0;
if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, 0, &retval,
directory.c_str(), cmSystemTools::OUTPUT_NORMAL, timeout) )
{
return retval;
@ -1350,7 +1350,7 @@ bool cmcmd::RunCommand(const char* comment,
int retCode =0;
// use rc command to create .res file
cmSystemTools::RunSingleCommand(command,
&output,
&output, &output,
&retCode, 0, cmSystemTools::OUTPUT_NONE);
// always print the output of the command, unless
// it is the dumb rc command banner, but if the command

View File

@ -130,7 +130,7 @@ int main ()
std::vector<std::string> command;
cmSystemTools::ParseUnixCommandLine(it->at("command").c_str(), command);
if (!cmSystemTools::RunSingleCommand(
command, 0, 0, it->at("directory").c_str()))
command, 0, 0, 0, it->at("directory").c_str()))
{
std::cout << "ERROR: Failed to run command \""
<< command[0] << "\"" << std::endl;