genericproject.cpp 15.3 KB
Newer Older
Roberto Raggi's avatar
Roberto Raggi committed
1 2 3 4 5 6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
Roberto Raggi's avatar
Roberto Raggi committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
Roberto Raggi's avatar
Roberto Raggi committed
27 28 29 30
**
**************************************************************************/

#include "genericproject.h"
Tobias Hunger's avatar
Tobias Hunger committed
31

dt's avatar
dt committed
32
#include "genericbuildconfiguration.h"
Tobias Hunger's avatar
Tobias Hunger committed
33 34
#include "genericprojectconstants.h"
#include "generictarget.h"
Roberto Raggi's avatar
Roberto Raggi committed
35

36
#include <projectexplorer/toolchain.h>
Roberto Raggi's avatar
Roberto Raggi committed
37 38 39
#include <projectexplorer/projectexplorerconstants.h>
#include <cpptools/cppmodelmanagerinterface.h>
#include <extensionsystem/pluginmanager.h>
40
#include <utils/pathchooser.h>
Roberto Raggi's avatar
Roberto Raggi committed
41 42
#include <coreplugin/icore.h>

43
#include <QtCore/QDir>
Roberto Raggi's avatar
Roberto Raggi committed
44

45 46 47
#include <QtGui/QFormLayout>
#include <QtGui/QMainWindow>
#include <QtGui/QComboBox>
Roberto Raggi's avatar
Roberto Raggi committed
48 49 50

using namespace GenericProjectManager;
using namespace GenericProjectManager::Internal;
51
using namespace ProjectExplorer;
Roberto Raggi's avatar
Roberto Raggi committed
52

53
namespace {
54
const char * const TOOLCHAIN_KEY("GenericProjectManager.GenericProject.Toolchain");
55 56
} // end of anonymous namespace

57 58 59 60
////////////////////////////////////////////////////////////////////////////////////
// GenericProject
////////////////////////////////////////////////////////////////////////////////////

Roberto Raggi's avatar
Roberto Raggi committed
61
GenericProject::GenericProject(Manager *manager, const QString &fileName)
62 63
    : m_manager(manager),
      m_fileName(fileName),
Tobias Hunger's avatar
Tobias Hunger committed
64
      m_targetFactory(new GenericTargetFactory(this)),
65
      m_toolChain(0)
Roberto Raggi's avatar
Roberto Raggi committed
66
{
67
    QFileInfo fileInfo(m_fileName);
68 69
    QDir dir = fileInfo.dir();

70
    m_projectName      = fileInfo.completeBaseName();
71 72 73
    m_filesFileName    = QFileInfo(dir, m_projectName + QLatin1String(".files")).absoluteFilePath();
    m_includesFileName = QFileInfo(dir, m_projectName + QLatin1String(".includes")).absoluteFilePath();
    m_configFileName   = QFileInfo(dir, m_projectName + QLatin1String(".config")).absoluteFilePath();
74

75 76
    m_file = new GenericProjectFile(this, fileName);
    m_rootNode = new GenericProjectNode(this, m_file);
77

78
    m_manager->registerProject(this);
Roberto Raggi's avatar
Roberto Raggi committed
79 80 81 82
}

GenericProject::~GenericProject()
{
83
    m_manager->unregisterProject(this);
84

85 86
    delete m_rootNode;
    delete m_toolChain;
Roberto Raggi's avatar
Roberto Raggi committed
87 88
}

Tobias Hunger's avatar
Tobias Hunger committed
89 90 91 92 93 94
GenericTargetFactory *GenericProject::targetFactory() const
{
    return m_targetFactory;
}

GenericTarget *GenericProject::activeTarget() const
95
{
Tobias Hunger's avatar
Tobias Hunger committed
96
    return static_cast<GenericTarget *>(Project::activeTarget());
97 98
}

99
QString GenericProject::filesFileName() const
100
{ return m_filesFileName; }
101 102

QString GenericProject::includesFileName() const
103
{ return m_includesFileName; }
104 105

QString GenericProject::configFileName() const
106
{ return m_configFileName; }
107

108
static QStringList readLines(const QString &absoluteFileName)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
{
    QStringList lines;

    QFile file(absoluteFileName);
    if (file.open(QFile::ReadOnly)) {
        QTextStream stream(&file);

        forever {
            QString line = stream.readLine();
            if (line.isNull())
                break;

            lines.append(line);
        }
    }

    return lines;
}

