genericproject.cpp 14.7 KB
Newer Older
Roberto Raggi's avatar
Roberto Raggi committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
Roberto Raggi's avatar
Roberto Raggi committed
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
Roberto Raggi's avatar
Roberto Raggi committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
Roberto Raggi's avatar
Roberto Raggi committed
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
Roberto Raggi's avatar
Roberto Raggi committed
28 29 30 31
**
**************************************************************************/

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

dt's avatar
dt committed
33
#include "genericbuildconfiguration.h"
Tobias Hunger's avatar
Tobias Hunger committed
34
#include "genericmakestep.h"
hjk's avatar
hjk committed
35
#include "genericprojectconstants.h"
Roberto Raggi's avatar
Roberto Raggi committed
36

hjk's avatar
hjk committed
37 38 39 40 41
#include <coreplugin/documentmanager.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <cpptools/ModelManagerInterface.h>
#include <extensionsystem/pluginmanager.h>
42
#include <projectexplorer/abi.h>
43
#include <projectexplorer/buildenvironmentwidget.h>
Tobias Hunger's avatar
Tobias Hunger committed
44
#include <projectexplorer/buildsteplist.h>
45
#include <projectexplorer/headerpath.h>
Tobias Hunger's avatar
Tobias Hunger committed
46 47
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/kitmanager.h>
Roberto Raggi's avatar
Roberto Raggi committed
48
#include <projectexplorer/projectexplorerconstants.h>
49
#include <qtsupport/customexecutablerunconfiguration.h>
50
#include <utils/fileutils.h>
hjk's avatar
hjk committed
51
#include <utils/qtcassert.h>
Roberto Raggi's avatar
Roberto Raggi committed
52

53 54
#include <QDir>
#include <QProcessEnvironment>
Roberto Raggi's avatar
Roberto Raggi committed
55

hjk's avatar
hjk committed
56
using namespace Core;
57
using namespace ProjectExplorer;
Roberto Raggi's avatar
Roberto Raggi committed
58

