qtversionmanager.cpp 69.7 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
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).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
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
**
con's avatar
con committed
25
26
27
28
29
30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31
**
32
**************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34
#include "qtversionmanager.h"
hjk's avatar
hjk committed
35

36
#include "qt4projectmanagerconstants.h"
Tobias Hunger's avatar
Tobias Hunger committed
37
#include "qt4target.h"
38
#include "profilereader.h"
con's avatar
con committed
39

40
#include "qt-maemo/maemoglobal.h"
ck's avatar
ck committed
41
#include "qt-maemo/maemomanager.h"
42
#include "qt-s60/s60manager.h"
43
#include "qt-s60/s60projectchecker.h"
44
45
#include "qt-s60/abldparser.h"
#include "qt-s60/sbsv2parser.h"
46
47
#include "qt-s60/gccetoolchain.h"
#include "qt-s60/winscwtoolchain.h"
48

49
#include "qmlobservertool.h"
50
#include "qmldumptool.h"
51
52
#include "qmldebugginglibrary.h"

53
#include <projectexplorer/debugginghelper.h>
54
#include <projectexplorer/gnumakeparser.h>
55
#include <projectexplorer/projectexplorer.h>
56
#include <projectexplorer/projectexplorerconstants.h>
57
#include <projectexplorer/toolchainmanager.h>
58
#include <projectexplorer/cesdkhandler.h>
59
60
#include <projectexplorer/gcctoolchain.h>
#include <projectexplorer/toolchainmanager.h>
61
62
63
64
#include <projectexplorer/headerpath.h>
#include <projectexplorer/ioutputparser.h>
#include <projectexplorer/task.h>

65
#include <coreplugin/coreconstants.h>
66
#include <coreplugin/icore.h>
kh1's avatar
kh1 committed
67
#include <coreplugin/helpmanager.h>
68

69
#include <extensionsystem/pluginmanager.h>
70
71

#include <utils/synchronousprocess.h>
hjk's avatar
hjk committed
72
#include <utils/qtcassert.h>
73
#include <utils/qtcprocess.h>
74
75
76
#ifdef Q_OS_WIN
#    include <utils/winutils.h>
#endif
con's avatar
con committed
77

78
#include <QtCore/QFile>
hjk's avatar
hjk committed
79
#include <QtCore/QProcess>
con's avatar
con committed
80
81
#include <QtCore/QSettings>
#include <QtCore/QTime>
82
#include <QtCore/QTimer>
83
#include <QtCore/QTextStream>
84
#include <QtCore/QDir>
85
86
#include <QtGui/QApplication>
#include <QtGui/QDesktopServices>
con's avatar
con committed
87

88
89
90
91
using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;

using ProjectExplorer::DebuggingHelperLibrary;
con's avatar
con committed
92
93
94

static const char *QtVersionsSectionName = "QtVersions";
static const char *newQtVersionsKey = "NewQtVersions";
95
static const char *PATH_AUTODETECTION_SOURCE = "PATH";
con's avatar
con committed
96

97
98
enum { debug = 0 };

99
100
101
102
103
104
105
106
107
108
109
110
111
112
template<class T>
static T *createToolChain(const QString &id)
{
    QList<ProjectExplorer::ToolChainFactory *> factories =
            ExtensionSystem::PluginManager::instance()->getObjects<ProjectExplorer::ToolChainFactory>();
    foreach (ProjectExplorer::ToolChainFactory *f, factories) {
       if (f->id() == id) {
           Q_ASSERT(f->canCreate());
           return static_cast<T *>(f->create());
       }
    }
    return 0;
}

113
114
115
116
117
118
119

// prefer newer qts otherwise compare on id
bool qtVersionNumberCompare(QtVersion *a, QtVersion *b)
{
    return a->qtVersion() > b->qtVersion() || (a->qtVersion() == b->qtVersion() && a->uniqueId() < b->uniqueId());
}

120
121
122
// --------------------------------------------------------------------------
// QtVersionManager
// --------------------------------------------------------------------------
123
QtVersionManager *QtVersionManager::m_self = 0;
124