128
bool GenericProject::saveRawFileList(const QStringList &rawFileList)
129
{
130
    // Make sure we can open the file for writing
131
    QFile file(filesFileName());
132 133 134 135
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return false;

    QTextStream stream(&file);
136 137
    foreach (const QString &filePath, rawFileList)
        stream << filePath << QLatin1Char('\n');
138 139 140 141

    file.close();
    refresh(GenericProject::Files);
    return true;
142
}
143

144 145
bool GenericProject::addFiles(const QStringList &filePaths)
{
146 147 148 149 150
    QStringList newList = m_rawFileList;

    QDir baseDir(QFileInfo(m_fileName).dir());
    foreach (const QString &filePath, filePaths)
        newList.append(baseDir.relativeFilePath(filePath));
151

152
    return saveRawFileList(newList);
153 154 155 156
}

bool GenericProject::removeFiles(const QStringList &filePaths)
{
157
    QStringList newList = m_rawFileList;
158

159 160 161 162
    foreach (const QString &filePath, filePaths) {
        QHash<QString, QString>::iterator i = m_rawListEntries.find(filePath);
        if (i != m_rawListEntries.end())
            newList.removeOne(i.value());
163
    }
164

165
    return saveRawFileList(newList);
166 167
}

168
void GenericProject::parseProject(RefreshOptions options)
Roberto Raggi's avatar
Roberto Raggi committed
169
{
170 171 172 173 174
    if (options & Files) {
        m_rawListEntries.clear();
        m_rawFileList = readLines(filesFileName());
        m_files = convertToAbsoluteFiles(m_rawFileList, &m_rawListEntries);
    }
175

176
    if (options & Configuration) {
177
        m_projectIncludePaths = convertToAbsoluteFiles(readLines(includesFileName()));
178

179 180
        // TODO: Possibly load some configuration from the project file
        //QSettings projectInfo(m_fileName, QSettings::IniFormat);
Roberto Raggi's avatar
Roberto Raggi committed
181

182
        m_defines.clear();
Roberto Raggi's avatar
Roberto Raggi committed
183

184 185 186 187
        QFile configFile(configFileName());
        if (configFile.open(QFile::ReadOnly))
            m_defines = configFile.readAll();
    }
188

189 190
    if (options & Files)
        emit fileListChanged();
Roberto Raggi's avatar
Roberto Raggi committed
191 192
}

193
void GenericProject::refresh(RefreshOptions options)
Roberto Raggi's avatar
Roberto Raggi committed
194
{
195 196 197
    QSet<QString> oldFileList;
    if (!(options & Configuration))
        oldFileList = m_files.toSet();
198

199 200 201 202
    parseProject(options);

    if (options & Files)
        m_rootNode->refresh();
Roberto Raggi's avatar
Roberto Raggi committed
203 204 205 206

    CppTools::CppModelManagerInterface *modelManager =
        ExtensionSystem::PluginManager::instance()->getObject<CppTools::CppModelManagerInterface>();

207 208
    if (m_toolChain && modelManager) {
        const QByteArray predefinedMacros = m_toolChain->predefinedMacros();
Roberto Raggi's avatar
Roberto Raggi committed
209 210 211

        CppTools::CppModelManagerInterface::ProjectInfo pinfo = modelManager->projectInfo(this);
        pinfo.defines = predefinedMacros;
Roberto Raggi's avatar
Roberto Raggi committed
212
        pinfo.defines += '\n';
213
        pinfo.defines += m_defines;
Roberto Raggi's avatar
Roberto Raggi committed
214

215 216
        QStringList allIncludePaths;
        QStringList allFrameworkPaths;
Roberto Raggi's avatar
Roberto Raggi committed
217

218
        foreach (const ProjectExplorer::HeaderPath &headerPath, m_toolChain->systemHeaderPaths()) {
Roberto Raggi's avatar
Roberto Raggi committed
219 220 221 222 223 224
            if (headerPath.kind() == ProjectExplorer::HeaderPath::FrameworkHeaderPath)
                allFrameworkPaths.append(headerPath.path());
            else
                allIncludePaths.append(headerPath.path());
        }

Roberto Raggi's avatar
Roberto Raggi committed
225
        allIncludePaths += this->allIncludePaths();
Roberto Raggi's avatar
Roberto Raggi committed
226 227 228 229 230

        pinfo.frameworkPaths = allFrameworkPaths;
        pinfo.includePaths = allIncludePaths;

        // ### add _defines.
Roberto Raggi's avatar
Roberto Raggi committed
231 232
        pinfo.sourceFiles = files();
        pinfo.sourceFiles += generated();
Roberto Raggi's avatar
Roberto Raggi committed
233

234 235 236 237 238 239 240 241 242 243 244
        QStringList filesToUpdate;

        if (options & Configuration) {
            filesToUpdate = pinfo.sourceFiles;
            filesToUpdate.append(QLatin1String("<configuration>")); // XXX don't hardcode configuration file name
        } else if (options & Files) {
            // Only update files that got added to the list
            QSet<QString> newFileList = m_files.toSet();
            newFileList.subtract(oldFileList);
            filesToUpdate.append(newFileList.toList());
        }
245

Roberto Raggi's avatar
Roberto Raggi committed
246
        modelManager->updateProjectInfo(pinfo);
247
        modelManager->updateSourceFiles(filesToUpdate);
Roberto Raggi's avatar
Roberto Raggi committed
248 249 250
    }
}

