cmakeproject.cpp 35.9 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
31
32
#include "cmakeproject.h"
#include "cmakeprojectconstants.h"
#include "cmakeprojectnodes.h"
hjk's avatar
hjk committed
33
#include "cmakerunconfiguration.h"
34
#include "makestep.h"
35
#include "cmakeopenprojectwizard.h"
36
#include "cmakebuildenvironmentwidget.h"
hjk's avatar
hjk committed
37

38
#include <projectexplorer/projectexplorerconstants.h>
con's avatar
con committed
39
#include <cpptools/cppmodelmanagerinterface.h>
dt's avatar
dt committed
40
#include <extensionsystem/pluginmanager.h>
hjk's avatar
hjk committed
41
#include <utils/qtcassert.h>
dt's avatar
dt committed
42
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
43

dt's avatar
dt committed
44
#include <QtCore/QMap>
con's avatar
con committed
45
#include <QtCore/QDebug>
hjk's avatar
hjk committed
46
#include <QtCore/QDir>
dt's avatar
dt committed
47
#include <QtCore/QDateTime>
hjk's avatar
hjk committed
48
#include <QtCore/QProcess>
49
#include <QtGui/QFormLayout>
dt's avatar
dt committed
50
#include <QtGui/QMainWindow>
51
#include <QtGui/QInputDialog>
con's avatar
con committed
52
53
54

using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
55
using namespace ProjectExplorer;
56
57
using ProjectExplorer::Environment;
using ProjectExplorer::EnvironmentItem;
58
59
60
61
62
63
64
65
66
67

// QtCreator CMake Generator wishlist:
// Which make targets we need to build to get all executables
// What is the make we need to call
// What is the actual compiler executable
// DEFINES

// Open Questions
// Who sets up the environment for cl.exe ? INCLUDEPATH and so on

68
69
70
/*!
  \class CMakeBuildConfigurationFactory
*/
71

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
CMakeBuildConfigurationFactory::CMakeBuildConfigurationFactory(CMakeProject *project)
    : IBuildConfigurationFactory(project),
    m_project(project)
{
}

CMakeBuildConfigurationFactory::~CMakeBuildConfigurationFactory()
{
}

QStringList CMakeBuildConfigurationFactory::availableCreationTypes() const
{
    return QStringList() << "Create";
}

Friedemann Kleint's avatar
Friedemann Kleint committed
87
QString CMakeBuildConfigurationFactory::displayNameForType(const QString & /* type */) const
88
89
90
91
{
    return tr("Create");
}

92
bool CMakeBuildConfigurationFactory::create(const QString &type) const
93
{
94
    QTC_ASSERT(type == "Create", return false);
95
96
97
98
99
100
101
102
103
104

    //TODO configuration name should be part of the cmakeopenprojectwizard
    bool ok;
    QString buildConfigurationName = QInputDialog::getText(0,
                          tr("New configuration"),
                          tr("New Configuration Name:"),
                          QLineEdit::Normal,
                          QString(),
                          &ok);
    if (!ok || buildConfigurationName.isEmpty())
105
        return false;
106
107
108
109
110
111
112
113
    BuildConfiguration *bc = new BuildConfiguration(buildConfigurationName);

    CMakeOpenProjectWizard copw(m_project->projectManager(),
                                m_project->sourceDirectory(),
                                m_project->buildDirectory(bc),
                                m_project->environment(bc));
    if (copw.exec() != QDialog::Accepted) {
        delete bc;
114
        return false;
115
    }
116
117
118
119
    m_project->addBuildConfiguration(bc); // this also makes the name unique
    // Default to all
    if (m_project->targets().contains("all"))
        m_project->makeStep()->setBuildTarget(buildConfigurationName, "all", true);
120
121
122
    bc->setValue("buildDirectory", copw.buildDirectory());
    bc->setValue("msvcVersion", copw.msvcVersion());
    m_project->parseCMakeLists();
123
    return true;
124
125
126
127
128
}

/*!
  \class CMakeProject
*/
con's avatar
con committed
129
CMakeProject::CMakeProject(CMakeManager *manager, const QString &fileName)
130
131
    : m_manager(manager),
      m_fileName(fileName),
132
      m_buildConfigurationFactory(new CMakeBuildConfigurationFactory(this)),
133
      m_rootNode(new CMakeProjectNode(m_fileName)),
134
135
      m_toolChain(0),
      m_insideFileChanged(false)
con's avatar
con committed
136
137
{
    m_file = new CMakeFile(this, fileName);
138
139
140
141
142
}

CMakeProject::~CMakeProject()
{
    delete m_rootNode;
143
    delete m_toolChain;
144
145
}

146
147
148
149
150
IBuildConfigurationFactory *CMakeProject::buildConfigurationFactory() const
{
    return m_buildConfigurationFactory;
}

