aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/buildgraph/commandexecutor.cpp
blob: 7a72a6c98d2cca7d9479e53f02932459272f4838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**************************************************************************
**
** This file is part of the Qt Build Suite
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file.
** Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**************************************************************************/
#include "commandexecutor.h"
#include "command.h"
#include "buildgraph.h"
#include "processoutput.h"

#include <buildgraph/artifact.h>
#include <buildgraph/transformer.h>
#include <tools/logger.h>

#include <QtConcurrentRun>
#include <QDebug>
#include <QFutureWatcher>
#include <QProcess>
#include <QScriptEngine>
#include <QThread>
#include <QTemporaryFile>

namespace qbs {

struct JavaScriptCommandFutureResult
{
    bool success;
    QString errorMessage;
};

class JavaScriptCommandFutureWatcher : public QFutureWatcher<JavaScriptCommandFutureResult>
{
public:
    JavaScriptCommandFutureWatcher(QObject *parent)
        : QFutureWatcher<JavaScriptCommandFutureResult>(parent)
    {}
};

CommandExecutor::CommandExecutor(QObject *parent)
    : QObject(parent)
    , m_processCommand(0)
    , m_process(0)
    , m_mainThreadScriptEngine(0)
    , m_transformer(0)
    , m_jsCommand(0)
    , m_jsFutureWatcher(0)
{
    connect(&m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onProcessError(QProcess::ProcessError)));
    connect(&m_process, SIGNAL(finished(int)), this, SLOT(onProcessFinished(int)));
}

void CommandExecutor::setProcessEnvironment(const QProcessEnvironment &processEnvironment)
{
    m_process.setProcessEnvironment(processEnvironment);
}

void CommandExecutor::waitForFinished()
{
    if (m_process.state() == QProcess::Running)
        m_process.waitForFinished(-1);
    if (m_jsFutureWatcher && m_jsFutureWatcher->isRunning())
        m_jsFutureWatcher->waitForFinished();
}

void CommandExecutor::start(Transformer *transformer, AbstractCommand *cmd)
{
    m_processCommand = 0;
    m_jsCommand = 0;

    switch (cmd->type()) {
    case AbstractCommand::AbstractCommandType:
        qWarning("CommandExecutor can't execute abstract commands.");
        return;
    case AbstractCommand::ProcessCommandType:
        m_processCommand = static_cast<ProcessCommand*>(cmd);
        startProcessCommand();
        return;
    case AbstractCommand::JavaScriptCommandType:
        m_jsCommand = static_cast<JavaScriptCommand*>(cmd);
        m_transformer = transformer;
        startJavaScriptCommand();
        return;
    }

    emit error("CommandExecutor: unknown command type.");
    return;
}

static QHash<QString, TextColor> setupColorTable()
{
    QHash<QString, TextColor> colorTable;
    colorTable["compiler"] = TextColorDefault;
    colorTable["linker"] = TextColorDarkGreen;
    colorTable["codegen"] = TextColorDarkYellow;
    return colorTable;
}

void CommandExecutor::printCommandInfo(AbstractCommand *cmd)
{
    if (!cmd->description().isEmpty()) {
        static QHash<QString, TextColor> colorTable = setupColorTable();
        qbsInfo() << DontPrintLogLevel << LogOutputStdOut
                  << colorTable.value(cmd->highlight(), TextColorDefault)
                  << cmd->description();
    }
}