con's avatar
con committed
125
126
127
QtVersionManager::QtVersionManager()
    : m_emptyVersion(new QtVersion)
{
128
    m_self = this;
129
    QSettings *s = Core::ICore::instance()->settings();
con's avatar
con committed
130
131
132
133
134
135
136
137
138
139
140
141

    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();
dt's avatar
dt committed
142
143
        else if (m_idcount < id)
            m_idcount = id + 1;
144
        bool isAutodetected;
145
146
        QString autodetectionSource;
        if (s->contains("isAutodetected")) {
147
            isAutodetected = s->value("isAutodetected", false).toBool();
148
149
            autodetectionSource = s->value("autodetectionSource", QString()).toString();
        } else {// compatibility
150
            isAutodetected = s->value("IsSystemVersion", false).toBool();
151
152
153
            if (isAutodetected)
                autodetectionSource = QLatin1String(PATH_AUTODETECTION_SOURCE);
        }
154
155
156
        QString qmakePath = s->value("QMakePath").toString();
        if (qmakePath.isEmpty()) {
            QString path = s->value("Path").toString();
157
158
159
160
161
162
163
164
165
166
167
            if (!path.isEmpty()) {
                foreach(const QString& command, ProjectExplorer::DebuggingHelperLibrary::possibleQMakeCommands())
                {
                    QFileInfo fi(path + "/bin/" + command);
                    if (fi.exists())
                    {
                        qmakePath = fi.filePath();
                        break;
                    }
                }
            }
168
        }
con's avatar
con committed
169
        QtVersion *version = new QtVersion(s->value("Name").toString(),
170
                                           qmakePath,
con's avatar
con committed
171
                                           id,
172
173
                                           isAutodetected,
                                           autodetectionSource);
174
        version->setS60SDKDirectory(s->value("S60SDKDirectory").toString());
175
        version->setSbsV2Directory(s->value(QLatin1String("SBSv2Directory")).toString());
176
177
178
179
180
181
182

        // Update from 2.1 or earlier:
        QString mingwDir = s->value(QLatin1String("MingwDirectory")).toString();
        if (!mingwDir.isEmpty()) {
            ProjectExplorer::MingwToolChain *tc = createToolChain<ProjectExplorer::MingwToolChain>(ProjectExplorer::Constants::MINGW_TOOLCHAIN_ID);
            if (tc) {
                tc->setCompilerPath(QDir::fromNativeSeparators(mingwDir) + QLatin1String("/bin/gcc.exe"));
Friedemann Kleint's avatar
Friedemann Kleint committed
183
                tc->setDisplayName(tr("MinGW from %1").arg(version->displayName()));
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
                ProjectExplorer::ToolChainManager::instance()->registerToolChain(tc);
            }
        }
        QString mwcDir = s->value(QLatin1String("MwcDirectory")).toString();
        if (!mwcDir.isEmpty()) {
            WinscwToolChain *tc = createToolChain<WinscwToolChain>(Constants::WINSCW_TOOLCHAIN_ID);
            if (tc) {
                tc->setCompilerPath(QDir::fromNativeSeparators(mwcDir)
                                    + QLatin1String("/x86Build/Symbian_Tools/Command_Line_Tools/mwwinrc.exe"));
                tc->setDisplayName(tr("WINSCW from %1").arg(version->displayName()));
                ProjectExplorer::ToolChainManager::instance()->registerToolChain(tc);
            }
        }
        QString gcceDir = s->value(QLatin1String("GcceDirectory")).toString();
        if (!gcceDir.isEmpty()) {
            GcceToolChain *tc = createToolChain<GcceToolChain>(Constants::GCCE_TOOLCHAIN_ID);
            if (tc) {
                tc->setCompilerPath(QDir::fromNativeSeparators(gcceDir)
                                    + QLatin1String("/bin/arm-none-symbianelf-g++.exe"));
                tc->setDisplayName(tr("GCCE from %1").arg(version->displayName()));
                ProjectExplorer::ToolChainManager::instance()->registerToolChain(tc);
            }
        }

208
        m_versions.insert(version->uniqueId(), version);
con's avatar
con committed
209
210
211
212
213
214
215
216
    }
    s->endArray();

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

    writeVersionsIntoSettings();
217

con's avatar
con committed
218
    updateDocumentation();
219

220
221
    // cannot call from ctor, needs to get connected extenernally first
    QTimer::singleShot(0, this, SLOT(updateExamples()));
con's avatar
con committed
222
223
224
225
226
227
228
229
230
231
}

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

232
QtVersionManager *QtVersionManager::instance()
233
{
234
    return m_self;
235
236
}

con's avatar
con committed
237
238
void QtVersionManager::addVersion(QtVersion *version)
{
239
    QTC_ASSERT(version != 0, return);
240
    if (m_versions.contains(version->uniqueId()))
241
242
        return;

243
    int uniqueId = version->uniqueId();
244
245
    m_versions.insert(uniqueId, version);

246
    emit qtVersionsChanged(QList<int>() << uniqueId);
247
    writeVersionsIntoSettings();
con's avatar
con committed
248
249
}

250
251
void QtVersionManager::removeVersion(QtVersion *version)
{
252
    QTC_ASSERT(version != 0, return);
253
254
    m_versions.remove(version->uniqueId());
    emit qtVersionsChanged(QList<int>() << version->uniqueId());
255
256
257
258
    writeVersionsIntoSettings();
    delete version;
}

Tobias Hunger's avatar
Tobias Hunger committed
259
260
261
262
263
264
265
266
267
bool QtVersionManager::supportsTargetId(const QString &id) const
{
    foreach (QtVersion *version, m_versions) {
        if (version->supportsTargetId(id))
            return true;
    }
    return false;
}

268
QList<QtVersion *> QtVersionManager::versionsForTargetId(const QString &id, const QtVersionNumber &minimumQtVersion) const
Tobias Hunger's avatar
Tobias Hunger committed
269
270
271
{
    QList<QtVersion *> targetVersions;
    foreach (QtVersion *version, m_versions) {
272
        if (version->supportsTargetId(id) && version->qtVersion() >= minimumQtVersion)
Tobias Hunger's avatar
Tobias Hunger committed
273
274
            targetVersions.append(version);
    }
275
    qSort(targetVersions.begin(), targetVersions.end(), &qtVersionNumberCompare);
Tobias Hunger's avatar
Tobias Hunger committed
276
277
278
279
280
281
282
283
284
285
286
    return targetVersions;
}

QSet<QString> QtVersionManager::supportedTargetIds() const
{
    QSet<QString> results;
    foreach (QtVersion *version, m_versions)
        results.unite(version->supportedTargetIds());
    return results;
}