151
152
void CMakeProject::slotActiveBuildConfiguration()
{
153
    BuildConfiguration *activeBC = activeBuildConfiguration();
154
155
156
    // Pop up a dialog asking the user to rerun cmake
    QFileInfo sourceFileInfo(m_fileName);

157
    QString cbpFile = CMakeManager::findCbpFile(QDir(buildDirectory(activeBC)));
158
    QFileInfo cbpFileFi(cbpFile);
159
160
    CMakeOpenProjectWizard::Mode mode = CMakeOpenProjectWizard::Nothing;
    if (!cbpFileFi.exists()) {
161
        mode = CMakeOpenProjectWizard::NeedToCreate;
162
163
    } else {
        foreach(const QString &file, m_watchedFiles) {
164
            if (QFileInfo(file).lastModified() > cbpFileFi.lastModified()) {
165
166
167
168
169
                mode = CMakeOpenProjectWizard::NeedToUpdate;
                break;
            }
        }
    }
170

171
    if (mode != CMakeOpenProjectWizard::Nothing) {
172
173
        CMakeOpenProjectWizard copw(m_manager,
                                    sourceFileInfo.absolutePath(),
174
                                    buildDirectory(activeBC),
175
                                    mode,
176
                                    environment(activeBC));
177
        copw.exec();
178
        activeBC->setValue("msvcVersion", copw.msvcVersion());
179
180
181
182
183
    }
    // reparse
    parseCMakeLists();
}

184
185
void CMakeProject::fileChanged(const QString &fileName)
{
186
    Q_UNUSED(fileName)
187
188
189
    if (m_insideFileChanged== true)
        return;
    m_insideFileChanged = true;
190
    slotActiveBuildConfiguration();
191
192
193
194
195
196
197
198
    m_insideFileChanged = false;
}