251 252 253 254 255 256
/**
 * The \a map variable is an optional argument that will map the returned
 * absolute paths back to their original \a paths.
 */
QStringList GenericProject::convertToAbsoluteFiles(const QStringList &paths,
                                                   QHash<QString, QString> *map) const
Roberto Raggi's avatar
Roberto Raggi committed
257
{
258
    const QDir projectDir(QFileInfo(m_fileName).dir());
Roberto Raggi's avatar
Roberto Raggi committed
259
    QStringList absolutePaths;
260 261 262 263 264 265 266 267
    foreach (const QString &path, paths) {
        if (path.trimmed().isEmpty())
            continue;

        const QString absPath = QFileInfo(projectDir, path).absoluteFilePath();
        absolutePaths.append(absPath);
        if (map)
            map->insert(absPath, path);
Roberto Raggi's avatar
Roberto Raggi committed
268 269 270 271 272
    }
    absolutePaths.removeDuplicates();
    return absolutePaths;
}

Roberto Raggi's avatar
Roberto Raggi committed
273 274 275
QStringList GenericProject::allIncludePaths() const
{
    QStringList paths;
276 277
    paths += m_includePaths;
    paths += m_projectIncludePaths;
Roberto Raggi's avatar
Roberto Raggi committed
278 279 280 281 282
    paths.removeDuplicates();
    return paths;
}

QStringList GenericProject::projectIncludePaths() const
283
{ return m_projectIncludePaths; }
Roberto Raggi's avatar
Roberto Raggi committed
284

Roberto Raggi's avatar
Roberto Raggi committed
285
QStringList GenericProject::files() const
286
{ return m_files; }
Roberto Raggi's avatar
Roberto Raggi committed
287 288

QStringList GenericProject::generated() const
289
{ return m_generated; }
Roberto Raggi's avatar
Roberto Raggi committed
290 291

QStringList GenericProject::includePaths() const
292
{ return m_includePaths; }
Roberto Raggi's avatar
Roberto Raggi committed
293 294

void GenericProject::setIncludePaths(const QStringList &includePaths)
295
{ m_includePaths = includePaths; }
Roberto Raggi's avatar
Roberto Raggi committed
296

Roberto Raggi's avatar
Roberto Raggi committed
297
QByteArray GenericProject::defines() const
298
{ return m_defines; }
Roberto Raggi's avatar
Roberto Raggi committed
299

300
void GenericProject::setToolChainType(ProjectExplorer::ToolChain::ToolChainType type)
Roberto Raggi's avatar
Roberto Raggi committed
301 302 303
{
    using namespace ProjectExplorer;

304
    m_toolChainType = type;
305

306 307
    delete m_toolChain;
    m_toolChain = 0;
Roberto Raggi's avatar
Roberto Raggi committed
308

309
    if (type == ToolChain::MinGW) {
Roberto Raggi's avatar
Roberto Raggi committed
310 311 312
        const QLatin1String qmake_cxx("g++"); // ### FIXME
        const QString mingwDirectory; // ### FIXME

313
        m_toolChain = ToolChain::createMinGWToolChain(qmake_cxx, mingwDirectory);
Roberto Raggi's avatar
Roberto Raggi committed
314

315
    } else if (type == ToolChain::MSVC) {
Roberto Raggi's avatar
Roberto Raggi committed
316
        const QString msvcVersion; // ### FIXME
317
        m_toolChain = ToolChain::createMSVCToolChain(msvcVersion, false);
Roberto Raggi's avatar
Roberto Raggi committed
318

319
    } else if (type == ToolChain::WINCE) {
Roberto Raggi's avatar
Roberto Raggi committed
320
        const QString msvcVersion, wincePlatform; // ### FIXME
321
        m_toolChain = ToolChain::createWinCEToolChain(msvcVersion, wincePlatform);
Roberto Raggi's avatar
Roberto Raggi committed
322

323
    } else if (type == ToolChain::GCC || type == ToolChain::GCC) {
Roberto Raggi's avatar
Roberto Raggi committed
324
        const QLatin1String qmake_cxx("g++"); // ### FIXME
325
        m_toolChain = ToolChain::createGccToolChain(qmake_cxx);
Roberto Raggi's avatar
Roberto Raggi committed
326 327 328
    }
}

