qtversionmanager.cpp 36 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "qtversionmanager.h"
hjk's avatar
hjk committed
31

32
#include "qt4projectmanagerconstants.h"
33
#include "profilereader.h"
con's avatar
con committed
34

35
36
37
#include <projectexplorer/debugginghelper.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/cesdkhandler.h>
38
#include <coreplugin/icore.h>
39
#include <extensionsystem/pluginmanager.h>
con's avatar
con committed
40
#include <help/helpplugin.h>
hjk's avatar
hjk committed
41
#include <utils/qtcassert.h>
con's avatar
con committed
42

43

hjk's avatar
hjk committed
44
#include <QtCore/QProcess>
con's avatar
con committed
45
46
#include <QtCore/QSettings>
#include <QtCore/QTime>
47
48
#include <QtGui/QApplication>
#include <QtGui/QDesktopServices>
con's avatar
con committed
49

50
51
52
53
#ifdef Q_OS_WIN32
#include <windows.h>
#endif

54
55
56
57
using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;

using ProjectExplorer::DebuggingHelperLibrary;
con's avatar
con committed
58
59
60
61
62

static const char *QtVersionsSectionName = "QtVersions";
static const char *defaultQtVersionKey = "DefaultQtVersion";
static const char *newQtVersionsKey = "NewQtVersions";

63
QtVersionManager *QtVersionManager::m_self = 0;
64

con's avatar
con committed
65
66
67
QtVersionManager::QtVersionManager()
    : m_emptyVersion(new QtVersion)
{
68
    QSettings *s = Core::ICore::instance()->settings();
con's avatar
con committed
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
    m_defaultVersion = s->value(defaultQtVersionKey, 0).toInt();

    m_idcount = 1;
    int size = s->beginReadArray(QtVersionsSectionName);
    for (int i = 0; i < size; ++i) {
        s->setArrayIndex(i);
        // Find the right id
        // Either something saved or something generated
        // Note: This code assumes that either all ids are read from the settings
        // or generated on the fly.
        int id = s->value("Id", -1).toInt();
        if (id == -1)
            id = getUniqueId();
        else if (id > m_idcount)
            m_idcount = id;
        QtVersion *version = new QtVersion(s->value("Name").toString(),
                                           s->value("Path").toString(),
                                           id,
                                           s->value("IsSystemVersion", false).toBool());
        version->setMingwDirectory(s->value("MingwDirectory").toString());
        version->setMsvcVersion(s->value("msvcVersion").toString());
        m_versions.append(version);
    }
    s->endArray();
    updateUniqueIdToIndexMap();

    ++m_idcount;
    addNewVersionsFromInstaller();
    updateSystemVersion();

    writeVersionsIntoSettings();
    updateDocumentation();
}

QtVersionManager::~QtVersionManager()
{
    qDeleteAll(m_versions);
    m_versions.clear();
    delete m_emptyVersion;
    m_emptyVersion = 0;
}

111
QtVersionManager *QtVersionManager::instance()
112
{
113
    return m_self;
114
115
}

con's avatar
con committed
116
117
118
119
void QtVersionManager::addVersion(QtVersion *version)
{
    m_versions.append(version);
    emit qtVersionsChanged();
120
    writeVersionsIntoSettings();
con's avatar
con committed
121
122
123
124
}

void QtVersionManager::updateDocumentation()
{
125
126
    Help::HelpManager *helpManager
        = ExtensionSystem::PluginManager::instance()->getObject<Help::HelpManager>();
dt's avatar
dt committed
127
    Q_ASSERT(helpManager);
con's avatar
con committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    QStringList fileEndings = QStringList() << "/qch/qt.qch" << "/qch/qmake.qch" << "/qch/designer.qch";
    QStringList files;
    foreach (QtVersion *version, m_versions) {
        QString docPath = version->versionInfo().value("QT_INSTALL_DOCS");
        foreach (const QString &fileEnding, fileEndings)
            files << docPath+fileEnding;
    }
    helpManager->registerDocumentation(files);
}

int QtVersionManager::getUniqueId()
{
    return m_idcount++;
}

void QtVersionManager::updateUniqueIdToIndexMap()
{
    m_uniqueIdToIndex.clear();
hjk's avatar
hjk committed
146
    for (int i = 0; i < m_versions.size(); ++i)
con's avatar
con committed
147
148
149
150
151
        m_uniqueIdToIndex.insert(m_versions.at(i)->uniqueId(), i);
}

void QtVersionManager::writeVersionsIntoSettings()
{
152
    QSettings *s = Core::ICore::instance()->settings();
con's avatar
con committed
153
    s->setValue(defaultQtVersionKey, m_defaultVersion);
154
    s->beginWriteArray(QtVersionsSectionName);
con's avatar
con committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    for (int i = 0; i < m_versions.size(); ++i) {
        s->setArrayIndex(i);
        s->setValue("Name", m_versions.at(i)->name());
        s->setValue("Path", m_versions.at(i)->path());
        s->setValue("Id", m_versions.at(i)->uniqueId());
        s->setValue("MingwDirectory", m_versions.at(i)->mingwDirectory());
        s->setValue("msvcVersion", m_versions.at(i)->msvcVersion());
        s->setValue("IsSystemVersion", m_versions.at(i)->isSystemVersion());
    }
    s->endArray();
}