void CMakeProject::updateToolChain(const QString &compiler)
{
    //qDebug()<<"CodeBlocks Compilername"<<compiler
    ProjectExplorer::ToolChain *newToolChain = 0;
    if (compiler == "gcc") {
199
200
201
#ifdef Q_OS_WIN
        newToolChain = ProjectExplorer::ToolChain::createMinGWToolChain("gcc", QString());
#else
202
        newToolChain = ProjectExplorer::ToolChain::createGccToolChain("gcc");
203
#endif
204
    } else if (compiler == "msvc8") {
205
        newToolChain = ProjectExplorer::ToolChain::createMSVCToolChain(activeBuildConfiguration()->value("msvcVersion").toString(), false);
206
    } else {
207
        // TODO other toolchains
208
209
210
211
212
213
214
215
216
217
218
219
        qDebug()<<"Not implemented yet!!! Qt Creator doesn't know which toolchain to use for"<<compiler;
    }

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

220
ProjectExplorer::ToolChain *CMakeProject::toolChain(BuildConfiguration *configuration) const
dt's avatar
dt committed
221
{
222
    if (configuration != activeBuildConfiguration())
dt's avatar
dt committed
223
224
225
226
        qWarning()<<"CMakeProject asked for toolchain of a not active buildconfiguration";
    return m_toolChain;
}

227
void CMakeProject::changeBuildDirectory(BuildConfiguration *configuration, const QString &newBuildDirectory)
228
{
229
    configuration->setValue("buildDirectory", newBuildDirectory);
230
231
232
233
234
235
236
237
    parseCMakeLists();
}

QString CMakeProject::sourceDirectory() const
{
    return QFileInfo(m_fileName).absolutePath();
}

dt's avatar
dt committed
238
bool CMakeProject::parseCMakeLists()
239
{
240
    // Find cbp file
241
    QString cbpFile = CMakeManager::findCbpFile(buildDirectory(activeBuildConfiguration()));
242
243

    // setFolderName
244
    m_rootNode->setFolderName(QFileInfo(cbpFile).completeBaseName());
245
    CMakeCbpParser cbpparser;
246
247
    // Parsing
    //qDebug()<<"Parsing file "<<cbpFile;
248
    if (cbpparser.parseCbpFile(cbpFile)) {
249
250
        // ToolChain
        updateToolChain(cbpparser.compilerName());
251

252
        m_projectName = cbpparser.projectName();
dt's avatar
dt committed
253
        m_rootNode->setFolderName(cbpparser.projectName());
254

255
256
        //qDebug()<<"Building Tree";

dt's avatar
dt committed
257

258
        QList<ProjectExplorer::FileNode *> fileList = cbpparser.fileList();
dt's avatar
dt committed
259

260
        QSet<QString> projectFiles;
dt's avatar
dt committed
261
262
        if (cbpparser.hasCMakeFiles()) {
            fileList.append(cbpparser.cmakeFileList());
263
264
            foreach(ProjectExplorer::FileNode *node, cbpparser.cmakeFileList())
                projectFiles.insert(node->path());
dt's avatar
dt committed
265
266
        } else {
            // Manually add the CMakeLists.txt file
267
268
269
            QString cmakeListTxt = sourceDirectory() + "/CMakeLists.txt";
            fileList.append(new ProjectExplorer::FileNode(cmakeListTxt, ProjectExplorer::ProjectFileType, false));
            projectFiles.insert(cmakeListTxt);
dt's avatar
dt committed
270
        }
271

272
273
274
275
276
277
278
279
280

        QSet<QString> added = projectFiles;
        added.subtract(m_watchedFiles);
        foreach(const QString &add, added)
            m_watcher->addFile(add);
        foreach(const QString &remove, m_watchedFiles.subtract(projectFiles))
            m_watcher->removeFile(remove);
        m_watchedFiles = projectFiles;

dt's avatar
dt committed
281
        m_files.clear();
282
        foreach (ProjectExplorer::FileNode *fn, fileList)
con's avatar
con committed
283
284
285
            m_files.append(fn->path());
        m_files.sort();

286
287
288
289
        buildTree(m_rootNode, fileList);


        //qDebug()<<"Adding Targets";
dt's avatar
dt committed
290
        m_targets = cbpparser.targets();
dt's avatar
dt committed
291
292
293
294
295
296
297
//        qDebug()<<"Printing targets";
//        foreach(CMakeTarget ct, m_targets) {
//            qDebug()<<ct.title<<" with executable:"<<ct.executable;
//            qDebug()<<"WD:"<<ct.workingDirectory;
//            qDebug()<<ct.makeCommand<<ct.makeCleanCommand;
//            qDebug()<<"";
//        }
dt's avatar
dt committed
298

299
        //qDebug()<<"Updating CodeModel";
300
301
302
303
304
305
306
307
308
309

        QStringList allIncludePaths;
        QStringList allFrameworkPaths;
        QList<ProjectExplorer::HeaderPath> allHeaderPaths = m_toolChain->systemHeaderPaths();
        foreach (ProjectExplorer::HeaderPath headerPath, allHeaderPaths) {
            if (headerPath.kind() == ProjectExplorer::HeaderPath::FrameworkHeaderPath)
                allFrameworkPaths.append(headerPath.path());
            else
                allIncludePaths.append(headerPath.path());
        }
310
        // This explicitly adds -I. to the include paths
311
        allIncludePaths.append(sourceDirectory());
312

313
        allIncludePaths.append(cbpparser.includeFiles());
con's avatar
con committed
314
315
        CppTools::CppModelManagerInterface *modelmanager = ExtensionSystem::PluginManager::instance()->getObject<CppTools::CppModelManagerInterface>();
        if (modelmanager) {
316
            CppTools::CppModelManagerInterface::ProjectInfo pinfo = modelmanager->projectInfo(this);
317
318
319
320
321
322
323
324
325
326
327
328
            if (pinfo.includePaths != allIncludePaths
                || pinfo.sourceFiles != m_files
                || pinfo.defines != m_toolChain->predefinedMacros()
                || pinfo.frameworkPaths != allFrameworkPaths)  {
                pinfo.includePaths = allIncludePaths;
                // TODO we only want C++ files, not all other stuff that might be in the project
                pinfo.sourceFiles = m_files;
                pinfo.defines = m_toolChain->predefinedMacros(); // TODO this is to simplistic
                pinfo.frameworkPaths = allFrameworkPaths;
                modelmanager->updateProjectInfo(pinfo);
                modelmanager->updateSourceFiles(pinfo.sourceFiles);
            }
con's avatar
con committed
329
        }
dt's avatar
dt committed
330
331

        // Create run configurations for m_targets
332
        //qDebug()<<"Create run configurations of m_targets";
333
        QMultiMap<QString, QSharedPointer<CMakeRunConfiguration> > existingRunConfigurations;
dt's avatar
dt committed
334
        foreach(QSharedPointer<ProjectExplorer::RunConfiguration> cmakeRunConfiguration, runConfigurations()) {
dt's avatar
dt committed
335
            if (QSharedPointer<CMakeRunConfiguration> rc = cmakeRunConfiguration.objectCast<CMakeRunConfiguration>()) {
dt's avatar
dt committed
336
337
338
339
                existingRunConfigurations.insert(rc->title(), rc);
            }
        }

340
        bool setActive = existingRunConfigurations.isEmpty();
dt's avatar
dt committed
341
342
343
344
345
        foreach(const CMakeTarget &ct, m_targets) {
            if (ct.executable.isEmpty())
                continue;
            if (ct.title.endsWith("/fast"))
                continue;
346
347
            QList<QSharedPointer<CMakeRunConfiguration> > list = existingRunConfigurations.values(ct.title);
            if (!list.isEmpty()) {
dt's avatar
dt committed
348
                // Already exists, so override the settings...
349
350
351
352
353
354
355
356
                foreach (QSharedPointer<CMakeRunConfiguration> rc, list) {
                    //qDebug()<<"Updating Run Configuration with title"<<ct.title;
                    //qDebug()<<"  Executable new:"<<ct.executable<< "old:"<<rc->executable();
                    //qDebug()<<"  WD new:"<<ct.workingDirectory<<"old:"<<rc->workingDirectory();
                    rc->setExecutable(ct.executable);
                    rc->setWorkingDirectory(ct.workingDirectory);
                }
                existingRunConfigurations.remove(ct.title);
dt's avatar
dt committed
357
358
            } else {
                // Does not exist yet
359
360
                //qDebug()<<"Adding new run configuration with title"<<ct.title;
                //qDebug()<<"  Executable:"<<ct.executable<<"WD:"<<ct.workingDirectory;
dt's avatar
dt committed
361
362
363
                QSharedPointer<ProjectExplorer::RunConfiguration> rc(new CMakeRunConfiguration(this, ct.executable, ct.workingDirectory, ct.title));
                addRunConfiguration(rc);
                // The first one gets the honour of beeing the active one
364
                if (setActive) {
dt's avatar
dt committed
365
                    setActiveRunConfiguration(rc);
366
                    setActive = false;
dt's avatar
dt committed
367
368
369
                }
            }
        }
370
        QMultiMap<QString, QSharedPointer<CMakeRunConfiguration> >::const_iterator it =
dt's avatar
dt committed
371
372
373
                existingRunConfigurations.constBegin();
        for( ; it != existingRunConfigurations.constEnd(); ++it) {
            QSharedPointer<CMakeRunConfiguration> rc = it.value();
374
375
            //qDebug()<<"Removing old RunConfiguration with title:"<<rc->title();
            //qDebug()<<"  Executable:"<<rc->executable()<<rc->workingDirectory();
dt's avatar
dt committed
376
377
            removeRunConfiguration(rc);
        }
378
        //qDebug()<<"\n";
con's avatar
con committed
379
380
    } else {
        // TODO report error
dt's avatar
dt committed
381
        qDebug()<<"Parsing failed";
382
383
        delete m_toolChain;
        m_toolChain = 0;
dt's avatar
dt committed
384
        return false;
con's avatar
con committed
385
    }
dt's avatar
dt committed
386
    return true;
con's avatar
con committed
387
388
}

389
QString CMakeProject::buildParser(BuildConfiguration *configuration) const
390
{
391
    Q_UNUSED(configuration)
392
    // TODO this is actually slightly wrong, but do i care?
393
    // this should call toolchain(configuration)
394
395
396
397
398
399
400
401
402
403
404
405
406
    if (!m_toolChain)
        return QString::null;
    if (m_toolChain->type() == ProjectExplorer::ToolChain::GCC
        || m_toolChain->type() == ProjectExplorer::ToolChain::LinuxICC
        || m_toolChain->type() == ProjectExplorer::ToolChain::MinGW) {
        return ProjectExplorer::Constants::BUILD_PARSER_GCC;
    } else if (m_toolChain->type() == ProjectExplorer::ToolChain::MSVC
               || m_toolChain->type() == ProjectExplorer::ToolChain::WINCE) {
        return ProjectExplorer::Constants::BUILD_PARSER_MSVC;
    }
    return QString::null;
}

407
QStringList CMakeProject::targets() const
con's avatar
con committed
408
{
409
    QStringList results;
410
411
412
413
414
    foreach (const CMakeTarget &ct, m_targets) {
        if (ct.executable.isEmpty())
            continue;
        if (ct.title.endsWith("/fast"))
            continue;
415
        results << ct.title;
416
    }
417
    return results;
con's avatar
con committed
418
419
}

420
void CMakeProject::gatherFileNodes(ProjectExplorer::FolderNode *parent, QList<ProjectExplorer::FileNode *> &list)
con's avatar
con committed
421
{
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
    foreach(ProjectExplorer::FolderNode *folder, parent->subFolderNodes())
        gatherFileNodes(folder, list);
    foreach(ProjectExplorer::FileNode *file, parent->fileNodes())
        list.append(file);
}

void CMakeProject::buildTree(CMakeProjectNode *rootNode, QList<ProjectExplorer::FileNode *> newList)
{
    // Gather old list
    QList<ProjectExplorer::FileNode *> oldList;
    gatherFileNodes(rootNode, oldList);
    qSort(oldList.begin(), oldList.end(), ProjectExplorer::ProjectNode::sortNodesByPath);
    qSort(newList.begin(), newList.end(), ProjectExplorer::ProjectNode::sortNodesByPath);

    // generate added and deleted list
    QList<ProjectExplorer::FileNode *>::const_iterator oldIt  = oldList.constBegin();
    QList<ProjectExplorer::FileNode *>::const_iterator oldEnd = oldList.constEnd();
    QList<ProjectExplorer::FileNode *>::const_iterator newIt  = newList.constBegin();
    QList<ProjectExplorer::FileNode *>::const_iterator newEnd = newList.constEnd();

    QList<ProjectExplorer::FileNode *> added;
    QList<ProjectExplorer::FileNode *> deleted;


    while(oldIt != oldEnd && newIt != newEnd) {
        if ( (*oldIt)->path() == (*newIt)->path()) {
            delete *newIt;
            ++oldIt;
            ++newIt;
        } else if ((*oldIt)->path() < (*newIt)->path()) {
            deleted.append(*oldIt);
            ++oldIt;
        } else {
            added.append(*newIt);
            ++newIt;
        }
    }

    while (oldIt != oldEnd) {
        deleted.append(*oldIt);
        ++oldIt;
    }

    while (newIt != newEnd) {
        added.append(*newIt);
        ++newIt;
    }

    // add added nodes
    foreach (ProjectExplorer::FileNode *fn, added) {
//        qDebug()<<"added"<<fn->path();
con's avatar
con committed
473
474
475
476
477
        // Get relative path to rootNode
        QString parentDir = QFileInfo(fn->path()).absolutePath();
        ProjectExplorer::FolderNode *folder = findOrCreateFolder(rootNode, parentDir);
        rootNode->addFileNodes(QList<ProjectExplorer::FileNode *>()<< fn, folder);
    }
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

    // remove old file nodes and check wheter folder nodes can be removed
    foreach (ProjectExplorer::FileNode *fn, deleted) {
        ProjectExplorer::FolderNode *parent = fn->parentFolderNode();
//        qDebug()<<"removed"<<fn->path();
        rootNode->removeFileNodes(QList<ProjectExplorer::FileNode *>() << fn, parent);
        // Check for empty parent
        while (parent->subFolderNodes().isEmpty() && parent->fileNodes().isEmpty()) {
            ProjectExplorer::FolderNode *grandparent = parent->parentFolderNode();
            rootNode->removeFolderNodes(QList<ProjectExplorer::FolderNode *>() << parent, grandparent);
            parent = grandparent;
            if (parent == rootNode)
                break;
        }
    }
con's avatar
con committed
493
494
495
496
497
}

ProjectExplorer::FolderNode *CMakeProject::findOrCreateFolder(CMakeProjectNode *rootNode, QString directory)
{
    QString relativePath = QDir(QFileInfo(rootNode->path()).path()).relativeFilePath(directory);
498
    QStringList parts = relativePath.split("/", QString::SkipEmptyParts);
con's avatar
con committed
499
    ProjectExplorer::FolderNode *parent = rootNode;
500
    QString path = QFileInfo(rootNode->path()).path();
hjk's avatar
hjk committed
501
    foreach (const QString &part, parts) {
502
        path += "/" + part;
con's avatar
con committed
503
504
        // Find folder in subFolders
        bool found = false;
hjk's avatar
hjk committed
505
        foreach (ProjectExplorer::FolderNode *folder, parent->subFolderNodes()) {
506
            if (folder->path() == path) {
con's avatar
con committed
507
508
509
510
511
512
513
514
                // yeah found something :)
                parent = folder;
                found = true;
                break;
            }
        }
        if (!found) {
            // No FolderNode yet, so create it
515
516
            ProjectExplorer::FolderNode *tmp = new ProjectExplorer::FolderNode(path);
            tmp->setFolderName(part);
con's avatar
con committed
517
518
519
520
521
522
523
524
525
            rootNode->addFolderNodes(QList<ProjectExplorer::FolderNode *>() << tmp, parent);
            parent = tmp;
        }
    }
    return parent;
}

QString CMakeProject::name() const
{
526
    return m_projectName;
con's avatar
con committed
527
528
}

529
530


con's avatar
con committed
531
532
533
534
535
Core::IFile *CMakeProject::file() const
{
    return m_file;
}

536
CMakeManager *CMakeProject::projectManager() const
con's avatar
con committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
{
    return m_manager;
}

QList<ProjectExplorer::Project *> CMakeProject::dependsOn()
{
    return QList<Project *>();
}

bool CMakeProject::isApplication() const
{
    return true;
}

551
ProjectExplorer::Environment CMakeProject::baseEnvironment(BuildConfiguration *configuration) const
552
{
553
    Environment env = useSystemEnvironment(configuration) ? Environment(QProcess::systemEnvironment()) : Environment();
554
555
556
    return env;
}

557
ProjectExplorer::Environment CMakeProject::environment(BuildConfiguration *configuration) const
con's avatar
con committed
558
{
559
560
    Environment env = baseEnvironment(configuration);
    env.modify(userEnvironmentChanges(configuration));
561
562
563
    return env;
}

564
void CMakeProject::setUseSystemEnvironment(BuildConfiguration *configuration, bool b)
565
{
566
    if (b == useSystemEnvironment(configuration))
567
        return;
568
569
    configuration->setValue("clearSystemEnvironment", !b);
    emit environmentChanged(configuration->name());
570
571
}

572
bool CMakeProject::useSystemEnvironment(BuildConfiguration *configuration) const
573
{
574
575
    bool b = !(configuration->value("clearSystemEnvironment").isValid() &&
               configuration->value("clearSystemEnvironment").toBool());
576
577
578
    return b;
}

579
QList<ProjectExplorer::EnvironmentItem> CMakeProject::userEnvironmentChanges(BuildConfiguration *configuration) const
580
{
581
    return EnvironmentItem::fromStringList(configuration->value("userEnvironmentChanges").toStringList());
582
583
}

584
void CMakeProject::setUserEnvironmentChanges(BuildConfiguration *configuration, const QList<ProjectExplorer::EnvironmentItem> &diff)
585
{
586
    QStringList list = EnvironmentItem::toStringList(diff);
587
    if (list == configuration->value("userEnvironmentChanges"))
588
        return;
589
590
    configuration->setValue("userEnvironmentChanges", list);
    emit environmentChanged(configuration->name());
con's avatar
con committed
591
592
}

593
QString CMakeProject::buildDirectory(BuildConfiguration *configuration) const
con's avatar
con committed
594
{
595
    QString buildDirectory = configuration->value("buildDirectory").toString();
596
    if (buildDirectory.isEmpty())
597
        buildDirectory = sourceDirectory() + "/qtcreator-build";
598
    return buildDirectory;
con's avatar
con committed
599
600
}

dt's avatar
dt committed
601
ProjectExplorer::BuildConfigWidget *CMakeProject::createConfigWidget()
con's avatar
con committed
602
{
603
    return new CMakeBuildSettingsWidget(this);
con's avatar
con committed
604
605
}

dt's avatar
dt committed
606
QList<ProjectExplorer::BuildConfigWidget*> CMakeProject::subConfigWidgets()
con's avatar
con committed
607
{
dt's avatar
dt committed
608
    QList<ProjectExplorer::BuildConfigWidget*> list;
609
610
    list <<  new CMakeBuildEnvironmentWidget(this);
    return list;
con's avatar
con committed
611
612
613
614
615
616
617
618
619
620
}

ProjectExplorer::ProjectNode *CMakeProject::rootProjectNode() const
{
    return m_rootNode;
}


QStringList CMakeProject::files(FilesMode fileMode) const
{
621
    Q_UNUSED(fileMode)
con's avatar
con committed
622
623
624
625
626
    return m_files;
}

void CMakeProject::saveSettingsImpl(ProjectExplorer::PersistentSettingsWriter &writer)
{
dt's avatar
dt committed
627
    Project::saveSettingsImpl(writer);
con's avatar
con committed
628
629
}

630
631
632
633
634
635
636
637
638
639
MakeStep *CMakeProject::makeStep() const
{
    foreach (ProjectExplorer::BuildStep *bs, buildSteps()) {
        MakeStep *ms = qobject_cast<MakeStep *>(bs);
        if (ms)
            return ms;
    }
    return 0;
}

640

dt's avatar
dt committed
641
bool CMakeProject::restoreSettingsImpl(ProjectExplorer::PersistentSettingsReader &reader)
con's avatar
con committed
642
{
dt's avatar
dt committed
643
    Project::restoreSettingsImpl(reader);
dt's avatar
dt committed
644
    bool hasUserFile = !buildConfigurations().isEmpty();
645
    MakeStep *makeStep = 0;
dt's avatar
dt committed
646
647
648
649
    if (!hasUserFile) {
        // Ask the user for where he wants to build it
        // and the cmake command line

650
        CMakeOpenProjectWizard copw(m_manager, sourceDirectory(), ProjectExplorer::Environment::systemEnvironment());
651
652
        if (copw.exec() != QDialog::Accepted)
            return false;
dt's avatar
dt committed
653

654
        qDebug()<<"ccd.buildDirectory()"<<copw.buildDirectory();
dt's avatar
dt committed
655
656

        // Now create a standard build configuration
657
        makeStep = new MakeStep(this);
658

659
        insertBuildStep(0, makeStep);
660

661
662
        ProjectExplorer::BuildConfiguration *bc = new ProjectExplorer::BuildConfiguration("all");
        addBuildConfiguration(bc);
663
        bc->setValue("msvcVersion", copw.msvcVersion());
664
        if (!copw.buildDirectory().isEmpty())
665
            bc->setValue("buildDirectory", copw.buildDirectory());
666
        //TODO save arguments somewhere copw.arguments()
dt's avatar
dt committed
667
668
669
670

        MakeStep *cleanMakeStep = new MakeStep(this);
        insertCleanStep(0, cleanMakeStep);
        cleanMakeStep->setValue("clean", true);
671
        setActiveBuildConfiguration(bc);
672
673
674
    } else {
        // We have a user file, but we could still be missing the cbp file
        // or simply run createXml with the saved settings
dt's avatar
dt committed
675
676
677
        QFileInfo sourceFileInfo(m_fileName);
        QStringList needToCreate;
        QStringList needToUpdate;
678
679
        BuildConfiguration *activeBC = activeBuildConfiguration();
        QString cbpFile = CMakeManager::findCbpFile(QDir(buildDirectory(activeBC)));
680
        QFileInfo cbpFileFi(cbpFile);
681
682

        CMakeOpenProjectWizard::Mode mode = CMakeOpenProjectWizard::Nothing;
683
        if (!cbpFileFi.exists())
684
            mode = CMakeOpenProjectWizard::NeedToCreate;
685
        else if (cbpFileFi.lastModified() < sourceFileInfo.lastModified())
686
            mode = CMakeOpenProjectWizard::NeedToUpdate;
687

688
        if (mode != CMakeOpenProjectWizard::Nothing) {
689
690
            CMakeOpenProjectWizard copw(m_manager,
                                        sourceFileInfo.absolutePath(),
691
                                        buildDirectory(activeBC),
692
                                        mode,
693
                                        environment(activeBC));
694
695
            if (copw.exec() != QDialog::Accepted)
                return false;
696
            activeBC->setValue("msvcVersion", copw.msvcVersion());
dt's avatar
dt committed
697
        }
dt's avatar
dt committed
698
    }
699

700
701
702
    if (!hasUserFile && targets().contains("all"))
        makeStep->setBuildTarget("all", "all", true);

703
704
    m_watcher = new ProjectExplorer::FileWatcher(this);
    connect(m_watcher, SIGNAL(fileChanged(QString)), this, SLOT(fileChanged(QString)));
705
706
707
    bool result = parseCMakeLists(); // Gets the directory from the active buildconfiguration
    if (!result)
        return false;
708
709
710

    connect(this, SIGNAL(activeBuildConfigurationChanged()),
            this, SLOT(slotActiveBuildConfiguration()));
dt's avatar
dt committed
711
    return true;
con's avatar
con committed
712
713
}

714
715
716
717
718
719
720
721
CMakeTarget CMakeProject::targetForTitle(const QString &title)
{
    foreach(const CMakeTarget &ct, m_targets)
        if (ct.title == title)
            return ct;
    return CMakeTarget();
}

722
723
724
725
726
727
728
729
730
ProjectExplorer::ToolChain::ToolChainType CMakeProject::toolChainType() const
{
    if (m_toolChain)
        return m_toolChain->type();
    return ProjectExplorer::ToolChain::UNKNOWN;
}

// CMakeFile

con's avatar
con committed
731
732
733
734
735
736
737
738
739
740
CMakeFile::CMakeFile(CMakeProject *parent, QString fileName)
    : Core::IFile(parent), m_project(parent), m_fileName(fileName)
{

}

bool CMakeFile::save(const QString &fileName)
{
    // Once we have an texteditor open for this file, we probably do
    // need to implement this, don't we.
741
    Q_UNUSED(fileName)
con's avatar
con committed
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
    return false;
}

QString CMakeFile::fileName() const
{
    return m_fileName;
}

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

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

QString CMakeFile::mimeType() const
{
    return Constants::CMAKEMIMETYPE;
}


bool CMakeFile::isModified() const
{
    return false;
}

bool CMakeFile::isReadOnly() const
{
    return true;
}

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

void CMakeFile::modified(ReloadBehavior *behavior)
{
783
    Q_UNUSED(behavior)
con's avatar
con committed
784
785
}

786
787
CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeProject *project)
    : m_project(project)