329 330 331 332 333
ProjectExplorer::ToolChain *GenericProject::toolChain() const
{
    return m_toolChain;
}

334 335
ProjectExplorer::ToolChain::ToolChainType GenericProject::toolChainType() const
{ return m_toolChainType; }
336

337
QString GenericProject::displayName() const
Roberto Raggi's avatar
Roberto Raggi committed
338
{
339
    return m_projectName;
Roberto Raggi's avatar
Roberto Raggi committed
340 341
}

Tobias Hunger's avatar
Tobias Hunger committed
342 343 344 345 346
QString GenericProject::id() const
{
    return QLatin1String("GenericProjectManager.GenericProject");
}

Roberto Raggi's avatar
Roberto Raggi committed
347 348
Core::IFile *GenericProject::file() const
{
349
    return m_file;
Roberto Raggi's avatar
Roberto Raggi committed
350 351 352 353
}

ProjectExplorer::IProjectManager *GenericProject::projectManager() const
{
354
    return m_manager;
Roberto Raggi's avatar
Roberto Raggi committed
355 356 357 358 359 360 361 362 363 364 365 366
}

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

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

dt's avatar
dt committed
367
ProjectExplorer::BuildConfigWidget *GenericProject::createConfigWidget()
Roberto Raggi's avatar
Roberto Raggi committed
368 369 370 371
{
    return new GenericBuildSettingsWidget(this);
}

dt's avatar
dt committed
372
QList<ProjectExplorer::BuildConfigWidget*> GenericProject::subConfigWidgets()
Roberto Raggi's avatar
Roberto Raggi committed
373
{
dt's avatar
dt committed
374
    return QList<ProjectExplorer::BuildConfigWidget*>();
Roberto Raggi's avatar
Roberto Raggi committed
375 376
}

377
GenericProjectNode *GenericProject::rootProjectNode() const
Roberto Raggi's avatar
Roberto Raggi committed
378
{
379
    return m_rootNode;
Roberto Raggi's avatar
Roberto Raggi committed
380 381 382 383
}

QStringList GenericProject::files(FilesMode fileMode) const
{
384
    Q_UNUSED(fileMode)
385
    return m_files; // ### TODO: handle generated files here.
Roberto Raggi's avatar
Roberto Raggi committed
386 387
}

Tobias Hunger's avatar
Tobias Hunger committed
388
QStringList GenericProject::buildTargets() const
Roberto Raggi's avatar
Roberto Raggi committed
389 390 391 392 393 394 395
{
    QStringList targets;
    targets.append(QLatin1String("all"));
    targets.append(QLatin1String("clean"));
    return targets;
}

396
QVariantMap GenericProject::toMap() const
Roberto Raggi's avatar
Roberto Raggi committed
397
{
398 399 400 401 402 403 404 405 406
    QVariantMap map(Project::toMap());
    map.insert(QLatin1String(TOOLCHAIN_KEY), static_cast<int>(m_toolChainType));
    return map;
}

bool GenericProject::fromMap(const QVariantMap &map)
{
    if (!Project::fromMap(map))
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
407

408
    // Add default BC:
Tobias Hunger's avatar
Tobias Hunger committed
409 410
    if (targets().isEmpty())
        addTarget(targetFactory()->create(this, QLatin1String(GENERIC_DESKTOP_TARGET_ID)));
411

412 413 414
    ToolChain::ToolChainType type =
            static_cast<ProjectExplorer::ToolChain::ToolChainType>
            (map.value(QLatin1String(TOOLCHAIN_KEY), 0).toInt());
Roberto Raggi's avatar
Roberto Raggi committed
415

416
    setToolChainType(type);
Roberto Raggi's avatar
Roberto Raggi committed
417 418

    setIncludePaths(allIncludePaths());
419

420
    refresh(Everything);
dt's avatar
dt committed
421
    return true;
422 423
}

