aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qt4projectmanager/qt-s60/rvcttoolchain.cpp
blob: 86700690ab05f421907ea21c652a4bd0daeebe2a (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** 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
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "rvcttoolchain.h"
#include "rvctparser.h"

#include <utils/qtcassert.h>
#include <utils/synchronousprocess.h>

#include <QtCore/QProcess>
#include <QtCore/QProcessEnvironment>
#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtCore/QDir>

using namespace ProjectExplorer;
using namespace Qt4ProjectManager::Internal;

static const char rvctBinaryC[] = "armcc";

static inline QStringList headerPathToStringList(const QList<ProjectExplorer::HeaderPath> &hl)
{
    QStringList rc;
    foreach (const ProjectExplorer::HeaderPath &hp, hl)
        rc.push_back(hp.path());
    return rc;
}

// ==========================================================================
// RVCTToolChain
// ==========================================================================

RVCTToolChain::RVCTToolChain(const S60Devices::Device &device, ProjectExplorer::ToolChainType type) :
    m_mixin(device),
    m_type(type),
    m_versionUpToDate(false),
    m_major(0),
    m_minor(0),
    m_build(0)
{
}

QSet<QPair<int, int> > RVCTToolChain::configuredRvctVersions()
{
    static QSet<QPair<int, int> > result;

    if (result.isEmpty()) {
        QRegExp regex(QLatin1String("^RVCT(\\d)(\\d)BIN=.*$"));
        Q_ASSERT(regex.isValid());
        QStringList environment = QProcessEnvironment::systemEnvironment().toStringList();
        foreach (const QString &v, environment) {
            if (regex.exactMatch(v)) {
                int major = regex.cap(1).toInt();
                int minor = regex.cap(2).toInt();
                result.insert(qMakePair(major, minor));
            }
        }
    }
    return result;
}

QStringList RVCTToolChain::configuredEnvironment()
{
    updateVersion();

    if (m_additionalEnvironment.isEmpty()) {
        const QString binVarName = QString::fromLocal8Bit(rvctBinEnvironmentVariable());
        const QString varName = binVarName.left(binVarName.count() - 3 /* BIN */);
        QStringList environment = QProcessEnvironment::systemEnvironment().toStringList();
        foreach (const QString &v, environment) {
            if ((v.startsWith(varName) && !v.startsWith(binVarName))
                 || v.startsWith(QLatin1String("ARMLMD_LICENSE_FILE="))) {
                m_additionalEnvironment.append(v);
            }
        }
    }
    return m_additionalEnvironment;
}

// Return the environment variable indicating the RVCT version
// 'RVCT<major><minor>BIN'
QByteArray RVCTToolChain::rvctBinEnvironmentVariableForVersion(int major)
{
    QSet<QPair<int, int> > versions = configuredRvctVersions();

    for (QSet<QPair<int, int> >::const_iterator it = versions.constBegin();
         it != versions.constEnd(); ++it) {
        if (it->first == major) {
            if (it->first < 0 || it->first > 9) continue;
            if (it->second < 0 || it->second > 9) continue;
            QByteArray result = "RVCT..BIN";
            result[4] = '0' + it->first;
            result[5] = '0' + it->second;
            return result;
        }
    }
    return QByteArray();
}

QString RVCTToolChain::rvctBinPath()
{
    if (m_binPath.isEmpty()) {
        const QByteArray binVar = rvctBinEnvironmentVariable();
        if (!binVar.isEmpty()) {
            const QByteArray binPathB = qgetenv(binVar);
            if (!binPathB.isEmpty()) {
                const QFileInfo fi(QString::fromLocal8Bit(binPathB));
                if (fi.isDir())
                    m_binPath = fi.absoluteFilePath();
            }
        }
    }
    return m_binPath;
}

// Return binary expanded by path or resort to PATH
QString RVCTToolChain::rvctBinary()
{
    QString executable = QLatin1String(rvctBinaryC);
#ifdef Q_OS_WIN
    executable += QLatin1String(".exe");
#endif
    const QString binPath = rvctBinPath();
    return binPath.isEmpty() ? executable : (binPath + QLatin1Char('/') + executable);
}

ProjectExplorer::ToolChainType RVCTToolChain::type() const
{
    return m_type;
}

void RVCTToolChain::updateVersion()
{
    if (m_versionUpToDate)
        return;

    m_versionUpToDate = true;
    m_major = 0;
    m_minor = 0;
    m_build = 0;
    QProcess armcc;
    Utils::Environment env = Utils::Environment::systemEnvironment();
    addToEnvironment(env);
    armcc.setEnvironment(env.toStringList());
    const QString binary = rvctBinary();
    armcc.start(binary, QStringList());
    if (!armcc.waitForStarted()) {
        qWarning("Unable to run rvct binary '%s' when trying to determine version.", qPrintable(binary));
        return;
    }
    armcc.closeWriteChannel();
    if (!armcc.waitForFinished()) {
        Utils::SynchronousProcess::stopProcess(armcc);
        qWarning("Timeout running rvct binary '%s' trying to determine version.", qPrintable(binary));
        return;
    }
    if (armcc.exitStatus() != QProcess::NormalExit) {
        qWarning("A crash occurred when running rvct binary '%s' trying to determine version.", qPrintable(binary));
        return;
    }
    QString versionLine = QString::fromLocal8Bit(armcc.readAllStandardOutput());
    versionLine += QString::fromLocal8Bit(armcc.readAllStandardError());
    const QRegExp versionRegExp(QLatin1String("RVCT(\\d*)\\.(\\d*).*\\[Build.(\\d*)\\]"),
                                Qt::CaseInsensitive);
    QTC_ASSERT(versionRegExp.isValid(), return);
    if (versionRegExp.indexIn(versionLine) != -1) {
        m_major = versionRegExp.cap(1).toInt();
        m_minor = versionRegExp.cap(2).toInt();
        m_build = versionRegExp.cap(3).toInt();
    }
}

QByteArray RVCTToolChain::predefinedMacros()
{
    // see http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0205f/Babbacdb.html (version 2.2)
    // and http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491b/BABJFEFG.html (version 4.0)
    updateVersion();
    QByteArray ba("#define __ARRAY_OPERATORS\n"
                  "#define _BOOL\n"
                  "#define __cplusplus\n"
                  "#define __CC_ARM 1\n"
                  "#define __EDG__\n"
                  "#define __STDC__\n"
                  "#define __STDC_VERSION__\n"
                  "#define __sizeof_int 4"
                  "#define __sizeof_long 4"
                  "#define __sizeof_ptr 4"
                  "#define __TARGET_FEATURE_DOUBLEWORD\n"
                  "#define __TARGET_FEATURE_DSPMUL\n"
                  "#define __TARGET_FEATURE_HALFWORD\n"
                  "#define __TARGET_FEATURE_THUMB\n"
                  "#define _WCHAR_T\n"
                  "#define __SYMBIAN32__\n");
    return ba;
}

QList<HeaderPath> RVCTToolChain::systemHeaderPaths()
{
    if (m_systemHeaderPaths.isEmpty()) {
        updateVersion();
        QString rvctInclude = qgetenv(QString::fromLatin1("RVCT%1%2INC").arg(m_major).arg(m_minor).toLatin1());
        if (!rvctInclude.isEmpty())
            m_systemHeaderPaths.append(HeaderPath(rvctInclude, HeaderPath::GlobalHeaderPath));
        switch (m_type) {
        case ProjectExplorer::ToolChain_RVCT_ARMV5_GNUPOC:
            m_systemHeaderPaths += m_mixin.gnuPocRvctHeaderPaths(m_major, m_minor);
            break;
        default:
            m_systemHeaderPaths += m_mixin.epocHeaderPaths();
            break;
        }
    }
    return m_systemHeaderPaths;
}

// Expand an RVCT variable, such as RVCT22BIN, by some new values
void RVCTToolChain::addToRVCTPathVariable(const QString &postfix, const QStringList &values,
                                          Utils::Environment &env) const
{
    // get old values
    const QChar separator = QLatin1Char(',');
    const QString variable = QString::fromLatin1("RVCT%1%2%3").arg(m_major).arg(m_minor).arg(postfix);
    const QString oldValueS = env.value(variable);
    const QStringList oldValue = oldValueS.isEmpty() ? QStringList() : oldValueS.split(separator);
    // merge new values
    QStringList newValue = oldValue;
    foreach(const QString &v, values) {
        const QString normalized = QDir::toNativeSeparators(v);
        if (!newValue.contains(normalized))
            newValue.push_back(normalized);
    }
    if (newValue != oldValue)
        env.set(variable, newValue.join(QString(separator)));
}

// Figure out lib path via
QStringList RVCTToolChain::libPaths()
{
    const QByteArray binLocation = qgetenv(rvctBinEnvironmentVariable());
    if (binLocation.isEmpty())
        return QStringList();
    const QString pathRoot = QFileInfo(QString::fromLocal8Bit(binLocation)).path();
    QStringList rc;
    rc.push_back(pathRoot + QLatin1String("/lib"));
    rc.push_back(pathRoot + QLatin1String("/lib/armlib"));
    return rc;
}

void RVCTToolChain::addToEnvironment(Utils::Environment &env)
{
    updateVersion();

    // Push additional configuration variables for the compiler through:
    QStringList additionalVariables = configuredEnvironment();
    foreach (const QString &var, additionalVariables) {
        int pos = var.indexOf(QLatin1Char('='));
        Q_ASSERT(pos >= 0);
        const QString key = var.left(pos);
        const QString value = var.mid(pos + 1);
        env.set(key, value);
    }

    switch (m_type) {
    case ProjectExplorer::ToolChain_RVCT_ARMV5_GNUPOC: {
        m_mixin.addGnuPocToEnvironment(&env);
        // setup RVCT22INC, LIB
        addToRVCTPathVariable(QLatin1String("INC"),
                      headerPathToStringList(m_mixin.gnuPocRvctHeaderPaths(m_major, m_minor)),
                      env);
        addToRVCTPathVariable(QLatin1String("LIB"),
                              libPaths() + m_mixin.gnuPocRvctLibPaths(5, true),
                              env);
        }
        break;
    default:
        m_mixin.addEpocToEnvironment(&env);
        break;
    }

    const QString binPath = rvctBinPath();
    env.set(rvctBinEnvironmentVariable(), QDir::toNativeSeparators(binPath));

    // Add rvct to path and set locale to 'C'
    if (!binPath.isEmpty())
        env.prependOrSetPath(binPath);
    env.set(QLatin1String("LANG"), QString(QLatin1Char('C')));

    env.set(QLatin1String("QT_RVCT_VERSION"), QString::fromLatin1("%1.%2").arg(m_major).arg(m_minor));
}

QString RVCTToolChain::makeCommand() const
{
#if defined(Q_OS_WIN)
    return QLatin1String("make.exe");
#else
    return QLatin1String("make");
#endif
}

ProjectExplorer::IOutputParser *RVCTToolChain::outputParser() const
{
    return new RvctParser;
}

// ==========================================================================
// RVCT2ToolChain
// ==========================================================================

RVCT2ToolChain::RVCT2ToolChain(const S60Devices::Device &device, ProjectExplorer::ToolChainType type) :
    RVCTToolChain(device, type)
{ }

QByteArray RVCT2ToolChain::rvctBinEnvironmentVariable()
{
    return rvctBinEnvironmentVariableForVersion(2);
}

QByteArray RVCT2ToolChain::predefinedMacros()
{
    QByteArray result = RVCTToolChain::predefinedMacros();
    result.append(QString::fromLatin1("#define __arm__arm__\n"
                                      "#define __ARMCC_VERSION %1%2%3%4\n"
                                      "#define c_plusplus\n"
                                      )
                  .arg(m_major, 1, 10, QLatin1Char('0'))
                  .arg(m_minor, 1, 10, QLatin1Char('0'))
                  .arg("0")
                  .arg(m_build, 3, 10, QLatin1Char('0')).toLatin1());
    return result;
}

bool RVCT2ToolChain::equals(const ToolChain *otherIn) const
{
    if (otherIn->type() != type())
        return false;
    const RVCT2ToolChain *other = static_cast<const RVCT2ToolChain *>(otherIn);
    return other->m_mixin == m_mixin;
}

// ==========================================================================
// RVCT4ToolChain
// ==========================================================================

RVCT4ToolChain::RVCT4ToolChain(const S60Devices::Device &device,
                               ProjectExplorer::ToolChainType type) :
    RVCT2ToolChain(device, type)
{ }

QByteArray RVCT4ToolChain::rvctBinEnvironmentVariable()
{
    return rvctBinEnvironmentVariableForVersion(4);
}

QByteArray RVCT4ToolChain::predefinedMacros()
{
    QByteArray result = RVCTToolChain::predefinedMacros();
    result.append(QString::fromLatin1("#define __arm__\n"
                                      "#define __ARMCC_VERSION %1%2%3\n")
                  .arg(m_major, 1, 10, QLatin1Char('0'))
                  .arg(m_minor, 1, 10, QLatin1Char('0'))
                  .arg(m_build, 3, 10, QLatin1Char('0')).toLatin1());
    return result;
}


bool RVCT4ToolChain::equals(const ToolChain *otherIn) const
{
    if (otherIn->type() != type())
        return false;
    const RVCT4ToolChain *other = static_cast<const RVCT4ToolChain *>(otherIn);
    return other->m_mixin == m_mixin;
}