con's avatar
con committed
788
{
789
    QFormLayout *fl = new QFormLayout(this);
790
    fl->setContentsMargins(20, -1, 0, -1);
791
    fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
792
    setLayout(fl);
793
794
    m_pathLineEdit = new QLineEdit(this);
    m_pathLineEdit->setReadOnly(true);
795
796
797
798
    // TODO currently doesn't work
    // since creating the cbp file also creates makefiles
    // and then cmake builds in that directory instead of shadow building
    // We need our own generator for that to work
799
800
801
802
803
804
805
806
807
808

    QHBoxLayout *hbox = new QHBoxLayout();
    hbox->addWidget(m_pathLineEdit);

    m_changeButton = new QPushButton(this);
    m_changeButton->setText(tr("&Change"));
    connect(m_changeButton, SIGNAL(clicked()), this, SLOT(openChangeBuildDirectoryDialog()));
    hbox->addWidget(m_changeButton);

    fl->addRow("Build directory:", hbox);
con's avatar
con committed
809
810
811
812
813
814
815
}

QString CMakeBuildSettingsWidget::displayName() const
{
    return "CMake";
}

816
void CMakeBuildSettingsWidget::init(const QString &buildConfigurationName)
con's avatar
con committed
817
{
818
819
820
821
    m_buildConfiguration = buildConfigurationName;
    BuildConfiguration *bc = m_project->buildConfiguration(buildConfigurationName);
    m_pathLineEdit->setText(m_project->buildDirectory(bc));
    if (m_project->buildDirectory(bc) == m_project->sourceDirectory())
822
823
        m_changeButton->setEnabled(false);
    else
824
        m_changeButton->setEnabled(true);
con's avatar
con committed
825
}
826