void CommandExecutor::startProcessCommand()
{
    Q_ASSERT(m_process.state() == QProcess::NotRunning);

    printCommandInfo(m_processCommand);
    if (!m_processCommand->isSilent()) {
        QString commandLine = m_processCommand->program() + QLatin1Char(' ') + m_processCommand->arguments().join(" ");
        qbsInfo() << DontPrintLogLevel << LogOutputStdOut << commandLine;
    }
    if (qbsLogLevel(LoggerDebug)) {
        qbsDebug() << "[EXEC] " << m_processCommand->program() + QLatin1Char(' ') + m_processCommand->arguments().join(" ");
    }

    // Automatically use response files, if the command line gets to long.
    QStringList arguments = m_processCommand->arguments();
    if (!m_processCommand->responseFileUsagePrefix().isEmpty()) {
        int commandLineLength = m_processCommand->program().length() + 1;
        for (int i = m_processCommand->arguments().count(); --i >= 0;)
            commandLineLength += m_processCommand->arguments().at(i).length();
        if (m_processCommand->responseFileThreshold() >= 0 && commandLineLength > m_processCommand->responseFileThreshold()) {
            if (qbsLogLevel(LoggerDebug))
                qbsDebug("[EXEC] Using response file. Threshold is %d. Command line length %d.", m_processCommand->responseFileThreshold(), commandLineLength);

            // The QTemporaryFile keeps a handle on the file, even if closed.
            // On Windows, some commands (e.g. msvc link.exe) won't accept that.
            // We need to delete the file manually, later.
            QTemporaryFile responseFile;
            responseFile.setAutoRemove(false);
            responseFile.setFileTemplate(QDir::tempPath() + "/qbsresp");
            if (!responseFile.open()) {
                QString errorMessage = "Cannot create response file.";
                emit error(errorMessage);
                return;
            }
            for (int i = 0; i < m_processCommand->arguments().count(); ++i) {
                responseFile.write(m_processCommand->arguments().at(i).toLocal8Bit());
                responseFile.write("\n");
            }
            responseFile.close();
            arguments.clear();
            arguments += QDir::toNativeSeparators(m_processCommand->responseFileUsagePrefix() + responseFile.fileName());
            if (qbsLogLevel(LoggerDebug))
                qbsDebug("[EXEC] command line with response file: %s %s", qPrintable(m_processCommand->program()), qPrintable(arguments.join(" ")));
        }
    }

    m_process.setWorkingDirectory(m_processCommand->workingDir());
    m_process.start(m_processCommand->program(), arguments);
}

QByteArray CommandExecutor::filterProcessOutput(const QByteArray &output, const QString &filterFunctionSource)
{
    if (filterFunctionSource.isEmpty())
        return output;

    QScriptValue filterFunction = m_mainThreadScriptEngine->evaluate("var f = " + filterFunctionSource + "; f");
    if (!filterFunction.isFunction()) {
        emit error(QString("Error in filter function: %1.\n%2").arg(filterFunctionSource, filterFunction.toString()));
        return output;
    }

    QScriptValue outputArg = m_mainThreadScriptEngine->newArray(1);
    outputArg.setProperty(0, m_mainThreadScriptEngine->toScriptValue(QString::fromLatin1(output)));
    QScriptValue filteredOutput = filterFunction.call(m_mainThreadScriptEngine->undefinedValue(), outputArg);
    if (filteredOutput.isError()) {
        emit error(QString("Error when calling ouput filter function: %1").arg(filteredOutput.toString()));
        return output;
    }

    return filteredOutput.toString().toLocal8Bit();
}

void CommandExecutor::sendProcessOutput(bool logCommandLine)
{
    QString commandLine = m_processCommand->program();
    if (!m_processCommand->arguments().isEmpty()) {
        commandLine += ' ';
        commandLine += m_processCommand->arguments().join(" ");
    }

    QByteArray processStdOut = filterProcessOutput(m_process.readAllStandardOutput(), m_processCommand->stdoutFilterFunction());
    QByteArray processStdErr = filterProcessOutput(m_process.readAllStandardError(), m_processCommand->stderrFilterFunction());

    bool processOutputEmpty = processStdOut.isEmpty() && processStdErr.isEmpty();
    if (logCommandLine || !processOutputEmpty) {
        qbsInfo() << DontPrintLogLevel << commandLine << (processOutputEmpty ? "" : "\n")
                  << processStdOut << processStdErr;
    }

    ProcessOutput processOutput;
    processOutput.setCommandLine(commandLine);
    processOutput.setStandardOutput(processStdOut);
    processOutput.setStandardError(processStdErr);
    Logger::instance().sendProcessOutput(processOutput);
}

void CommandExecutor::onProcessError(QProcess::ProcessError processError)
{
    removeResponseFile();
    sendProcessOutput(true);
    QString errorMessage;
    switch (processError) {
    case QProcess::FailedToStart:
        errorMessage = "Process could not be started.";
        break;
    case QProcess::Crashed:
        errorMessage = "Process crashed.";
        break;
    case QProcess::Timedout:
        errorMessage = "Process timed out.";
        break;
    case QProcess::ReadError:
        errorMessage = "Error when reading process output.";
        break;
    case QProcess::WriteError:
        errorMessage = "Error when writing to process.";
        break;
    default:
        errorMessage = "Unknown process error.";
        break;
    }
    emit error(errorMessage);
}