QList<QtVersion* > QtVersionManager::versions() const
{
    return m_versions;
}

QtVersion *QtVersionManager::version(int id) const
{
    int pos = m_uniqueIdToIndex.value(id, -1);
    if (pos != -1)
        return m_versions.at(pos);

hjk's avatar
hjk committed
178
    if (m_defaultVersion < m_versions.count())
con's avatar
con committed
179
180
181
182
183
184
185
186
187
188
189
        return m_versions.at(m_defaultVersion);
    else
        return m_emptyVersion;
}

void QtVersionManager::addNewVersionsFromInstaller()
{
    // Add new versions which may have been installed by the WB installer in the form:
    // NewQtVersions="qt 4.3.2=c:\\qt\\qt432;qt embedded=c:\\qtembedded;"
    // or NewQtVersions="qt 4.3.2=c:\\qt\\qt432=c:\\qtcreator\\mingw\\=prependToPath;
    // Duplicate entries are not added, the first new version is set as default.
190
    QSettings *settings = Core::ICore::instance()->settings();
191
192
193

    if (!settings->contains(newQtVersionsKey) &&
        !settings->contains(QLatin1String("Installer/")+newQtVersionsKey))
con's avatar
con committed
194
195
196
197
198
        return;

//    qDebug()<<"QtVersionManager::addNewVersionsFromInstaller()";

    QString newVersionsValue = settings->value(newQtVersionsKey).toString();
199
200
201
    if (newVersionsValue.isEmpty())
        newVersionsValue = settings->value(QLatin1String("Installer/")+newQtVersionsKey).toString();

con's avatar
con committed
202
203
204
205
    QStringList newVersionsList = newVersionsValue.split(';', QString::SkipEmptyParts);
    bool defaultVersionWasReset = false;
    foreach (QString newVersion, newVersionsList) {
        QStringList newVersionData = newVersion.split('=');
hjk's avatar
hjk committed
206
        if (newVersionData.count()>=2) {
con's avatar
con committed
207
208
            if (QDir(newVersionData[1]).exists()) {
                QtVersion *version = new QtVersion(newVersionData[0], newVersionData[1], m_idcount++ );
hjk's avatar
hjk committed
209
                if (newVersionData.count() >= 3)
con's avatar
con committed
210
211
212
213
                    version->setMingwDirectory(newVersionData[2]);

                bool versionWasAlreadyInList = false;
                foreach(const QtVersion * const it, m_versions) {
hjk's avatar
hjk committed
214
                    if (QDir(version->path()).canonicalPath() == QDir(it->path()).canonicalPath()) {
con's avatar
con committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
                        versionWasAlreadyInList = true;
                        break;
                    }
                }

                if (!versionWasAlreadyInList) {
                    m_versions.append(version);
                } else {
                    // clean up
                    delete version;
                }
                if (!defaultVersionWasReset) {
                    m_defaultVersion = versionWasAlreadyInList? m_defaultVersion : m_versions.count() - 1;
                    defaultVersionWasReset = true;
                }
            }
        }
    }
    settings->remove(newQtVersionsKey);
234
    settings->remove(QLatin1String("Installer/")+newQtVersionsKey);
con's avatar
con committed
235
236
237
238
239
240
    updateUniqueIdToIndexMap();
}

void QtVersionManager::updateSystemVersion()
{
    bool haveSystemVersion = false;
241
    QString systemQMakePath = DebuggingHelperLibrary::findSystemQt(ProjectExplorer::Environment::systemEnvironment());
242
243
244
245
246
247
248
249
250
    QString systemQtPath;
    if (systemQMakePath.isNull()) {
        systemQtPath = tr("<not found>");
    } else {
        QDir dir(QFileInfo(systemQMakePath).absoluteDir());
        dir.cdUp();
        systemQtPath = dir.absolutePath();
    }

con's avatar
con committed
251
252
    foreach (QtVersion *version, m_versions) {
        if (version->isSystemVersion()) {
253
            version->setPath(systemQtPath);
254
            version->setName(tr("Auto-detected Qt"));
con's avatar
con committed
255
256
257
258
259
            haveSystemVersion = true;
        }
    }
    if (haveSystemVersion)
        return;
260
    QtVersion *version = new QtVersion(tr("Auto-detected Qt"),
261
                                       systemQtPath,
con's avatar
con committed
262
263
264
265
266
267
268
269
270
271
                                       getUniqueId(),
                                       true);
    m_versions.prepend(version);
    updateUniqueIdToIndexMap();
    if (m_versions.size() > 1) // we had other versions before adding system version
        ++m_defaultVersion;
}

QtVersion *QtVersionManager::currentQtVersion() const
{
hjk's avatar
hjk committed
272
    if (m_defaultVersion < m_versions.count())
con's avatar
con committed
273
274
275
276
277
        return m_versions.at(m_defaultVersion);
    else
        return m_emptyVersion;
}

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
void QtVersionManager::setNewQtVersions(QList<QtVersion *> newVersions, int newDefaultVersion)
{
    bool versionPathsChanged = m_versions.size() != newVersions.size();
    if (!versionPathsChanged) {
        for (int i = 0; i < m_versions.size(); ++i) {
            if (m_versions.at(i)->path() != newVersions.at(i)->path()) {
                versionPathsChanged = true;
                break;
            }
        }
    }
    qDeleteAll(m_versions);
    m_versions.clear();
    foreach(QtVersion *version, newVersions)
        m_versions.append(new QtVersion(*version));
    if (versionPathsChanged)
        updateDocumentation();
    updateUniqueIdToIndexMap();

    bool emitDefaultChanged = false;
    if (m_defaultVersion != newDefaultVersion) {
        m_defaultVersion = newDefaultVersion;
        emitDefaultChanged = true;
    }

    emit qtVersionsChanged();
    if (emitDefaultChanged)
        emit defaultQtVersionChanged();

    writeVersionsIntoSettings();
}

