Files
qt-creator/src/plugins/projectexplorer/abstractprocessstep.cpp

257 lines
8.3 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2008-12-02 12:01:29 +01:00
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
2009-08-14 09:30:56 +02:00
** contact the sales department at http://qt.nokia.com/contact.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 16:19:05 +01:00
2008-12-02 12:01:29 +01:00
#include "abstractprocessstep.h"
#include "buildstep.h"
#include "project.h"
2008-12-02 16:19:05 +01:00
2008-12-02 12:01:29 +01:00
#include <QtCore/QProcess>
#include <QtCore/QEventLoop>
#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtGui/QTextDocument>
2008-12-02 12:01:29 +01:00
using namespace ProjectExplorer;
2009-03-18 11:53:09 +01:00
static const char * const PROCESS_COMMAND = "abstractProcess.command";
static const char * const PROCESS_WORKINGDIRECTORY = "abstractProcess.workingDirectory";
static const char * const PROCESS_ARGUMENTS = "abstractProcess.arguments";
static const char * const PROCESS_ENABLED = "abstractProcess.enabled";
static const char * const PROCESS_ENVIRONMENT = "abstractProcess.Environment";
static const char * const PROCESS_IGNORE_RETURN_VALUE = "abstractProcess.IgnoreReturnValue";
2009-03-18 11:53:09 +01:00
2008-12-02 12:01:29 +01:00
AbstractProcessStep::AbstractProcessStep(Project *pro)
2008-12-02 16:19:05 +01:00
: BuildStep(pro)
2008-12-02 12:01:29 +01:00
{
}
void AbstractProcessStep::setCommand(const QString &buildConfiguration, const QString &cmd)
{
2009-03-18 11:53:09 +01:00
setValue(buildConfiguration, PROCESS_COMMAND, cmd);
2008-12-02 12:01:29 +01:00
}
QString AbstractProcessStep::command(const QString &buildConfiguration) const
{
QString result = value(buildConfiguration, PROCESS_COMMAND).toString();
if (QFileInfo(result).isRelative()) {
QString searchInPath = environment(buildConfiguration).searchInPath(result);
if (!searchInPath.isEmpty())
result = searchInPath;
}
return result;
2008-12-02 12:01:29 +01:00
}
void AbstractProcessStep::setWorkingDirectory(const QString &buildConfiguration, const QString &workingDirectory)
{
2009-03-18 11:53:09 +01:00
setValue(buildConfiguration, PROCESS_WORKINGDIRECTORY, workingDirectory);
2008-12-02 12:01:29 +01:00
}
QString AbstractProcessStep::workingDirectory(const QString &buildConfiguration) const
{
2009-03-18 11:53:09 +01:00
return value(buildConfiguration, PROCESS_WORKINGDIRECTORY).toString();
2008-12-02 12:01:29 +01:00
}
void AbstractProcessStep::setArguments(const QString &buildConfiguration, const QStringList &arguments)
{
2009-03-18 11:53:09 +01:00
setValue(buildConfiguration, PROCESS_ARGUMENTS, arguments);
2008-12-02 12:01:29 +01:00
}
QStringList AbstractProcessStep::arguments(const QString &buildConfiguration) const
{
2009-03-18 11:53:09 +01:00
return value(buildConfiguration, PROCESS_ARGUMENTS).toStringList();
2008-12-02 12:01:29 +01:00
}
void AbstractProcessStep::setEnabled(const QString &buildConfiguration, bool b)
{
2009-03-18 11:53:09 +01:00
setValue(buildConfiguration, PROCESS_ENABLED, b);
2008-12-02 12:01:29 +01:00
}
bool AbstractProcessStep::enabled(const QString &buildConfiguration) const
{
2009-03-18 11:53:09 +01:00
return value(buildConfiguration, PROCESS_ENABLED).toBool();
2008-12-02 12:01:29 +01:00
}
void AbstractProcessStep::setIgnoreReturnValue(const QString &buildConfiguration, bool b)
{
setValue(buildConfiguration, PROCESS_IGNORE_RETURN_VALUE, b);
}
bool AbstractProcessStep::ignoreReturnValue(const QString &buildConfiguration) const
{
return value(buildConfiguration, PROCESS_IGNORE_RETURN_VALUE).toBool();
}
2008-12-02 12:01:29 +01:00
void AbstractProcessStep::setEnvironment(const QString &buildConfiguration, Environment env)
{
2009-03-18 11:53:09 +01:00
setValue(buildConfiguration, PROCESS_ENVIRONMENT, env.toStringList());
2008-12-02 12:01:29 +01:00
}
Environment AbstractProcessStep::environment(const QString &buildConfiguration) const
{
2009-03-18 11:53:09 +01:00
return Environment(value(buildConfiguration, PROCESS_ENVIRONMENT).toStringList());
2008-12-02 12:01:29 +01:00
}
bool AbstractProcessStep::init(const QString &name)
{
2009-03-18 11:53:09 +01:00
m_command = command(name);
m_arguments = arguments(name);
m_enabled = enabled(name);
m_workingDirectory = workingDirectory(name);
m_environment = environment(name);
m_ignoreReturnValue = ignoreReturnValue(name);
2008-12-02 12:01:29 +01:00
return true;
}
void AbstractProcessStep::run(QFutureInterface<bool> & fi)
{
m_futureInterface = &fi;
2008-12-09 11:07:24 +01:00
if (!m_enabled) {
2008-12-02 12:01:29 +01:00
fi.reportResult(true);
return;
}
QDir wd(m_workingDirectory);
if (!wd.exists())
wd.mkpath(wd.absolutePath());
m_process = new QProcess();
m_process->setWorkingDirectory(m_workingDirectory);
m_process->setEnvironment(m_environment.toStringList());
connect(m_process, SIGNAL(readyReadStandardOutput()),
this, SLOT(processReadyReadStdOutput()),
Qt::DirectConnection);
connect(m_process, SIGNAL(readyReadStandardError()),
this, SLOT(processReadyReadStdError()),
Qt::DirectConnection);
connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(slotProcessFinished(int, QProcess::ExitStatus)),
Qt::DirectConnection);
m_process->start(m_command, m_arguments);
2008-12-09 11:07:24 +01:00
if (!m_process->waitForStarted()) {
2008-12-02 12:01:29 +01:00
processStartupFailed();
delete m_process;
m_process = 0;
fi.reportResult(false);
return;
}
processStarted();
m_timer = new QTimer();
connect(m_timer, SIGNAL(timeout()), this, SLOT(checkForCancel()), Qt::DirectConnection);
m_timer->start(500);
m_eventLoop = new QEventLoop;
m_eventLoop->exec();
m_timer->stop();
delete m_timer;
// The process has finished, leftover data is read in processFinished
bool returnValue = processFinished(m_process->exitCode(), m_process->exitStatus());
delete m_process;
m_process = 0;
delete m_eventLoop;
m_eventLoop = 0;
fi.reportResult(returnValue);
return;
}
void AbstractProcessStep::processStarted()
{
emit addToOutputWindow(tr("<font color=\"#0000ff\">Starting: %1 %2</font>\n").arg(m_command, Qt::escape(m_arguments.join(" "))));
2008-12-02 12:01:29 +01:00
}
bool AbstractProcessStep::processFinished(int exitCode, QProcess::ExitStatus status)
{
const bool ok = status == QProcess::NormalExit && (exitCode == 0 || m_ignoreReturnValue);
2008-12-02 12:01:29 +01:00
if (ok) {
emit addToOutputWindow(tr("<font color=\"#0000ff\">Exited with code %1.</font>").arg(m_process->exitCode()));
} else {
emit addToOutputWindow(tr("<font color=\"#ff0000\"><b>Exited with code %1.</b></font>").arg(m_process->exitCode()));
}
return ok;
}
void AbstractProcessStep::processStartupFailed()
{
emit addToOutputWindow(tr("<font color=\"#ff0000\">Could not start process %1 </b></font>").arg(m_command));
}
void AbstractProcessStep::processReadyReadStdOutput()
{
m_process->setReadChannel(QProcess::StandardOutput);
2008-12-09 11:07:24 +01:00
while (m_process->canReadLine()) {
2008-12-02 12:01:29 +01:00
QString line = QString::fromLocal8Bit(m_process->readLine()).trimmed();
stdOut(line);
}
}
void AbstractProcessStep::stdOut(const QString &line)
{
emit addToOutputWindow(Qt::escape(line));
2008-12-02 12:01:29 +01:00
}
void AbstractProcessStep::processReadyReadStdError()
{
m_process->setReadChannel(QProcess::StandardError);
2008-12-09 11:07:24 +01:00
while (m_process->canReadLine()) {
2008-12-02 12:01:29 +01:00
QString line = QString::fromLocal8Bit(m_process->readLine()).trimmed();
stdError(line);
}
}
void AbstractProcessStep::stdError(const QString &line)
{
emit addToOutputWindow(QLatin1String("<font color=\"#ff0000\">") + Qt::escape(line) + QLatin1String("</font>"));
2008-12-02 12:01:29 +01:00
}
void AbstractProcessStep::checkForCancel()
{
2008-12-09 11:07:24 +01:00
if (m_futureInterface->isCanceled() && m_timer->isActive()) {
2008-12-02 12:01:29 +01:00
m_timer->stop();
m_process->terminate();
m_process->waitForFinished(5000);
m_process->kill();
}
}
void AbstractProcessStep::slotProcessFinished(int, QProcess::ExitStatus)
{
QString line = QString::fromLocal8Bit(m_process->readAllStandardError()).trimmed();
2008-12-09 11:07:24 +01:00
if (!line.isEmpty())
2008-12-02 12:01:29 +01:00
stdOut(line);
line = QString::fromLocal8Bit(m_process->readAllStandardOutput()).trimmed();
2008-12-09 11:07:24 +01:00
if (!line.isEmpty())
2008-12-02 12:01:29 +01:00
stdError(line);
2008-12-09 11:07:24 +01:00
2008-12-02 12:01:29 +01:00
m_eventLoop->exit(0);
}