cmake/Source/CTest/cmProcess.h

129 lines
3.1 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. */
2009-10-04 10:30:41 +03:00
#ifndef cmProcess_h
#define cmProcess_h
2017-07-20 19:35:53 +02:00
#include "cmConfigure.h" // IWYU pragma: keep
2018-04-23 21:13:27 +02:00
#include "cmDuration.h"
2009-10-04 10:30:41 +03:00
2018-04-23 21:13:27 +02:00
#include "cmProcessOutput.h"
#include "cmUVHandlePtr.h"
#include "cm_uv.h"
#include <chrono>
#include <stddef.h>
#include <stdint.h>
2016-10-30 18:24:19 +01:00
#include <string>
#include <vector>
2009-10-04 10:30:41 +03:00
2018-04-23 21:13:27 +02:00
class cmCTestRunTest;
2009-10-04 10:30:41 +03:00
/** \class cmProcess
* \brief run a process with c++
*
* cmProcess wraps the kwsys process stuff in a c++ class.
*/
2013-03-16 19:13:01 +02:00
class cmProcess
2009-10-04 10:30:41 +03:00
{
public:
2018-04-23 21:13:27 +02:00
explicit cmProcess(cmCTestRunTest& runner);
2009-10-04 10:30:41 +03:00
~cmProcess();
2016-07-09 11:21:54 +02:00
const char* GetCommand() { return this->Command.c_str(); }
2009-10-04 10:30:41 +03:00
void SetCommand(const char* command);
void SetCommandArguments(std::vector<std::string> const& arg);
2016-07-09 11:21:54 +02:00
void SetWorkingDirectory(const char* dir) { this->WorkingDirectory = dir; }
2018-04-23 21:13:27 +02:00
void SetTimeout(cmDuration t) { this->Timeout = t; }
void ChangeTimeout(cmDuration t);
2016-07-09 11:21:54 +02:00
void ResetStartTime();
2009-10-04 10:30:41 +03:00
// Return true if the process starts
2018-08-09 18:06:22 +02:00
bool StartProcess(uv_loop_t& loop, std::vector<size_t>* affinity);
2018-04-23 21:13:27 +02:00
enum class State
{
Starting,
Error,
Exception,
Executing,
Exited,
Expired,
Killed,
Disowned
};
2009-10-04 10:30:41 +03:00
2018-04-23 21:13:27 +02:00
State GetProcessStatus();
2009-10-04 10:30:41 +03:00
int GetId() { return this->Id; }
2016-07-09 11:21:54 +02:00
void SetId(int id) { this->Id = id; }
int GetExitValue() { return this->ExitValue; }
2018-04-23 21:13:27 +02:00
cmDuration GetTotalTime() { return this->TotalTime; }
enum class Exception
{
None,
Fault,
Illegal,
Interrupt,
Numerical,
Other
};
Exception GetExitException();
2018-01-26 17:06:56 +01:00
std::string GetExitExceptionString();
2016-07-09 11:21:54 +02:00
2009-10-04 10:30:41 +03:00
private:
2018-04-23 21:13:27 +02:00
cmDuration Timeout;
std::chrono::steady_clock::time_point StartTime;
cmDuration TotalTime;
bool ReadHandleClosed = false;
bool ProcessHandleClosed = false;
cm::uv_process_ptr Process;
cm::uv_pipe_ptr PipeReader;
cm::uv_timer_ptr Timer;
std::vector<char> Buf;
cmCTestRunTest& Runner;
cmProcessOutput Conv;
int Signal = 0;
cmProcess::State ProcessState = cmProcess::State::Starting;
static void OnExitCB(uv_process_t* process, int64_t exit_status,
int term_signal);
static void OnTimeoutCB(uv_timer_t* timer);
static void OnReadCB(uv_stream_t* stream, ssize_t nread,
const uv_buf_t* buf);
static void OnAllocateCB(uv_handle_t* handle, size_t suggested_size,
uv_buf_t* buf);
void OnExit(int64_t exit_status, int term_signal);
void OnTimeout();
void OnRead(ssize_t nread, const uv_buf_t* buf);
void OnAllocate(size_t suggested_size, uv_buf_t* buf);
void StartTimer();
2016-07-09 11:21:54 +02:00
class Buffer : public std::vector<char>
2009-10-04 10:30:41 +03:00
{
// Half-open index range of partial line already scanned.
size_type First;
size_type Last;
2016-07-09 11:21:54 +02:00
2009-10-04 10:30:41 +03:00
public:
2016-07-09 11:21:54 +02:00
Buffer()
: First(0)
, Last(0)
{
}
2009-10-04 10:30:41 +03:00
bool GetLine(std::string& line);
bool GetLast(std::string& line);
};
2015-11-17 17:22:37 +01:00
Buffer Output;
2009-10-04 10:30:41 +03:00
std::string Command;
std::string WorkingDirectory;
std::vector<std::string> Arguments;
std::vector<const char*> ProcessArgs;
int Id;
int ExitValue;
};
#endif