310

con's avatar
con committed
311
312
313
314
315
316

///
/// QtVersion
///

QtVersion::QtVersion(const QString &name, const QString &path, int id, bool isSystemVersion)
317
318
    : m_name(name),
    m_isSystemVersion(isSystemVersion),
319
    m_hasDebuggingHelper(false),
320
321
322
    m_notInstalled(false),
    m_defaultConfigIsDebug(true),
    m_defaultConfigIsDebugAndRelease(true),
323
    m_toolChain(0)
con's avatar
con committed
324
{
hjk's avatar
hjk committed
325
    if (id == -1)
con's avatar
con committed
326
327
328
        m_id = getUniqueId();
    else
        m_id = id;
329
    setPath(path);
con's avatar
con committed
330
331
332
333
}

QtVersion::QtVersion(const QString &name, const QString &path)
    : m_name(name),
334
    m_isSystemVersion(false),
335
336
337
338
    m_hasDebuggingHelper(false),
    m_mkspecUpToDate(false),
    m_versionInfoUpToDate(false),
    m_toolChain(0)
con's avatar
con committed
339
340
{
    m_id = getUniqueId();
341
    setPath(path);
con's avatar
con committed
342
343
}

344
345
346
347
348
349
QtVersion::~QtVersion()
{
    m_toolChain = 0;
    delete m_toolChain;
}

con's avatar
con committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
QString QtVersion::name() const
{
    return m_name;
}

QString QtVersion::path() const
{
    return m_path;
}

QString QtVersion::sourcePath() const
{
    return m_sourcePath;
}

QString QtVersion::mkspec() const
{
    updateMkSpec();
    return m_mkspec;
}

dt's avatar
dt committed
371
372
373
374
375
376
QString QtVersion::mkspecPath() const
{
    updateMkSpec();
    return m_mkspecFullPath;
}

377
378
379
380
381
382
QString QtVersion::qtVersionString() const
{
    qmakeCommand();
    return m_qtVersionString;
}

con's avatar
con committed
383
384
385
386
387
388
QHash<QString,QString> QtVersion::versionInfo() const
{
    updateVersionInfo();
    return m_versionInfo;
}

389
390
391
392
393
394
395
QString QtVersion::qmakeCXX() const
{
    updateQMakeCXX();
    return m_qmakeCXX;
}


con's avatar
con committed
396
397
398
399
400
401
402
403
404
405
406
void QtVersion::setName(const QString &name)
{
    m_name = name;
}

void QtVersion::setPath(const QString &path)
{
    m_path = QDir::cleanPath(path);
    updateSourcePath();
    m_versionInfoUpToDate = false;
    m_mkspecUpToDate = false;
407
408
    m_designerCommand = m_linguistCommand = m_qmakeCommand = m_uicCommand = QString::null;
    // TODO do i need to optimize this?
409
    m_hasDebuggingHelper = !debuggingHelperLibrary().isEmpty();
410
411
412
    m_qmakeCXX = QString::null;
    m_qmakeCXXUpToDate = false;
    m_toolChainUpToDate = false;
con's avatar
con committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
}

void QtVersion::updateSourcePath()
{
    m_sourcePath = m_path;
    QFile qmakeCache(m_path + QLatin1String("/.qmake.cache"));
    if (qmakeCache.exists()) {
        qmakeCache.open(QIODevice::ReadOnly | QIODevice::Text);
        QTextStream stream(&qmakeCache);
        while (!stream.atEnd()) {
            QString line = stream.readLine().trimmed();
            if (line.startsWith(QLatin1String("QT_SOURCE_TREE"))) {
                m_sourcePath = line.split(QLatin1Char('=')).at(1).trimmed();
                if (m_sourcePath.startsWith(QLatin1String("$$quote("))) {
                    m_sourcePath.remove(0, 8);
                    m_sourcePath.chop(1);
                }
                break;
            }
        }
    }
}

// Returns the version that was used to build the project in that directory
// That is returns the directory
// To find out wheter we already have a qtversion for that directory call
// QtVersion *QtVersionManager::qtVersionForDirectory(const QString directory);
QString QtVersionManager::findQtVersionFromMakefile(const QString &directory)
{
    bool debugAdding = false;
    QFile makefile(directory + "/Makefile" );
    if (makefile.exists() && makefile.open(QFile::ReadOnly)) {
        QTextStream ts(&makefile);
        while (!ts.atEnd()) {
            QString line = ts.readLine();
            QRegExp r1("QMAKE\\s*=(.*)");
            if (r1.exactMatch(line)) {
                if (debugAdding)
                    qDebug()<<"#~~ QMAKE is:"<<r1.cap(1).trimmed();
                QFileInfo qmake(r1.cap(1).trimmed());
                QFileInfo binDir(qmake.absolutePath());
                QString qtDir = binDir.absolutePath();
                if (debugAdding)
                    qDebug() << "#~~ QtDir:"<<qtDir;
                return qtDir;
            }
        }
        makefile.close();
    }
462
    return QString::null;
con's avatar
con committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
}

