qt4project.cpp 43.8 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
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
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
** 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.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

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

con's avatar
con committed
32
33
#include "qt4projectmanager.h"
#include "profilereader.h"
dt's avatar
dt committed
34
#include "prowriter.h"
con's avatar
con committed
35
36
37
38
39
#include "makestep.h"
#include "qmakestep.h"
#include "deployhelper.h"
#include "qt4runconfiguration.h"
#include "qt4nodes.h"
40
#include "qt4projectconfigwidget.h"
con's avatar
con committed
41
42
43
#include "qt4buildenvironmentwidget.h"
#include "qt4projectmanagerconstants.h"
#include "projectloadwizard.h"
44
#include "qtversionmanager.h"
con's avatar
con committed
45

46
47
#ifdef QTCREATOR_WITH_S60
#include "qt-s60/gccetoolchain.h"
con's avatar
con committed
48
#include "qt-s60/rvcttoolchain.h"
49
50
#endif

51
#include <coreplugin/icore.h>
con's avatar
con committed
52
53
#include <coreplugin/messagemanager.h>
#include <coreplugin/coreconstants.h>
54
#include <extensionsystem/pluginmanager.h>
con's avatar
con committed
55
56
57
#include <projectexplorer/nodesvisitor.h>
#include <projectexplorer/project.h>
#include <projectexplorer/customexecutablerunconfiguration.h>
58
#include <utils/qtcassert.h>
con's avatar
con committed
59
60

#include <QtCore/QDebug>
61
62
#include <QtCore/QDir>
#include <QtGui/QFileDialog>
63
#include <QtGui/QInputDialog>
con's avatar
con committed
64
65
66
67
68
69
70

using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;
using namespace ProjectExplorer;

enum { debug = 0 };

con's avatar
con committed
71
72
73
74
namespace {
    const char * const KEY_QT_VERSION_ID = "QtVersionId";
}

con's avatar
con committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
namespace Qt4ProjectManager {
namespace Internal {

// Qt4ProjectFiles: Struct for (Cached) lists of files in a project
struct Qt4ProjectFiles {
    void clear();
    bool equals(const Qt4ProjectFiles &f) const;

    QStringList files[ProjectExplorer::FileTypeSize];
    QStringList generatedFiles[ProjectExplorer::FileTypeSize];
    QStringList proFiles;
};

void Qt4ProjectFiles::clear()
{
    for (int i = 0; i < FileTypeSize; ++i) {
        files[i].clear();
        generatedFiles[i].clear();
    }
    proFiles.clear();
}

bool Qt4ProjectFiles::equals(const Qt4ProjectFiles &f) const
{
    for (int i = 0; i < FileTypeSize; ++i)
        if (files[i] != f.files[i] || generatedFiles[i] != f.generatedFiles[i])
            return false;
    if (proFiles != f.proFiles)
        return false;
    return true;
}

inline bool operator==(const Qt4ProjectFiles &f1, const Qt4ProjectFiles &f2)
{       return f1.equals(f2); }

inline bool operator!=(const Qt4ProjectFiles &f1, const Qt4ProjectFiles &f2)
{       return !f1.equals(f2); }

QDebug operator<<(QDebug d, const  Qt4ProjectFiles &f)
{
    QDebug nsp = d.nospace();
    nsp << "Qt4ProjectFiles: proFiles=" <<  f.proFiles << '\n';
    for (int i = 0; i < FileTypeSize; ++i)
        nsp << "Type " << i << " files=" << f.files[i] <<  " generated=" << f.generatedFiles[i] << '\n';
    return d;
}

// A visitor to collect all files of a project in a Qt4ProjectFiles struct
class ProjectFilesVisitor : public ProjectExplorer::NodesVisitor
{
    Q_DISABLE_COPY(ProjectFilesVisitor)
    ProjectFilesVisitor(Qt4ProjectFiles *files);
public:

    static void findProjectFiles(Qt4ProFileNode *rootNode, Qt4ProjectFiles *files);

    void visitProjectNode(ProjectNode *projectNode);
    void visitFolderNode(FolderNode *folderNode);

private:
    Qt4ProjectFiles *m_files;
};

ProjectFilesVisitor::ProjectFilesVisitor(Qt4ProjectFiles *files) :
    m_files(files)
{
}

void ProjectFilesVisitor::findProjectFiles(Qt4ProFileNode *rootNode, Qt4ProjectFiles *files)
{
    files->clear();
    ProjectFilesVisitor visitor(files);
    rootNode->accept(&visitor);
    for (int i = 0; i < FileTypeSize; ++i) {
        qSort(files->files[i]);
        qSort(files->generatedFiles[i]);
    }
    qSort(files->proFiles);
}

void ProjectFilesVisitor::visitProjectNode(ProjectNode *projectNode)
{
    const QString path = projectNode->path();
    if (!m_files->proFiles.contains(path))
        m_files->proFiles.append(path);
    visitFolderNode(projectNode);
}

void ProjectFilesVisitor::visitFolderNode(FolderNode *folderNode)
{
    foreach (FileNode *fileNode, folderNode->fileNodes()) {
        const QString path = fileNode->path();
        const int type = fileNode->fileType();
        QStringList &targetList = fileNode->isGenerated() ? m_files->generatedFiles[type] : m_files->files[type];
        if (!targetList.contains(path))
            targetList.push_back(path);
    }
}

}
}

// ----------- Qt4ProjectFile
Qt4ProjectFile::Qt4ProjectFile(Qt4Project *project, const QString &filePath, QObject *parent)
    : Core::IFile(parent),
      m_mimeType(QLatin1String(Qt4ProjectManager::Constants::PROFILE_MIMETYPE)),
      m_project(project),
      m_filePath(filePath)
{
}

bool Qt4ProjectFile::save(const QString &)
{
188
189
    // This is never used
    return false;
con's avatar
con committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
}

QString Qt4ProjectFile::fileName() const
{
    return m_filePath;
}

QString Qt4ProjectFile::defaultPath() const
{
    return QString();
}

QString Qt4ProjectFile::suggestedFileName() const
{
    return QString();
}

QString Qt4ProjectFile::mimeType() const
{
    return m_mimeType;
}

bool Qt4ProjectFile::isModified() const
{
214
    return false; // we save after changing anyway
con's avatar
con committed
215
216
217
218
}

bool Qt4ProjectFile::isReadOnly() const
{
dt's avatar
dt committed
219
220
    QFileInfo fi(m_filePath);
    return !fi.isWritable();
con's avatar
con committed
221
222
223
224
225
226
227
228
229
230
231
}

bool Qt4ProjectFile::isSaveAsAllowed() const
{
    return false;
}

void Qt4ProjectFile::modified(Core::IFile::ReloadBehavior *)
{
}

232
233
234
235
236
237
238
239
240

/*!
  \class Qt4BuildConfigurationFactory
*/

