CMakeProcess: Simplify implementation by using ProcessProgress

Change-Id: I9e9665f7e57ebb7d3028396e792161aa2c3a648e
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2022-10-10 12:28:29 +02:00
parent 953000b981
commit 165d364a6d
2 changed files with 9 additions and 45 deletions

View File

@@ -7,7 +7,7 @@
#include "cmakeparser.h" #include "cmakeparser.h"
#include "cmakeprojectmanagertr.h" #include "cmakeprojectmanagertr.h"
#include <coreplugin/progressmanager/progressmanager.h> #include <coreplugin/progressmanager/processprogress.h>
#include <projectexplorer/buildsystem.h> #include <projectexplorer/buildsystem.h>
#include <projectexplorer/projectexplorerconstants.h> #include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/taskhub.h> #include <projectexplorer/taskhub.h>
@@ -16,15 +16,12 @@
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <utils/stringutils.h> #include <utils/stringutils.h>
#include <QFutureWatcher> using namespace Core;
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils; using namespace Utils;
namespace CMakeProjectManager::Internal { namespace CMakeProjectManager::Internal {
const int USER_STOP_EXIT_CODE = 15;
static QString stripTrailingNewline(QString str) static QString stripTrailingNewline(QString str)
{ {
if (str.endsWith('\n')) if (str.endsWith('\n'))
@@ -37,13 +34,6 @@ CMakeProcess::CMakeProcess() = default;
CMakeProcess::~CMakeProcess() CMakeProcess::~CMakeProcess()
{ {
m_parser.flush(); m_parser.flush();
if (m_futureWatcher) {
m_futureWatcher.reset();
// None of the progress related functions will work after this!
m_futureInterface.reportCanceled();
m_futureInterface.reportFinished();
}
} }
void CMakeProcess::run(const BuildDirParameters &parameters, const QStringList &arguments) void CMakeProcess::run(const BuildDirParameters &parameters, const QStringList &arguments)
@@ -130,17 +120,10 @@ void CMakeProcess::run(const BuildDirParameters &parameters, const QStringList &
BuildSystem::startNewBuildSystemOutput(::CMakeProjectManager::Tr::tr("Running %1 in %2.") BuildSystem::startNewBuildSystemOutput(::CMakeProjectManager::Tr::tr("Running %1 in %2.")
.arg(commandLine.toUserOutput(), buildDirectory.toUserOutput())); .arg(commandLine.toUserOutput(), buildDirectory.toUserOutput()));
m_futureInterface = QFutureInterface<void>(); ProcessProgress *progress = new ProcessProgress(m_process.get());
m_futureInterface.setProgressRange(0, 1); progress->setDisplayName(::CMakeProjectManager::Tr::tr("Configuring \"%1\"")
Core::ProgressManager::addTimedTask(m_futureInterface, .arg(parameters.projectName));
::CMakeProjectManager::Tr::tr("Configuring \"%1\"") m_process->setTimeoutS(10); // for process progress timeout estimation
.arg(parameters.projectName),
"CMake.Configure",
10);
m_futureWatcher.reset(new QFutureWatcher<void>);
connect(m_futureWatcher.get(), &QFutureWatcher<void>::canceled, this, &CMakeProcess::stop);
m_futureWatcher->setFuture(m_futureInterface.future());
m_process->setCommand(commandLine); m_process->setCommand(commandLine);
emit started(); emit started();
m_elapsed.start(); m_elapsed.start();
@@ -149,25 +132,19 @@ void CMakeProcess::run(const BuildDirParameters &parameters, const QStringList &
void CMakeProcess::stop() void CMakeProcess::stop()
{ {
if (!m_process) if (m_process)
return; m_process->stop();
m_process->close();
handleProcessDone({USER_STOP_EXIT_CODE, QProcess::CrashExit, QProcess::Crashed, {}});
} }
void CMakeProcess::handleProcessDone(const Utils::ProcessResultData &resultData) void CMakeProcess::handleProcessDone(const Utils::ProcessResultData &resultData)
{ {
if (m_futureWatcher) {
m_futureWatcher->disconnect();
m_futureWatcher.release()->deleteLater();
}
const int code = resultData.m_exitCode; const int code = resultData.m_exitCode;
QString msg; QString msg;
if (resultData.m_error == QProcess::FailedToStart) { if (resultData.m_error == QProcess::FailedToStart) {
msg = ::CMakeProjectManager::Tr::tr("CMake process failed to start."); msg = ::CMakeProjectManager::Tr::tr("CMake process failed to start.");
} else if (resultData.m_exitStatus != QProcess::NormalExit) { } else if (resultData.m_exitStatus != QProcess::NormalExit) {
if (m_futureInterface.isCanceled() || code == USER_STOP_EXIT_CODE) if (resultData.m_canceledByUser)
msg = ::CMakeProjectManager::Tr::tr("CMake process was canceled by the user."); msg = ::CMakeProjectManager::Tr::tr("CMake process was canceled by the user.");
else else
msg = ::CMakeProjectManager::Tr::tr("CMake process crashed."); msg = ::CMakeProjectManager::Tr::tr("CMake process crashed.");
@@ -179,13 +156,8 @@ void CMakeProcess::handleProcessDone(const Utils::ProcessResultData &resultData)
if (!msg.isEmpty()) { if (!msg.isEmpty()) {
BuildSystem::appendBuildSystemOutput(msg + '\n'); BuildSystem::appendBuildSystemOutput(msg + '\n');
TaskHub::addTask(BuildSystemTask(Task::Error, msg)); TaskHub::addTask(BuildSystemTask(Task::Error, msg));
m_futureInterface.reportCanceled();
} else {
m_futureInterface.setProgressValue(1);
} }
m_futureInterface.reportFinished();
emit finished(); emit finished();
const QString elapsedTime = Utils::formatElapsedTime(m_elapsed.elapsed()); const QString elapsedTime = Utils::formatElapsedTime(m_elapsed.elapsed());

View File

@@ -6,17 +6,11 @@
#include <utils/outputformatter.h> #include <utils/outputformatter.h>
#include <QElapsedTimer> #include <QElapsedTimer>
#include <QFutureInterface>
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringList>
#include <memory> #include <memory>
QT_BEGIN_NAMESPACE
template<class T>
class QFutureWatcher;
QT_END_NAMESPACE
namespace Utils { namespace Utils {
class ProcessResultData; class ProcessResultData;
class QtcProcess; class QtcProcess;
@@ -48,8 +42,6 @@ private:
std::unique_ptr<Utils::QtcProcess> m_process; std::unique_ptr<Utils::QtcProcess> m_process;
Utils::OutputFormatter m_parser; Utils::OutputFormatter m_parser;
QFutureInterface<void> m_futureInterface;
std::unique_ptr<QFutureWatcher<void>> m_futureWatcher;
QElapsedTimer m_elapsed; QElapsedTimer m_elapsed;
int m_lastExitCode = 0; int m_lastExitCode = 0;
}; };