OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / abstractprocessstep.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "abstractprocessstep.h"
35 #include "buildconfiguration.h"
36 #include "buildstep.h"
37 #include "ioutputparser.h"
38 #include "project.h"
39 #include "target.h"
40
41 #include <utils/qtcassert.h>
42 #include <utils/qtcprocess.h>
43
44 #include <QtCore/QEventLoop>
45 #include <QtCore/QTimer>
46 #include <QtCore/QDir>
47
48 using namespace ProjectExplorer;
49
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)
54 {
55 }
56
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)
62 {
63 }
64
65 AbstractProcessStep::~AbstractProcessStep()
66 {
67     delete m_process;
68     delete m_timer;
69     // do not delete m_futureInterface, we do not own it.
70     delete m_outputParserChain;
71 }
72
73 void AbstractProcessStep::setOutputParser(ProjectExplorer::IOutputParser *parser)
74 {
75     delete m_outputParserChain;
76     m_outputParserChain = parser;
77
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)));
83     }
84 }
85
86 void AbstractProcessStep::appendOutputParser(ProjectExplorer::IOutputParser *parser)
87 {
88     if (!parser)
89         return;
90
91     QTC_ASSERT(m_outputParserChain, return);
92     m_outputParserChain->appendOutputParser(parser);
93     return;
94 }
95
96 ProjectExplorer::IOutputParser *AbstractProcessStep::outputParser() const
97 {
98     return m_outputParserChain;
99 }
100
101 void AbstractProcessStep::setIgnoreReturnValue(bool b)
102 {
103     m_ignoreReturnValue = b;
104 }
105
106 bool AbstractProcessStep::init()
107 {
108     return true;
109 }
110
111 void AbstractProcessStep::run(QFutureInterface<bool> &fi)
112 {
113     m_futureInterface = &fi;
114     if (!m_enabled) {
115         fi.reportResult(true);
116         return;
117     }
118     QDir wd(m_param.effectiveWorkingDirectory());
119     if (!wd.exists())
120         wd.mkpath(wd.absolutePath());
121
122     m_process = new Utils::QtcProcess();
123     m_process->setWorkingDirectory(wd.absolutePath());
124     m_process->setEnvironment(m_param.environment());
125
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);
132
133     connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
134             this, SLOT(slotProcessFinished(int, QProcess::ExitStatus)),
135             Qt::DirectConnection);
136
137     m_process->setCommand(m_param.effectiveCommand(), m_param.effectiveArguments());
138     m_process->start();
139     if (!m_process->waitForStarted()) {
140         processStartupFailed();
141         delete m_process;
142         m_process = 0;
143         fi.reportResult(false);
144         return;
145     }
146     processStarted();
147
148     m_timer = new QTimer();
149     connect(m_timer, SIGNAL(timeout()), this, SLOT(checkForCancel()), Qt::DirectConnection);
150     m_timer->start(500);
151     m_eventLoop = new QEventLoop;
152     m_eventLoop->exec();
153     m_timer->stop();
154     delete m_timer;
155     m_timer = 0;
156
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;
160
161     // Clean up output parsers
162     if (m_outputParserChain) {
163         delete m_outputParserChain;
164         m_outputParserChain = 0;
165     }
166
167     delete m_process;
168     m_process = 0;
169     delete m_eventLoop;
170     m_eventLoop = 0;
171     fi.reportResult(returnValue);
172     m_futureInterface = 0;
173     return;
174 }
175
176 void AbstractProcessStep::processStarted()
177 {
178     emit addOutput(tr("Starting: \"%1\" %2\n")
179                    .arg(QDir::toNativeSeparators(m_param.effectiveCommand()),
180                         m_param.prettyArguments()),
181                    BuildStep::MessageOutput);
182 }
183
184 void AbstractProcessStep::processFinished(int exitCode, QProcess::ExitStatus status)
185 {
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);
194     } else {
195         emit addOutput(tr("The process \"%1\" crashed.").arg(command), BuildStep::ErrorMessageOutput);
196     }
197 }
198
199 void AbstractProcessStep::processStartupFailed()
200 {
201     emit addOutput(tr("Could not start process \"%1\" %2")
202                    .arg(QDir::toNativeSeparators(m_param.effectiveCommand()),
203                         m_param.prettyArguments()),
204                    BuildStep::ErrorMessageOutput);
205 }
206
207 bool AbstractProcessStep::processSucceeded(int exitCode, QProcess::ExitStatus status)
208 {
209     return exitCode == 0 && status == QProcess::NormalExit;
210 }
211
212 void AbstractProcessStep::processReadyReadStdOutput()
213 {
214     m_process->setReadChannel(QProcess::StandardOutput);
215     while (m_process->canReadLine()) {
216         QString line = QString::fromLocal8Bit(m_process->readLine());
217         stdOutput(line);
218     }
219 }
220
221 void AbstractProcessStep::stdOutput(const QString &line)
222 {
223     if (m_outputParserChain)
224         m_outputParserChain->stdOutput(line);
225     emit addOutput(line, BuildStep::NormalOutput);
226 }
227
228 void AbstractProcessStep::processReadyReadStdError()
229 {
230     m_process->setReadChannel(QProcess::StandardError);
231     while (m_process->canReadLine()) {
232         QString line = QString::fromLocal8Bit(m_process->readLine());
233         stdError(line);
234     }
235 }
236
237 void AbstractProcessStep::stdError(const QString &line)
238 {
239     if (m_outputParserChain)
240         m_outputParserChain->stdError(line);
241     emit addOutput(line, BuildStep::ErrorOutput);
242 }
243
244 void AbstractProcessStep::checkForCancel()
245 {
246     if (m_futureInterface->isCanceled() && m_timer->isActive()) {
247         m_timer->stop();
248         m_process->terminate();
249         m_process->waitForFinished(5000);
250         m_process->kill();
251     }
252 }
253
254 void AbstractProcessStep::taskAdded(const ProjectExplorer::Task &task)
255 {
256     // Do not bother to report issues if we do not care about the results of
257     // the buildstep anyway:
258     if (m_ignoreReturnValue)
259         return;
260
261     Task editable(task);
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 ../
268         // 3. give up.
269
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;
276         }
277
278         if (possibleFiles.count() == 1) {
279             editable.file = possibleFiles.first().filePath();
280         } else {
281             // More then one filename, so do a better compare
282             // Chop of any "../"
283             while (filePath.startsWith(QLatin1String("../")))
284                 filePath.remove(0, 3);
285             int count = 0;
286             QString possibleFilePath;
287             foreach(const QFileInfo &fi, possibleFiles) {
288                 if (fi.filePath().endsWith(filePath)) {
289                     possibleFilePath = fi.filePath();
290                     ++count;
291                 }
292             }
293             if (count == 1)
294                 editable.file = possibleFilePath;
295             else
296                 qWarning() << "Could not find absolute location of file " << filePath;
297         }
298     }
299     emit addTask(editable);
300 }
301
302 void AbstractProcessStep::outputAdded(const QString &string, ProjectExplorer::BuildStep::OutputFormat format)
303 {
304     emit addOutput(string, format);
305 }
306
307 void AbstractProcessStep::slotProcessFinished(int, QProcess::ExitStatus)
308 {
309     QString line = QString::fromLocal8Bit(m_process->readAllStandardError());
310     if (!line.isEmpty())
311         stdError(line);
312
313     line = QString::fromLocal8Bit(m_process->readAllStandardOutput());
314     if (!line.isEmpty())
315         stdOutput(line);
316
317     m_eventLoop->exit(0);
318 }