con's avatar
con committed
287
288
void QtVersionManager::updateDocumentation()
{
kh1's avatar
kh1 committed
289
    Core::HelpManager *helpManager = Core::HelpManager::instance();
dt's avatar
dt committed
290
    Q_ASSERT(helpManager);
con's avatar
con committed
291
292
    QStringList files;
    foreach (QtVersion *version, m_versions) {
293
294
295
296
297
298
        const QString docPath = version->documentationPath() + QLatin1String("/qch/");
        const QDir versionHelpDir(docPath);
        foreach (const QString &helpFile,
                versionHelpDir.entryList(QStringList() << QLatin1String("*.qch"), QDir::Files))
            files << docPath + helpFile;

con's avatar
con committed
299
300
301
302
    }
    helpManager->registerDocumentation(files);
}

303
304
305
void QtVersionManager::updateExamples()
{
    QtVersion *version = 0;
306
307
    QList<QtVersion*> candidates;

Tobias Hunger's avatar
Tobias Hunger committed
308
    // try to find a version which has both, demos and examples
309
310
311
312
313
    foreach (version, m_versions) {
        if (version->hasExamples() && version->hasDemos())
        candidates.append(version);
    }

314
    // in SDKs, we want to prefer the Qt version shipping with the SDK
315
316
317
    QSettings *settings = Core::ICore::instance()->settings();
    QString preferred = settings->value(QLatin1String("PreferredQMakePath")).toString();
    preferred = QDir::fromNativeSeparators(preferred);
318
    if (!preferred.isEmpty()) {
319
320
321
322
323
#ifdef Q_OS_WIN
        preferred = preferred.toLower();
        if (!preferred.endsWith(QLatin1String(".exe")))
            preferred.append(QLatin1String(".exe"));
#endif
324
325
326
327
328
329
        foreach (version, candidates) {
            if (version->qmakeCommand() == preferred) {
                emit updateExamples(version->examplesPath(), version->demosPath(), version->sourcePath());
                return;
            }
        }
330
331
332
333
334
335
    }

    // prefer versions with declarative examples
    foreach (version, candidates) {
        if (QDir(version->examplesPath()+"/declarative").exists()) {
            emit updateExamples(version->examplesPath(), version->demosPath(), version->sourcePath());
336
337
338
            return;
        }
    }
339
340
341
342
343
344
345
346

    if (!candidates.isEmpty()) {
        version = candidates.first();
        emit updateExamples(version->examplesPath(), version->demosPath(), version->sourcePath());
        return;
    }
    return;

347
348
}

con's avatar
con committed
349
350
351
352
353
354
355
int QtVersionManager::getUniqueId()
{
    return m_idcount++;
}

void QtVersionManager::writeVersionsIntoSettings()
{
356
    QSettings *s = Core::ICore::instance()->settings();
357
    s->beginWriteArray(QtVersionsSectionName);
358
    QMap<int, QtVersion *>::const_iterator it = m_versions.constBegin();
con's avatar
con committed
359
    for (int i = 0; i < m_versions.size(); ++i) {
360
        const QtVersion *version = it.value();
con's avatar
con committed
361
        s->setArrayIndex(i);
362
        s->setValue("Name", version->displayName());
363
364
365
        // for downwards compat
        s->setValue("Path", version->versionInfo().value("QT_INSTALL_DATA"));
        s->setValue("QMakePath", version->qmakeCommand());
366
367
368
369
        s->setValue("Id", version->uniqueId());
        s->setValue("isAutodetected", version->isAutodetected());
        if (version->isAutodetected())
            s->setValue("autodetectionSource", version->autodetectionSource());
370
        s->setValue("S60SDKDirectory", version->s60SDKDirectory());
371
        s->setValue(QLatin1String("SBSv2Directory"), version->sbsV2Directory());
372
        ++it;
con's avatar
con committed
373
374
375
376
    }
    s->endArray();
}

377
QList<QtVersion *> QtVersionManager::versions() const
con's avatar
con committed
378
{
379
380
381
382
383
    QList<QtVersion *> versions;
    foreach (QtVersion *version, m_versions)
        versions << version;
    qSort(versions.begin(), versions.end(), &qtVersionNumberCompare);
    return versions;
con's avatar
con committed
384
385
}

386
387
388
389
390
391
392
QList<QtVersion *> QtVersionManager::validVersions() const
{
    QList<QtVersion *> results;
    foreach(QtVersion *v, m_versions) {
        if (v->isValid())
            results.append(v);
    }
393
    qSort(results.begin(), results.end(), &qtVersionNumberCompare);
394
395
396
    return results;
}

397
398
bool QtVersionManager::isValidId(int id) const
{
399
    return m_versions.contains(id);
400
401
}

con's avatar
con committed
402
403
QtVersion *QtVersionManager::version(int id) const
{
404
405
406
407
    QMap<int, QtVersion *>::const_iterator it = m_versions.find(id);
    if (it == m_versions.constEnd())
        return m_emptyVersion;
    return it.value();
con's avatar
con committed
408
409
}