Qt4BuildConfigurationFactory::Qt4BuildConfigurationFactory(Qt4Project *project)
    : IBuildConfigurationFactory(project),
    m_project(project)
{
con's avatar
con committed
241
    update();
242
243
244
245
246
247
}

Qt4BuildConfigurationFactory::~Qt4BuildConfigurationFactory()
{
}

con's avatar
con committed
248
249
250
251
252
253
254
255
256
257
258
259
260
void Qt4BuildConfigurationFactory::update()
{

    m_versions.clear();
    m_versions.insert(QLatin1String("DefaultQt"), VersionInfo(tr("Using Default Qt Version"), 0));
    QtVersionManager *vm = QtVersionManager::instance();
    foreach (const QtVersion *version, vm->versions()) {
        m_versions.insert(QString::fromLatin1("Qt%1").arg(version->uniqueId()),
                          VersionInfo(tr("Using Qt Version \"%1\"").arg(version->name()), version->uniqueId()));
    }
    emit availableCreationTypesChanged();
}

261
262
QStringList Qt4BuildConfigurationFactory::availableCreationTypes() const
{
con's avatar
con committed
263
    return m_versions.keys();
264
265
266
267
}

QString Qt4BuildConfigurationFactory::displayNameForType(const QString &type) const
{
con's avatar
con committed
268
269
270
    if (m_versions.contains(type))
        return m_versions.value(type).displayName;
    return QString();
271
272
}

273
bool Qt4BuildConfigurationFactory::create(const QString &type) const
274
{
275
    QTC_ASSERT(m_versions.contains(type), return false);
con's avatar
con committed
276
    const VersionInfo &info = m_versions.value(type);
277
278
279
280
281
282
283
284
    bool ok;
    QString buildConfigurationName = QInputDialog::getText(0,
                          tr("New configuration"),
                          tr("New Configuration Name:"),
                          QLineEdit::Normal,
                          QString(),
                          &ok);
    if (!ok || buildConfigurationName.isEmpty())
285
        return false;
286
    BuildConfiguration *bc = new BuildConfiguration(buildConfigurationName);
con's avatar
con committed
287
    bc->setValue(KEY_QT_VERSION_ID, info.versionId);
288
289
    m_project->addBuildConfiguration(bc);
    return true;
290
291
}

con's avatar
con committed
292
/*!
293
  \class Qt4Project
con's avatar
con committed
294
295
296
297
298
299

  Qt4Project manages information about an individual Qt 4 (.pro) project file.
  */

Qt4Project::Qt4Project(Qt4Manager *manager, const QString& fileName) :
    m_manager(manager),
dt's avatar
dt committed
300
    m_rootProjectNode(0),
con's avatar
con committed
301
    m_nodesWatcher(new Internal::Qt4NodesWatcher(this)),
302
    m_buildConfigurationFactory(new Qt4BuildConfigurationFactory(this)),
con's avatar
con committed
303
304
    m_fileInfo(new Qt4ProjectFile(this, fileName, this)),
    m_isApplication(true),
305
306
    m_projectFiles(new Qt4ProjectFiles),
    m_toolChain(0)
con's avatar
con committed
307
{
dt's avatar
dt committed
308
    m_manager->registerProject(this);
con's avatar
con committed
309
310
311
312
313
314
315
316

    m_updateCodeModelTimer.setSingleShot(true);
    m_updateCodeModelTimer.setInterval(20);
    connect(&m_updateCodeModelTimer, SIGNAL(timeout()), this, SLOT(updateCodeModel()));
}

Qt4Project::~Qt4Project()
{
dt's avatar
dt committed
317
    m_manager->unregisterProject(this);
con's avatar
con committed
318
    delete m_projectFiles;
319
    delete m_toolChain;
con's avatar
con committed
320
321
322
323
324
}

void Qt4Project::defaultQtVersionChanged()
{
    if (qtVersionId(activeBuildConfiguration()) == 0)
325
        m_rootProjectNode->update();
con's avatar
con committed
326
327
328
329
}

void Qt4Project::qtVersionsChanged()
{
330
    QtVersionManager *vm = QtVersionManager::instance();
331
    foreach (BuildConfiguration *bc, buildConfigurations()) {
332
        if (!vm->version(qtVersionId(bc))->isValid()) {
con's avatar
con committed
333
            setQtVersion(bc, 0);
hjk's avatar
hjk committed
334
            if (bc == activeBuildConfiguration())
335
                m_rootProjectNode->update();
con's avatar
con committed
336
337
        }
    }
con's avatar
con committed
338
    m_buildConfigurationFactory->update();
con's avatar
con committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
}

void Qt4Project::updateFileList()
{
    Qt4ProjectFiles newFiles;
    ProjectFilesVisitor::findProjectFiles(m_rootProjectNode, &newFiles);
    if (newFiles != *m_projectFiles) {
        *m_projectFiles = newFiles;
        emit fileListChanged();
        if (debug)
            qDebug() << Q_FUNC_INFO << *m_projectFiles;
    }
}

