toolchain.cpp 13.8 KB
Newer Older
1
2
3
4
5
6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** 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
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
28
29
**
**************************************************************************/

dt's avatar
dt committed
30
31
#include "toolchain.h"
#include "cesdkhandler.h"
32

dt's avatar
dt committed
33
34
35
36
37
38
39
#include <QtCore/QFileInfo>
#include <QtCore/QProcess>
#include <QtCore/QDebug>
#include <QtCore/QSettings>
#include <QtCore/QDir>
#include <QtCore/QTemporaryFile>
#include <QtCore/QString>
40
#include <QtCore/QCoreApplication>
dt's avatar
dt committed
41
42
43
44

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

45
46
47
#ifdef Q_OS_WIN64
static const char * MSVC_RegKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7";
#else
dt's avatar
dt committed
48
static const char * MSVC_RegKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VC7";
49
50
#endif

dt's avatar
dt committed
51
52
53
54
55
56
57
bool ToolChain::equals(ToolChain *a, ToolChain *b)
{
    if (a == b)
        return true;
    if (a == 0 || b == 0)
        return false;
    if (a->type() == b->type())
58
        return a->equals(b);
dt's avatar
dt committed
59
    return false;
dt's avatar
dt committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
}

ToolChain::ToolChain()
{
}

ToolChain::~ToolChain()
{
}

ToolChain *ToolChain::createGccToolChain(const QString &gcc)
{
    return new GccToolChain(gcc);
}

ToolChain *ToolChain::createMinGWToolChain(const QString &gcc, const QString &mingwPath)
{
    return new MinGWToolChain(gcc, mingwPath);
}

80
ToolChain *ToolChain::createMSVCToolChain(const QString &name, bool amd64 = false)
dt's avatar
dt committed
81
{
82
    return new MSVCToolChain(name, amd64);
dt's avatar
dt committed
83
84
85
86
87
88
89
90
91
}

ToolChain *ToolChain::createWinCEToolChain(const QString &name, const QString &platform)
{
    return new WinCEToolChain(name, platform);
}

QStringList ToolChain::availableMSVCVersions()
{
92
    QSettings registry(MSVC_RegKey, QSettings::NativeFormat);
dt's avatar
dt committed
93
    QStringList versions = registry.allKeys();
94
    // qDebug() << "AVAILABLE MSVC VERSIONS:" << versions << "at" << MSVC_RegKey;
dt's avatar
dt committed
95
96
97
    return versions;
}

98
QList<ToolChain::ToolChainType> ToolChain::supportedToolChains()
99
{
100
101
102
103
104
    QList<ToolChain::ToolChainType> toolChains;
    for (int i = 0; i < LAST_VALID; ++i) {
        toolChains.append(ToolChainType(i));
    }
    return toolChains;
105
106
}

107
108
109
110
QString ToolChain::toolChainName(ToolChainType tc)
{
    switch (tc) {
    case GCC:
111
        return QCoreApplication::translate("ToolChain", "GCC");
112
    case LinuxICC:
113
        return QCoreApplication::translate("ToolChain", "Intel C++ Compiler (Linux)");
114
    case MinGW:
115
        return QCoreApplication::translate("ToolChain", "MinGW");
116
    case MSVC:
117
        return QCoreApplication::translate("ToolChain", "Microsoft Visual C++");
118
    case WINCE:
119
        return QCoreApplication::translate("ToolChain", "Windows CE");
120
121
122
123
124
125
126
127
128
129
130
131

#ifdef QTCREATOR_WITH_S60
    case WINSCW:
        return QCoreApplication::translate("ToolChain", "WINSCW");
    case GCCE:
        return QCoreApplication::translate("ToolChain", "GCCE");
    case RVCT_ARMV5:
        return QCoreApplication::translate("ToolChain", "RVCT (ARMV5)");
    case RVCT_ARMV6:
        return QCoreApplication::translate("ToolChain", "RVCT (ARMV6)");
#endif

132
133
134
135
136
137
    case OTHER:
        return QCoreApplication::translate("ToolChain", "Other");
    case INVALID:
        return QCoreApplication::translate("ToolChain", "<Invalid>");
    case UNKNOWN:
        break;
138
    default:
139
        Q_ASSERT("Missing name for Toolchaintype");
140
141
142
143
    };
    return QCoreApplication::translate("ToolChain", "<Unknown>");
}