hjk's avatar
hjk committed
59 60 61
namespace GenericProjectManager {
namespace Internal {

62
////////////////////////////////////////////////////////////////////////////////////
hjk's avatar
hjk committed
63
//
64
// GenericProject
hjk's avatar
hjk committed
65
//
66 67
////////////////////////////////////////////////////////////////////////////////////

Roberto Raggi's avatar
Roberto Raggi committed
68
GenericProject::GenericProject(Manager *manager, const QString &fileName)
69
    : m_manager(manager),
Tobias Hunger's avatar
Tobias Hunger committed
70
      m_fileName(fileName)
Roberto Raggi's avatar
Roberto Raggi committed
71
{
hjk's avatar
hjk committed
72 73
    setProjectContext(Context(GenericProjectManager::Constants::PROJECTCONTEXT));
    setProjectLanguage(Context(ProjectExplorer::Constants::LANG_CXX));
74

75
    QFileInfo fileInfo(m_fileName);
76 77
    QDir dir = fileInfo.dir();

78
    m_projectName      = fileInfo.completeBaseName();
79 80 81
    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();
82

83 84 85 86
    m_creatorIDocument  = new GenericProjectFile(this, m_fileName, GenericProject::Everything);
    m_filesIDocument    = new GenericProjectFile(this, m_filesFileName, GenericProject::Files);
    m_includesIDocument = new GenericProjectFile(this, m_includesFileName, GenericProject::Configuration);
    m_configIDocument   = new GenericProjectFile(this, m_configFileName, GenericProject::Configuration);
87

hjk's avatar
hjk committed
88 89 90 91
    DocumentManager::addDocument(m_creatorIDocument);
    DocumentManager::addDocument(m_filesIDocument);
    DocumentManager::addDocument(m_includesIDocument);
    DocumentManager::addDocument(m_configIDocument);
92

93
    m_rootNode = new GenericProjectNode(this, m_creatorIDocument);
94

95
    m_manager->registerProject(this);
Roberto Raggi's avatar
Roberto Raggi committed
96 97 98 99
}

GenericProject::~GenericProject()
{
100
    m_codeModelFuture.cancel();
101
    m_manager->unregisterProject(this);
102

103
    delete m_rootNode;
104 105
}

106
QString GenericProject::filesFileName() const
hjk's avatar
hjk committed
107 108 109
{
    return m_filesFileName;
}
110 111

QString GenericProject::includesFileName() const
hjk's avatar
hjk committed
112 113 114
{
    return m_includesFileName;
}
115 116

QString GenericProject::configFileName() const
hjk's avatar
hjk committed
117 118 119
{
    return m_configFileName;
}
120

121
static QStringList readLines(const QString &absoluteFileName)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
    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;
}

141
bool GenericProject::saveRawFileList(const QStringList &rawFileList)
142
{
143
    // Make sure we can open the file for writing
144 145 146 147 148 149 150
    Utils::FileSaver saver(filesFileName(), QIODevice::Text);
    if (!saver.hasError()) {
        QTextStream stream(saver.file());
        foreach (const QString &filePath, rawFileList)
            stream << filePath << QLatin1Char('\n');
        saver.setResult(&stream);
    }
hjk's avatar
hjk committed
151
    if (!saver.finalize(ICore::mainWindow()))
152 153 154
        return false;
    refresh(GenericProject::Files);
    return true;
155
}
156

157 158
bool GenericProject::addFiles(const QStringList &filePaths)
{
159 160 161 162 163
    QStringList newList = m_rawFileList;

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

165
    return saveRawFileList(newList);
166 167 168 169
}

bool GenericProject::removeFiles(const QStringList &filePaths)
{
170
    QStringList newList = m_rawFileList;
171

172 173 174 175
    foreach (const QString &filePath, filePaths) {
        QHash<QString, QString>::iterator i = m_rawListEntries.find(filePath);
        if (i != m_rawListEntries.end())
            newList.removeOne(i.value());
176
    }
177

178
    return saveRawFileList(newList);
179 180
}

181 182 183 184 185 186 187 188 189 190
bool GenericProject::setFiles(const QStringList &filePaths)
{
    QStringList newList;
    QDir baseDir(QFileInfo(m_fileName).dir());
    foreach (const QString &filePath, filePaths)
        newList.append(baseDir.relativeFilePath(filePath));

    return saveRawFileList(newList);
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
bool GenericProject::renameFile(const QString &filePath, const QString &newFilePath)
{
    QStringList newList = m_rawFileList;

    QHash<QString, QString>::iterator i = m_rawListEntries.find(filePath);
    if (i != m_rawListEntries.end()) {
        int index = newList.indexOf(i.value());
        if (index != -1) {
            QDir baseDir(QFileInfo(m_fileName).dir());
            newList.replace(index, baseDir.relativeFilePath(newFilePath));
        }
    }

    return saveRawFileList(newList);
}

207
void GenericProject::parseProject(RefreshOptions options)
Roberto Raggi's avatar
Roberto Raggi committed
208
{
209 210 211
    if (options & Files) {
        m_rawListEntries.clear();
        m_rawFileList = readLines(filesFileName());
212
        m_files = processEntries(m_rawFileList, &m_rawListEntries);
213
    }
214

215
    if (options & Configuration) {
216
        m_projectIncludePaths = processEntries(readLines(includesFileName()));
217

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

221
        m_defines.clear();
Roberto Raggi's avatar
Roberto Raggi committed
222

223 224 225 226
        QFile configFile(configFileName());
        if (configFile.open(QFile::ReadOnly))
            m_defines = configFile.readAll();
    }
227

228 229
    if (options & Files)
        emit fileListChanged();
Roberto Raggi's avatar
Roberto Raggi committed
230 231
}

232
void GenericProject::refresh(RefreshOptions options)
Roberto Raggi's avatar
Roberto Raggi committed
233
{
234 235 236
    QSet<QString> oldFileList;
    if (!(options & Configuration))
        oldFileList = m_files.toSet();
237

238 239 240 241
    parseProject(options);

    if (options & Files)
        m_rootNode->refresh();
Roberto Raggi's avatar
Roberto Raggi committed
242

243
    CPlusPlus::CppModelManagerInterface *modelManager =
244
        CPlusPlus::CppModelManagerInterface::instance();
Roberto Raggi's avatar
Roberto Raggi committed
245

246
    if (modelManager) {
247
        CPlusPlus::CppModelManagerInterface::ProjectInfo pinfo = modelManager->projectInfo(this);
248 249 250
        pinfo.clearProjectParts();
        CPlusPlus::CppModelManagerInterface::ProjectPart::Ptr part(
                    new CPlusPlus::CppModelManagerInterface::ProjectPart);
Roberto Raggi's avatar
Roberto Raggi committed
251

252 253
        Kit *k = activeTarget() ? activeTarget()->kit() : KitManager::instance()->defaultKit();
        ToolChain *tc = k ? ToolChainKitInformation::toolChain(k) : 0;
Tobias Hunger's avatar
Tobias Hunger committed
254 255
        if (tc) {
            part->defines = tc->predefinedMacros(QStringList());
256
            part->defines += '\n';
Roberto Raggi's avatar
Roberto Raggi committed
257

258
            foreach (const HeaderPath &headerPath, tc->systemHeaderPaths(SysRootKitInformation::sysRoot(k))) {
259
                if (headerPath.kind() == HeaderPath::FrameworkHeaderPath)
260
                    part->frameworkPaths.append(headerPath.path());
261
                else
262
                    part->includePaths.append(headerPath.path());
263
            }
Roberto Raggi's avatar
Roberto Raggi committed
264 265
        }

266 267
        part->includePaths += allIncludePaths();
        part->defines += m_defines;
Roberto Raggi's avatar
Roberto Raggi committed
268 269

        // ### add _defines.
270 271
        part->sourceFiles = files();
        part->sourceFiles += generated();
Roberto Raggi's avatar
Roberto Raggi committed
272

273 274 275
        QStringList filesToUpdate;

        if (options & Configuration) {
276
            filesToUpdate = part->sourceFiles;
277
            filesToUpdate.append(QLatin1String("<configuration>")); // XXX don't hardcode configuration file name
278 279
            // Full update, if there's a code model update, cancel it
            m_codeModelFuture.cancel();
280 281 282 283 284 285
        } 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());
        }
286

287 288
        pinfo.appendProjectPart(part);

Roberto Raggi's avatar
Roberto Raggi committed
289
        modelManager->updateProjectInfo(pinfo);
290
        m_codeModelFuture = modelManager->updateSourceFiles(filesToUpdate);
Roberto Raggi's avatar
Roberto Raggi committed
291 292 293
    }
}