410
// FIXME: Rework this!
con's avatar
con committed
411
412
413
void QtVersionManager::addNewVersionsFromInstaller()
{
    // Add new versions which may have been installed by the WB installer in the form:
414
    // NewQtVersions="qt 4.3.2=c:\\qt\\qt432\bin\qmake.exe;qt embedded=c:\\qtembedded;"
415
    // or NewQtVersions="qt 4.3.2=c:\\qt\\qt432bin\qmake.exe;
416
    // i.e.
417
    // NewQtVersions="versionname=pathtoversion=s60sdk;"
con's avatar
con committed
418
    // Duplicate entries are not added, the first new version is set as default.
419
    QSettings *settings = Core::ICore::instance()->settings();
420
    QSettings *globalSettings = Core::ICore::instance()->settings(QSettings::SystemScope);
421

422
423
424
425
    QDateTime lastUpdateFromGlobalSettings = globalSettings->value(
            QLatin1String("General/LastQtVersionUpdate")).toDateTime();

    const QFileInfo gsFi(globalSettings->fileName());
426
427
    if ( !lastUpdateFromGlobalSettings.isNull() &&
         (!gsFi.exists() || (gsFi.lastModified() > lastUpdateFromGlobalSettings)) )
con's avatar
con committed
428
429
        return;

430
431
432
433
434
    if (!globalSettings->contains(newQtVersionsKey) &&
        !globalSettings->contains(QLatin1String("Installer/")+newQtVersionsKey))
    {
        return;
    }
con's avatar
con committed
435
436

    QString newVersionsValue = settings->value(newQtVersionsKey).toString();
437
438
439
    if (newVersionsValue.isEmpty())
        newVersionsValue = settings->value(QLatin1String("Installer/")+newQtVersionsKey).toString();

con's avatar
con committed
440
    QStringList newVersionsList = newVersionsValue.split(';', QString::SkipEmptyParts);
441
    foreach (const QString &newVersion, newVersionsList) {
con's avatar
con committed
442
        QStringList newVersionData = newVersion.split('=');
443
        if (newVersionData.count() >= 2) {
444
            if (QFile::exists(newVersionData[1])) {
con's avatar
con committed
445
                QtVersion *version = new QtVersion(newVersionData[0], newVersionData[1], m_idcount++ );
hjk's avatar
hjk committed
446
                if (newVersionData.count() >= 3)
447
                    version->setS60SDKDirectory(QDir::fromNativeSeparators(newVersionData[2]));
448
                if (newVersionData.count() >= 4)
449
                    version->setSbsV2Directory(QDir::fromNativeSeparators(newVersionData[3]));
con's avatar
con committed
450
451
452

                bool versionWasAlreadyInList = false;
                foreach(const QtVersion * const it, m_versions) {
453
                    if (QDir(version->qmakeCommand()).canonicalPath() == QDir(it->qmakeCommand()).canonicalPath()) {
con's avatar
con committed
454
455
456
457
458
459
                        versionWasAlreadyInList = true;
                        break;
                    }
                }

                if (!versionWasAlreadyInList) {
460
                    m_versions.insert(version->uniqueId(), version);
con's avatar
con committed
461
462
463
464
465
466
467
                } else {
                    // clean up
                    delete version;
                }
            }
        }
    }
468
    settings->setValue(QLatin1String("General/LastQtVersionUpdate"), QDateTime::currentDateTime());
con's avatar
con committed
469
470
471
472
473
}

void QtVersionManager::updateSystemVersion()
{
    bool haveSystemVersion = false;
474
    QString systemQMakePath = DebuggingHelperLibrary::findSystemQt(Utils::Environment::systemEnvironment());
475
476
    if (systemQMakePath.isNull())
        systemQMakePath = tr("<not found>");
477

con's avatar
con committed
478
    foreach (QtVersion *version, m_versions) {
479
480
        if (version->isAutodetected()
            && version->autodetectionSource() == PATH_AUTODETECTION_SOURCE) {
481
            version->setQMakeCommand(systemQMakePath);
482
            version->setDisplayName(tr("Qt in PATH"));
con's avatar
con committed
483
484
485
486
487
            haveSystemVersion = true;
        }
    }
    if (haveSystemVersion)
        return;
488
    QtVersion *version = new QtVersion(tr("Qt in PATH"),
489
                                       systemQMakePath,
con's avatar
con committed
490
                                       getUniqueId(),
491
492
                                       true,
                                       PATH_AUTODETECTION_SOURCE);
493
    m_versions.insert(version->uniqueId(), version);
con's avatar
con committed
494
495
}

Tobias Hunger's avatar
Tobias Hunger committed
496
QtVersion *QtVersionManager::emptyVersion() const
con's avatar
con committed
497
{
Tobias Hunger's avatar
Tobias Hunger committed
498
    return m_emptyVersion;
con's avatar
con committed
499
500
}

501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
class SortByUniqueId
{
public:
    bool operator()(QtVersion *a, QtVersion *b)
    {
        return a->uniqueId() < b->uniqueId();
    }
};

bool QtVersionManager::equals(QtVersion *a, QtVersion *b)
{
    if (a->m_qmakeCommand != b->m_qmakeCommand)
        return false;
    if (a->m_id != b->m_id)
        return false;
516
517
    if (a->m_displayName != b->displayName())
        return false;
518
519
520
    return true;
}

