genericproject.cpp 14.9 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
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
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.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
Roberto Raggi's avatar
Roberto Raggi committed
30 31 32 33
**
**************************************************************************/

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

dt's avatar
dt committed
35
#include "genericbuildconfiguration.h"
Tobias Hunger's avatar
Tobias Hunger committed
36
#include "genericprojectconstants.h"
Tobias Hunger's avatar
Tobias Hunger committed
37 38

#include "genericmakestep.h"
Roberto Raggi's avatar
Roberto Raggi committed
39

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

56 57
#include <QDir>
#include <QProcessEnvironment>
Roberto Raggi's avatar
Roberto Raggi committed
58

59 60
#include <QMainWindow>
#include <QComboBox>
Roberto Raggi's avatar
Roberto Raggi committed
61 62 63

using namespace GenericProjectManager;
using namespace GenericProjectManager::Internal;
64
using namespace ProjectExplorer;
Roberto Raggi's avatar
Roberto Raggi committed
65

66 67 68 69
////////////////////////////////////////////////////////////////////////////////////
// GenericProject
////////////////////////////////////////////////////////////////////////////////////

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

77
    QFileInfo fileInfo(m_fileName);
78 79
    QDir dir = fileInfo.dir();

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

85 86 87 88
    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);
89

90 91 92 93
    Core::DocumentManager::addDocument(m_creatorIDocument);
    Core::DocumentManager::addDocument(m_filesIDocument);
    Core::DocumentManager::addDocument(m_includesIDocument);
    Core::DocumentManager::addDocument(m_configIDocument);
94

95
    m_rootNode = new GenericProjectNode(this, m_creatorIDocument);
96

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

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

105
    delete m_rootNode;
106 107
}

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

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

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

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

143
bool GenericProject::saveRawFileList(const QStringList &rawFileList)
144
{
145
    // Make sure we can open the file for writing
146 147 148 149 150 151 152
    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
153
    if (!saver.finalize(Core::ICore::mainWindow()))
154 155 156
        return false;
    refresh(GenericProject::Files);
    return true;
157
}
158

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

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

167
    return saveRawFileList(newList);
168 169 170 171
}

bool GenericProject::removeFiles(const QStringList &filePaths)
{
172
    QStringList newList = m_rawFileList;
173

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

180
    return saveRawFileList(newList);
181 182
}

183 184 185 186 187 188 189 190 191 192
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);
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
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);
}

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

217
    if (options & Configuration) {
218
        m_projectIncludePaths = processEntries(readLines(includesFileName()));
219

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

223
        m_defines.clear();
Roberto Raggi's avatar
Roberto Raggi committed
224

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

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

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

240 241 242 243
    parseProject(options);

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

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

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

Tobias Hunger's avatar
Tobias Hunger committed
254 255 256 257
        ToolChain *tc = activeTarget() ?
                    ProjectExplorer::ToolChainProfileInformation::toolChain(activeTarget()->profile()) : 0;
        if (tc) {
            part->defines = tc->predefinedMacros(QStringList());
258
            part->defines += '\n';
Roberto Raggi's avatar
Roberto Raggi committed
259

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

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

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

275 276 277
        QStringList filesToUpdate;

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

289 290
        pinfo.appendProjectPart(part);

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

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

    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.
 *
319
 * The \a map variable is an optional argument that will map the returned
320
 * absolute paths back to their original \a entries.
321
 */
322 323
QStringList GenericProject::processEntries(const QStringList &paths,
                                           QHash<QString, QString> *map) const
Roberto Raggi's avatar
Roberto Raggi committed
324
{
325
    const QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
326
    const QDir projectDir(QFileInfo(m_fileName).dir());
327

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

334 335 336
        expandEnvironmentVariables(env, trimmedPath);

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

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

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

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

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

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

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

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

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

389
Core::Id GenericProject::id() const
Tobias Hunger's avatar
Tobias Hunger committed
390
{
391
    return Core::Id(Constants::GENERICPROJECT_ID);
Tobias Hunger's avatar
Tobias Hunger committed
392 393
}

394
Core::IDocument *GenericProject::document() const
Roberto Raggi's avatar
Roberto Raggi committed
395
{
396
    return m_creatorIDocument;
Roberto Raggi's avatar
Roberto Raggi committed
397 398
}

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

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

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

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

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

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

Tobias Hunger's avatar
Tobias Hunger committed
435 436 437
    if (!activeTarget())
        addTarget(createTarget(ProfileManager::instance()->defaultProfile()));

438 439 440 441 442 443 444 445 446
    // 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())
447
            t->addRunConfiguration(new QtSupport::CustomExecutableRunConfiguration(t));
448 449
    }

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

Tobias Hunger's avatar
Tobias Hunger committed
452
    evaluateBuildSystem();
dt's avatar
dt committed
453
    return true;
454 455
}

Tobias Hunger's avatar
Tobias Hunger committed
456
void GenericProject::evaluateBuildSystem()
457
{
Tobias Hunger's avatar
Tobias Hunger committed
458 459
    refresh(Everything);
    buildSystemEvaluationFinished(true);
460 461
}

Roberto Raggi's avatar
Roberto Raggi committed
462 463 464
////////////////////////////////////////////////////////////////////////////////////
// GenericProjectFile
////////////////////////////////////////////////////////////////////////////////////
465

466
GenericProjectFile::GenericProjectFile(GenericProject *parent, QString fileName, GenericProject::RefreshOptions options)
467
    : Core::IDocument(parent),
468
      m_project(parent),
469 470
      m_fileName(fileName),
      m_options(options)
Roberto Raggi's avatar
Roberto Raggi committed
471 472 473 474 475
{ }

GenericProjectFile::~GenericProjectFile()
{ }

476
bool GenericProjectFile::save(QString *, const QString &, bool)
Roberto Raggi's avatar
Roberto Raggi committed
477 478 479 480 481 482
{
    return false;
}

QString GenericProjectFile::fileName() const
{
483
    return m_fileName;
Roberto Raggi's avatar
Roberto Raggi committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
}

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
511 512 513 514
void GenericProjectFile::rename(const QString &newName)
{
    // Can't happen
    Q_UNUSED(newName);
515
    QTC_CHECK(false);
dt's avatar
dt committed
516 517
}

518
Core::IDocument::ReloadBehavior GenericProjectFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
Roberto Raggi's avatar
Roberto Raggi committed
519
{
520 521 522 523 524
    Q_UNUSED(state)
    Q_UNUSED(type)
    return BehaviorSilent;
}

525
bool GenericProjectFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
526
{
527
    Q_UNUSED(errorString)
528
    Q_UNUSED(flag)
529 530 531
    if (type == TypePermissions)
        return true;
    m_project->refresh(m_options);
532
    return true;
Roberto Raggi's avatar
Roberto Raggi committed
533
}