maemoqemuruntimeparser.cpp 16.6 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** 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.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
31
32
33
34
**
**************************************************************************/
#include "maemoqemuruntimeparser.h"

#include "maemoglobal.h"
35
#include "maemoqemusettings.h"
36

37
#include <qtsupport/baseqtversion.h>
38
#include <utils/qtcassert.h>
39
40
41
42
43
44

#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtCore/QStringList>
#include <QtCore/QTextStream>

45
46
47
using namespace RemoteLinux;

namespace Madde {
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace Internal {

class MaemoQemuRuntimeParserV1 : public MaemoQemuRuntimeParser
{
public:
    MaemoQemuRuntimeParserV1(const QString &madInfoOutput,
        const QString &targetName, const QString &maddeRoot);
    MaemoQemuRuntime parseRuntime();

private:
    void fillRuntimeInformation(MaemoQemuRuntime *runtime) const;
    void setEnvironment(MaemoQemuRuntime *runTime, const QString &envSpec) const;
};

class MaemoQemuRuntimeParserV2 : public MaemoQemuRuntimeParser
{
public:
    MaemoQemuRuntimeParserV2(const QString &madInfoOutput,
66
        const QString &targetName, const QString &maddeRoot);
67
68
69
    MaemoQemuRuntime parseRuntime();

private:
70
    struct Port {
71
        Port() : port(-1), ssh(false) {}
72
73
74
75
        int port;
        bool ssh;
    };

76
77
    void handleTargetTag(QString &runtimeName);
    MaemoQemuRuntime handleRuntimeTag();
78
79
    void handleEnvironmentTag(MaemoQemuRuntime &runtime);
    void handleVariableTag(MaemoQemuRuntime &runtime);
80
81
    QList<Port> handleTcpPortListTag();
    Port handlePortTag();
82
    MaemoQemuSettings::OpenGlMode openGlTagToEnum(const QString &tag) const;
83
84
85
};

MaemoQemuRuntimeParser::MaemoQemuRuntimeParser(const QString &madInfoOutput,
86
87
88
89
    const QString &targetName, const QString &maddeRoot)
    : m_targetName(targetName),
      m_maddeRoot(maddeRoot),
      m_madInfoReader(madInfoOutput)
90
91
92
{
}

93
MaemoQemuRuntime MaemoQemuRuntimeParser::parseRuntime(const QtSupport::BaseQtVersion *qtVersion)
94
95
{
    MaemoQemuRuntime runtime;
96
    const QString maddeRootPath = MaemoGlobal::maddeRoot(qtVersion->qmakeCommand().toString());
97
    QProcess madProc;
98
    if (!MaemoGlobal::callMad(madProc, QStringList() << QLatin1String("info"), qtVersion->qmakeCommand().toString(), false))
99
        return runtime;
100
101
102
    if (!madProc.waitForStarted() || !madProc.waitForFinished())
        return runtime;
    const QByteArray &madInfoOutput = madProc.readAllStandardOutput();
103
    const QString &targetName = MaemoGlobal::targetName(qtVersion->qmakeCommand().toString());
104
105
    runtime = MaemoQemuRuntimeParserV2(madInfoOutput, targetName, maddeRootPath)
        .parseRuntime();
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    if (!runtime.m_name.isEmpty()) {
        runtime.m_root = maddeRootPath + QLatin1String("/runtimes/")
            + runtime.m_name;
    } else {
        runtime = MaemoQemuRuntimeParserV1(madInfoOutput, targetName,
            maddeRootPath).parseRuntime();
    }
    runtime.m_watchPath = runtime.m_root
        .left(runtime.m_root.lastIndexOf(QLatin1Char('/')));

    return runtime;
}

MaemoQemuRuntimeParserV1::MaemoQemuRuntimeParserV1(const QString &madInfoOutput,
    const QString &targetName, const QString &maddeRoot)
121
    : MaemoQemuRuntimeParser(madInfoOutput, targetName, maddeRoot)
122
123
124
125
126
127
128
129
130
131
132
133
{
}

MaemoQemuRuntime MaemoQemuRuntimeParserV1::parseRuntime()
{
    QStringList installedRuntimes;
    QString targetRuntime;
    while (!m_madInfoReader.atEnd() && !installedRuntimes.contains(targetRuntime)) {
        if (m_madInfoReader.readNext() == QXmlStreamReader::StartElement) {
            if (targetRuntime.isEmpty()
                && m_madInfoReader.name() == QLatin1String("target")) {
                const QXmlStreamAttributes &attrs = m_madInfoReader.attributes();
134
                if (attrs.value(QLatin1String("target_id")) == m_targetName)
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
                    targetRuntime = attrs.value("runtime_id").toString();
            } else if (m_madInfoReader.name() == QLatin1String("runtime")) {
                const QXmlStreamAttributes attrs = m_madInfoReader.attributes();
                while (!m_madInfoReader.atEnd()) {
                    if (m_madInfoReader.readNext() == QXmlStreamReader::EndElement
                         && m_madInfoReader.name() == QLatin1String("runtime"))
                        break;
                    if (m_madInfoReader.tokenType() == QXmlStreamReader::StartElement
                        && m_madInfoReader.name() == QLatin1String("installed")) {
                        if (m_madInfoReader.readNext() == QXmlStreamReader::Characters
                            && m_madInfoReader.text() == QLatin1String("true")) {
                            if (attrs.hasAttribute(QLatin1String("runtime_id")))
                                installedRuntimes << attrs.value(QLatin1String("runtime_id")).toString();
                            else if (attrs.hasAttribute(QLatin1String("id"))) {
                                // older MADDE seems to use only id
                                installedRuntimes << attrs.value(QLatin1String("id")).toString();
                            }
                        }
                        break;
                    }
                }
            }
        }
    }

    MaemoQemuRuntime runtime;
    if (installedRuntimes.contains(targetRuntime)) {
        runtime.m_name = targetRuntime;
        runtime.m_root = m_maddeRoot + QLatin1String("/runtimes/")
            + targetRuntime;
        fillRuntimeInformation(&runtime);
    }
    return runtime;

}

void MaemoQemuRuntimeParserV1::fillRuntimeInformation(MaemoQemuRuntime *runtime) const
{
    const QStringList files = QDir(runtime->m_root).entryList(QDir::Files
        | QDir::NoSymLinks | QDir::NoDotAndDotDot);

    const QLatin1String infoFile("information");
    if (files.contains(infoFile)) {
        QFile file(runtime->m_root + QLatin1Char('/') + infoFile);
        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QMap<QString, QString> map;
            QTextStream stream(&file);
            while (!stream.atEnd()) {
                const QString &line = stream.readLine().trimmed();
                const int index = line.indexOf(QLatin1Char('='));
                map.insert(line.mid(0, index).remove(QLatin1Char('\'')),
                    line.mid(index + 1).remove(QLatin1Char('\'')));
            }

            runtime->m_bin = map.value(QLatin1String("qemu"));
            runtime->m_args = map.value(QLatin1String("qemu_args"));
            setEnvironment(runtime, map.value(QLatin1String("libpath")));
            runtime->m_sshPort = map.value(QLatin1String("sshport"));
193
            runtime->m_freePorts = PortList();
194
195
196
197
198
199
200
201
            int i = 2;
            while (true) {
                const QString port = map.value(QLatin1String("redirport")
                    + QString::number(i++));
                if (port.isEmpty())
                    break;
                runtime->m_freePorts.addPort(port.toInt());
            }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

            // This is complex because of extreme MADDE weirdness.
            const QString root = m_maddeRoot + QLatin1Char('/');
            const bool pathIsRelative = QFileInfo(runtime->m_bin).isRelative();
            runtime->m_bin =
        #ifdef Q_OS_WIN
                root + (pathIsRelative
                    ? QLatin1String("madlib/") + runtime->m_bin // Fremantle.
                    : runtime->m_bin)                           // Harmattan.
                    + QLatin1String(".exe");
        #else
                pathIsRelative
                    ? root + QLatin1String("madlib/") + runtime->m_bin // Fremantle.
                    : runtime->m_bin;                                  // Harmattan.
        #endif
217
218
219
220
221
222
223
224
225
226
227
228
229
230
        }
    }
}

void MaemoQemuRuntimeParserV1::setEnvironment(MaemoQemuRuntime *runTime,
    const QString &envSpec) const
{
    QString remainingEnvSpec = envSpec;
    QString currentKey;
    while (true) {
        const int nextEqualsSignPos
            = remainingEnvSpec.indexOf(QLatin1Char('='));
        if (nextEqualsSignPos == -1) {
            if (!currentKey.isEmpty())
231
232
                runTime->m_normalVars << MaemoQemuRuntime::Variable(currentKey,
                    remainingEnvSpec);
233
234
235
236
237
238
239
240
241
            break;
        }
        const int keyStartPos
            = remainingEnvSpec.lastIndexOf(QRegExp(QLatin1String("\\s")),
                nextEqualsSignPos) + 1;
        if (!currentKey.isEmpty()) {
            const int valueEndPos
                = remainingEnvSpec.lastIndexOf(QRegExp(QLatin1String("\\S")),
                    qMax(0, keyStartPos - 1)) + 1;
242
            runTime->m_normalVars << MaemoQemuRuntime::Variable(currentKey,
243
244
245
246
247
248
249
250
251
252
                remainingEnvSpec.left(valueEndPos));
        }
        currentKey = remainingEnvSpec.mid(keyStartPos,
            nextEqualsSignPos - keyStartPos);
        remainingEnvSpec.remove(0, nextEqualsSignPos + 1);
    }
}


MaemoQemuRuntimeParserV2::MaemoQemuRuntimeParserV2(const QString &madInfoOutput,
253
254
    const QString &targetName, const QString &maddeRoot)
    : MaemoQemuRuntimeParser(madInfoOutput, targetName, maddeRoot)
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
{
}

MaemoQemuRuntime MaemoQemuRuntimeParserV2::parseRuntime()
{
    QString runtimeName;
    QList<MaemoQemuRuntime> runtimes;
    while (m_madInfoReader.readNextStartElement()) {
        if (m_madInfoReader.name() == QLatin1String("madde")) {
            while (m_madInfoReader.readNextStartElement()) {
                if (m_madInfoReader.name() == QLatin1String("targets")) {
                    while (m_madInfoReader.readNextStartElement())
                        handleTargetTag(runtimeName);
                } else if (m_madInfoReader.name() == QLatin1String("runtimes")) {
                    while (m_madInfoReader.readNextStartElement()) {
                        const MaemoQemuRuntime &rt = handleRuntimeTag();
                        if (!rt.m_name.isEmpty() && !rt.m_bin.isEmpty()
                                && !rt.m_args.isEmpty()) {
                            runtimes << rt;
                        }
                    }
                } else {
                    m_madInfoReader.skipCurrentElement();
                }
            }
        }
    }
    foreach (const MaemoQemuRuntime &rt, runtimes) {
        if (rt.m_name == runtimeName)
            return rt;
    }
    return MaemoQemuRuntime();
}

void MaemoQemuRuntimeParserV2::handleTargetTag(QString &runtimeName)
{
    const QXmlStreamAttributes &attrs = m_madInfoReader.attributes();
    if (m_madInfoReader.name() == QLatin1String("target") && runtimeName.isEmpty()
293
            && attrs.value(QLatin1String("name")) == m_targetName
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
            && attrs.value(QLatin1String("installed")) == QLatin1String("true")) {
        while (m_madInfoReader.readNextStartElement()) {
            if (m_madInfoReader.name() == QLatin1String("runtime"))
                runtimeName = m_madInfoReader.readElementText();
            else
                m_madInfoReader.skipCurrentElement();
        }
    } else {
        m_madInfoReader.skipCurrentElement();
    }
}

MaemoQemuRuntime MaemoQemuRuntimeParserV2::handleRuntimeTag()
{
    MaemoQemuRuntime runtime;
    const QXmlStreamAttributes &attrs = m_madInfoReader.attributes();
    if (m_madInfoReader.name() != QLatin1String("runtime")
            || attrs.value(QLatin1String("installed")) != QLatin1String("true")) {
        m_madInfoReader.skipCurrentElement();
        return runtime;
    }
    runtime.m_name = attrs.value(QLatin1String("name")).toString();
    while (m_madInfoReader.readNextStartElement()) {
        if (m_madInfoReader.name() == QLatin1String("exec-path")) {
            runtime.m_bin = m_madInfoReader.readElementText();
        } else if (m_madInfoReader.name() == QLatin1String("args")) {
            runtime.m_args = m_madInfoReader.readElementText();
        } else if (m_madInfoReader.name() == QLatin1String("environment")) {
322
            handleEnvironmentTag(runtime);
323
        } else if (m_madInfoReader.name() == QLatin1String("tcpportmap")) {
324
325
326
327
328
329
330
            const QList<Port> &ports = handleTcpPortListTag();
            foreach (const Port &port, ports) {
                if (port.ssh)
                    runtime.m_sshPort = QString::number(port.port);
                else
                    runtime.m_freePorts.addPort(port.port);
            }
331
332
333
334
335
336
337
        } else {
            m_madInfoReader.skipCurrentElement();
        }
    }
    return runtime;
}

338
void MaemoQemuRuntimeParserV2::handleEnvironmentTag(MaemoQemuRuntime &runtime)
339
{
340
341
342
    while (m_madInfoReader.readNextStartElement())
        handleVariableTag(runtime);

343
344
345
346
347
#ifdef Q_OS_WIN
    const QString root = QDir::toNativeSeparators(m_maddeRoot)
        + QLatin1Char('/');
    const QLatin1Char colon(';');
    const QLatin1String key("PATH");
348
349
350
351
352
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    runtime.m_normalVars << MaemoQemuRuntime::Variable(key,
        root + QLatin1String("bin") + colon + env.value(key));
    runtime.m_normalVars << MaemoQemuRuntime::Variable(key,
        root + QLatin1String("madlib") + colon + env.value(key));
353
#endif
354
355
}

356
void MaemoQemuRuntimeParserV2::handleVariableTag(MaemoQemuRuntime &runtime)
357
358
359
{
    if (m_madInfoReader.name() != QLatin1String("variable")) {
        m_madInfoReader.skipCurrentElement();
360
        return;
361
362
    }

363
364
    const bool isGlBackend = m_madInfoReader.attributes().value(QLatin1String("purpose"))
        == QLatin1String("glbackend");
365
366
    QString varName;
    QString varValue;
367
    while (m_madInfoReader.readNextStartElement()) {
368
369
        const QXmlStreamAttributes &attrs = m_madInfoReader.attributes();
        if (m_madInfoReader.name() == QLatin1String("name")) {
370
            varName = m_madInfoReader.readElementText();
371
        } else if (m_madInfoReader.name() == QLatin1String("value")
372
373
374
375
376
377
378
                   && attrs.value(QLatin1String("set")) != QLatin1String("false")) {
            varValue = m_madInfoReader.readElementText();
            if (isGlBackend) {
                MaemoQemuSettings::OpenGlMode openGlMode
                    = openGlTagToEnum(attrs.value(QLatin1String("option")).toString());
                runtime.m_openGlBackendVarValues.insert(openGlMode, varValue);
            }
379
        } else {
380
            m_madInfoReader.skipCurrentElement();
381
        }
382
    }
383
384
385
386
387
388
389
390

    if (varName.isEmpty())
        return;
    if (isGlBackend) {
        runtime.m_openGlBackendVarName = varName;
    } else {
        runtime.m_normalVars << MaemoQemuRuntime::Variable(varName, varValue);
    }
391
392
}

393
QList<MaemoQemuRuntimeParserV2::Port> MaemoQemuRuntimeParserV2::handleTcpPortListTag()
394
{
395
    QList<Port> ports;
396
    while (m_madInfoReader.readNextStartElement()) {
397
398
399
        const Port &port = handlePortTag();
        if (port.port != -1)
            ports << port;
400
401
402
403
    }
    return ports;
}

404
MaemoQemuRuntimeParserV2::Port MaemoQemuRuntimeParserV2::handlePortTag()
405
{
406
    Port port;
407
    if (m_madInfoReader.name() == QLatin1String("port")) {
408
409
        const QXmlStreamAttributes &attrs = m_madInfoReader.attributes();
        port.ssh = attrs.value(QLatin1String("service")) == QLatin1String("ssh");
410
411
        while (m_madInfoReader.readNextStartElement()) {
            if (m_madInfoReader.name() == QLatin1String("host"))
412
                port.port = m_madInfoReader.readElementText().toInt();
413
414
415
416
417
418
419
            else
                m_madInfoReader.skipCurrentElement();
        }
    }
    return port;
}

420
421
422
423
424
425
426
427
MaemoQemuSettings::OpenGlMode MaemoQemuRuntimeParserV2::openGlTagToEnum(const QString &tag) const
{
    if (tag == QLatin1String("hardware-acceleration"))
        return MaemoQemuSettings::HardwareAcceleration;
    if (tag == QLatin1String("software-rendering"))
        return MaemoQemuSettings::SoftwareRendering;
    if (tag == QLatin1String("autodetect"))
        return MaemoQemuSettings::AutoDetect;
428
    QTC_CHECK(false);
429
    return MaemoQemuSettings::AutoDetect;
430
431
}

432
}   // namespace Internal
433
}   // namespace Madde