827
void CMakeBuildSettingsWidget::openChangeBuildDirectoryDialog()
828
{
829
    BuildConfiguration *bc = m_project->buildConfiguration(m_buildConfiguration);
830
831
    CMakeOpenProjectWizard copw(m_project->projectManager(),
                                m_project->sourceDirectory(),
832
833
                                m_project->buildDirectory(bc),
                                m_project->environment(bc));
834
    if (copw.exec() == QDialog::Accepted) {
835
836
        m_project->changeBuildDirectory(bc, copw.buildDirectory());
        m_pathLineEdit->setText(m_project->buildDirectory(bc));
837
    }
838
839
840
841
842
843
}

/////
// CMakeCbpParser
////

844
845
846
847
848
849
bool CMakeCbpParser::parseCbpFile(const QString &fileName)
{
    QFile fi(fileName);
    if (fi.exists() && fi.open(QFile::ReadOnly)) {
        setDevice(&fi);

hjk's avatar
hjk committed
850
        while (!atEnd()) {
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
            readNext();
            if (name() == "CodeBlocks_project_file") {
                parseCodeBlocks_project_file();
            } else if (isStartElement()) {
                parseUnknownElement();
            }
        }
        fi.close();
        m_includeFiles.sort();
        m_includeFiles.removeDuplicates();
        return true;
    }
    return false;
}