QtVersion *QtVersionManager::qtVersionForDirectory(const QString &directory)
{
   foreach(QtVersion *v, versions()) {
        if (v->path() == directory) {
            return v;
            break;
        }
    }
   return 0;
}

QtVersion::QmakeBuildConfig QtVersionManager::scanMakefileForQmakeConfig(const QString &directory, QtVersion::QmakeBuildConfig defaultBuildConfig)
{
    bool debugScan = false;
    QtVersion::QmakeBuildConfig result = QtVersion::NoBuild;
    QFile makefile(directory + "/Makefile" );
    if (makefile.exists() && makefile.open(QFile::ReadOnly)) {
        QTextStream ts(&makefile);
        while (!ts.atEnd()) {
            QString line = ts.readLine();
            if (line.startsWith("# Command:")) {
                // if nothing is specified
                result = defaultBuildConfig;

                // Actually parsing that line is not trivial in the general case
                // There might things like this
                // # Command: /home/dteske/git/bqt-45/bin/qmake -unix CONFIG+=debug\ release CONFIG\ +=\ debug_and_release\ debug -o Makefile test.pro
                // which sets debug_and_release and debug
                // or something like this:
                //[...] CONFIG+=debug\ release CONFIG\ +=\ debug_and_release\ debug CONFIG\ -=\ debug_and_release CONFIG\ -=\ debug -o Makefile test.pro
                // which sets -build_all and release

                // To parse that, we search for the first CONFIG, then look for " " which is not after a "\" or the end
                // And then look at each config individually
                // we then remove all "\ " with just " "
                // += sets adding flags
                // -= sets removing flags
                // and then split the string after the =
                // and go over each item separetly
                // debug sets/removes the flag DebugBuild
                // release removes/sets the flag DebugBuild
                // debug_and_release sets/removes the flag BuildAll
                int pos = line.indexOf("CONFIG");
                if (pos != -1) {
                    // Chopped of anything that is not interesting
                    line = line.mid(pos);
                    line = line.trimmed();
                    if (debugScan)
                        qDebug()<<"chopping line :"<<line;

                    //Now chop into parts that are intresting
                    QStringList parts;
                    int lastpos = 0;
hjk's avatar
hjk committed
518
                    for (int i = 1; i < line.size(); ++i) {
con's avatar
con committed
519
520
521
522
523
524
525
526
527
528
529
530
531
                        if (line.at(i) == QLatin1Char(' ') && line.at(i-1) != QLatin1Char('\\')) {
                            // found a part
                            parts.append(line.mid(lastpos, i-lastpos));
                            if (debugScan)
                                qDebug()<<"part appended:"<<line.mid(lastpos, i-lastpos);
                            lastpos = i + 1; // Nex one starts after the space
                        }
                    }
                    parts.append(line.mid(lastpos));
                    if (debugScan)
                        qDebug()<<"part appended:"<<line.mid(lastpos);

                    foreach(const QString &part, parts) {
hjk's avatar
hjk committed
532
                        if (debugScan)
con's avatar
con committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
                            qDebug()<<"now interpreting part"<<part;
                        bool setFlags;
                        // Now try to understand each part for that we do a rather stupid approach, optimize it if you care
                        if (part.startsWith("CONFIG")) {
                            // Yep something interesting
                            if (part.indexOf("+=") != -1) {
                                setFlags = true;
                            } else if (part.indexOf("-=") != -1) {
                                setFlags = false;
                            } else {
                                setFlags = true;
                                if (debugScan)
                                    qDebug()<<"This can never happen, except if we can't parse Makefiles...";
                            }
                            if (debugScan)
                                qDebug()<<"part has setFlags:"<<setFlags;
                            // now loop forward, looking for something that looks like debug, release or debug_and_release

hjk's avatar
hjk committed
551
                            for (int i = 0; i < part.size(); ++i) {
con's avatar
con committed
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
                                int left = part.size() - i;
                                if (left >= 17  && QStringRef(&part, i, 17) == "debug_and_release") {
                                        if (setFlags)
                                            result = QtVersion::QmakeBuildConfig(result | QtVersion::BuildAll);
                                        else
                                            result = QtVersion::QmakeBuildConfig(result & ~QtVersion::BuildAll);
                                        if (debugScan)
                                            qDebug()<<"found debug_and_release new value"<<result;
                                        i += 17;
                                } else if (left >=7 && QStringRef(&part, i, 7) ==  "release") {
                                        if (setFlags)
                                            result = QtVersion::QmakeBuildConfig(result & ~QtVersion::DebugBuild);
                                        else
                                            result = QtVersion::QmakeBuildConfig(result | QtVersion::DebugBuild);
                                        if (debugScan)
                                            qDebug()<<"found release new value"<<result;
hjk's avatar
hjk committed
568
                                        i += 7;
con's avatar
con committed
569
570
571
572
573
574
575
                                } else if (left >= 5 && QStringRef(&part, i, 5) == "debug") {
                                        if (setFlags)
                                            result = QtVersion::QmakeBuildConfig(result  | QtVersion::DebugBuild);
                                        else
                                            result = QtVersion::QmakeBuildConfig(result  & ~QtVersion::DebugBuild);
                                        if (debugScan)
                                            qDebug()<<"found debug new value"<<result;
hjk's avatar
hjk committed
576
                                        i += 5;
con's avatar
con committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
                                }
                            }
                        }
                    }
                }
                if (debugScan)
                    qDebug()<<"returning: "<<result;
                if (debugScan)
                    qDebug()<<"buildall = "<<bool(result & QtVersion::BuildAll);
                if (debugScan)
                    qDebug()<<"debug ="<<bool(result & QtVersion::DebugBuild);
            }
        }
        makefile.close();
    }
    return result;
}