Tobias Hunger's avatar
Tobias Hunger committed
521
void QtVersionManager::setNewQtVersions(QList<QtVersion *> newVersions)
522
{
523
524
525
526
527
528
529
530
531
532
533
    // We want to preserve the same order as in the settings dialog
    // so we sort a copy
    QList<QtVersion *> sortedNewVersions = newVersions;
    SortByUniqueId sortByUniqueId;
    qSort(sortedNewVersions.begin(), sortedNewVersions.end(), sortByUniqueId);

    QList<int> changedVersions;
    // So we trying to find the minimal set of changed versions,
    // iterate over both sorted list

    // newVersions and oldVersions iterator
534
535
    QList<QtVersion *>::const_iterator nit, nend;
    QMap<int, QtVersion *>::const_iterator oit, oend;
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
    nit = sortedNewVersions.constBegin();
    nend = sortedNewVersions.constEnd();
    oit = m_versions.constBegin();
    oend = m_versions.constEnd();

    while (nit != nend && oit != oend) {
        int nid = (*nit)->uniqueId();
        int oid = (*oit)->uniqueId();
        if (nid < oid) {
            changedVersions.push_back(nid);
            ++nit;
        } else if (oid < nid) {
            changedVersions.push_back(oid);
            ++oit;
        } else {
            if (!equals(*oit, *nit))
                changedVersions.push_back(oid);
            ++oit;
            ++nit;
555
556
        }
    }
557
558
559

    while (nit != nend) {
        changedVersions.push_back((*nit)->uniqueId());
560
        ++nit;
561
562
563
564
    }

    while (oit != oend) {
        changedVersions.push_back((*oit)->uniqueId());
565
        ++oit;
566
567
    }

568
569
    qDeleteAll(m_versions);
    m_versions.clear();
570
571
    foreach (QtVersion *v, sortedNewVersions)
        m_versions.insert(v->uniqueId(), v);
572
573

    if (!changedVersions.isEmpty())
574
575
        updateDocumentation();

576
    updateExamples();
577
    writeVersionsIntoSettings();
578
579
580

    if (!changedVersions.isEmpty())
        emit qtVersionsChanged(changedVersions);
581
582
}

583
584
585
// --------------------------------------------------------------------------
// QtVersion
// --------------------------------------------------------------------------
con's avatar
con committed
586

587
QtVersion::QtVersion(const QString &name, const QString &qmakeCommand, int id,
588
                     bool isAutodetected, const QString &autodetectionSource)
589
    : m_displayName(name),
590
    m_isAutodetected(isAutodetected),
591
    m_autodetectionSource(autodetectionSource),
592
    m_hasDebuggingHelper(false),
593
    m_hasQmlDump(false),
594
    m_hasQmlDebuggingLibrary(false),
595
    m_hasQmlObserver(false),
596
    m_abiUpToDate(false),
597
    m_versionInfoUpToDate(false),
598
599
600
    m_notInstalled(false),
    m_defaultConfigIsDebug(true),
    m_defaultConfigIsDebugAndRelease(true),
601
602
    m_hasExamples(false),
    m_hasDemos(false),
603
    m_hasDocumentation(false)
con's avatar
con committed
604
{
hjk's avatar
hjk committed
605
    if (id == -1)
con's avatar
con committed
606
607
608
        m_id = getUniqueId();
    else
        m_id = id;
609
    setQMakeCommand(qmakeCommand);
con's avatar
con committed
610
611
}

612
QtVersion::QtVersion(const QString &name, const QString &qmakeCommand,
613
                     bool isAutodetected, const QString &autodetectionSource)
614
    : m_displayName(name),
615
616
    m_isAutodetected(isAutodetected),
    m_autodetectionSource(autodetectionSource),
617
    m_hasDebuggingHelper(false),
618
    m_hasQmlDump(false),
619
    m_hasQmlDebuggingLibrary(false),
620
    m_hasQmlObserver(false),
621
    m_abiUpToDate(false),
622
623
624
625
626
627
    m_versionInfoUpToDate(false),
    m_notInstalled(false),
    m_defaultConfigIsDebug(true),
    m_defaultConfigIsDebugAndRelease(true),
    m_hasExamples(false),
    m_hasDemos(false),
628
    m_hasDocumentation(false)
629
630
631
632
633
634
635
636
637
638
{
    m_id = getUniqueId();
    setQMakeCommand(qmakeCommand);
}


QtVersion::QtVersion(const QString &qmakeCommand, bool isAutodetected, const QString &autodetectionSource)
    : m_isAutodetected(isAutodetected),
    m_autodetectionSource(autodetectionSource),
    m_hasDebuggingHelper(false),
639
    m_hasQmlDump(false),
640
    m_hasQmlDebuggingLibrary(false),
641
    m_hasQmlObserver(false),
642
    m_abiUpToDate(false),
643
644
645
646
647
648
    m_versionInfoUpToDate(false),
    m_notInstalled(false),
    m_defaultConfigIsDebug(true),
    m_defaultConfigIsDebugAndRelease(true),
    m_hasExamples(false),
    m_hasDemos(false),
649
    m_hasDocumentation(false)
con's avatar
con committed
650
651
{
    m_id = getUniqueId();
652
    setQMakeCommand(qmakeCommand);
653
    m_displayName = qtVersionString();
con's avatar
con committed
654
655
}

656
QtVersion::QtVersion()
657
    :  m_id(-1),
658
659
    m_isAutodetected(false),
    m_hasDebuggingHelper(false),
660
    m_hasQmlDump(false),
661
    m_hasQmlDebuggingLibrary(false),
662
    m_hasQmlObserver(false),
663
    m_abiUpToDate(false),