dt's avatar
dt committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
GccToolChain::GccToolChain(const QString &gcc)
    : m_gcc(gcc)
{

}

ToolChain::ToolChainType GccToolChain::type() const
{
    return ToolChain::GCC;
}

QByteArray GccToolChain::predefinedMacros()
{
    if (m_predefinedMacros.isEmpty()) {
        QStringList arguments;
        arguments << QLatin1String("-xc++")
                  << QLatin1String("-E")
                  << QLatin1String("-dM")
                  << QLatin1String("-");

        QProcess cpp;
165
166
167
        ProjectExplorer::Environment env = ProjectExplorer::Environment::systemEnvironment();
        addToEnvironment(env);
        cpp.setEnvironment(env.toStringList());
dt's avatar
dt committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
        cpp.start(m_gcc, arguments);
        cpp.closeWriteChannel();
        cpp.waitForFinished();
        m_predefinedMacros = cpp.readAllStandardOutput();
    }
    return m_predefinedMacros;
}

QList<HeaderPath> GccToolChain::systemHeaderPaths()
{
    if (m_systemHeaderPaths.isEmpty()) {
        QStringList arguments;
        arguments << QLatin1String("-xc++")
                  << QLatin1String("-E")
                  << QLatin1String("-v")
                  << QLatin1String("-");

        QProcess cpp;
186
187
188
        ProjectExplorer::Environment env = ProjectExplorer::Environment::systemEnvironment();
        addToEnvironment(env);
        cpp.setEnvironment(env.toStringList());
dt's avatar
dt committed
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
        cpp.setReadChannelMode(QProcess::MergedChannels);
        cpp.start(m_gcc, arguments);
        cpp.closeWriteChannel();
        cpp.waitForFinished();

        QByteArray line;
        while (cpp.canReadLine()) {
            line = cpp.readLine();
            if (line.startsWith("#include"))
                break;
        }

        if (! line.isEmpty() && line.startsWith("#include")) {
            HeaderPath::Kind kind = HeaderPath::UserHeaderPath;
            while (cpp.canReadLine()) {
                line = cpp.readLine();
                if (line.startsWith("#include")) {
                    kind = HeaderPath::GlobalHeaderPath;
                } else if (! line.isEmpty() && QChar(line.at(0)).isSpace()) {
                    HeaderPath::Kind thisHeaderKind = kind;

                    line = line.trimmed();
                    if (line.endsWith('\n'))
                        line.chop(1);

                    int index = line.indexOf(" (framework directory)");
                    if (index != -1) {
                        line = line.left(index);
                        thisHeaderKind = HeaderPath::FrameworkHeaderPath;
                    }

                    m_systemHeaderPaths.append(HeaderPath(QFile::decodeName(line), thisHeaderKind));
                } else if (line.startsWith("End of search list.")) {
                    break;
                } else {
                    qWarning() << "ignore line:" << line;
                }
            }
        }
    }
    return m_systemHeaderPaths;
}

void GccToolChain::addToEnvironment(ProjectExplorer::Environment &env)
{
    Q_UNUSED(env)
}

237
238
239
240
241
QString GccToolChain::makeCommand() const
{
    return "make";
}

dt's avatar
dt committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
bool GccToolChain::equals(ToolChain *other) const
{
    return (m_gcc == static_cast<GccToolChain *>(other)->m_gcc);
}

MinGWToolChain::MinGWToolChain(const QString &gcc, const QString &mingwPath)
    : GccToolChain(gcc), m_mingwPath(mingwPath)
{

}

ToolChain::ToolChainType MinGWToolChain::type() const
{
    return ToolChain::MinGW;
}