void QtVersion::updateVersionInfo() const
{
    if (m_versionInfoUpToDate)
        return;
    // extract data from qmake executable
    m_versionInfo.clear();
    m_notInstalled = false;
    QFileInfo qmake(qmakeCommand());
    if (qmake.exists()) {
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
        static const char * const variables[] = {
             "QT_INSTALL_DATA",
             "QT_INSTALL_LIBS",
             "QT_INSTALL_HEADERS",
             "QT_INSTALL_DEMOS",
             "QT_INSTALL_EXAMPLES",
             "QT_INSTALL_CONFIGURATION",
             "QT_INSTALL_TRANSLATIONS",
             "QT_INSTALL_PLUGINS",
             "QT_INSTALL_BINS",
             "QT_INSTALL_DOCS",
             "QT_INSTALL_PREFIX"
        };
        QStringList args;
        for (uint i = 0; i < sizeof variables / sizeof variables[0]; ++i)
            args << "-query" << variables[i];
con's avatar
con committed
620
621
622
623
624
625
626
627
628
        QProcess process;
        process.start(qmake.absoluteFilePath(), args, QIODevice::ReadOnly);
        if (process.waitForFinished(2000)) {
            QByteArray output = process.readAllStandardOutput();
            QTextStream stream(&output);
            while (!stream.atEnd()) {
                QString line = stream.readLine();
                int index = line.indexOf(":");
                if (index != -1)
Joerg Bornemann's avatar
Joerg Bornemann committed
629
                    m_versionInfo.insert(line.left(index), QDir::fromNativeSeparators(line.mid(index+1)));
con's avatar
con committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
            }
        }

        if (m_versionInfo.contains("QT_INSTALL_DATA"))
            m_versionInfo.insert("QMAKE_MKSPECS", QDir::cleanPath(m_versionInfo.value("QT_INSTALL_DATA")+"/mkspecs"));

        // Now check for a qt that is configured with a prefix but not installed
        if (m_versionInfo.contains("QT_INSTALL_BINS")) {
            QFileInfo fi(m_versionInfo.value("QT_INSTALL_BINS"));
            if (!fi.exists())
                m_notInstalled = true;
        }
        if (m_versionInfo.contains("QT_INSTALL_HEADERS")){
            QFileInfo fi(m_versionInfo.value("QT_INSTALL_HEADERS"));
            if (!fi.exists())
                m_notInstalled = true;
        }

        // Parse qconfigpri
        QString baseDir = m_versionInfo.contains("QT_INSTALL_DATA") ?
                           m_versionInfo.value("QT_INSTALL_DATA") :
                           m_path;
        QFile qconfigpri(baseDir + QLatin1String("/mkspecs/qconfig.pri"));
        if (qconfigpri.exists()) {
            qconfigpri.open(QIODevice::ReadOnly | QIODevice::Text);
            QTextStream stream(&qconfigpri);
            while (!stream.atEnd()) {
                QString line = stream.readLine().trimmed();
                if (line.startsWith(QLatin1String("CONFIG"))) {
                    m_defaultConfigIsDebugAndRelease = false;
                    QStringList values = line.split(QLatin1Char('=')).at(1).trimmed().split(" ");
                    foreach(const QString &value, values) {
                        if (value == "debug")
                            m_defaultConfigIsDebug = true;
hjk's avatar
hjk committed
664
                        else if (value == "release")
con's avatar
con committed
665
                            m_defaultConfigIsDebug = false;
hjk's avatar
hjk committed
666
                        else if (value == "build_all")
con's avatar
con committed
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
                            m_defaultConfigIsDebugAndRelease = true;
                    }
                }
            }
        }
    }
    m_versionInfoUpToDate = true;
}

bool QtVersion::isInstalled() const
{
    updateVersionInfo();
    return !m_notInstalled;
}