Roberto Raggi's avatar
Roberto Raggi committed
424 425 426
////////////////////////////////////////////////////////////////////////////////////
// GenericBuildSettingsWidget
////////////////////////////////////////////////////////////////////////////////////
427

Roberto Raggi's avatar
Roberto Raggi committed
428
GenericBuildSettingsWidget::GenericBuildSettingsWidget(GenericProject *project)
dt's avatar
dt committed
429
    : m_project(project), m_buildConfiguration(0)
Roberto Raggi's avatar
Roberto Raggi committed
430 431
{
    QFormLayout *fl = new QFormLayout(this);
432 433
    fl->setContentsMargins(0, -1, 0, -1);
    fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
434 435

    // build directory
436
    m_pathChooser = new Utils::PathChooser(this);
437 438
    m_pathChooser->setEnabled(true);
    fl->addRow(tr("Build directory:"), m_pathChooser);
con's avatar
con committed
439
    connect(m_pathChooser, SIGNAL(changed(QString)), this, SLOT(buildDirectoryChanged()));
440

441
    // tool chain
442
    QComboBox *toolChainChooser = new QComboBox;
443
    toolChainChooser->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
444
    using namespace ProjectExplorer;
445 446
    int index = 0;
    int selectedIndex = -1;
447
    foreach (ToolChain::ToolChainType tc, ToolChain::supportedToolChains()) {
448 449 450 451
        toolChainChooser->addItem(ToolChain::toolChainName(tc), QVariant::fromValue<ToolChain::ToolChainType>(tc));
        if (m_project->toolChainType() == tc)
            selectedIndex = index;
        ++index;
452 453
    }

454
    toolChainChooser->setCurrentIndex(selectedIndex);
455
    fl->addRow(tr("Tool Chain:"), toolChainChooser);
456
    connect(toolChainChooser, SIGNAL(activated(int)), this, SLOT(toolChainSelected(int)));
Roberto Raggi's avatar
Roberto Raggi committed
457 458 459 460 461 462 463 464
}

GenericBuildSettingsWidget::~GenericBuildSettingsWidget()
{ }

QString GenericBuildSettingsWidget::displayName() const
{ return tr("Generic Manager"); }

dt's avatar
dt committed
465
void GenericBuildSettingsWidget::init(BuildConfiguration *bc)
Roberto Raggi's avatar
Roberto Raggi committed
466
{
467 468
    m_buildConfiguration = static_cast<GenericBuildConfiguration *>(bc);
    m_pathChooser->setPath(m_buildConfiguration->buildDirectory());
Roberto Raggi's avatar
Roberto Raggi committed
469 470 471 472
}

void GenericBuildSettingsWidget::buildDirectoryChanged()
{
473
    m_buildConfiguration->setBuildDirectory(m_pathChooser->path());
Roberto Raggi's avatar
Roberto Raggi committed
474 475
}

476 477 478 479 480 481 482 483 484
void GenericBuildSettingsWidget::toolChainSelected(int index)
{
    using namespace ProjectExplorer;

    QComboBox *toolChainChooser = qobject_cast<QComboBox*>(sender());
    ToolChain::ToolChainType type = toolChainChooser->itemData(index).value<ToolChain::ToolChainType>();
    m_project->setToolChainType(type);
}

Roberto Raggi's avatar
Roberto Raggi committed
485 486 487
////////////////////////////////////////////////////////////////////////////////////
// GenericProjectFile
////////////////////////////////////////////////////////////////////////////////////
488

Roberto Raggi's avatar
Roberto Raggi committed
489 490
GenericProjectFile::GenericProjectFile(GenericProject *parent, QString fileName)
    : Core::IFile(parent),
491 492
      m_project(parent),
      m_fileName(fileName)
Roberto Raggi's avatar
Roberto Raggi committed
493 494 495 496 497 498 499 500 501 502 503 504
{ }

GenericProjectFile::~GenericProjectFile()
{ }

bool GenericProjectFile::save(const QString &)
{
    return false;
}

QString GenericProjectFile::fileName() const
{
505
    return m_fileName;
Roberto Raggi's avatar
Roberto Raggi committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
}

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

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

QString GenericProjectFile::mimeType() const
{
    return Constants::GENERICMIMETYPE;
}

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

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

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

void GenericProjectFile::modified(ReloadBehavior *)
{
}