RedPanda-CPP/RedPandaIDE/compiler/executablerunner.cpp

229 lines
6.5 KiB
C++
Raw Normal View History

#include "executablerunner.h"
#include <QDebug>
2021-06-25 12:40:11 +08:00
#include "compilermanager.h"
#include "../settings.h"
#include "../systemconsts.h"
2021-12-24 23:18:20 +08:00
#ifdef Q_OS_WIN
#include <windows.h>
#endif
2021-11-01 09:18:23 +08:00
ExecutableRunner::ExecutableRunner(const QString &filename, const QString &arguments, const QString &workDir
,QObject* parent):
Runner(filename,arguments,workDir,parent),
2021-10-25 09:16:00 +08:00
mRedirectInput(false),
mStartConsole(false)
{
}
2021-10-25 09:16:00 +08:00
bool ExecutableRunner::startConsole() const
{
2021-10-25 09:16:00 +08:00
return mStartConsole;
}
2021-10-25 09:16:00 +08:00
void ExecutableRunner::setStartConsole(bool newStartConsole)
{
2021-10-25 09:16:00 +08:00
mStartConsole = newStartConsole;
}
bool ExecutableRunner::redirectInput() const
{
return mRedirectInput;
}
void ExecutableRunner::setRedirectInput(bool isRedirect)
{
mRedirectInput = isRedirect;
}
const QString &ExecutableRunner::redirectInputFilename() const
{
return mRedirectInputFilename;
}
void ExecutableRunner::setRedirectInputFilename(const QString &newDataFile)
{
mRedirectInputFilename = newDataFile;
}
void ExecutableRunner::run()
{
emit started();
2021-11-01 09:18:23 +08:00
auto action = finally([this]{
mProcess.reset();
setPausing(false);
2021-11-01 09:18:23 +08:00
emit terminated();
});
mStop = false;
2021-06-25 12:40:11 +08:00
bool errorOccurred = false;
mProcess = std::make_shared<QProcess>();
mProcess->setProgram(mFilename);
mProcess->setArguments(QProcess::splitCommand(mArguments));
mProcess->setWorkingDirectory(mWorkDir);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString path = env.value("PATH");
QStringList pathAdded;
if (pSettings->compilerSets().defaultSet()) {
foreach(const QString& dir, pSettings->compilerSets().defaultSet()->binDirs()) {
pathAdded.append(dir);
}
}
2021-12-26 15:08:54 +08:00
pathAdded.append(pSettings->dirs().appDir());
if (!path.isEmpty()) {
path+= PATH_SEPARATOR + pathAdded.join(PATH_SEPARATOR);
} else {
path = pathAdded.join(PATH_SEPARATOR);
}
env.insert("PATH",path);
mProcess->setProcessEnvironment(env);
2021-12-24 23:18:20 +08:00
connect(
mProcess.get(), &QProcess::errorOccurred,
[&errorOccurred](){
errorOccurred= true;
});
#ifdef Q_OS_WIN
mProcess->setCreateProcessArgumentsModifier([this](QProcess::CreateProcessArguments * args){
2021-10-25 09:16:00 +08:00
if (mStartConsole) {
args->flags |= CREATE_NEW_CONSOLE;
args->flags &= ~CREATE_NO_WINDOW;
2021-10-25 09:16:00 +08:00
}
if (!mRedirectInput) {
args->startupInfo -> dwFlags &= ~STARTF_USESTDHANDLES;
2021-10-25 09:16:00 +08:00
}
});
HANDLE hSharedMemory=INVALID_HANDLE_VALUE;
int BUF_SIZE=1024;
char* pBuf=nullptr;
if (mStartConsole) {
hSharedMemory = CreateFileMappingA(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
100,
"RED_PANDA_IDE_CONSOLE_PAUSER20211223"
);
if (hSharedMemory != NULL)
{
pBuf = (char*) MapViewOfFile(hSharedMemory, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf) {
pBuf[0]=0;
}
}
}
2021-12-24 23:18:20 +08:00
#endif
// if (!redirectInput()) {
// process.closeWriteChannel();
// }
mProcess->start();
mProcess->waitForStarted(5000);
if (mProcess->state()==QProcess::Running && redirectInput()) {
mProcess->write(readFileToByteArray(redirectInputFilename()));
mProcess->closeWriteChannel();
}
while (true) {
mProcess->waitForFinished(1000);
if (mProcess->state()!=QProcess::Running) {
break;
}
if (mStop) {
mProcess->closeReadChannel(QProcess::StandardOutput);
mProcess->closeReadChannel(QProcess::StandardError);
mProcess->closeWriteChannel();
mProcess->terminate();
if (mProcess->waitForFinished(1000)) {
break;
}
for (int i=0;i<10;i++) {
mProcess->kill();
if (mProcess->waitForFinished(500)) {
break;
}
}
2021-08-01 23:24:37 +08:00
break;
2021-06-25 12:40:11 +08:00
}
2021-12-24 23:18:20 +08:00
#ifdef Q_OS_WIN
if (mStartConsole && !mPausing && pBuf) {
if (strncmp(pBuf,"FINISHED",sizeof("FINISHED"))==0) {
setPausing(true);
emit pausingForFinish();
}
}
2021-12-24 23:18:20 +08:00
#endif
2021-06-25 12:40:11 +08:00
if (errorOccurred)
break;
}
2021-12-24 23:18:20 +08:00
#ifdef Q_OS_WIN
if (pBuf)
UnmapViewOfFile(pBuf);
if (hSharedMemory!=INVALID_HANDLE_VALUE)
CloseHandle(hSharedMemory);
2021-12-24 23:18:20 +08:00
#endif
2021-06-25 12:40:11 +08:00
if (errorOccurred) {
//qDebug()<<"process error:"<<process.error();
switch (mProcess->error()) {
2021-06-25 12:40:11 +08:00
case QProcess::FailedToStart:
2021-09-04 19:37:33 +08:00
emit runErrorOccurred(tr("The runner process '%1' failed to start.").arg(mFilename));
2021-06-25 12:40:11 +08:00
break;
2021-09-04 19:37:33 +08:00
// case QProcess::Crashed:
// if (!mStop)
// emit runErrorOccurred(tr("The runner process crashed after starting successfully."));
// break;
2021-06-25 12:40:11 +08:00
case QProcess::Timedout:
emit runErrorOccurred(tr("The last waitFor...() function timed out."));
break;
case QProcess::WriteError:
emit runErrorOccurred(tr("An error occurred when attempting to write to the runner process."));
break;
case QProcess::ReadError:
emit runErrorOccurred(tr("An error occurred when attempting to read from the runner process."));
break;
2021-10-20 18:05:43 +08:00
default:
break;
}
}
}
void ExecutableRunner::doStop()
{
std::shared_ptr<QProcess> process = mProcess;
if (process) {
// qDebug()<<"??1";
// process->closeReadChannel(QProcess::StandardOutput);
// process->closeReadChannel(QProcess::StandardError);
// process->closeWriteChannel();
// qDebug()<<"??2";
// #ifdef Q_OS_WIN
// if (!mStartConsole) {
// qDebug()<<"??3";
// process->terminate();
// qDebug()<<"??4";
// if (process->waitForFinished(1000)) {
// return;
// }
// }
// #else
// process->terminate();
// if (process->waitForFinished(1000)) {
// break;
// }
// #endif
// for (int i=0;i<10;i++) {
// qDebug()<<"??5";
// process->kill();
// qDebug()<<"??6";
// if (process->waitForFinished(100)) {
// break;
// }
// }
}
}