2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2008-07-03 17:49:49 +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.
|
2008-07-03 17:49:49 +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.
|
|
|
|
============================================================================*/
|
2008-07-03 17:49:49 +04:00
|
|
|
|
|
|
|
#include <cmProcess.h>
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2008-12-30 01:49:17 +03:00
|
|
|
#include <cmSystemTools.h>
|
2008-07-03 17:49:49 +04:00
|
|
|
|
|
|
|
cmProcess::cmProcess()
|
|
|
|
{
|
|
|
|
this->Process = 0;
|
|
|
|
this->Timeout = 0;
|
2009-09-04 18:16:06 +04:00
|
|
|
this->TotalTime = 0;
|
|
|
|
this->ExitValue = 0;
|
|
|
|
this->Id = 0;
|
|
|
|
this->StartTime = 0;
|
2008-07-03 17:49:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmProcess::~cmProcess()
|
|
|
|
{
|
|
|
|
cmsysProcess_Delete(this->Process);
|
|
|
|
}
|
|
|
|
void cmProcess::SetCommand(const char* command)
|
|
|
|
{
|
|
|
|
this->Command = command;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmProcess::SetCommandArguments(std::vector<std::string> const& args)
|
|
|
|
{
|
|
|
|
this->Arguments = args;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmProcess::StartProcess()
|
|
|
|
{
|
2015-01-15 02:31:49 +03:00
|
|
|
if(this->Command.empty())
|
2008-07-03 17:49:49 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-30 01:49:17 +03:00
|
|
|
this->StartTime = cmSystemTools::GetTime();
|
2008-07-03 17:49:49 +04:00
|
|
|
this->ProcessArgs.clear();
|
|
|
|
// put the command as arg0
|
|
|
|
this->ProcessArgs.push_back(this->Command.c_str());
|
|
|
|
// now put the command arguments in
|
|
|
|
for(std::vector<std::string>::iterator i = this->Arguments.begin();
|
|
|
|
i != this->Arguments.end(); ++i)
|
|
|
|
{
|
|
|
|
this->ProcessArgs.push_back(i->c_str());
|
|
|
|
}
|
|
|
|
this->ProcessArgs.push_back(0); // null terminate the list
|
|
|
|
this->Process = cmsysProcess_New();
|
|
|
|
cmsysProcess_SetCommand(this->Process, &*this->ProcessArgs.begin());
|
2015-01-16 01:04:33 +03:00
|
|
|
if(!this->WorkingDirectory.empty())
|
2008-07-03 17:49:49 +04:00
|
|
|
{
|
|
|
|
cmsysProcess_SetWorkingDirectory(this->Process,
|
|
|
|
this->WorkingDirectory.c_str());
|
|
|
|
}
|
|
|
|
cmsysProcess_SetTimeout(this->Process, this->Timeout);
|
2015-06-03 15:47:49 +03:00
|
|
|
cmsysProcess_SetOption(this->Process, cmsysProcess_Option_MergeOutput, 1);
|
2008-07-03 17:49:49 +04:00
|
|
|
cmsysProcess_Execute(this->Process);
|
|
|
|
return (cmsysProcess_GetState(this->Process)
|
|
|
|
== cmsysProcess_State_Executing);
|
|
|
|
}
|
2009-09-03 23:33:44 +04:00
|
|
|
|
2009-09-11 20:26:41 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmProcess::Buffer::GetLine(std::string& line)
|
2008-07-03 17:49:49 +04:00
|
|
|
{
|
2009-09-11 20:26:41 +04:00
|
|
|
// Scan for the next newline.
|
|
|
|
for(size_type sz = this->size(); this->Last != sz; ++this->Last)
|
2009-09-03 23:33:44 +04:00
|
|
|
{
|
2009-09-11 20:26:41 +04:00
|
|
|
if((*this)[this->Last] == '\n' || (*this)[this->Last] == '\0')
|
|
|
|
{
|
|
|
|
// Extract the range first..last as a line.
|
2009-09-12 18:20:00 +04:00
|
|
|
const char* text = &*this->begin() + this->First;
|
2009-09-11 20:26:41 +04:00
|
|
|
size_type length = this->Last - this->First;
|
2009-11-16 18:58:43 +03:00
|
|
|
while(length && text[length-1] == '\r')
|
|
|
|
{
|
|
|
|
length --;
|
|
|
|
}
|
2009-09-12 18:20:00 +04:00
|
|
|
line.assign(text, length);
|
2009-09-04 21:50:06 +04:00
|
|
|
|
2009-09-11 20:26:41 +04:00
|
|
|
// Start a new range for the next line.
|
|
|
|
++this->Last;
|
|
|
|
this->First = Last;
|
2009-09-08 18:16:16 +04:00
|
|
|
|
2009-09-11 20:26:41 +04:00
|
|
|
// Return the line extracted.
|
|
|
|
return true;
|
2008-07-03 17:49:49 +04:00
|
|
|
}
|
2009-09-03 23:33:44 +04:00
|
|
|
}
|
2008-07-03 17:49:49 +04:00
|
|
|
|
2009-09-11 20:26:41 +04:00
|
|
|
// Available data have been exhausted without a newline.
|
|
|
|
if(this->First != 0)
|
2009-09-03 23:33:44 +04:00
|
|
|
{
|
2009-09-11 20:26:41 +04:00
|
|
|
// Move the partial line to the beginning of the buffer.
|
|
|
|
this->erase(this->begin(), this->begin() + this->First);
|
|
|
|
this->First = 0;
|
|
|
|
this->Last = this->size();
|
2009-09-03 23:33:44 +04:00
|
|
|
}
|
2009-09-11 20:26:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-08 22:48:23 +04:00
|
|
|
|
2009-09-11 20:26:41 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmProcess::Buffer::GetLast(std::string& line)
|
|
|
|
{
|
|
|
|
// Return the partial last line, if any.
|
|
|
|
if(!this->empty())
|
|
|
|
{
|
|
|
|
line.assign(&*this->begin(), this->size());
|
2009-09-12 00:20:24 +04:00
|
|
|
this->First = this->Last = 0;
|
2009-09-11 20:26:41 +04:00
|
|
|
this->clear();
|
|
|
|
return true;
|
2009-09-08 22:48:23 +04:00
|
|
|
}
|
2009-09-11 20:26:41 +04:00
|
|
|
return false;
|
2009-09-03 23:33:44 +04:00
|
|
|
}
|
2008-07-03 17:49:49 +04:00
|
|
|
|
2009-09-11 20:26:41 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
int cmProcess::GetNextOutputLine(std::string& line, double timeout)
|
|
|
|
{
|
|
|
|
for(;;)
|
2009-09-03 23:33:44 +04:00
|
|
|
{
|
2009-09-11 20:26:41 +04:00
|
|
|
// Look for lines already buffered.
|
2015-06-03 15:47:49 +03:00
|
|
|
if(this->Output.GetLine(line))
|
2008-07-03 17:49:49 +04:00
|
|
|
{
|
2009-09-11 20:26:41 +04:00
|
|
|
return cmsysProcess_Pipe_STDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for more data from the process.
|
|
|
|
char* data;
|
|
|
|
int length;
|
|
|
|
int p = cmsysProcess_WaitForData(this->Process, &data, &length, &timeout);
|
|
|
|
if(p == cmsysProcess_Pipe_Timeout)
|
|
|
|
{
|
|
|
|
return cmsysProcess_Pipe_Timeout;
|
|
|
|
}
|
|
|
|
else if(p == cmsysProcess_Pipe_STDOUT)
|
2008-07-03 17:49:49 +04:00
|
|
|
{
|
2015-06-03 15:47:49 +03:00
|
|
|
this->Output.insert(this->Output.end(), data, data+length);
|
2008-07-03 17:49:49 +04:00
|
|
|
}
|
2009-09-11 20:26:41 +04:00
|
|
|
else // p == cmsysProcess_Pipe_None
|
2008-07-03 17:49:49 +04:00
|
|
|
{
|
2009-09-11 20:26:41 +04:00
|
|
|
// The process will provide no more data.
|
|
|
|
break;
|
2008-07-03 17:49:49 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-11 20:26:41 +04:00
|
|
|
|
|
|
|
// Look for partial last lines.
|
2015-06-03 15:47:49 +03:00
|
|
|
if(this->Output.GetLast(line))
|
2009-09-11 20:26:41 +04:00
|
|
|
{
|
|
|
|
return cmsysProcess_Pipe_STDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No more data. Wait for process exit.
|
|
|
|
if(!cmsysProcess_WaitForExit(this->Process, &timeout))
|
|
|
|
{
|
|
|
|
return cmsysProcess_Pipe_Timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record exit information.
|
|
|
|
this->ExitValue = cmsysProcess_GetExitValue(this->Process);
|
|
|
|
this->TotalTime = cmSystemTools::GetTime() - this->StartTime;
|
2013-05-10 22:50:22 +04:00
|
|
|
// Because of a processor clock scew the runtime may become slightly
|
|
|
|
// negative. If someone changed the system clock while the process was
|
|
|
|
// running this may be even more. Make sure not to report a negative
|
|
|
|
// duration here.
|
|
|
|
if (this->TotalTime <= 0.0)
|
|
|
|
{
|
|
|
|
this->TotalTime = 0.0;
|
|
|
|
}
|
2009-09-11 20:26:41 +04:00
|
|
|
// std::cerr << "Time to run: " << this->TotalTime << "\n";
|
|
|
|
return cmsysProcess_Pipe_None;
|
2008-07-03 17:49:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// return the process status
|
|
|
|
int cmProcess::GetProcessStatus()
|
|
|
|
{
|
|
|
|
if(!this->Process)
|
|
|
|
{
|
|
|
|
return cmsysProcess_State_Exited;
|
|
|
|
}
|
|
|
|
return cmsysProcess_GetState(this->Process);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmProcess::ReportStatus()
|
|
|
|
{
|
|
|
|
int result = 1;
|
|
|
|
switch(cmsysProcess_GetState(this->Process))
|
|
|
|
{
|
|
|
|
case cmsysProcess_State_Starting:
|
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
std::cerr << "cmProcess: Never started "
|
2008-07-03 17:49:49 +04:00
|
|
|
<< this->Command << " process.\n";
|
|
|
|
} break;
|
|
|
|
case cmsysProcess_State_Error:
|
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
std::cerr << "cmProcess: Error executing " << this->Command
|
2008-10-02 17:18:47 +04:00
|
|
|
<< " process: "
|
|
|
|
<< cmsysProcess_GetErrorString(this->Process)
|
|
|
|
<< "\n";
|
2008-07-03 17:49:49 +04:00
|
|
|
} break;
|
|
|
|
case cmsysProcess_State_Exception:
|
|
|
|
{
|
|
|
|
std::cerr << "cmProcess: " << this->Command
|
|
|
|
<< " process exited with an exception: ";
|
|
|
|
switch(cmsysProcess_GetExitException(this->Process))
|
|
|
|
{
|
|
|
|
case cmsysProcess_Exception_None:
|
|
|
|
{
|
|
|
|
std::cerr << "None";
|
|
|
|
} break;
|
|
|
|
case cmsysProcess_Exception_Fault:
|
|
|
|
{
|
|
|
|
std::cerr << "Segmentation fault";
|
|
|
|
} break;
|
|
|
|
case cmsysProcess_Exception_Illegal:
|
|
|
|
{
|
|
|
|
std::cerr << "Illegal instruction";
|
|
|
|
} break;
|
|
|
|
case cmsysProcess_Exception_Interrupt:
|
|
|
|
{
|
|
|
|
std::cerr << "Interrupted by user";
|
|
|
|
} break;
|
|
|
|
case cmsysProcess_Exception_Numerical:
|
|
|
|
{
|
|
|
|
std::cerr << "Numerical exception";
|
|
|
|
} break;
|
|
|
|
case cmsysProcess_Exception_Other:
|
|
|
|
{
|
|
|
|
std::cerr << "Unknown";
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
std::cerr << "\n";
|
|
|
|
} break;
|
|
|
|
case cmsysProcess_State_Executing:
|
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
std::cerr << "cmProcess: Never terminated " <<
|
2008-10-02 17:18:47 +04:00
|
|
|
this->Command << " process.\n";
|
2008-07-03 17:49:49 +04:00
|
|
|
} break;
|
|
|
|
case cmsysProcess_State_Exited:
|
|
|
|
{
|
|
|
|
result = cmsysProcess_GetExitValue(this->Process);
|
2012-08-13 21:42:58 +04:00
|
|
|
std::cerr << "cmProcess: " << this->Command
|
2008-10-02 17:18:47 +04:00
|
|
|
<< " process exited with code "
|
|
|
|
<< result << "\n";
|
2008-07-03 17:49:49 +04:00
|
|
|
} break;
|
|
|
|
case cmsysProcess_State_Expired:
|
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
std::cerr << "cmProcess: killed " << this->Command
|
2008-10-02 17:18:47 +04:00
|
|
|
<< " process due to timeout.\n";
|
2008-07-03 17:49:49 +04:00
|
|
|
} break;
|
|
|
|
case cmsysProcess_State_Killed:
|
|
|
|
{
|
|
|
|
std::cerr << "cmProcess: killed " << this->Command << " process.\n";
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
2010-06-22 17:55:09 +04:00
|
|
|
|
2016-02-18 21:59:18 +03:00
|
|
|
void cmProcess::ChangeTimeout(double t)
|
|
|
|
{
|
|
|
|
this->Timeout = t;
|
|
|
|
cmsysProcess_SetTimeout(this->Process, this->Timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmProcess::ResetStartTime()
|
|
|
|
{
|
|
|
|
cmsysProcess_ResetStartTime(this->Process);
|
|
|
|
}
|
2010-06-22 17:55:09 +04:00
|
|
|
|
|
|
|
int cmProcess::GetExitException()
|
|
|
|
{
|
|
|
|
return cmsysProcess_GetExitException(this->Process);
|
|
|
|
}
|