294
/**
295 296 297 298 299
 * Expands environment variables in the given \a string when they are written
 * like $$(VARIABLE).
 */
static void expandEnvironmentVariables(const QProcessEnvironment &env, QString &string)
{
300
    static QRegExp candidate(QLatin1String("\\$\\$\\((.+)\\)"));
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

    int index = candidate.indexIn(string);
    while (index != -1) {
        const QString value = env.value(candidate.cap(1));

        string.replace(index, candidate.matchedLength(), value);
        index += value.length();

        index = candidate.indexIn(string, index);
    }
}

/**
 * Expands environment variables and converts the path from relative to the
 * project to an absolute path.
 *
317
 * The \a map variable is an optional argument that will map the returned
318
 * absolute paths back to their original \a entries.
319
 */
320 321
QStringList GenericProject::processEntries(const QStringList &paths,
                                           QHash<QString, QString> *map) const
Roberto Raggi's avatar
Roberto Raggi committed
322
{
323
    const QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
324
    const QDir projectDir(QFileInfo(m_fileName).dir());
325

Roberto Raggi's avatar
Roberto Raggi committed
326
    QStringList absolutePaths;
327
    foreach (const QString &path, paths) {
328 329
        QString trimmedPath = path.trimmed();
        if (trimmedPath.isEmpty())
330 331
            continue;

332 333 334
        expandEnvironmentVariables(env, trimmedPath);

        const QString absPath = QFileInfo(projectDir, trimmedPath).absoluteFilePath();
335 336
        absolutePaths.append(absPath);
        if (map)
337
            map->insert(absPath, trimmedPath);
Roberto Raggi's avatar
Roberto Raggi committed
338 339 340 341 342
    }
    absolutePaths.removeDuplicates();
    return absolutePaths;
}

