CMake/Source/CTest/cmProcess.cxx

234 lines
6.8 KiB
C++
Raw Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
2016-09-27 22:01:08 +03:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmProcess.h"
2008-07-03 17:49:49 +04:00
#include <cmConfigure.h>
2008-12-30 01:49:17 +03:00
#include <cmSystemTools.h>
#include <iostream>
2008-07-03 17:49:49 +04:00
cmProcess::cmProcess()
{
2016-06-27 23:44:16 +03:00
this->Process = CM_NULLPTR;
2008-07-03 17:49:49 +04:00
this->Timeout = 0;
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()
{
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) {
2008-07-03 17:49:49 +04:00
this->ProcessArgs.push_back(i->c_str());
}
2016-06-27 23:44:16 +03:00
this->ProcessArgs.push_back(CM_NULLPTR); // null terminate the list
2008-07-03 17:49:49 +04:00
this->Process = cmsysProcess_New();
cmsysProcess_SetCommand(this->Process, &*this->ProcessArgs.begin());
if (!this->WorkingDirectory.empty()) {
2008-07-03 17:49:49 +04:00
cmsysProcess_SetWorkingDirectory(this->Process,
this->WorkingDirectory.c_str());
}
2008-07-03 17:49:49 +04:00
cmsysProcess_SetTimeout(this->Process, this->Timeout);
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);
2008-07-03 17:49:49 +04:00
}
bool cmProcess::Buffer::GetLine(std::string& line)
2008-07-03 17:49:49 +04:00
{
// Scan for the next newline.
for (size_type sz = this->size(); this->Last != sz; ++this->Last) {
if ((*this)[this->Last] == '\n' || (*this)[this->Last] == '\0') {
// Extract the range first..last as a line.
const char* text = &*this->begin() + this->First;
size_type length = this->Last - this->First;
while (length && text[length - 1] == '\r') {
length--;
}
line.assign(text, length);
// Start a new range for the next line.
++this->Last;
this->First = Last;
// Return the line extracted.
return true;
}
}
2008-07-03 17:49:49 +04:00
// Available data have been exhausted without a newline.
if (this->First != 0) {
// 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();
}
return false;
}
bool cmProcess::Buffer::GetLast(std::string& line)
{
// Return the partial last line, if any.
if (!this->empty()) {
line.assign(&*this->begin(), this->size());
this->First = this->Last = 0;
this->clear();
return true;
}
return false;
}
2008-07-03 17:49:49 +04:00
int cmProcess::GetNextOutputLine(std::string& line, double timeout)
{
for (;;) {
// Look for lines already buffered.
if (this->Output.GetLine(line)) {
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;
2016-08-18 21:04:21 +03:00
}
if (p == cmsysProcess_Pipe_STDOUT) {
this->Output.insert(this->Output.end(), data, data + length);
2016-08-18 21:04:21 +03:00
} else { // p == cmsysProcess_Pipe_None
// The process will provide no more data.
break;
2008-07-03 17:49:49 +04:00
}
}
// Look for partial last lines.
if (this->Output.GetLast(line)) {
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;
// 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;
}
// 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) {
2008-07-03 17:49:49 +04:00
return cmsysProcess_State_Exited;
}
2008-07-03 17:49:49 +04:00
return cmsysProcess_GetState(this->Process);
}
int cmProcess::ReportStatus()
{
int result = 1;
switch (cmsysProcess_GetState(this->Process)) {
case cmsysProcess_State_Starting: {
std::cerr << "cmProcess: Never started " << this->Command
<< " process.\n";
} break;
case cmsysProcess_State_Error: {
std::cerr << "cmProcess: Error executing " << this->Command
<< " process: " << cmsysProcess_GetErrorString(this->Process)
2008-10-02 17:18:47 +04:00
<< "\n";
} break;
case cmsysProcess_State_Exception: {
2008-07-03 17:49:49 +04:00
std::cerr << "cmProcess: " << this->Command
<< " process exited with an exception: ";
switch (cmsysProcess_GetExitException(this->Process)) {
case cmsysProcess_Exception_None: {
2008-07-03 17:49:49 +04:00
std::cerr << "None";
} break;
case cmsysProcess_Exception_Fault: {
2008-07-03 17:49:49 +04:00
std::cerr << "Segmentation fault";
} break;
case cmsysProcess_Exception_Illegal: {
2008-07-03 17:49:49 +04:00
std::cerr << "Illegal instruction";
} break;
case cmsysProcess_Exception_Interrupt: {
2008-07-03 17:49:49 +04:00
std::cerr << "Interrupted by user";
} break;
case cmsysProcess_Exception_Numerical: {
2008-07-03 17:49:49 +04:00
std::cerr << "Numerical exception";
} break;
case cmsysProcess_Exception_Other: {
2008-07-03 17:49:49 +04:00
std::cerr << "Unknown";
} break;
}
2008-07-03 17:49:49 +04:00
std::cerr << "\n";
} break;
case cmsysProcess_State_Executing: {
std::cerr << "cmProcess: Never terminated " << this->Command
<< " process.\n";
} break;
case cmsysProcess_State_Exited: {
2008-07-03 17:49:49 +04:00
result = cmsysProcess_GetExitValue(this->Process);
std::cerr << "cmProcess: " << this->Command
<< " process exited with code " << result << "\n";
} break;
case cmsysProcess_State_Expired: {
std::cerr << "cmProcess: killed " << this->Command
2008-10-02 17:18:47 +04:00
<< " process due to timeout.\n";
} break;
case cmsysProcess_State_Killed: {
2008-07-03 17:49:49 +04:00
std::cerr << "cmProcess: killed " << this->Command << " process.\n";
} break;
}
2008-07-03 17:49:49 +04:00
return result;
}
void cmProcess::ChangeTimeout(double t)
{
this->Timeout = t;
cmsysProcess_SetTimeout(this->Process, this->Timeout);
}
void cmProcess::ResetStartTime()
{
cmsysProcess_ResetStartTime(this->Process);
}
int cmProcess::GetExitException()
{
return cmsysProcess_GetExitException(this->Process);
}