dt's avatar
dt committed
353
bool Qt4Project::restoreSettingsImpl(PersistentSettingsReader &settingsReader)
con's avatar
con committed
354
355
356
357
358
{
    Project::restoreSettingsImpl(settingsReader);

    addDefaultBuild();

359
    // Ensure that the qt version and tool chain in each build configuration is valid
con's avatar
con committed
360
    // or if not, is reset to the default
361
    foreach (BuildConfiguration *bc, buildConfigurations()) {
con's avatar
con committed
362
        qtVersionId(bc);
363
364
        toolChainType(bc);
    }
con's avatar
con committed
365

dt's avatar
dt committed
366
367
368
369
370
371
372
    m_rootProjectNode = new Qt4ProFileNode(this, m_fileInfo->fileName(), this);
    m_rootProjectNode->registerWatcher(m_nodesWatcher);
    connect(m_nodesWatcher, SIGNAL(foldersAdded()), this, SLOT(updateFileList()));
    connect(m_nodesWatcher, SIGNAL(foldersRemoved()), this, SLOT(updateFileList()));
    connect(m_nodesWatcher, SIGNAL(filesAdded()), this, SLOT(updateFileList()));
    connect(m_nodesWatcher, SIGNAL(filesRemoved()), this, SLOT(updateFileList()));
    connect(m_nodesWatcher, SIGNAL(proFileUpdated(Qt4ProjectManager::Internal::Qt4ProFileNode *)),
373
            this, SLOT(scheduleUpdateCodeModel(Qt4ProjectManager::Internal::Qt4ProFileNode *)));
dt's avatar
dt committed
374

con's avatar
con committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
    update();

    // restored old runconfigurations
    if (runConfigurations().isEmpty()) {
        // Oha no runConfigurations, add some
        QList<Qt4ProFileNode *> list;
        collectApplicationProFiles(list, m_rootProjectNode);

        if (!list.isEmpty()) {
            foreach (Qt4ProFileNode *node, list) {
                QSharedPointer<RunConfiguration> rc(new Qt4RunConfiguration(this, node->path()));
                addRunConfiguration(rc);
            }
            setActiveRunConfiguration(runConfigurations().first());
        } else {
            QSharedPointer<RunConfiguration> rc(new ProjectExplorer::CustomExecutableRunConfiguration(this));
            addRunConfiguration(rc);
            setActiveRunConfiguration(rc);
            m_isApplication = false;
        }
    }

    // Now connect
398
399
400
401
402
403
404
    QtVersionManager *vm = QtVersionManager::instance();
    connect(vm, SIGNAL(defaultQtVersionChanged()),
            this, SLOT(defaultQtVersionChanged()));
    connect(vm, SIGNAL(qtVersionsChanged()),
            this, SLOT(qtVersionsChanged()));


con's avatar
con committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
    connect(m_nodesWatcher, SIGNAL(foldersAboutToBeAdded(FolderNode *, const QList<FolderNode*> &)),
            this, SLOT(foldersAboutToBeAdded(FolderNode *, const QList<FolderNode*> &)));
    connect(m_nodesWatcher, SIGNAL(foldersAdded()), this, SLOT(checkForNewApplicationProjects()));

    connect(m_nodesWatcher, SIGNAL(foldersRemoved()), this, SLOT(checkForDeletedApplicationProjects()));

    connect(m_nodesWatcher, SIGNAL(projectTypeChanged(Qt4ProjectManager::Internal::Qt4ProFileNode *,
                                                      const Qt4ProjectManager::Internal::Qt4ProjectType,
                                                      const Qt4ProjectManager::Internal::Qt4ProjectType)),
            this, SLOT(projectTypeChanged(Qt4ProjectManager::Internal::Qt4ProFileNode *,
                                          const Qt4ProjectManager::Internal::Qt4ProjectType,
                                          const Qt4ProjectManager::Internal::Qt4ProjectType)));

    connect(m_nodesWatcher, SIGNAL(proFileUpdated(Qt4ProjectManager::Internal::Qt4ProFileNode *)),
            this, SLOT(proFileUpdated(Qt4ProjectManager::Internal::Qt4ProFileNode *)));
dt's avatar
dt committed
420
    return true;
con's avatar
con committed
421
422
423
424
425
426
427
}

void Qt4Project::saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer)
{
    Project::saveSettingsImpl(writer);
}

428
429
430
431
432
ProjectExplorer::IBuildConfigurationFactory *Qt4Project::buildConfigurationFactory() const
{
    return m_buildConfigurationFactory;
}

con's avatar
con committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
namespace {
    class FindQt4ProFiles: protected ProjectExplorer::NodesVisitor {
        QList<Qt4ProFileNode *> m_proFiles;

    public:
        QList<Qt4ProFileNode *> operator()(ProjectNode *root)
        {
            m_proFiles.clear();
            root->accept(this);
            return m_proFiles;
        }

    protected:
        virtual void visitProjectNode(ProjectNode *projectNode)
        {
            if (Qt4ProFileNode *pro = qobject_cast<Qt4ProFileNode *>(projectNode))
                m_proFiles.append(pro);
        }
    };
}

454
void Qt4Project::scheduleUpdateCodeModel(Qt4ProjectManager::Internal::Qt4ProFileNode *pro)
con's avatar
con committed
455
456
{
    m_updateCodeModelTimer.start();
457
    m_proFilesForCodeModelUpdate.append(pro);
con's avatar
con committed
458
459
}

460
ProjectExplorer::ToolChain *Qt4Project::toolChain(BuildConfiguration *configuration) const
461
462
{
    ProjectExplorer::ToolChain *tempToolChain;
463
    tempToolChain = qtVersion(configuration)->createToolChain(toolChainType(configuration));
464
465
466
467
468
469
470
    if (!ProjectExplorer::ToolChain::equals(m_toolChain, tempToolChain)) {
        if (m_toolChain)
            delete m_toolChain;
        m_toolChain = tempToolChain;
    } else {
        delete tempToolChain;
    }
471
472

    return m_toolChain;
473
474
}

475
QString Qt4Project::makeCommand(BuildConfiguration *configuration) const
476
{
477
    ToolChain *tc = toolChain(configuration);
dt's avatar
dt committed
478
    return tc ? tc->makeCommand() : "make";
dt's avatar
dt committed
479
480
}

481
QString Qt4Project::defaultMakeTarget(BuildConfiguration *configuration) const
con's avatar
con committed
482
{
483
#ifdef QTCREATOR_WITH_S60
con's avatar
con committed
484
    ToolChain *tc = toolChain(configuration);
485
486
487
    if (!tc)
        return QString::null;
    QtVersion::QmakeBuildConfig buildConfig
con's avatar
con committed
488
            = QtVersion::QmakeBuildConfig(activeBuildConfiguration()->value("buildConfiguration").toInt());
489
490
491
492
493
494
495
496
497
498
499

    if (tc->type() == ToolChain::GCCE) {
        if (!(buildConfig & QtVersion::DebugBuild)) {
            return "release-gcce";
        }
        return "debug-gcce";
    } else if (tc->type() == ToolChain::RVCT_ARMV5) {
        return (buildConfig & QtVersion::DebugBuild ? "debug-" : "release-") + QLatin1String("armv5");
    } else if (tc->type() == ToolChain::RVCT_ARMV6) {
        return (buildConfig & QtVersion::DebugBuild ? "debug-" : "release-") + QLatin1String("armv6");
    }
dt's avatar
dt committed
500
#else
501
    Q_UNUSED(configuration);
502
503
#endif
    return QString::null;
con's avatar
con committed
504
505
}