void CommandExecutor::onProcessFinished(int exitCode)
{
    removeResponseFile();
    bool errorOccurred = exitCode > m_processCommand->maxExitCode();
    sendProcessOutput(errorOccurred);
    if (errorOccurred) {
        QString msg = "Process failed with exit code %1.";
        emit error(msg.arg(exitCode));
        return;
    }

    emit finished();
}

class JSRunner
{
public:
    typedef JavaScriptCommandFutureResult result_type;

    JSRunner(JavaScriptCommand *jsCommand)
        : m_jsCommand(jsCommand)
    {}

    JavaScriptCommandFutureResult operator() (Transformer *transformer)
    {
        result_type result;
        result.success = true;
        QThread *currentThread = QThread::currentThread();
        QScriptEngine *scriptEngine = m_scriptEnginesPerThread.value(currentThread);
        if (!scriptEngine) {
            scriptEngine = new QScriptEngine();
            m_scriptEnginesPerThread.insert(currentThread, scriptEngine);

            // import script extension plugins
            foreach (const QString &name, scriptEngine->availableExtensions()) {
                if (!name.startsWith("qbs"))
                    continue;
                QScriptValue e = scriptEngine->importExtension(name);
                if (e.isError()) {
                    qbsWarning("JS thread %x, unable to load %s into QScriptEngine: %s",
                               (void*)currentThread,
                               qPrintable(name),
                               qPrintable(e.toString()));
                }
                qbsDebug("JS thread %x, script plugin loaded: %s", (void*)currentThread, qPrintable(name));
            }
        }

        QString trafoPtrStr = QString::number((qulonglong)transformer);
        if (scriptEngine->globalObject().property("_qbs_transformer_ptr").toString() != trafoPtrStr) {
            scriptEngine->globalObject().setProperty("_qbs_transformer_ptr", scriptEngine->toScriptValue(trafoPtrStr));

            Artifact *someOutputArtifact = *transformer->outputs.begin();
            if (someOutputArtifact->product) {
                ResolvedProduct::Ptr product = someOutputArtifact->product->rProduct;
                BuildGraph::setupScriptEngineForProduct(scriptEngine, product, transformer->rule);
            }
            transformer->setupInputs(scriptEngine, scriptEngine->globalObject());
            transformer->setupOutputs(scriptEngine, scriptEngine->globalObject());
        }

        scriptEngine->pushContext();
        for (QVariantMap::const_iterator it = m_jsCommand->properties().constBegin(); it != m_jsCommand->properties().constEnd(); ++it)
            scriptEngine->currentContext()->activationObject().setProperty(it.key(), scriptEngine->toScriptValue(it.value()));
        scriptEngine->evaluate(m_jsCommand->sourceCode());
        if (scriptEngine->hasUncaughtException()) {
            result.success = false;
            result.errorMessage = scriptEngine->uncaughtException().toString();
        }
        scriptEngine->popContext();
        return result;
    }

private:
    static QHash<QThread *, QScriptEngine *> m_scriptEnginesPerThread;
    JavaScriptCommand *m_jsCommand;
};

QHash<QThread *, QScriptEngine *> JSRunner::m_scriptEnginesPerThread;

void CommandExecutor::startJavaScriptCommand()
{
    printCommandInfo(m_jsCommand);
    QFuture<JSRunner::result_type> future = QtConcurrent::run(JSRunner(m_jsCommand), m_transformer);
    if (!m_jsFutureWatcher) {
        m_jsFutureWatcher = new JavaScriptCommandFutureWatcher(this);
        connect(m_jsFutureWatcher, SIGNAL(finished()), SLOT(onJavaScriptCommandFinished()));
    }
    m_jsFutureWatcher->setFuture(future);
}

void CommandExecutor::onJavaScriptCommandFinished()
{
    JavaScriptCommandFutureResult result = m_jsFutureWatcher->future().result();
    if (result.success) {
        emit finished();
    } else {
        qbsInfo() << DontPrintLogLevel << "JS context:\n" << m_jsCommand->properties();
        qbsInfo() << DontPrintLogLevel << "JS code:\n" << m_jsCommand->sourceCode();
        QString msg = "Error while executing JavaScriptCommand: ";
        msg += result.errorMessage;
        emit error(msg);
    }
}

void CommandExecutor::removeResponseFile()
{
    if (m_responseFileName.isEmpty())
        return;
    QFile::remove(m_responseFileName);
    m_responseFileName.clear();
}

} // namespace qbs