Roberto Raggi's avatar
Roberto Raggi committed
343 344 345
QStringList GenericProject::allIncludePaths() const
{
    QStringList paths;
346 347
    paths += m_includePaths;
    paths += m_projectIncludePaths;
Roberto Raggi's avatar
Roberto Raggi committed
348 349 350 351 352
    paths.removeDuplicates();
    return paths;
}

QStringList GenericProject::projectIncludePaths() const
hjk's avatar
hjk committed
353 354 355
{
    return m_projectIncludePaths;
}
Roberto Raggi's avatar
Roberto Raggi committed
356

Roberto Raggi's avatar
Roberto Raggi committed
357
QStringList GenericProject::files() const
hjk's avatar
hjk committed
358 359 360
{
    return m_files;
}
Roberto Raggi's avatar
Roberto Raggi committed
361 362

QStringList GenericProject::generated() const
hjk's avatar
hjk committed
363 364 365
{
    return m_generated;
}
Roberto Raggi's avatar
Roberto Raggi committed
366 367

QStringList GenericProject::includePaths() const
hjk's avatar
hjk committed
368 369 370
{
    return m_includePaths;
}
Roberto Raggi's avatar
Roberto Raggi committed
371 372

void GenericProject::setIncludePaths(const QStringList &includePaths)
hjk's avatar
hjk committed
373 374 375
{
    m_includePaths = includePaths;
}
Roberto Raggi's avatar
Roberto Raggi committed
376

Roberto Raggi's avatar
Roberto Raggi committed
377
QByteArray GenericProject::defines() const
hjk's avatar
hjk committed
378 379 380
{
    return m_defines;
}
Roberto Raggi's avatar
Roberto Raggi committed
381

382
QString GenericProject::displayName() const
Roberto Raggi's avatar
Roberto Raggi committed
383
{
384
    return m_projectName;
Roberto Raggi's avatar
Roberto Raggi committed
385 386
}

hjk's avatar
hjk committed
387
Id GenericProject::id() const
Tobias Hunger's avatar
Tobias Hunger committed
388
{
hjk's avatar
hjk committed
389
    return Id(Constants::GENERICPROJECT_ID);
Tobias Hunger's avatar
Tobias Hunger committed
390 391
}

hjk's avatar
hjk committed
392
IDocument *GenericProject::document() const
Roberto Raggi's avatar
Roberto Raggi committed
393
{
394
    return m_creatorIDocument;
Roberto Raggi's avatar
Roberto Raggi committed
395 396
}

397
IProjectManager *GenericProject::projectManager() const
Roberto Raggi's avatar
Roberto Raggi committed
398
{
399
    return m_manager;
Roberto Raggi's avatar
Roberto Raggi committed
400 401
}

402
QList<BuildConfigWidget*> GenericProject::subConfigWidgets()
Roberto Raggi's avatar
Roberto Raggi committed
403
{
404
    QList<BuildConfigWidget*> list;
405 406
    list << new BuildEnvironmentWidget;
    return list;
Roberto Raggi's avatar
Roberto Raggi committed
407 408
}

409
GenericProjectNode *GenericProject::rootProjectNode() const
Roberto Raggi's avatar
Roberto Raggi committed
410
{
411
    return m_rootNode;
Roberto Raggi's avatar
Roberto Raggi committed
412 413 414 415
}