con's avatar
con committed
506
507
508
509
510
511
void Qt4Project::updateCodeModel()
{
    if (debug)
        qDebug()<<"Qt4Project::updateCodeModel()";

    CppTools::CppModelManagerInterface *modelmanager =
512
513
        ExtensionSystem::PluginManager::instance()
            ->getObject<CppTools::CppModelManagerInterface>();
con's avatar
con committed
514

515
    if (!modelmanager)
con's avatar
con committed
516
517
        return;

518
519
520
    QStringList predefinedIncludePaths;
    QStringList predefinedFrameworkPaths;
    QByteArray predefinedMacros;
con's avatar
con committed
521

con's avatar
con committed
522
    ToolChain *tc = toolChain(activeBuildConfiguration());
523
524
525
526
    QList<HeaderPath> allHeaderPaths;
    if (tc) {
        predefinedMacros = tc->predefinedMacros();
        allHeaderPaths = tc->systemHeaderPaths();
527
528
529
530
531
532
        //qDebug()<<"Predifined Macros";
        //qDebug()<<tc->predefinedMacros();
        //qDebug()<<"";
        //qDebug()<<"System Header Paths";
        //foreach(const HeaderPath &hp, tc->systemHeaderPaths())
        //    qDebug()<<hp.path();
533
    }
dt's avatar
dt committed
534
535
    foreach (HeaderPath headerPath, allHeaderPaths) {
        if (headerPath.kind() == HeaderPath::FrameworkHeaderPath)
536
            predefinedFrameworkPaths.append(headerPath.path());
dt's avatar
dt committed
537
        else
538
            predefinedIncludePaths.append(headerPath.path());
con's avatar
con committed
539
540
    }

541
542
543
544
545
    const QHash<QString, QString> versionInfo = qtVersion(activeBuildConfiguration())->versionInfo();
    const QString newQtIncludePath = versionInfo.value(QLatin1String("QT_INSTALL_HEADERS"));
    const QString newQtLibsPath = versionInfo.value(QLatin1String("QT_INSTALL_LIBS"));

    predefinedIncludePaths.append(newQtIncludePath);
con's avatar
con committed
546
547
    QDir dir(newQtIncludePath);
    foreach (QFileInfo info, dir.entryInfoList(QDir::Dirs)) {
548
549
550
551
552
553
        const QString path = info.fileName();

        if (path == QLatin1String("Qt"))
            continue; // skip $QT_INSTALL_HEADERS/Qt. There's no need to include it.
        else if (path.startsWith(QLatin1String("Qt")) || path == QLatin1String("phonon"))
            predefinedIncludePaths.append(info.absoluteFilePath());
con's avatar
con committed
554
555
    }

Roberto Raggi's avatar
Compile    
Roberto Raggi committed
556
557
558
559
560
561
    FindQt4ProFiles findQt4ProFiles;
    QList<Qt4ProFileNode *> proFiles = findQt4ProFiles(rootProjectNode());
    QByteArray definedMacros = predefinedMacros;
    QStringList allIncludePaths = predefinedIncludePaths;
    QStringList allFrameworkPaths = predefinedFrameworkPaths;

con's avatar
con committed
562
563
564
565
566
567
568
569
570
571
572
573
#ifdef Q_OS_MAC
    allFrameworkPaths.append(newQtLibsPath);
    // put QtXXX.framework/Headers directories in include path since that qmake's behavior
    QDir frameworkDir(newQtLibsPath);
    foreach (QFileInfo info, frameworkDir.entryInfoList(QDir::Dirs)) {
        if (! info.fileName().startsWith(QLatin1String("Qt")))
            continue;
        allIncludePaths.append(info.absoluteFilePath()+"/Headers");
    }
#endif

    foreach (Qt4ProFileNode *pro, proFiles) {
574
575
576
577
578
579
        Internal::CodeModelInfo info;
        info.defines = predefinedMacros;
        info.includes = predefinedIncludePaths;
        info.frameworkPaths = predefinedFrameworkPaths;

        // Add custom defines
con's avatar
con committed
580
581
        foreach (const QString def, pro->variableValue(DefinesVar)) {
            definedMacros += "#define ";
582
            info.defines += "#define ";
con's avatar
con committed
583
584
585
586
            const int index = def.indexOf(QLatin1Char('='));
            if (index == -1) {
                definedMacros += def.toLatin1();
                definedMacros += " 1\n";
587
588
                info.defines += def.toLatin1();
                info.defines += " 1\n";
con's avatar
con committed
589
590
591
592
593
594
595
            } else {
                const QString name = def.left(index);
                const QString value = def.mid(index + 1);
                definedMacros += name.toLatin1();
                definedMacros += ' ';
                definedMacros += value.toLocal8Bit();
                definedMacros += '\n';
596
597
598
599
                info.defines += name.toLatin1();
                info.defines += ' ';
                info.defines += value.toLocal8Bit();
                info.defines += '\n';
con's avatar
con committed
600
601
602
603
            }
        }

        const QStringList proIncludePaths = pro->variableValue(IncludePathVar);
604
        foreach (const QString &includePath, proIncludePaths) {
605
606
607
608
609
610
            if (!allIncludePaths.contains(includePath))
                allIncludePaths.append(includePath);
            if (!info.includes.contains(includePath))
                info.includes.append(includePath);
        }

611
612
613
614
615
616
617
618
619
620
621
622
623
624
        { // Pkg Config support
            QStringList pkgConfig = pro->variableValue(PkgConfigVar);
            if (!pkgConfig.isEmpty()) {
                pkgConfig.prepend("--cflags-only-I");
                QProcess process;
                process.start("pkg-config", pkgConfig);
                process.waitForFinished();
                QString result = process.readAllStandardOutput();
                foreach(const QString &part, result.trimmed().split(' ', QString::SkipEmptyParts)) {
                    info.includes.append(part.mid(2)); // Chop off "-I"
                }
            }
        }

625
626
627
628
        // Add mkspec directory
        info.includes.append(qtVersion(activeBuildConfiguration())->mkspecPath());

        info.frameworkPaths = allFrameworkPaths;
con's avatar
con committed
629

630
631
632
633
634
635
        foreach (FileNode *fileNode, pro->fileNodes()) {
            const QString path = fileNode->path();
            const int type = fileNode->fileType();
            if (type == HeaderType || type == SourceType) {
                m_codeModelInfo.insert(path, info);
            }
con's avatar
con committed
636
637
638
        }
    }

dt's avatar
dt committed
639
640
641
    // Add mkspec directory
    allIncludePaths.append(qtVersion(activeBuildConfiguration())->mkspecPath());

642
643
644
645
646
647
648
649
650
651
    // Dump things out
    // This is debugging output...
//    qDebug()<<"CodeModel stuff:";
//    QMap<QString, CodeModelInfo>::const_iterator it, end;
//    end = m_codeModelInfo.constEnd();
//    for(it = m_codeModelInfo.constBegin(); it != end; ++it) {
//        qDebug()<<"File: "<<it.key()<<"\nIncludes:"<<it.value().includes<<"\nDefines"<<it.value().defines<<"\n";
//    }
//    qDebug()<<"----------------------------";

con's avatar
con committed
652
653
654
655
656
657
    QStringList files;
    files += m_projectFiles->files[HeaderType];
    files += m_projectFiles->generatedFiles[HeaderType];
    files += m_projectFiles->files[SourceType];
    files += m_projectFiles->generatedFiles[SourceType];

658
    CppTools::CppModelManagerInterface::ProjectInfo pinfo = modelmanager->projectInfo(this);
con's avatar
con committed
659

660
661
662
663
664
    if (pinfo.defines == predefinedMacros             &&
            pinfo.includePaths == allIncludePaths     &&
            pinfo.frameworkPaths == allFrameworkPaths &&
            pinfo.sourceFiles == files) {
        modelmanager->updateProjectInfo(pinfo);
con's avatar
con committed
665
    } else {
666
667
668
669
670
671
672
        if (pinfo.defines != predefinedMacros         ||
            pinfo.includePaths != allIncludePaths     ||
            pinfo.frameworkPaths != allFrameworkPaths) {
            pinfo.sourceFiles.append(QLatin1String("<configuration>"));
        }


673
        pinfo.defines = predefinedMacros;
674
        // pinfo.defines += definedMacros;   // ### FIXME: me
675
676
677
678
679
680
        pinfo.includePaths = allIncludePaths;
        pinfo.frameworkPaths = allFrameworkPaths;
        pinfo.sourceFiles = files;

        modelmanager->updateProjectInfo(pinfo);
        modelmanager->updateSourceFiles(pinfo.sourceFiles);
con's avatar
con committed
681
    }
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697

    // TODO use this information
    // These are the pro files that were actually changed
    // if the list is empty we are at the initial stage
    // TODO check that this also works if pro files get added
    // and removed
    m_proFilesForCodeModelUpdate.clear();
}