664
665
666
667
668
669
    m_versionInfoUpToDate(false),
    m_notInstalled(false),
    m_defaultConfigIsDebug(true),
    m_defaultConfigIsDebugAndRelease(true),
    m_hasExamples(false),
    m_hasDemos(false),
670
    m_hasDocumentation(false)
671
{
672
    setQMakeCommand(QString());
673
674
}

675
676
677
678
QtVersion::~QtVersion()
{
}

679
680
681
682
QString QtVersion::toHtml() const
{
    QString rc;
    QTextStream str(&rc);
Robert Loehning's avatar
Robert Loehning committed
683
    str << "<html><body><table>";
684
    str << "<tr><td><b>" << QtVersionManager::tr("Name:")
685
        << "</b></td><td>" << displayName() << "</td></tr>";
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
    if (!isValid()) {
        str << "<tr><td colspan=2><b>" + QtVersionManager::tr("Invalid Qt version") +"</b></td></tr>";
    } else {
        QString prefix = QLatin1String("<tr><td><b>") + QtVersionManager::tr("ABI:") + QLatin1String("</b></td>");
        foreach (const ProjectExplorer::Abi &abi, qtAbis()) {
            str << prefix << "<td>" << abi.toString() << "</td></tr>";
            prefix = QLatin1String("<tr><td></td>");
        }
        str << "<tr><td><b>" << QtVersionManager::tr("Source:")
            << "</b></td><td>" << sourcePath() << "</td></tr>";
        str << "<tr><td><b>" << QtVersionManager::tr("mkspec:")
            << "</b></td><td>" << mkspec() << "</td></tr>";
        str << "<tr><td><b>" << QtVersionManager::tr("qmake:")
            << "</b></td><td>" << m_qmakeCommand << "</td></tr>";
        updateAbiAndMkspec();
        if (m_defaultConfigIsDebug || m_defaultConfigIsDebugAndRelease) {
            str << "<tr><td><b>" << QtVersionManager::tr("Default:") << "</b></td><td>"
                << (m_defaultConfigIsDebug ? "debug" : "release");
            if (m_defaultConfigIsDebugAndRelease)
                str << " debug_and_release";
            str << "</td></tr>";
        } // default config.
        str << "<tr><td><b>" << QtVersionManager::tr("Version:")
            << "</b></td><td>" << qtVersionString() << "</td></tr>";
        const QHash<QString,QString> vInfo = versionInfo();
        if (!vInfo.isEmpty()) {
            const QHash<QString,QString>::const_iterator vcend = vInfo.constEnd();
            for (QHash<QString,QString>::const_iterator it = vInfo.constBegin(); it != vcend; ++it)
                str << "<tr><td><pre>" << it.key() <<  "</pre></td><td>" << it.value() << "</td></tr>";
        }
716
    }
Robert Loehning's avatar
Robert Loehning committed
717
    str << "</table></body></html>";
718
719
720
    return rc;
}

721
722
723
724
725
726
bool QtVersion::supportsShadowBuilds() const
{
    QSet<QString> targets = supportedTargetIds();
    // Symbian does not support shadow building
    if (targets.contains(Constants::S60_DEVICE_TARGET_ID) ||
        targets.contains(Constants::S60_EMULATOR_TARGET_ID)) {
Tobias Hunger's avatar
Tobias Hunger committed
727
        // We can not support shadow building with the ABLD system
728
729
730
731
732
        return false;
    }
    return true;
}

733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
ProjectExplorer::IOutputParser *QtVersion::createOutputParser() const
{
    if (supportsTargetId(Qt4ProjectManager::Constants::S60_DEVICE_TARGET_ID) ||
        supportsTargetId(Qt4ProjectManager::Constants::S60_EMULATOR_TARGET_ID)) {
        if (isBuildWithSymbianSbsV2()) {
            return new SbsV2Parser;
        } else {
            ProjectExplorer::IOutputParser *parser = new AbldParser;
            parser->appendOutputParser(new ProjectExplorer::GnuMakeParser);
            return parser;
        }
    }
    return new ProjectExplorer::GnuMakeParser;
}

