2021-04-20 22:24:33 +08:00
|
|
|
#include "compilermanager.h"
|
|
|
|
#include "filecompiler.h"
|
2021-06-24 20:43:09 +08:00
|
|
|
#include "stdincompiler.h"
|
2021-04-20 22:24:33 +08:00
|
|
|
#include <QDebug>
|
|
|
|
#include "../mainwindow.h"
|
2021-04-21 18:58:35 +08:00
|
|
|
#include "executablerunner.h"
|
|
|
|
#include "utils.h"
|
2021-07-01 19:44:38 +08:00
|
|
|
#include "../settings.h"
|
2021-07-26 22:29:47 +08:00
|
|
|
#include <QMessageBox>
|
2021-04-20 22:24:33 +08:00
|
|
|
|
|
|
|
CompilerManager::CompilerManager(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
mCompiler = nullptr;
|
|
|
|
mBackgroundSyntaxChecker = nullptr;
|
2021-04-21 18:58:35 +08:00
|
|
|
mRunner = nullptr;
|
2021-06-23 22:38:02 +08:00
|
|
|
mSyntaxCheckErrorCount = 0;
|
|
|
|
mCompileErrorCount = 0;
|
2021-04-20 22:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CompilerManager::compiling()
|
|
|
|
{
|
2021-06-25 12:40:11 +08:00
|
|
|
QMutexLocker locker(&mCompileMutex);
|
2021-04-20 22:24:33 +08:00
|
|
|
return mCompiler!=nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CompilerManager::backgroundSyntaxChecking()
|
|
|
|
{
|
2021-06-25 12:40:11 +08:00
|
|
|
QMutexLocker locker(&mBackgroundSyntaxCheckMutex);
|
2021-04-20 22:24:33 +08:00
|
|
|
return mBackgroundSyntaxChecker!=nullptr;
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:40:11 +08:00
|
|
|
bool CompilerManager::running()
|
2021-04-20 22:24:33 +08:00
|
|
|
{
|
2021-06-25 12:40:11 +08:00
|
|
|
QMutexLocker locker(&mRunnerMutex);
|
|
|
|
return mRunner!=nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerManager::compile(const QString& filename, const QByteArray& encoding, bool rebuild, bool silent, bool onlyCheckSyntax)
|
|
|
|
{
|
2021-07-26 22:29:47 +08:00
|
|
|
if (!pSettings->compilerSets().defaultSet()) {
|
|
|
|
QMessageBox::critical(pMainWindow,
|
|
|
|
tr("No compiler set"),
|
|
|
|
tr("No compiler set is configured.")+tr("Can't start debugging."));
|
2021-04-21 18:58:35 +08:00
|
|
|
return;
|
2021-04-20 22:24:33 +08:00
|
|
|
}
|
2021-07-26 22:29:47 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mCompileMutex);
|
|
|
|
if (mCompiler!=nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCompileErrorCount = 0;
|
|
|
|
mCompiler = new FileCompiler(filename,encoding,silent,onlyCheckSyntax);
|
|
|
|
mCompiler->setRebuild(rebuild);
|
|
|
|
connect(mCompiler, &Compiler::compileFinished, this ,&CompilerManager::onCompileFinished);
|
|
|
|
connect(mCompiler, &Compiler::compileIssue, this, &CompilerManager::onCompileIssue);
|
|
|
|
connect(mCompiler, &Compiler::compileFinished, pMainWindow, &MainWindow::onCompileFinished);
|
|
|
|
connect(mCompiler, &Compiler::compileOutput, pMainWindow, &MainWindow::onCompileLog);
|
|
|
|
connect(mCompiler, &Compiler::compileIssue, pMainWindow, &MainWindow::onCompileIssue);
|
|
|
|
connect(mCompiler, &Compiler::compileErrorOccured, pMainWindow, &MainWindow::onCompileErrorOccured);
|
|
|
|
mCompiler->start();
|
|
|
|
}
|
2021-04-21 18:58:35 +08:00
|
|
|
}
|
|
|
|
|
2021-06-24 20:43:09 +08:00
|
|
|
void CompilerManager::checkSyntax(const QString &filename, const QString &content)
|
|
|
|
{
|
2021-07-26 22:29:47 +08:00
|
|
|
if (!pSettings->compilerSets().defaultSet()) {
|
|
|
|
QMessageBox::critical(pMainWindow,
|
|
|
|
tr("No compiler set"),
|
|
|
|
tr("No compiler set is configured.")+tr("Can't start debugging."));
|
2021-06-24 20:43:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-07-26 22:29:47 +08:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&mBackgroundSyntaxCheckMutex);
|
|
|
|
if (mBackgroundSyntaxChecker!=nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSyntaxCheckErrorCount = 0;
|
|
|
|
mBackgroundSyntaxChecker = new StdinCompiler(filename,content,true,true);
|
|
|
|
connect(mBackgroundSyntaxChecker, &Compiler::compileFinished, this ,&CompilerManager::onSyntaxCheckFinished);
|
|
|
|
connect(mBackgroundSyntaxChecker, &Compiler::compileIssue, this, &CompilerManager::onSyntaxCheckIssue);
|
|
|
|
connect(mBackgroundSyntaxChecker, &Compiler::compileFinished, pMainWindow, &MainWindow::onCompileFinished);
|
|
|
|
connect(mBackgroundSyntaxChecker, &Compiler::compileOutput, pMainWindow, &MainWindow::onCompileLog);
|
|
|
|
connect(mBackgroundSyntaxChecker, &Compiler::compileIssue, pMainWindow, &MainWindow::onCompileIssue);
|
|
|
|
connect(mBackgroundSyntaxChecker, &Compiler::compileErrorOccured, pMainWindow, &MainWindow::onCompileErrorOccured);
|
|
|
|
mBackgroundSyntaxChecker->start();
|
|
|
|
}
|
2021-06-24 20:43:09 +08:00
|
|
|
}
|
|
|
|
|
2021-04-21 18:58:35 +08:00
|
|
|
void CompilerManager::run(const QString &filename, const QString &arguments, const QString &workDir)
|
|
|
|
{
|
2021-06-25 12:40:11 +08:00
|
|
|
QMutexLocker locker(&mRunnerMutex);
|
2021-04-21 18:58:35 +08:00
|
|
|
if (mRunner!=nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-01 19:44:38 +08:00
|
|
|
if (pSettings->executor().pauseConsole() && programHasConsole(filename)) {
|
2021-04-21 18:58:35 +08:00
|
|
|
QString newArguments = QString(" 0 \"%1\" %2").arg(toLocalPath(filename)).arg(arguments);
|
|
|
|
mRunner = new ExecutableRunner(includeTrailingPathDelimiter(pSettings->dirs().app())+"ConsolePauser.exe",newArguments,workDir);
|
|
|
|
} else {
|
|
|
|
mRunner = new ExecutableRunner(filename,arguments,workDir);
|
|
|
|
}
|
2021-08-01 23:24:37 +08:00
|
|
|
connect(mRunner, &ExecutableRunner::finished, this ,&CompilerManager::onRunnerTerminated);
|
|
|
|
connect(mRunner, &ExecutableRunner::finished, pMainWindow ,&MainWindow::onRunFinished);
|
2021-06-25 12:40:11 +08:00
|
|
|
connect(mRunner, &ExecutableRunner::runErrorOccurred, pMainWindow ,&MainWindow::onRunErrorOccured);
|
2021-04-21 18:58:35 +08:00
|
|
|
mRunner->start();
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:40:11 +08:00
|
|
|
void CompilerManager::stopRun()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mRunnerMutex);
|
|
|
|
if (mRunner!=nullptr)
|
|
|
|
mRunner->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerManager::stopCompile()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mCompileMutex);
|
|
|
|
if (mCompiler!=nullptr)
|
|
|
|
mCompiler->stopCompile();
|
|
|
|
}
|
|
|
|
|
2021-04-21 18:58:35 +08:00
|
|
|
bool CompilerManager::canCompile(const QString &filename)
|
|
|
|
{
|
|
|
|
return !compiling();
|
2021-04-20 22:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerManager::onCompileFinished()
|
|
|
|
{
|
2021-06-25 12:40:11 +08:00
|
|
|
QMutexLocker locker(&mCompileMutex);
|
2021-04-24 15:57:45 +08:00
|
|
|
delete mCompiler;
|
2021-06-24 16:05:19 +08:00
|
|
|
mCompiler=nullptr;
|
2021-04-20 22:24:33 +08:00
|
|
|
}
|
|
|
|
|
2021-04-21 18:58:35 +08:00
|
|
|
void CompilerManager::onRunnerTerminated()
|
|
|
|
{
|
2021-06-25 12:40:11 +08:00
|
|
|
QMutexLocker locker(&mRunnerMutex);
|
2021-08-01 23:24:37 +08:00
|
|
|
ExecutableRunner* p=mRunner;
|
2021-04-21 18:58:35 +08:00
|
|
|
mRunner=nullptr;
|
2021-08-01 23:24:37 +08:00
|
|
|
p->deleteLater();
|
2021-04-21 18:58:35 +08:00
|
|
|
}
|
|
|
|
|
2021-06-24 16:05:19 +08:00
|
|
|
void CompilerManager::onCompileIssue(PCompileIssue)
|
|
|
|
{
|
|
|
|
mCompileErrorCount ++;
|
|
|
|
}
|
|
|
|
|
2021-06-24 20:43:09 +08:00
|
|
|
void CompilerManager::onSyntaxCheckFinished()
|
|
|
|
{
|
2021-06-25 12:40:11 +08:00
|
|
|
QMutexLocker locker(&mBackgroundSyntaxCheckMutex);
|
2021-06-24 20:43:09 +08:00
|
|
|
delete mBackgroundSyntaxChecker;
|
|
|
|
mBackgroundSyntaxChecker=nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerManager::onSyntaxCheckIssue(PCompileIssue)
|
|
|
|
{
|
|
|
|
mSyntaxCheckErrorCount++;
|
|
|
|
}
|
|
|
|
|
2021-06-23 22:38:02 +08:00
|
|
|
int CompilerManager::syntaxCheckErrorCount() const
|
|
|
|
{
|
|
|
|
return mSyntaxCheckErrorCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CompilerManager::compileErrorCount() const
|
|
|
|
{
|
|
|
|
return mCompileErrorCount;
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:26 +08:00
|
|
|
CompileError::CompileError(const QString &reason):BaseError(reason)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|