bool MinGWToolChain::equals(ToolChain *other) const
{
    MinGWToolChain *o = static_cast<MinGWToolChain *>(other);
    return (m_mingwPath == o->m_mingwPath && this->GccToolChain::equals(other));
}

void MinGWToolChain::addToEnvironment(ProjectExplorer::Environment &env)
{
266
    //qDebug()<<"MinGWToolChain::addToEnvironment";
267
268
    if (m_mingwPath.isEmpty())
        return;
dt's avatar
dt committed
269
270
271
    QString binDir = m_mingwPath + "/bin";
    if (QFileInfo(binDir).exists())
        env.prependOrSetPath(binDir);
272
273
//    if (QFileInfo(binDir).exists())
//        qDebug()<<"Adding "<<binDir<<" to the PATH";
dt's avatar
dt committed
274
275
}

276
277
278
279
280
QString MinGWToolChain::makeCommand() const
{
    return "mingw32-make.exe";
}

dt's avatar
dt committed
281

282
283
MSVCToolChain::MSVCToolChain(const QString &name, bool amd64)
    : m_name(name), m_valuesSet(false), m_amd64(amd64)
dt's avatar
dt committed
284
{
285
    if (m_name.isEmpty()) { // Could be because system qt doesn't set this
286
        QSettings registry(MSVC_RegKey, QSettings::NativeFormat);
287
288
289
        QStringList keys = registry.allKeys();
        if (keys.count())
            m_name = keys.first();
290
    }
dt's avatar
dt committed
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
}

ToolChain::ToolChainType MSVCToolChain::type() const
{
    return ToolChain::MSVC;
}

bool MSVCToolChain::equals(ToolChain *other) const
{
    MSVCToolChain *o = static_cast<MSVCToolChain *>(other);
    return (m_name == o->m_name);
}

QByteArray MSVCToolChain::predefinedMacros()
{
    return  "#define __WIN32__\n"
            "#define __WIN32\n"
            "#define _WIN32\n"
            "#define WIN32\n"
            "#define __WINNT__\n"
            "#define __WINNT\n"
            "#define WINNT\n"
            "#define _X86_\n"
            "#define __MSVCRT__\n";
}

QList<HeaderPath> MSVCToolChain::systemHeaderPaths()
{
    //TODO fix this code
    ProjectExplorer::Environment env = ProjectExplorer::Environment::systemEnvironment();
    addToEnvironment(env);
322
323
324
325
326
    QList<HeaderPath> headerPaths;
    foreach(const QString &path, env.value("INCLUDE").split(QLatin1Char(';'))) {
        headerPaths.append(HeaderPath(path, HeaderPath::GlobalHeaderPath));
    }
    return headerPaths;
dt's avatar
dt committed
327
328
329
330
}

void MSVCToolChain::addToEnvironment(ProjectExplorer::Environment &env)
{
331
332
    if (!m_valuesSet || env != m_lastEnvironment) {
        m_lastEnvironment = env;
333
        QSettings registry(MSVC_RegKey, QSettings::NativeFormat);
334
335
        if (m_name.isEmpty())
            return;
dt's avatar
dt committed
336
337
        QString path = registry.value(m_name).toString();
        QString desc;
338
339
        QString varsbat;
        if (m_amd64)
con's avatar
con committed
340
            varsbat = path + "bin\\amd64\\vcvarsamd64.bat";
341
        else
con's avatar
con committed
342
            varsbat = path + "bin\\vcvars32.bat";
dt's avatar
dt committed
343
        //        qDebug() << varsbat;
dt's avatar
dt committed
344
345
346
347
348
349
350
351
352
353
        if (QFileInfo(varsbat).exists()) {
            QTemporaryFile tf(QDir::tempPath() + "\\XXXXXX.bat");
            if (!tf.open())
                return;
            QString filename = tf.fileName();
            tf.write("call \"" + varsbat.toLocal8Bit()+"\"\r\n");
            tf.write(("set > \"" + QDir::tempPath() + "\\qtcreator-msvc-environment.txt\"\r\n").toLocal8Bit());
            tf.flush();
            tf.waitForBytesWritten(30000);

354
355
            QProcess run;
            run.setEnvironment(env.toStringList());
dt's avatar
dt committed
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
            QString cmdPath = env.searchInPath("cmd");
            run.start(cmdPath, QStringList()<<"/c"<<filename);
            run.waitForFinished();
            tf.close();

            QFile vars(QDir::tempPath() + "\\qtcreator-msvc-environment.txt");
            if (vars.exists() && vars.open(QIODevice::ReadOnly)) {
                while (!vars.atEnd()) {
                    QByteArray line = vars.readLine();
                    QString line2 = QString::fromLocal8Bit(line);
                    line2 = line2.trimmed();
                    QRegExp regexp("(\\w*)=(.*)");
                    if (regexp.exactMatch(line2)) {
                        QString variable = regexp.cap(1);
                        QString value = regexp.cap(2);
                        m_values.append(QPair<QString, QString>(variable, value));
                    }
                }
                vars.close();
                vars.remove();
            }
        }
        m_valuesSet = true;
    }

    QList< QPair<QString, QString> >::const_iterator it, end;
    end = m_values.constEnd();
    for (it = m_values.constBegin(); it != end; ++it) {
        env.set((*it).first, (*it).second);
    }
}

