1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "abstractprocessstep.h"
35 #include "buildconfiguration.h"
36 #include "buildstep.h"
37 #include "ioutputparser.h"
41 #include <utils/qtcassert.h>
42 #include <utils/qtcprocess.h>
44 #include <QtCore/QEventLoop>
45 #include <QtCore/QTimer>
46 #include <QtCore/QDir>
48 using namespace ProjectExplorer;
50 AbstractProcessStep::AbstractProcessStep(BuildStepList *bsl, const QString &id) :
51 BuildStep(bsl, id), m_timer(0), m_futureInterface(0),
52 m_enabled(true), m_ignoreReturnValue(false),
53 m_process(0), m_eventLoop(0), m_outputParserChain(0)
57 AbstractProcessStep::AbstractProcessStep(BuildStepList *bsl,
58 AbstractProcessStep *bs) :
59 BuildStep(bsl, bs), m_timer(0), m_futureInterface(0),
60 m_enabled(bs->m_enabled), m_ignoreReturnValue(bs->m_ignoreReturnValue),
61 m_process(0), m_eventLoop(0), m_outputParserChain(0)
65 AbstractProcessStep::~AbstractProcessStep()
69 // do not delete m_futureInterface, we do not own it.
70 delete m_outputParserChain;
73 void AbstractProcessStep::setOutputParser(ProjectExplorer::IOutputParser *parser)
75 delete m_outputParserChain;
76 m_outputParserChain = parser;
78 if (m_outputParserChain) {
79 connect(parser, SIGNAL(addOutput(QString, ProjectExplorer::BuildStep::OutputFormat)),
80 this, SLOT(outputAdded(QString, ProjectExplorer::BuildStep::OutputFormat)));
81 connect(parser, SIGNAL(addTask(ProjectExplorer::Task)),
82 this, SLOT(taskAdded(ProjectExplorer::Task)));
86 void AbstractProcessStep::appendOutputParser(ProjectExplorer::IOutputParser *parser)
91 QTC_ASSERT(m_outputParserChain, return);
92 m_outputParserChain->appendOutputParser(parser);
96 ProjectExplorer::IOutputParser *AbstractProcessStep::outputParser() const
98 return m_outputParserChain;
101 void AbstractProcessStep::setIgnoreReturnValue(bool b)
103 m_ignoreReturnValue = b;
106 bool AbstractProcessStep::init()
111 void AbstractProcessStep::run(QFutureInterface<bool> &fi)
113 m_futureInterface = &fi;
115 fi.reportResult(true);
118 QDir wd(m_param.effectiveWorkingDirectory());
120 wd.mkpath(wd.absolutePath());
122 m_process = new Utils::QtcProcess();
123 m_process->setWorkingDirectory(wd.absolutePath());
124 m_process->setEnvironment(m_param.environment());
126 connect(m_process, SIGNAL(readyReadStandardOutput()),
127 this, SLOT(processReadyReadStdOutput()),
128 Qt::DirectConnection);
129 connect(m_process, SIGNAL(readyReadStandardError()),
130 this, SLOT(processReadyReadStdError()),
131 Qt::DirectConnection);
133 connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
134 this, SLOT(slotProcessFinished(int, QProcess::ExitStatus)),
135 Qt::DirectConnection);
137 m_process->setCommand(m_param.effectiveCommand(), m_param.effectiveArguments());
139 if (!m_process->waitForStarted()) {
140 processStartupFailed();
143 fi.reportResult(false);
148 m_timer = new QTimer();
149 connect(m_timer, SIGNAL(timeout()), this, SLOT(checkForCancel()), Qt::DirectConnection);
151 m_eventLoop = new QEventLoop;
157 // The process has finished, leftover data is read in processFinished
158 processFinished(m_process->exitCode(), m_process->exitStatus());
159 bool returnValue = processSucceeded(m_process->exitCode(), m_process->exitStatus()) || m_ignoreReturnValue;
161 // Clean up output parsers
162 if (m_outputParserChain) {
163 delete m_outputParserChain;
164 m_outputParserChain = 0;
171 fi.reportResult(returnValue);
172 m_futureInterface = 0;
176 void AbstractProcessStep::processStarted()
178 emit addOutput(tr("Starting: \"%1\" %2\n")
179 .arg(QDir::toNativeSeparators(m_param.effectiveCommand()),
180 m_param.prettyArguments()),
181 BuildStep::MessageOutput);
184 void AbstractProcessStep::processFinished(int exitCode, QProcess::ExitStatus status)
186 QString command = QDir::toNativeSeparators(m_param.effectiveCommand());
187 if (status == QProcess::NormalExit && exitCode == 0) {
188 emit addOutput(tr("The process \"%1\" exited normally.").arg(command),
189 BuildStep::MessageOutput);
190 } else if (status == QProcess::NormalExit) {
191 emit addOutput(tr("The process \"%1\" exited with code %2.")
192 .arg(command, QString::number(m_process->exitCode())),
193 BuildStep::ErrorMessageOutput);
195 emit addOutput(tr("The process \"%1\" crashed.").arg(command), BuildStep::ErrorMessageOutput);
199 void AbstractProcessStep::processStartupFailed()
201 emit addOutput(tr("Could not start process \"%1\" %2")
202 .arg(QDir::toNativeSeparators(m_param.effectiveCommand()),
203 m_param.prettyArguments()),
204 BuildStep::ErrorMessageOutput);
207 bool AbstractProcessStep::processSucceeded(int exitCode, QProcess::ExitStatus status)
209 return exitCode == 0 && status == QProcess::NormalExit;
212 void AbstractProcessStep::processReadyReadStdOutput()
214 m_process->setReadChannel(QProcess::StandardOutput);
215 while (m_process->canReadLine()) {
216 QString line = QString::fromLocal8Bit(m_process->readLine());
221 void AbstractProcessStep::stdOutput(const QString &line)
223 if (m_outputParserChain)
224 m_outputParserChain->stdOutput(line);
225 emit addOutput(line, BuildStep::NormalOutput);
228 void AbstractProcessStep::processReadyReadStdError()
230 m_process->setReadChannel(QProcess::StandardError);
231 while (m_process->canReadLine()) {
232 QString line = QString::fromLocal8Bit(m_process->readLine());
237 void AbstractProcessStep::stdError(const QString &line)
239 if (m_outputParserChain)
240 m_outputParserChain->stdError(line);
241 emit addOutput(line, BuildStep::ErrorOutput);
244 void AbstractProcessStep::checkForCancel()
246 if (m_futureInterface->isCanceled() && m_timer->isActive()) {
248 m_process->terminate();
249 m_process->waitForFinished(5000);
254 void AbstractProcessStep::taskAdded(const ProjectExplorer::Task &task)
256 // Do not bother to report issues if we do not care about the results of
257 // the buildstep anyway:
258 if (m_ignoreReturnValue)
262 QString filePath = QDir::cleanPath(task.file.trimmed());
263 if (!filePath.isEmpty() && !QDir::isAbsolutePath(filePath)) {
264 // We have no save way to decide which file in which subfolder
265 // is meant. Therefore we apply following heuristics:
266 // 1. Check if file is unique in whole project
267 // 2. Otherwise try again without any ../
270 QList<QFileInfo> possibleFiles;
271 QString fileName = QFileInfo(filePath).fileName();
272 foreach (const QString &file, buildConfiguration()->target()->project()->files(ProjectExplorer::Project::AllFiles)) {
273 QFileInfo candidate(file);
274 if (candidate.fileName() == fileName)
275 possibleFiles << candidate;
278 if (possibleFiles.count() == 1) {
279 editable.file = possibleFiles.first().filePath();
281 // More then one filename, so do a better compare
283 while (filePath.startsWith(QLatin1String("../")))
284 filePath.remove(0, 3);
286 QString possibleFilePath;
287 foreach(const QFileInfo &fi, possibleFiles) {
288 if (fi.filePath().endsWith(filePath)) {
289 possibleFilePath = fi.filePath();
294 editable.file = possibleFilePath;
296 qWarning() << "Could not find absolute location of file " << filePath;
299 emit addTask(editable);
302 void AbstractProcessStep::outputAdded(const QString &string, ProjectExplorer::BuildStep::OutputFormat format)
304 emit addOutput(string, format);
307 void AbstractProcessStep::slotProcessFinished(int, QProcess::ExitStatus)
309 QString line = QString::fromLocal8Bit(m_process->readAllStandardError());
313 line = QString::fromLocal8Bit(m_process->readAllStandardOutput());
317 m_eventLoop->exit(0);