void QtVersion::updateMkSpec() const
{
    if (m_mkspecUpToDate)
        return;
    //qDebug()<<"Finding mkspec for"<<path();

    QString mkspec;
689
690
691
692
693
694
    // no .qmake.cache so look at the default mkspec
    QString mkspecPath = versionInfo().value("QMAKE_MKSPECS");
    if (mkspecPath.isEmpty())
        mkspecPath = path() + "/mkspecs/default";
    else
        mkspecPath = mkspecPath + "/default";
con's avatar
con committed
695
696
//        qDebug() << "default mkspec is located at" << mkspecPath;
#ifdef Q_OS_WIN
697
698
699
700
701
702
703
704
    QFile f2(mkspecPath + "/qmake.conf");
    if (f2.exists() && f2.open(QIODevice::ReadOnly)) {
        while (!f2.atEnd()) {
            QByteArray line = f2.readLine();
            if (line.startsWith("QMAKESPEC_ORIGINAL")) {
                const QList<QByteArray> &temp = line.split('=');
                if (temp.size() == 2) {
                    mkspec = temp.at(1).trimmed();
con's avatar
con committed
705
                }
706
                break;
con's avatar
con committed
707
708
            }
        }
709
710
        f2.close();
    }
con's avatar
con committed
711
#elif defined(Q_OS_MAC)
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
    QFile f2(mkspecPath + "/qmake.conf");
    if (f2.exists() && f2.open(QIODevice::ReadOnly)) {
        while (!f2.atEnd()) {
            QByteArray line = f2.readLine();
            if (line.startsWith("MAKEFILE_GENERATOR")) {
                const QList<QByteArray> &temp = line.split('=');
                if (temp.size() == 2) {
                    const QByteArray &value = temp.at(1);
                    if (value.contains("XCODE")) {
                        // we don't want to generate xcode projects...
//                      qDebug() << "default mkspec is xcode, falling back to g++";
                        mkspec = "macx-g++";
                    } else {
                        //resolve mkspec link
                        QFileInfo f3(mkspecPath);
                        if (f3.isSymLink()) {
                            mkspec = f3.symLinkTarget();
con's avatar
con committed
729
730
731
                        }
                    }
                }
732
                break;
con's avatar
con committed
733
734
            }
        }
735
736
        f2.close();
    }
con's avatar
con committed
737
#else
738
739
740
741
    QFileInfo f2(mkspecPath);
    if (f2.isSymLink()) {
        mkspec = f2.symLinkTarget();
    }
con's avatar
con committed
742
743
#endif

dt's avatar
dt committed
744
745
    m_mkspecFullPath = mkspec;
    int index = mkspec.lastIndexOf('/');
hjk's avatar
hjk committed
746
    if (index == -1)
con's avatar
con committed
747
        index = mkspec.lastIndexOf('\\');
Joerg Bornemann's avatar
Joerg Bornemann committed
748
749
    QString mkspecDir = QDir(m_path + "/mkspecs/").canonicalPath();
    if (index >= 0 && QDir(mkspec.left(index)).canonicalPath() == mkspecDir)
con's avatar
con committed
750
751
752
753
754
755
756
757
758
759
760
761
762
763
        mkspec = mkspec.mid(index+1).trimmed();

    m_mkspec = mkspec;
    m_mkspecUpToDate = true;
//    qDebug()<<"mkspec for "<<m_path<<" is "<<mkspec;
}

QString QtVersion::qmakeCommand() const
{
    // We can't use versionInfo QT_INSTALL_BINS here
    // because that functions calls us to find out the values for versionInfo
    if (!m_qmakeCommand.isNull())
        return m_qmakeCommand;

764
    QDir qtDir = path() + QLatin1String("/bin/");
765
    foreach (const QString &possibleCommand, DebuggingHelperLibrary::possibleQMakeCommands()) {
con's avatar
con committed
766
767
768
        QString s = qtDir.absoluteFilePath(possibleCommand);
        QFileInfo qmake(s);
        if (qmake.exists() && qmake.isExecutable()) {
769
            QString qtVersion = DebuggingHelperLibrary::qtVersionForQMake(qmake.absoluteFilePath());
770
771
            if (!qtVersion.isNull()) {
                m_qtVersionString = qtVersion;
con's avatar
con committed
772
773
774
775
776
777
778
779
                m_qmakeCommand = qmake.absoluteFilePath();
                return qmake.absoluteFilePath();
            }
        }
    }
    return QString::null;
}

780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
void QtVersion::updateQMakeCXX() const
{
    if (m_qmakeCXXUpToDate)
        return;
    ProFileReader *reader = new ProFileReader();
    reader->setCumulative(false);
    reader->setParsePreAndPostFiles(false);
    reader->readProFile(mkspecPath() + "/qmake.conf");
    m_qmakeCXX = reader->value("QMAKE_CXX");
    delete reader;
    m_qmakeCXXUpToDate = true;
}

ProjectExplorer::ToolChain *QtVersion::toolChain() const
{
    updateToolChain();
    return m_toolChain;
}