388
389
QString MSVCToolChain::makeCommand() const
{
390
391
392
393
394
395
396
397
    if (ProjectExplorerPlugin::instance()->projectExplorerSettings().useJom) {
        // We want jom! Try to find it.
        QString jom = QCoreApplication::applicationDirPath() + QLatin1String("/jom.exe");
        if (QFileInfo(jom).exists())
            return jom;
        else
            return "jom.exe";
    }
398
399
400
    return "nmake.exe";
}

dt's avatar
dt committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
WinCEToolChain::WinCEToolChain(const QString &name, const QString &platform)
    : MSVCToolChain(name), m_platform(platform)
{

}

ToolChain::ToolChainType WinCEToolChain::type() const
{
    return ToolChain::WINCE;
}

bool WinCEToolChain::equals(ToolChain *other) const
{
    WinCEToolChain *o = static_cast<WinCEToolChain *>(other);
    return (m_platform == o->m_platform && this->MSVCToolChain::equals(other));
}

QByteArray WinCEToolChain::predefinedMacros()
{
    //TODO
    return MSVCToolChain::predefinedMacros();
}

QList<HeaderPath> WinCEToolChain::systemHeaderPaths()
{
    //TODO fix this code
    ProjectExplorer::Environment env = ProjectExplorer::Environment::systemEnvironment();
    addToEnvironment(env);
Roberto Raggi's avatar
Compile    
Roberto Raggi committed
429
430
431
432
433
434
435
436
437
438
439

    QList<HeaderPath> headerPaths;

    const QStringList includes = env.value("INCLUDE").split(QLatin1Char(';'));

    foreach (const QString &path, includes) {
        const HeaderPath headerPath(path, HeaderPath::GlobalHeaderPath);
        headerPaths.append(headerPath);
    }

    return headerPaths;
dt's avatar
dt committed
440
441
442
443
444
}

void WinCEToolChain::addToEnvironment(ProjectExplorer::Environment &env)
{
    MSVCToolChain::addToEnvironment(env);
445
    QSettings registry(MSVC_RegKey, QSettings::NativeFormat);
dt's avatar
dt committed
446
    QString path = registry.value(m_name).toString();
447

dt's avatar
dt committed
448
449
450
451
452
453
454
455
456
457
458
459
    // Find MSVC path

    path += "/";

//        qDebug()<<"MSVC path"<<msvcPath;
//        qDebug()<<"looking for platform name in"<< path() + "/mkspecs/" + mkspec() +"/qmake.conf";
    // Find Platform name
//        qDebug()<<"Platform Name"<<platformName;

    CeSdkHandler cesdkhandler;
    cesdkhandler.parse(path);
    cesdkhandler.find(m_platform).addToEnvironment(env);
460
461
    //qDebug()<<"WinCE Final Environment:";
    //qDebug()<<env.toStringList();
dt's avatar
dt committed
462
}