QByteArray Qt4Project::predefinedMacros(const QString &fileName) const
{
    QMap<QString, CodeModelInfo>::const_iterator it = m_codeModelInfo.constFind(fileName);
    if (it == m_codeModelInfo.constEnd())
        return QByteArray();
    else
        return (*it).defines;
con's avatar
con committed
698
699
}

700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
QStringList Qt4Project::includePaths(const QString &fileName) const
{
    QMap<QString, CodeModelInfo>::const_iterator it = m_codeModelInfo.constFind(fileName);
    if (it == m_codeModelInfo.constEnd())
        return QStringList();
    else
        return (*it).includes;
}

QStringList Qt4Project::frameworkPaths(const QString &fileName) const
{
    QMap<QString, CodeModelInfo>::const_iterator it = m_codeModelInfo.constFind(fileName);
    if (it == m_codeModelInfo.constEnd())
        return QStringList();
    else
        return (*it).frameworkPaths;
}
con's avatar
con committed
717

718
719
720
///*!
//  Updates complete project
//  */
con's avatar
con committed
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
void Qt4Project::update()
{
    // TODO Maybe remove this method completely?
    m_rootProjectNode->update();
    //updateCodeModel();
}

/*!
  Returns whether the project is an application, or has an application as a subproject.
 */
bool Qt4Project::isApplication() const
{
    return m_isApplication;
}

ProjectExplorer::ProjectExplorerPlugin *Qt4Project::projectExplorer() const
{
    return m_manager->projectExplorer();
}

ProjectExplorer::IProjectManager *Qt4Project::projectManager() const
{
    return m_manager;
}

Qt4Manager *Qt4Project::qt4ProjectManager() const
{
    return m_manager;
}

QString Qt4Project::name() const
{
    return QFileInfo(file()->fileName()).completeBaseName();
}

Core::IFile *Qt4Project::file() const
{
    return m_fileInfo;
}

QStringList Qt4Project::files(FilesMode fileMode) const
{
    QStringList files;
    for (int i = 0; i < FileTypeSize; ++i) {
        files += m_projectFiles->files[i];
        if (fileMode == AllFiles)
            files += m_projectFiles->generatedFiles[i];
    }
    return files;
}

QList<ProjectExplorer::Project*> Qt4Project::dependsOn()
{
    // NBS implement dependsOn
    return QList<Project *>();
}

void Qt4Project::addDefaultBuild()
{
    if (buildConfigurations().isEmpty()) {
        // We don't have any buildconfigurations, so this is a new project
        // The Project Load Wizard is a work of art
        // It will ask the user what kind of build setup he want
        // It will add missing Qt Versions
        // And get the project into a buildable state

        //TODO have a better check wheter there is already a configuration?
        QMakeStep *qmakeStep = 0;
        MakeStep *makeStep = 0;

        qmakeStep = new QMakeStep(this);
        qmakeStep->setValue("mkspec", "");
        insertBuildStep(1, qmakeStep);

        makeStep = new MakeStep(this);
        insertBuildStep(2, makeStep);

        MakeStep* cleanStep = new MakeStep(this);
        cleanStep->setValue("clean", true);
800
        insertCleanStep(1, cleanStep);
con's avatar
con committed
801
802
803
804

        ProjectLoadWizard wizard(this);
        wizard.execDialog();
    } else {
805
        // Migrate settings
806
        QMakeStep *qs = qmakeStep();
807
808
        foreach (BuildConfiguration *bc, buildConfigurations()) {
            QVariant v = qs ? qs->value(bc->name(), "buildConfiguration") : QVariant();
809
            if (v.isValid()) {
810
811
812
813
814
                qs->setValue(bc->name(), "buildConfiguration", QVariant());
                bc->setValue("buildConfiguration", v);
            } else if (!bc->value("buildConfiguration").isValid()) {
                if (QtVersion *version = qtVersion(bc))
                    bc->setValue("buildConfiguration", version->defaultBuildConfig());
815
                else
816
                    bc->setValue("buildConfiguration", int(QtVersion::BuildAll & QtVersion::DebugBuild));
817
818
819
            }
        }

820

con's avatar
con committed
821
        // Restoring configuration
822
823
        foreach(BuildConfiguration *bc, buildConfigurations()) {
            bc->setValue("addQDumper", QVariant());
824
        }
con's avatar
con committed
825
826
827
828
829
    }
}

void Qt4Project::proFileParseError(const QString &errorMessage)
{
830
    Core::ICore::instance()->messageManager()->printToOutputPane(errorMessage);
con's avatar
con committed
831
832
833
834
835
836
837
}

Qt4ProFileNode *Qt4Project::rootProjectNode() const
{
    return m_rootProjectNode;
}

838
QString Qt4Project::buildDirectory(BuildConfiguration *configuration) const
839
840
{
    QString workingDirectory;
841
842
    if (configuration->value("useShadowBuild").toBool())
        workingDirectory = configuration->value("buildDirectory").toString();
843
844
845
846
847
    if (workingDirectory.isEmpty())
        workingDirectory = QFileInfo(file()->fileName()).absolutePath();
    return workingDirectory;
}

848
ProjectExplorer::Environment Qt4Project::baseEnvironment(BuildConfiguration *configuration) const
con's avatar
con committed
849
{
850
851
852
    Environment env = useSystemEnvironment(configuration) ? Environment::systemEnvironment() : Environment();
    qtVersion(configuration)->addToEnvironment(env);
    ToolChain *tc = toolChain(configuration);
dt's avatar
dt committed
853
854
    if (tc)
        tc->addToEnvironment(env);
con's avatar
con committed
855
856
857
    return env;
}