void CMakeCbpParser::parseCodeBlocks_project_file()
{
hjk's avatar
hjk committed
868
    while (!atEnd()) {
869
870
871
872
873
874
875
876
877
878
879
880
881
        readNext();
        if (isEndElement()) {
            return;
        } else if (name() == "Project") {
            parseProject();
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

void CMakeCbpParser::parseProject()
{
hjk's avatar
hjk committed
882
    while (!atEnd()) {
883
884
885
        readNext();
        if (isEndElement()) {
            return;
886
887
        } else if (name() == "Option") {
            parseOption();
888
889
890
891
892
893
894
895
896
897
898
899
        } else if (name() == "Unit") {
            parseUnit();
        } else if (name() == "Build") {
            parseBuild();
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

void CMakeCbpParser::parseBuild()
{
hjk's avatar
hjk committed
900
    while (!atEnd()) {
901
902
903
904
905
906
907
908
909
910
911
912
913
        readNext();
        if (isEndElement()) {
            return;
        } else if (name() == "Target") {
            parseTarget();
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

void CMakeCbpParser::parseTarget()
{
dt's avatar
dt committed
914
    m_targetType = false;
dt's avatar
dt committed
915
916
917
918
    m_target.clear();

    if (attributes().hasAttribute("title"))
        m_target.title = attributes().value("title").toString();
dt's avatar
dt committed
919
    while (!atEnd()) {
920
921
        readNext();
        if (isEndElement()) {
922
            if (m_targetType || m_target.title == "all" || m_target.title == "install") {
dt's avatar
dt committed
923
                m_targets.append(m_target);
dt's avatar
dt committed
924
            }
925
926
927
            return;
        } else if (name() == "Compiler") {
            parseCompiler();
dt's avatar
dt committed
928
929
930
931
932
933
934
935
936
937
938
        } else if (name() == "Option") {
            parseTargetOption();
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

void CMakeCbpParser::parseTargetOption()
{
    if (attributes().hasAttribute("output"))
dt's avatar
dt committed
939
        m_target.executable = attributes().value("output").toString();
940
    else if (attributes().hasAttribute("type") && (attributes().value("type") == "1" || attributes().value("type") == "0"))
dt's avatar
dt committed
941
        m_targetType = true;
dt's avatar
dt committed
942
943
    else if (attributes().hasAttribute("working_dir"))
        m_target.workingDirectory = attributes().value("working_dir").toString();
dt's avatar
dt committed
944
    while (!atEnd()) {
dt's avatar
dt committed
945
946
947
948
949
950
951
952
953
954
955
        readNext();
        if (isEndElement()) {
            return;
        } else if (name() == "MakeCommand") {
            parseMakeCommand();
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

956
957
958
959
960
961
962
963
964
QString CMakeCbpParser::projectName() const
{
    return m_projectName;
}

void CMakeCbpParser::parseOption()
{
    if (attributes().hasAttribute("title"))
        m_projectName = attributes().value("title").toString();
965

966
967
968
    if (attributes().hasAttribute("compiler"))
        m_compiler = attributes().value("compiler").toString();

969
970
971
972
973
974
975
976
    while (!atEnd()) {
        readNext();
        if (isEndElement()) {
            return;
        } else if(isStartElement()) {
            parseUnknownElement();
        }
    }
977
978
}

dt's avatar
dt committed
979
980
void CMakeCbpParser::parseMakeCommand()
{
dt's avatar
dt committed
981
    while (!atEnd()) {
dt's avatar
dt committed
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
        readNext();
        if (isEndElement()) {
            return;
        } else if (name() == "Build") {
            parseTargetBuild();
        } else if (name() == "Clean") {
            parseTargetClean();
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

void CMakeCbpParser::parseTargetBuild()
{
    if (attributes().hasAttribute("command"))
        m_target.makeCommand = attributes().value("command").toString();
dt's avatar
dt committed
999
    while (!atEnd()) {
dt's avatar
dt committed
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
        readNext();
        if (isEndElement()) {
            return;
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

void CMakeCbpParser::parseTargetClean()
{
    if (attributes().hasAttribute("command"))
        m_target.makeCleanCommand = attributes().value("command").toString();
dt's avatar
dt committed
1013
    while (!atEnd()) {
dt's avatar
dt committed
1014
1015
1016
        readNext();
        if (isEndElement()) {
            return;
1017
1018
1019
1020
1021
1022
1023
1024
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

void CMakeCbpParser::parseCompiler()
{
hjk's avatar
hjk committed
1025
    while (!atEnd()) {
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
        readNext();
        if (isEndElement()) {
            return;
        } else if (name() == "Add") {
            parseAdd();
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

void CMakeCbpParser::parseAdd()
{
    m_includeFiles.append(attributes().value("directory").toString());
hjk's avatar
hjk committed
1040
    while (!atEnd()) {
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
        readNext();
        if (isEndElement()) {
            return;
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

void CMakeCbpParser::parseUnit()
{
    //qDebug()<<stream.attributes().value("filename");
    QString fileName = attributes().value("filename").toString();
dt's avatar
dt committed
1054
    m_parsingCmakeUnit = false;
hjk's avatar
hjk committed
1055
    while (!atEnd()) {
1056
1057
        readNext();
        if (isEndElement()) {
dt's avatar
dt committed
1058
1059
            if (!fileName.endsWith(".rule") && !m_processedUnits.contains(fileName)) {
                // Now check whether we found a virtual element beneath
1060
                if (m_parsingCmakeUnit) {
dt's avatar
dt committed
1061
                    m_cmakeFileList.append( new ProjectExplorer::FileNode(fileName, ProjectExplorer::ProjectFileType, false));
1062
1063
1064
1065
1066
1067
                } else {
                    if (fileName.endsWith(".qrc"))
                        m_fileList.append( new ProjectExplorer::FileNode(fileName, ProjectExplorer::ResourceType, false));
                    else
                        m_fileList.append( new ProjectExplorer::FileNode(fileName, ProjectExplorer::SourceType, false));
                }
dt's avatar
dt committed
1068
1069
                m_processedUnits.insert(fileName);
            }
1070
            return;
dt's avatar
dt committed
1071
1072
        } else if (name() == "Option") {
            parseUnitOption();
1073
1074
1075
1076
1077
1078
        } else if (isStartElement()) {
            parseUnknownElement();
        }
    }
}

dt's avatar
dt committed
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
void CMakeCbpParser::parseUnitOption()
{
    if (attributes().hasAttribute("virtualFolder"))
        m_parsingCmakeUnit = true;

    while (!atEnd()) {
        readNext();

        if (isEndElement())
            break;

        if (isStartElement())
            parseUnknownElement();
    }
}

1095
1096
void CMakeCbpParser::parseUnknownElement()
{
dt's avatar
dt committed
1097
    Q_ASSERT(isStartElement());
1098
1099
1100
1101