748
QList<ProjectExplorer::Task>
749
QtVersion::reportIssues(const QString &proFile, const QString &buildDir)
750
751
752
{
    QList<ProjectExplorer::Task> results;

753
754
755
    QString tmpBuildDir = QDir(buildDir).absolutePath();
    if (!tmpBuildDir.endsWith(QLatin1Char('/')))
        tmpBuildDir.append(QLatin1Char('/'));
756

Friedemann Kleint's avatar
Friedemann Kleint committed
757
758
759
760
    if (!isValid()) {
        //: %1: Reason for being invalid
        const QString msg = QCoreApplication::translate("Qt4ProjectManager::QtVersion", "The Qt version is invalid: %1").arg(invalidReason());
        results.append(ProjectExplorer::Task(ProjectExplorer::Task::Error, msg, QString(), -1,
761
                                             QLatin1String(ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM)));
Friedemann Kleint's avatar
Friedemann Kleint committed
762
    }
763
764
765

    QFileInfo qmakeInfo(qmakeCommand());
    if (!qmakeInfo.exists() ||
Friedemann Kleint's avatar
Friedemann Kleint committed
766
767
768
769
770
        !qmakeInfo.isExecutable()) {
        //: %1: Path to qmake executable
        const QString msg = QCoreApplication::translate("Qt4ProjectManager::QtVersion",
                                                        "The qmake command \"%1\" was not found or is not executable.").arg(qmakeCommand());
        results.append(ProjectExplorer::Task(ProjectExplorer::Task::Error, msg, QString(), -1,
771
                                             QLatin1String(ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM)));
Friedemann Kleint's avatar
Friedemann Kleint committed
772
    }
773

774
    QString sourcePath = QFileInfo(proFile).absolutePath();
775
776
    if (!sourcePath.endsWith(QLatin1Char('/')))
        sourcePath.append(QLatin1Char('/'));
777
778
779
780
781
782
783
    if ((tmpBuildDir.startsWith(sourcePath)) && (tmpBuildDir != sourcePath)) {
        const QString msg = QCoreApplication::translate("Qt4ProjectManager::QtVersion",
                                                        "Qmake does not support build directories below the source directory.");
        results.append(ProjectExplorer::Task(ProjectExplorer::Task::Warning, msg, QString(), -1,
                                             QLatin1String(ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM)));
    } else if (tmpBuildDir.count(QChar('/')) != sourcePath.count(QChar('/'))) {
        const QString msg = QCoreApplication::translate("Qt4ProjectManager::QtVersion",
784
                                                        "The build directory needs to be at the same level as the source directory.");
785

786
787
788
789
        results.append(ProjectExplorer::Task(ProjectExplorer::Task::Warning, msg, QString(), -1,
                                             QLatin1String(ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM)));
    }

790
791
792
793
794
795
796
    QSet<QString> targets = supportedTargetIds();
    if (targets.contains(Constants::S60_DEVICE_TARGET_ID) ||
        targets.contains(Constants::S60_EMULATOR_TARGET_ID))
        results.append(S60ProjectChecker::reportIssues(proFile, this));
    return results;
}

797
QString QtVersion::displayName() const
con's avatar
con committed
798
{
799
    return m_displayName;
con's avatar
con committed
800
801
}

802
QString QtVersion::qmakeCommand() const
con's avatar
con committed
803
{
804
    return m_qmakeCommand;
con's avatar
con committed
805
806
807
808
809
810
811
812
813
}

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

QString QtVersion::mkspec() const
{
814
    updateAbiAndMkspec();
con's avatar
con committed
815
816
817
    return m_mkspec;
}

dt's avatar
dt committed
818
819
QString QtVersion::mkspecPath() const
{
820
    updateAbiAndMkspec();
dt's avatar
dt committed
821
822
823
    return m_mkspecFullPath;
}

824
825
bool QtVersion::isBuildWithSymbianSbsV2() const
{
826
    updateAbiAndMkspec();
827
828
829
    return m_isBuildUsingSbsV2;
}

830
831
QString QtVersion::qtVersionString() const
{
832
833
834
835
836
837
838
839
    if (m_qtVersionString.isNull()) {
        QFileInfo qmake(m_qmakeCommand);
        if (qmake.exists() && qmake.isExecutable()) {
            m_qtVersionString = DebuggingHelperLibrary::qtVersionForQMake(qmake.absoluteFilePath());
        } else {
            m_qtVersionString = QLatin1String("");
        }
    }
840
841
842
    return m_qtVersionString;
}

843
QtVersionNumber QtVersion::qtVersion() const
844
{
845
846
    //todo cache this;
    return QtVersionNumber(qtVersionString());
847
848
}

con's avatar
con committed
849
850
851
852
853
854
QHash<QString,QString> QtVersion::versionInfo() const
{
    updateVersionInfo();
    return m_versionInfo;
}

855
void QtVersion::setDisplayName(const QString &name)
con's avatar
con committed
856
{
857
    m_displayName = name;
con's avatar
con committed
858
859
}

860
void QtVersion::setQMakeCommand(const QString& qmakeCommand)
con's avatar
con committed
861
{
862
    m_qmakeCommand = QDir::fromNativeSeparators(qmakeCommand);
863
#ifdef Q_OS_WIN
864
    m_qmakeCommand = m_qmakeCommand.toLower();
865
#endif
866
867
    m_designerCommand.clear();
    m_linguistCommand.clear();
868
    m_qmlviewerCommand.clear();
869
    m_uicCommand.clear();
870
    m_abiUpToDate = false;
871
872
    // TODO do i need to optimize this?
    m_versionInfoUpToDate = false;
873
    m_qtVersionString.clear();
874
    updateSourcePath();
con's avatar
con committed
875
876
877
878
}

void QtVersion::updateSourcePath()
{
879
880
881
882
    updateVersionInfo();
    const QString installData = m_versionInfo["QT_INSTALL_DATA"];
    m_sourcePath = installData;
    QFile qmakeCache(installData + QLatin1String("/.qmake.cache"));
con's avatar
con committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
    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;
            }
        }
    }
898
    m_sourcePath = QDir::cleanPath(m_sourcePath);
dt's avatar
dt committed
899
900
901
#ifdef Q_OS_WIN
    m_sourcePath = m_sourcePath.toLower();
#endif
con's avatar
con committed
902
903
904
905
}