858
ProjectExplorer::Environment Qt4Project::environment(BuildConfiguration *configuration) const
con's avatar
con committed
859
{
860
861
    Environment env = baseEnvironment(configuration);
    env.modify(userEnvironmentChanges(configuration));
con's avatar
con committed
862
863
864
    return env;
}

865
void Qt4Project::setUseSystemEnvironment(BuildConfiguration *configuration, bool b)
con's avatar
con committed
866
{
867
    if (useSystemEnvironment(configuration) == b)
868
        return;
869
870
    configuration->setValue("clearSystemEnvironment", !b);
    emit environmentChanged(configuration->name());
con's avatar
con committed
871
872
}

873
bool Qt4Project::useSystemEnvironment(BuildConfiguration *configuration) const
con's avatar
con committed
874
{
875
876
    bool b = !(configuration->value("clearSystemEnvironment").isValid()
               && configuration->value("clearSystemEnvironment").toBool());
con's avatar
con committed
877
878
879
    return b;
}

880
QList<ProjectExplorer::EnvironmentItem> Qt4Project::userEnvironmentChanges(BuildConfiguration *configuration) const
881
{
882
    return EnvironmentItem::fromStringList(configuration->value("userEnvironmentChanges").toStringList());
883
884
}

885
void Qt4Project::setUserEnvironmentChanges(BuildConfiguration *configuration, const QList<ProjectExplorer::EnvironmentItem> &diff)
886
{
887
    QStringList list = EnvironmentItem::toStringList(diff);
888
    if (list == configuration->value("userEnvironmentChanges").toStringList())
889
        return;
890
891
    configuration->setValue("userEnvironmentChanges", list);
    emit environmentChanged(configuration->name());
892
893
}

894
QString Qt4Project::qtDir(BuildConfiguration *configuration) const
con's avatar
con committed
895
{
896
    QtVersion *version = qtVersion(configuration);
con's avatar
con committed
897
    if (version)
898
        return version->versionInfo().value("QT_INSTALL_DATA");
con's avatar
con committed
899
900
901
    return QString::null;
}

902
QtVersion *Qt4Project::qtVersion(BuildConfiguration *configuration) const
con's avatar
con committed
903
{
904
    return QtVersionManager::instance()->version(qtVersionId(configuration));
con's avatar
con committed
905
906
}

907
int Qt4Project::qtVersionId(BuildConfiguration *configuration) const
con's avatar
con committed
908
{
909
    QtVersionManager *vm = QtVersionManager::instance();
con's avatar
con committed
910
    if (debug)
911
        qDebug()<<"Looking for qtVersion ID of "<<configuration->name();
con's avatar
con committed
912
    int id = 0;
con's avatar
con committed
913
    QVariant vid = configuration->value(KEY_QT_VERSION_ID);
hjk's avatar
hjk committed
914
    if (vid.isValid()) {
con's avatar
con committed
915
        id = vid.toInt();
916
        if (vm->version(id)->isValid()) {
con's avatar
con committed
917
918
            return id;
        } else {
con's avatar
con committed
919
            configuration->setValue(KEY_QT_VERSION_ID, 0);
con's avatar
con committed
920
921
922
923
            return 0;
        }
    } else {
        // Backward compatibilty, we might have just the name:
924
        QString vname = configuration->value("QtVersion").toString();
con's avatar
con committed
925
926
        if (debug)
            qDebug()<<"  Backward compatibility reading QtVersion"<<vname;
hjk's avatar
hjk committed
927
        if (!vname.isEmpty()) {
928
            const QList<QtVersion *> &versions = vm->versions();
con's avatar
con committed
929
            foreach (const QtVersion * const version, versions) {
hjk's avatar
hjk committed
930
                if (version->name() == vname) {
con's avatar
con committed
931
932
                    if (debug)
                        qDebug()<<"found name in versions";
con's avatar
con committed
933
                    configuration->setValue(KEY_QT_VERSION_ID, version->uniqueId());
con's avatar
con committed
934
935
936
937
938
939
940
941
                    return version->uniqueId();
                }
            }
        }
    }
    if (debug)
        qDebug()<<"  using qtversion with id ="<<id;
    // Nothing found, reset to default
con's avatar
con committed
942
    configuration->setValue(KEY_QT_VERSION_ID, id);
con's avatar
con committed
943
944
945
    return id;
}

946
void Qt4Project::setQtVersion(BuildConfiguration *configuration, int id)
con's avatar
con committed
947
{
con's avatar
con committed
948
    configuration->setValue(KEY_QT_VERSION_ID, id);
949
    updateActiveRunConfiguration();
con's avatar
con committed
950
951
}

952
void Qt4Project::setToolChainType(BuildConfiguration *configuration, ProjectExplorer::ToolChain::ToolChainType type)
953
{
954
    configuration->setValue("ToolChain", (int)type);
955
956
957
958
959
    updateActiveRunConfiguration();
}

void Qt4Project::updateActiveRunConfiguration()
{
960
961
962
    const QSharedPointer<RunConfiguration> activeRunConfig = activeRunConfiguration();
    if (!activeRunConfig.isNull() && !activeRunConfig->isEnabled()) {
        foreach (const QSharedPointer<RunConfiguration> &runConfiguration, runConfigurations()) {
963
964
            if (runConfiguration->isEnabled()) {
                setActiveRunConfiguration(runConfiguration);
965
                break;
966
967
968
969
970
            }
        }
    }
    emit runConfigurationsEnabledStateChanged();
    emit invalidateCachedTargetInformation();
971
972
}

973
ProjectExplorer::ToolChain::ToolChainType Qt4Project::toolChainType(BuildConfiguration *configuration) const
974
{
975
    const ProjectExplorer::ToolChain::ToolChainType originalType =
976
        (ProjectExplorer::ToolChain::ToolChainType)configuration->value("ToolChain").toInt();
977
    ProjectExplorer::ToolChain::ToolChainType type = originalType;
978
    const QtVersion *version = qtVersion(configuration);
979
980
    if (!version->possibleToolChainTypes().contains(type)) // use default tool chain
        type = version->defaultToolchainType();
981
    if (type != originalType)
982
        const_cast<Qt4Project *>(this)->setToolChainType(configuration, type);
983
984
985
    return type;
}

dt's avatar
dt committed
986
BuildConfigWidget *Qt4Project::createConfigWidget()
con's avatar
con committed
987
{
988
    return new Qt4ProjectConfigWidget(this);
con's avatar
con committed
989
990
}

dt's avatar
dt committed
991
QList<BuildConfigWidget*> Qt4Project::subConfigWidgets()
con's avatar
con committed
992
{
dt's avatar
dt committed
993
    QList<BuildConfigWidget*> subWidgets;
con's avatar
con committed
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
    subWidgets << new Qt4BuildEnvironmentWidget(this);
    return subWidgets;
}