QStringList GenericProject::files(FilesMode fileMode) const
{
416
    Q_UNUSED(fileMode)
417
    return m_files; // ### TODO: handle generated files here.
Roberto Raggi's avatar
Roberto Raggi committed
418 419
}

Tobias Hunger's avatar
Tobias Hunger committed
420
QStringList GenericProject::buildTargets() const
Roberto Raggi's avatar
Roberto Raggi committed
421 422 423 424 425 426 427
{
    QStringList targets;
    targets.append(QLatin1String("all"));
    targets.append(QLatin1String("clean"));
    return targets;
}

428 429 430 431
bool GenericProject::fromMap(const QVariantMap &map)
{
    if (!Project::fromMap(map))
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
432

Tobias Hunger's avatar
Tobias Hunger committed
433 434 435
    Kit *defaultKit = KitManager::instance()->defaultKit();
    if (!activeTarget() && defaultKit)
        addTarget(createTarget(defaultKit));
Tobias Hunger's avatar
Tobias Hunger committed
436

437 438 439 440 441 442 443 444 445
    // Sanity check: We need both a buildconfiguration and a runconfiguration!
    QList<Target *> targetList = targets();
    foreach (Target *t, targetList) {
        if (!t->activeBuildConfiguration()) {
            removeTarget(t);
            delete t;
            continue;
        }
        if (!t->activeRunConfiguration())
446
            t->addRunConfiguration(new QtSupport::CustomExecutableRunConfiguration(t));
447 448
    }

Roberto Raggi's avatar
Roberto Raggi committed
449
    setIncludePaths(allIncludePaths());
450

Tobias Hunger's avatar
Tobias Hunger committed
451
    refresh(Everything);
452
    return true;
453 454
}

Roberto Raggi's avatar
Roberto Raggi committed
455
////////////////////////////////////////////////////////////////////////////////////
hjk's avatar
hjk committed
456
//
Roberto Raggi's avatar
Roberto Raggi committed
457
// GenericProjectFile
hjk's avatar
hjk committed
458
//
Roberto Raggi's avatar
Roberto Raggi committed
459
////////////////////////////////////////////////////////////////////////////////////
460

461
GenericProjectFile::GenericProjectFile(GenericProject *parent, QString fileName, GenericProject::RefreshOptions options)
hjk's avatar
hjk committed
462
    : IDocument(parent),
463
      m_project(parent),
464 465
      m_fileName(fileName),
      m_options(options)
Roberto Raggi's avatar
Roberto Raggi committed
466 467
{ }

468
bool GenericProjectFile::save(QString *, const QString &, bool)
Roberto Raggi's avatar
Roberto Raggi committed
469 470 471 472 473 474
{
    return false;
}

QString GenericProjectFile::fileName() const
{
475
    return m_fileName;
Roberto Raggi's avatar
Roberto Raggi committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
}

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::isSaveAsAllowed() const
{
    return false;
}

dt's avatar
dt committed
503 504 505 506
void GenericProjectFile::rename(const QString &newName)
{
    // Can't happen
    Q_UNUSED(newName);
507
    QTC_CHECK(false);
dt's avatar
dt committed
508 509
}

hjk's avatar
hjk committed
510
IDocument::ReloadBehavior GenericProjectFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
Roberto Raggi's avatar
Roberto Raggi committed
511
{
512 513 514 515 516
    Q_UNUSED(state)
    Q_UNUSED(type)
    return BehaviorSilent;
}

517
bool GenericProjectFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
518
{
519
    Q_UNUSED(errorString)
520
    Q_UNUSED(flag)
521 522 523
    if (type == TypePermissions)
        return true;
    m_project->refresh(m_options);
524
    return true;
Roberto Raggi's avatar
Roberto Raggi committed
525
}
hjk's avatar
hjk committed
526 527 528

} // namespace Internal
} // namespace GenericProjectManager