void QtVersion::updateToolChain() const
{
    if (m_toolChainUpToDate)
        return;
    ProjectExplorer::ToolChain *m_test= 0;
    ProjectExplorer::ToolChain::ToolChainType t = toolchainType();
    if (t == ProjectExplorer::ToolChain::MinGW) {
        QString qmake_cxx = qmakeCXX();
        ProjectExplorer::Environment env = ProjectExplorer::Environment::systemEnvironment();
        //addToEnvironment(env);
        env.prependOrSetPath(mingwDirectory()+"/bin");
        qmake_cxx = env.searchInPath(qmake_cxx);
        m_test = ProjectExplorer::ToolChain::createMinGWToolChain(qmake_cxx, mingwDirectory());
        //qDebug()<<"Mingw ToolChain";
    } else if(t == ProjectExplorer::ToolChain::MSVC) {
        m_test = ProjectExplorer::ToolChain::createMSVCToolChain(msvcVersion(), isMSVC64Bit());
        //qDebug()<<"MSVC ToolChain ("<<version->msvcVersion()<<")";
    } else if(t == ProjectExplorer::ToolChain::WINCE) {
        m_test = ProjectExplorer::ToolChain::createWinCEToolChain(msvcVersion(), wincePlatform());
        //qDebug()<<"WinCE ToolChain ("<<version->msvcVersion()<<","<<version->wincePlatform()<<")";
    } else if(t == ProjectExplorer::ToolChain::GCC || t == ProjectExplorer::ToolChain::LinuxICC) {
        QString qmake_cxx = qmakeCXX();
        ProjectExplorer::Environment env = ProjectExplorer::Environment::systemEnvironment();
        //addToEnvironment(env);
        qmake_cxx = env.searchInPath(qmake_cxx);
        if (qmake_cxx.isEmpty()) {
            // macx-xcode mkspec resets the value of QMAKE_CXX.
            // Unfortunately, we need a valid QMAKE_CXX to configure the parser.
            qmake_cxx = QLatin1String("cc");
        }
        m_test = ProjectExplorer::ToolChain::createGccToolChain(qmake_cxx);
        //qDebug()<<"GCC ToolChain ("<<qmake_cxx<<")";
    } else {
        qDebug()<<"Could not detect ToolChain for"<<mkspec();
        qDebug()<<"Qt Creator doesn't know about the system includes, nor the systems defines.";
    }

    if (ProjectExplorer::ToolChain::equals(m_test, m_toolChain)) {
        delete m_test;
    } else {
        delete m_toolChain;
        m_toolChain = m_test;
    }

    m_toolChainUpToDate = true;
}


847
848
849
850
851
852
853
854
855
856
857
QString QtVersion::findQtBinary(const QStringList &possibleCommands) const
{
    const QString qtdirbin = versionInfo().value(QLatin1String("QT_INSTALL_BINS")) + QLatin1Char('/');
    foreach (const QString &possibleCommand, possibleCommands) {
        const QString fullPath = qtdirbin + possibleCommand;
        if (QFileInfo(fullPath).isFile())
            return QDir::cleanPath(fullPath);
    }
    return QString::null;
}

dt's avatar
dt committed
858
859
860
861
862
863
QString QtVersion::uicCommand() const
{
    if (!isValid())
        return QString::null;
    if (!m_uicCommand.isNull())
        return m_uicCommand;
864
865
866
#ifdef Q_OS_WIN
    const QStringList possibleCommands(QLatin1String("uic.exe"));
#else
dt's avatar
dt committed
867
    QStringList possibleCommands;
868
869
870
871
872
873
874
875
876
877
    possibleCommands << QLatin1String("uic-qt4") << QLatin1String("uic4") << QLatin1String("uic");
#endif
    m_uicCommand = findQtBinary(possibleCommands);
    return m_uicCommand;
}

// Return a list of GUI binary names
// 'foo', 'foo.exe', 'Foo.app/Contents/MacOS/Foo'
static inline QStringList possibleGuiBinaries(const QString &name)
{
dt's avatar
dt committed
878
#ifdef Q_OS_WIN
879
880
881
882
883
884
885
886
887
    return QStringList(name + QLatin1String(".exe"));
#endif
#ifdef Q_OS_MAC // 'Foo.app/Contents/MacOS/Foo'
    QString upCaseName = name;
    upCaseName[0] = upCaseName.at(0).toUpper();
    QString macBinary = upCaseName;
    macBinary += QLatin1String(".app/Contents/MacOS/");
    macBinary += upCaseName;
    return QStringList(macBinary);
dt's avatar
dt committed
888
#else
889
    return QStringList(name);
dt's avatar
dt committed
890
#endif
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
}

QString QtVersion::designerCommand() const
{
    if (!isValid())
        return QString::null;
    if (m_designerCommand.isNull())
        m_designerCommand = findQtBinary(possibleGuiBinaries(QLatin1String("designer")));
    return m_designerCommand;
}

QString QtVersion::linguistCommand() const
{
    if (!isValid())
        return QString::null;
    if (m_linguistCommand.isNull())
        m_linguistCommand = findQtBinary(possibleGuiBinaries(QLatin1String("linguist")));
    return m_linguistCommand;
dt's avatar
dt committed
909
910
}

dt's avatar
dt committed
911
ProjectExplorer::ToolChain::ToolChainType QtVersion::toolchainType() const
con's avatar
con committed
912
913
{
    if (!isValid())
dt's avatar
dt committed
914
        return ProjectExplorer::ToolChain::INVALID;
con's avatar
con committed
915
    const QString &spec = mkspec();
916
//    qDebug()<<"spec="<<spec;
hjk's avatar
hjk committed
917
    if (spec.contains("win32-msvc") || spec.contains(QLatin1String("win32-icc")))
dt's avatar
dt committed
918
        return ProjectExplorer::ToolChain::MSVC;
919
    else if (spec.contains("win32-g++"))
dt's avatar
dt committed
920
        return ProjectExplorer::ToolChain::MinGW;
hjk's avatar
hjk committed
921
    else if (spec == QString::null)
dt's avatar
dt committed
922
        return ProjectExplorer::ToolChain::INVALID;
923
    else if (spec.contains("wince"))
dt's avatar
dt committed
924
        return ProjectExplorer::ToolChain::WINCE;
925
    else if (spec.contains("linux-icc"))
926
        return ProjectExplorer::ToolChain::LinuxICC;
con's avatar
con committed
927
    else
dt's avatar
dt committed
928
        return ProjectExplorer::ToolChain::GCC;
con's avatar
con committed
929
930
931
932
933
934
935
936
937
938
}