void Qt4Project::collectApplicationProFiles(QList<Qt4ProFileNode *> &list, Qt4ProFileNode *node)
{
    if (node->projectType() == Internal::ApplicationTemplate
        || node->projectType() == Internal::ScriptTemplate) {
        list.append(node);
    }
    foreach (ProjectNode *n, node->subProjectNodes()) {
        Qt4ProFileNode *qt4ProFileNode = qobject_cast<Qt4ProFileNode *>(n);
        if (qt4ProFileNode)
            collectApplicationProFiles(list, qt4ProFileNode);
    }
}

void Qt4Project::foldersAboutToBeAdded(FolderNode *, const QList<FolderNode*> &nodes)
{
    QList<Qt4ProFileNode *> list;
    foreach (FolderNode *node, nodes) {
        Qt4ProFileNode *qt4ProFileNode = qobject_cast<Qt4ProFileNode *>(node);
        if (qt4ProFileNode)
            collectApplicationProFiles(list, qt4ProFileNode);
    }
    m_applicationProFileChange = list;
}

void Qt4Project::checkForNewApplicationProjects()
{
    // Check all new project nodes
    // against all runConfigurations

    foreach (Qt4ProFileNode *qt4proFile, m_applicationProFileChange) {
        bool found = false;
        foreach (QSharedPointer<RunConfiguration> rc, runConfigurations()) {
dt's avatar
dt committed
1030
            QSharedPointer<Qt4RunConfiguration> qtrc = rc.objectCast<Qt4RunConfiguration>();
con's avatar
con committed
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
            if (qtrc && qtrc->proFilePath() == qt4proFile->path()) {
                found = true;
                break;
            }
        }
        if (!found) {
            QSharedPointer<Qt4RunConfiguration> newRc(new Qt4RunConfiguration(this, qt4proFile->path()));
            addRunConfiguration(newRc);
            m_isApplication = true;
        }
    }
}

void Qt4Project::checkForDeletedApplicationProjects()
{
    QStringList paths;
    foreach (Qt4ProFileNode * node, applicationProFiles())
        paths.append(node->path());

1050
//    qDebug()<<"Still existing paths :"<<paths;
con's avatar
con committed
1051
1052
1053

    QList<QSharedPointer<Qt4RunConfiguration> > removeList;
    foreach (QSharedPointer<RunConfiguration> rc, runConfigurations()) {
dt's avatar
dt committed
1054
        if (QSharedPointer<Qt4RunConfiguration> qt4rc = rc.objectCast<Qt4RunConfiguration>()) {
con's avatar
con committed
1055
1056
            if (!paths.contains(qt4rc->proFilePath())) {
                removeList.append(qt4rc);
1057
//                qDebug()<<"Removing runConfiguration for "<<qt4rc->proFilePath();
con's avatar
con committed
1058
1059
1060
1061
1062
1063
            }
        }
    }

    bool resetActiveRunConfiguration = false;
    QSharedPointer<RunConfiguration> rc(new ProjectExplorer::CustomExecutableRunConfiguration(this));
hjk's avatar
hjk committed
1064
    foreach (QSharedPointer<Qt4RunConfiguration> qt4rc, removeList) {
con's avatar
con committed
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
        removeRunConfiguration(qt4rc);
        if (activeRunConfiguration() == qt4rc)
            resetActiveRunConfiguration = true;
    }

    if (runConfigurations().isEmpty()) {
        QSharedPointer<RunConfiguration> rc(new ProjectExplorer::CustomExecutableRunConfiguration(this));
        addRunConfiguration(rc);
        setActiveRunConfiguration(rc);
        m_isApplication = false;
    } else if (resetActiveRunConfiguration) {
        setActiveRunConfiguration(runConfigurations().first());
    }
}

QList<Qt4ProFileNode *> Qt4Project::applicationProFiles() const
{
    QList<Qt4ProFileNode *> list;
    collectApplicationProFiles(list, rootProjectNode());
    return list;
}

void Qt4Project::projectTypeChanged(Qt4ProFileNode *node, const Qt4ProjectType oldType, const Qt4ProjectType newType)
{
    if (oldType == Internal::ApplicationTemplate
        || oldType == Internal::ScriptTemplate) {
        // check wheter we need to delete a Run Configuration
        checkForDeletedApplicationProjects();
    }

    if (newType == Internal::ApplicationTemplate
        || newType == Internal::ScriptTemplate) {
        // add a new Run Configuration
        m_applicationProFileChange.clear();
        m_applicationProFileChange.append(node);
        checkForNewApplicationProjects();
    }
}

void Qt4Project::proFileUpdated(Qt4ProjectManager::Internal::Qt4ProFileNode *node)
{
    foreach (QSharedPointer<RunConfiguration> rc, runConfigurations()) {
dt's avatar
dt committed
1107
        if (QSharedPointer<Qt4RunConfiguration> qt4rc = rc.objectCast<Qt4RunConfiguration>()) {
con's avatar
con committed
1108
            if (qt4rc->proFilePath() == node->path()) {
1109
                qt4rc->invalidateCachedTargetInformation();
con's avatar
con committed
1110
1111
1112
1113
1114
            }
        }
    }
}

1115
1116
1117
1118
1119
1120
1121
1122
1123
QMakeStep *Qt4Project::qmakeStep() const
{
    QMakeStep *qs = 0;
    foreach(BuildStep *bs, buildSteps())
        if ((qs = qobject_cast<QMakeStep *>(bs)) != 0)
            return qs;
    return 0;
}

con's avatar
con committed
1124
1125
1126
1127
1128
1129
1130
1131
MakeStep *Qt4Project::makeStep() const
{
    MakeStep *qs = 0;
    foreach(BuildStep *bs, buildSteps())
        if ((qs = qobject_cast<MakeStep *>(bs)) != 0)
            return qs;
    return 0;
}
dt's avatar
dt committed
1132

1133
bool Qt4Project::hasSubNode(Qt4PriFileNode *root, const QString &path)
dt's avatar
dt committed
1134
{
1135
1136
1137
1138
1139
    if (root->path() == path)
        return true;
    foreach (FolderNode *fn, root->subFolderNodes()) {
        if (qobject_cast<Qt4ProFileNode *>(fn)) {
            // we aren't interested in pro file nodes
hjk's avatar
hjk committed
1140
        } else if (Qt4PriFileNode *qt4prifilenode = qobject_cast<Qt4PriFileNode *>(fn)) {
1141
1142
1143
1144
1145
            if (hasSubNode(qt4prifilenode, path))
                return true;
        }
    }
    return false;
dt's avatar
dt committed
1146
1147
1148
1149
}

void Qt4Project::findProFile(const QString& fileName, Qt4ProFileNode *root, QList<Qt4ProFileNode *> &list)
{
1150
    if (hasSubNode(root, fileName))
dt's avatar
dt committed
1151
1152
1153
1154
1155
1156
1157
1158
1159
        list.append(root);

    foreach (FolderNode *fn, root->subFolderNodes())
        if (Qt4ProFileNode *qt4proFileNode =  qobject_cast<Qt4ProFileNode *>(fn))
            findProFile(fileName, qt4proFileNode, list);
}