// Returns the version that was used to build the project in that directory
// That is returns the directory
Tobias Hunger's avatar
Tobias Hunger committed
906
// To find out whether we already have a qtversion for that directory call
con's avatar
con committed
907
// QtVersion *QtVersionManager::qtVersionForDirectory(const QString directory);
908
QString QtVersionManager::findQMakeBinaryFromMakefile(const QString &makefile)
con's avatar
con committed
909
910
{
    bool debugAdding = false;
911
912
913
    QFile fi(makefile);
    if (fi.exists() && fi.open(QFile::ReadOnly)) {
        QTextStream ts(&fi);
914
        QRegExp r1("QMAKE\\s*=(.*)");
con's avatar
con committed
915
916
917
918
919
920
        while (!ts.atEnd()) {
            QString line = ts.readLine();
            if (r1.exactMatch(line)) {
                if (debugAdding)
                    qDebug()<<"#~~ QMAKE is:"<<r1.cap(1).trimmed();
                QFileInfo qmake(r1.cap(1).trimmed());
921
                QString qmakePath = qmake.filePath();
922
#ifdef Q_OS_WIN
923
924
                if (!qmakePath.endsWith(QLatin1String(".exe")))
                    qmakePath.append(QLatin1String(".exe"));
925
#endif
926
                // Is qmake still installed?
927
928
929
930
931
932
                QFileInfo fi(qmakePath);
                if (fi.exists()) {
                    qmakePath = fi.absoluteFilePath();
#ifdef Q_OS_WIN
                    qmakePath = qmakePath.toLower();
#endif
933
                    return qmakePath;
934
                }
con's avatar
con committed
935
936
937
            }
        }
    }
938
    return QString();
con's avatar
con committed
939
940
}

941
QtVersion *QtVersionManager::qtVersionForQMakeBinary(const QString &qmakePath)
con's avatar
con committed
942
943
{
   foreach(QtVersion *v, versions()) {
Tobias Hunger's avatar
Tobias Hunger committed
944
945
946
947
948
       if (v->qmakeCommand() == qmakePath) {
           return v;
           break;
       }
   }
con's avatar
con committed
949
950
951
   return 0;
}

952
void dumpQMakeAssignments(const QList<QMakeAssignment> &list)
con's avatar
con committed
953
{
954
    foreach(const QMakeAssignment &qa, list) {
955
956
957
958
        qDebug()<<qa.variable<<qa.op<<qa.value;
    }
}

959
bool QtVersionManager::makefileIsFor(const QString &makefile, const QString &proFile)
960
961
962
963
{
    if (proFile.isEmpty())
        return true;

964
    QString line = findQMakeLine(makefile, QLatin1String("# Project:")).trimmed();
965
966
967
968
969
970
    if (line.isEmpty())
        return false;

    line = line.mid(line.indexOf(QChar(':')) + 1);
    line = line.trimmed();

971
    QFileInfo srcFileInfo(QFileInfo(makefile).absoluteDir(), line);
972
973
974
975
    QFileInfo proFileInfo(proFile);
    return srcFileInfo == proFileInfo;
}

976
QPair<QtVersion::QmakeBuildConfigs, QString> QtVersionManager::scanMakeFile(const QString &makefile, QtVersion::QmakeBuildConfigs defaultBuildConfig)
977
{
978
979
    if (debug)
        qDebug()<<"ScanMakeFile, the gory details:";
980
    QtVersion::QmakeBuildConfigs result = defaultBuildConfig;
981
    QString result2;
982

983
    QString line = findQMakeLine(makefile, QLatin1String("# Command:"));
984
    if (!line.isEmpty()) {
985
986
        if (debug)
            qDebug()<<"Found line"<<line;
987
988
989
        line = trimLine(line);
        QList<QMakeAssignment> assignments;
        QList<QMakeAssignment> afterAssignments;
990
        parseArgs(line, &assignments, &afterAssignments, &result2);
991

992
993
994
995
996
997
        if (debug) {
            dumpQMakeAssignments(assignments);
            if (!afterAssignments.isEmpty())
                qDebug()<<"-after";
            dumpQMakeAssignments(afterAssignments);
        }
998
999
1000
1001
1002

        // Search in assignments for CONFIG(+=,-=,=)(debug,release,debug_and_release)
        // Also remove them from the list
        result = qmakeBuildConfigFromCmdArgs(&assignments, defaultBuildConfig);

dt's avatar
dt committed
1003
1004
        if (debug)
            dumpQMakeAssignments(assignments);
1005

1006
        foreach(const QMakeAssignment &qa, assignments)
1007
            Utils::QtcProcess::addArg(&result2, qa.variable + qa.op + qa.value);
1008
        if (!afterAssignments.isEmpty()) {
1009
            Utils::QtcProcess::addArg(&result2, QLatin1String("-after"));
1010
            foreach(const QMakeAssignment &qa, afterAssignments)
1011
                Utils::QtcProcess::addArg(&result2, qa.variable + qa.op + qa.value);
1012
1013
1014
1015
        }
    }

    // Dump the gathered information:
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
    if (debug) {
        qDebug()<<"\n\nDumping information from scanMakeFile";
        qDebug()<<"QMake CONFIG variable parsing";
        qDebug()<<"  "<< (result & QtVersion::NoBuild ? "No Build" : QString::number(int(result)));
        qDebug()<<"  "<< (result & QtVersion::DebugBuild ? "debug" : "release");
        qDebug()<<"  "<< (result & QtVersion::BuildAll ? "debug_and_release" : "no debug_and_release");
        qDebug()<<"\nAddtional Arguments";
        qDebug()<<result2;
        qDebug()<<"\n\n";
    }
1026
1027
1028
    return qMakePair(result, result2);
}

1029
QString QtVersionManager::findQMakeLine(const QString &makefile, const QString &key)
1030
{
dt's avatar