QString QtVersion::mingwDirectory() const
{
    return m_mingwDirectory;
}

void QtVersion::setMingwDirectory(const QString &directory)
{
    m_mingwDirectory = directory;
939
    m_toolChainUpToDate = false;
con's avatar
con committed
940
941
942
943
944
945
946
}

QString QtVersion::msvcVersion() const
{
    return m_msvcVersion;
}

dt's avatar
dt committed
947
948
QString QtVersion::wincePlatform() const
{
949
//    qDebug()<<"QtVersion::wincePlatform returning"<<ProjectExplorer::CeSdkHandler::platformName(mkspecPath() + "/qmake.conf");
dt's avatar
dt committed
950
951
952
    return ProjectExplorer::CeSdkHandler::platformName(mkspecPath() + "/qmake.conf");
}

con's avatar
con committed
953
954
955
void QtVersion::setMsvcVersion(const QString &version)
{
    m_msvcVersion = version;
956
    m_toolChainUpToDate = false;
con's avatar
con committed
957
958
}

959
void QtVersion::addToEnvironment(ProjectExplorer::Environment &env) const
con's avatar
con committed
960
{
dt's avatar
dt committed
961
    env.set("QTDIR", m_path);
con's avatar
con committed
962
    QString qtdirbin = versionInfo().value("QT_INSTALL_BINS");
dt's avatar
dt committed
963
    env.prependOrSetPath(qtdirbin);
con's avatar
con committed
964
965
966
    // add libdir, includedir and bindir
    // or add Mingw dirs
    // or do nothing on other
967
968
969
    ProjectExplorer::ToolChain *tc = toolChain();
    if (tc)
        tc->addToEnvironment(env);
con's avatar
con committed
970
971
972
973
974
975
976
977
978
}

int QtVersion::uniqueId() const
{
    return m_id;
}

int QtVersion::getUniqueId()
{
979
    return QtVersionManager::instance()->getUniqueId();
con's avatar
con committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
}

bool QtVersion::isValid() const
{
    return (!(m_id == -1 || m_path == QString::null || m_name == QString::null || mkspec() == QString::null) && !m_notInstalled);
}

QtVersion::QmakeBuildConfig QtVersion::defaultBuildConfig() const
{
    updateVersionInfo();
    QtVersion::QmakeBuildConfig result = QtVersion::QmakeBuildConfig(0);
    if (m_defaultConfigIsDebugAndRelease)
        result = QtVersion::BuildAll;
    if (m_defaultConfigIsDebug)
        result = QtVersion::QmakeBuildConfig(result | QtVersion::DebugBuild);
    return result;
}
997
998
999
1000
1001
1002

bool QtVersion::hasDebuggingHelper() const
{
    return m_hasDebuggingHelper;
}

1003
1004
1005
1006
1007
QString QtVersion::debuggingHelperLibrary() const
{
    QString qtInstallData = versionInfo().value("QT_INSTALL_DATA");
    if (qtInstallData.isEmpty())
        qtInstallData = path();
1008
    return DebuggingHelperLibrary::debuggingHelperLibrary(qtInstallData, path());
1009
1010
}

1011

1012
1013
1014
1015
1016
1017
bool QtVersion::isMSVC64Bit() const
{
        const QString make = qmakeCommand();
        qDebug() << make;
        bool isAmd64 = false;
#ifdef Q_OS_WIN32
Friedemann Kleint's avatar
Friedemann Kleint committed
1018
1019
1020
#  ifdef __GNUC__   // MinGW lacking some definitions/winbase.h
#    define SCS_64BIT_BINARY 6
#  endif   
1021
        DWORD binaryType = 0;
Friedemann Kleint's avatar
Friedemann Kleint committed
1022
        bool success = GetBinaryTypeW(reinterpret_cast<const TCHAR*>(make.utf16()), &binaryType) != 0;
1023
1024
1025
1026
        if (success && binaryType == SCS_64BIT_BINARY)
            isAmd64=true;
//        qDebug() << "isAmd64:" << isAmd64 << binaryType;
        return isAmd64;
1027
1028
1029
#else
        Q_UNUSED(isAmd64)
        return false;
1030
1031
1032
#endif
}

1033
1034
QString QtVersion::buildDebuggingHelperLibrary()
{
1035
1036
1037
    QString qtInstallData = versionInfo().value("QT_INSTALL_DATA");
    if (qtInstallData.isEmpty())
        qtInstallData = path();
1038
1039
1040
    ProjectExplorer::Environment env = ProjectExplorer::Environment::systemEnvironment();
    addToEnvironment(env);

1041
    ProjectExplorer::ToolChain *tc = toolChain();
1042
    QString directory = DebuggingHelperLibrary::copyDebuggingHelperLibrary(qtInstallData, path());
1043
    QString output = DebuggingHelperLibrary::buildDebuggingHelperLibrary(directory, tc->makeCommand(), qmakeCommand(), mkspec(), env);
1044
1045
1046
1047
    m_hasDebuggingHelper = !debuggingHelperLibrary().isEmpty();
    return output;
}