void Qt4Project::notifyChanged(const QString &name)
{
1160
1161
1162
1163
1164
1165
    if (files(Qt4Project::ExcludeGeneratedFiles).contains(name)) {
        QList<Qt4ProFileNode *> list;
        findProFile(name, rootProjectNode(), list);
        foreach(Qt4ProFileNode *node, list)
            node->update();
    }
dt's avatar
dt committed
1166
}
1167
1168
1169

void Qt4Project::invalidateCachedTargetInformation()
{
1170
    emit targetInformationChanged();
1171
1172
}

1173
1174
1175
1176
1177
1178
1179
1180
// We match -spec and -platfrom separetly
// We ignore -cache, because qmake contained a bug that it didn't
// mention the -cache in the Makefile
// That means changing the -cache option in the additional arguments
// does not automatically rerun qmake. Alas, we could try more
// intelligent matching for -cache, but i guess people rarely
// do use that.

dt's avatar
dt committed
1181
QStringList Qt4Project::removeSpecFromArgumentList(const QStringList &old)
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
{
    if (!old.contains("-spec") && !old.contains("-platform") && !old.contains("-cache"))
        return old;
    QStringList newList;
    bool ignoreNext = false;
    foreach(const QString &item, old) {
        if (ignoreNext) {
            ignoreNext = false;
        } else if (item == "-spec" || item == "-platform" || item == "-cache") {
            ignoreNext = true;
        } else {
            newList << item;
        }
    }
    return newList;
}

dt's avatar
dt committed
1199
QString Qt4Project::extractSpecFromArgumentList(const QStringList &list)
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
{
    int index = list.indexOf("-spec");
    if (index == -1)
        index = list.indexOf("-platform");
    if (index == -1)
        return QString();
    if (index + 1 < list.length())
        return list.at(index +1);
    else
        return QString();
}

1212
// returns true if both are equal
1213
bool Qt4Project::compareBuildConfigurationToImportFrom(BuildConfiguration *configuration, const QString &workingDirectory)
1214
{
1215
1216
    QMakeStep *qs = qmakeStep();
    if (QDir(workingDirectory).exists(QLatin1String("Makefile")) && qs) {
1217
        QString qmakePath = QtVersionManager::findQMakeBinaryFromMakefile(workingDirectory);
1218
        QtVersion *version = qtVersion(configuration);
1219
        if (version->qmakeCommand() == qmakePath) {
1220
1221
1222
            // same qtversion
            QPair<QtVersion::QmakeBuildConfig, QStringList> result =
                    QtVersionManager::scanMakeFile(workingDirectory, version->defaultBuildConfig());
1223
            if (QtVersion::QmakeBuildConfig(configuration->value("buildConfiguration").toInt()) == result.first) {
1224
1225
1226
1227
                // The QMake Build Configuration are the same,
                // now compare arguments lists
                // we have to compare without the spec/platform cmd argument
                // and compare that on its own
1228
                QString actualSpec = extractSpecFromArgumentList(qs->value(configuration->name(), "qmakeArgs").toStringList());
1229
                if (actualSpec.isEmpty())
dt's avatar
dt committed
1230
                    actualSpec = version->mkspec();
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248

                // Now to convert the actualSpec to a absolute path, we go through a few hops
                if (QFileInfo(actualSpec).isRelative()) {
                    QString path = version->sourcePath() + "/mkspecs/" + actualSpec;
                    if (QFileInfo(path).exists()) {
                        actualSpec = QDir::cleanPath(path);
                    } else {
                        path = version->versionInfo().value("QMAKE_MKSPECS") + "/" + actualSpec;
                        if (QFileInfo(path).exists()) {
                            actualSpec = QDir::cleanPath(path);
                        } else {
                            path = workingDirectory + "/" + actualSpec;
                            if (QFileInfo(path).exists())
                                actualSpec = QDir::cleanPath(path);
                        }
                    }
                }

1249
1250
1251
1252
1253

                QString parsedSpec = extractSpecFromArgumentList(result.second);
                // if the MakeFile did not contain a mkspec, then it is the default for that qmake
                if (parsedSpec.isEmpty())
                    parsedSpec = version->sourcePath() + "/mkspecs/" + version->mkspec();
1254
1255
1256
                if (QFileInfo(parsedSpec).isRelative())
                    parsedSpec = QDir::cleanPath(workingDirectory + "/" + parsedSpec);

1257
                QStringList actualArgs = removeSpecFromArgumentList(qs->value(configuration->name(), "qmakeArgs").toStringList());
1258
1259
                QStringList parsedArgs = removeSpecFromArgumentList(result.second);

1260
1261
1262
1263
1264
#ifdef Q_OS_WIN
                actualSpec = actualSpec.toLower();
                parsedSpec = parsedSpec.toLower();
#endif

1265
1266
1267
1268
1269
1270
                if (debug) {
                    qDebug()<<"Actual args:"<<actualArgs;
                    qDebug()<<"Parsed args:"<<parsedArgs;
                    qDebug()<<"Actual spec:"<<actualSpec;
                    qDebug()<<"Parsed spec:"<<parsedSpec;
                }
1271
1272

                if (actualArgs == parsedArgs && actualSpec == parsedSpec)
1273
                    return true;
1274
1275
1276
            }
        }
    }
1277
    return false;
1278
1279
}

1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308

/*!
  Handle special case were a subproject of the qt directory is opened, and
  qt was configured to be built as a shadow build -> also build in the sub-
  project in the correct shadow build directory.
  */

// TODO this function should be called on project first load
// and it should check against all configured qt versions ?
//void Qt4Project::detectQtShadowBuild(const QString &buildConfiguration) const
//{
//    if (project()->activeBuildConfiguration() == buildConfiguration)
//        return;
//
//    const QString currentQtDir = static_cast<Qt4Project *>(project())->qtDir(buildConfiguration);
//    const QString qtSourceDir = static_cast<Qt4Project *>(project())->qtVersion(buildConfiguration)->sourcePath();
//
//    // if the project is a sub-project of Qt and Qt was shadow-built then automatically
//    // adjust the build directory of the sub-project.
//    if (project()->file()->fileName().startsWith(qtSourceDir) && qtSourceDir != currentQtDir) {
//        project()->setValue(buildConfiguration, "useShadowBuild", true);
//        QString buildDir = QFileInfo(project()->file()->fileName()).absolutePath();
//        buildDir.replace(qtSourceDir, currentQtDir);
//        project()->setValue(buildConfiguration, "buildDirectory", buildDir);
//        project()->setValue(buildConfiguration, "autoShadowBuild", true);
//    }
//}