genericproject.cpp 15 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Roberto Raggi's avatar
Roberto Raggi committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Roberto Raggi's avatar
Roberto Raggi committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Roberto Raggi's avatar
Roberto Raggi committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Roberto Raggi's avatar
Roberto Raggi committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Roberto Raggi's avatar
Roberto Raggi committed
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
#include "genericmakestep.h"
hjk's avatar
hjk committed
34
#include "genericprojectconstants.h"
Roberto Raggi's avatar
Roberto Raggi committed
35

hjk's avatar
hjk committed
36 37 38
#include <coreplugin/documentmanager.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
39 40
#include <coreplugin/mimedatabase.h>
#include <cpptools/cpptoolsconstants.h>
hjk's avatar
hjk committed
41 42
#include <cpptools/ModelManagerInterface.h>
#include <extensionsystem/pluginmanager.h>
43
#include <projectexplorer/abi.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
    setProjectContext(Context(GenericProjectManager::Constants::PROJECTCONTEXT));
73
    setProjectLanguages(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
    parseProject(options);

    if (options & Files)
241
        m_rootNode->refresh(oldFileList);
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
        pinfo.clearProjectParts();
249
        CPlusPlus::ProjectPart::Ptr part(new CPlusPlus::ProjectPart);
Roberto Raggi's avatar
Roberto Raggi committed
250

251
        Kit *k = activeTarget() ? activeTarget()->kit() : KitManager::instance()->defaultKit();
252
        if (ToolChain *tc = ToolChainKitInformation::toolChain(k)) {
253 254
            QStringList cxxflags; // FIXME: Can we do better?
            part->defines = tc->predefinedMacros(cxxflags);
255
            part->defines += '\n';
Roberto Raggi's avatar
Roberto Raggi committed
256

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

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

        // ### add _defines.
269 270

        // Add any C/C++ files to be parsed
271 272 273
        CPlusPlus::ProjectFileAdder adder(part->files);
        foreach (const QString &file, files())
            adder.maybeAdd(file);
Roberto Raggi's avatar
Roberto Raggi committed
274

275 276 277
        QStringList filesToUpdate;

        if (options & Configuration) {
278 279
            foreach (const CPlusPlus::ProjectFile &file, part->files)
                filesToUpdate << file.path;
280
            filesToUpdate.append(CPlusPlus::CppModelManagerInterface::configurationFileName());
281 282
            // Full update, if there's a code model update, cancel it
            m_codeModelFuture.cancel();
283 284
        } else if (options & Files) {
            // Only update files that got added to the list
285 286 287
            QSet<QString> newFileList;
            foreach (const CPlusPlus::ProjectFile &file, part->files)
                newFileList.insert(file.path);
288 289 290
            newFileList.subtract(oldFileList);
            filesToUpdate.append(newFileList.toList());
        }
291

292 293
        pinfo.appendProjectPart(part);

Roberto Raggi's avatar
Roberto Raggi committed
294
        modelManager->updateProjectInfo(pinfo);
295
        m_codeModelFuture = modelManager->updateSourceFiles(filesToUpdate);
Roberto Raggi's avatar
Roberto Raggi committed
296 297 298
    }
}

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

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

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

337 338
        expandEnvironmentVariables(env, trimmedPath);

339 340
        trimmedPath = Utils::FileName::fromUserInput(trimmedPath).toString();

341
        const QString absPath = QFileInfo(projectDir, trimmedPath).absoluteFilePath();
342 343
        absolutePaths.append(absPath);
        if (map)
344
            map->insert(absPath, trimmedPath);
Roberto Raggi's avatar
Roberto Raggi committed
345 346 347 348 349
    }
    absolutePaths.removeDuplicates();
    return absolutePaths;
}

Roberto Raggi's avatar
Roberto Raggi committed
350 351 352
QStringList GenericProject::allIncludePaths() const
{
    QStringList paths;
353 354
    paths += m_includePaths;
    paths += m_projectIncludePaths;
Roberto Raggi's avatar
Roberto Raggi committed
355 356 357 358 359
    paths.removeDuplicates();
    return paths;
}

QStringList GenericProject::projectIncludePaths() const
hjk's avatar
hjk committed
360 361 362
{
    return m_projectIncludePaths;
}
Roberto Raggi's avatar
Roberto Raggi committed
363

Roberto Raggi's avatar
Roberto Raggi committed
364
QStringList GenericProject::files() const
hjk's avatar
hjk committed
365 366 367
{
    return m_files;
}
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
}

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

hjk's avatar
hjk committed
394
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
GenericProjectNode *GenericProject::rootProjectNode() const
Roberto Raggi's avatar
Roberto Raggi committed
405
{
406
    return m_rootNode;
Roberto Raggi's avatar
Roberto Raggi committed
407 408 409 410
}

QStringList GenericProject::files(FilesMode fileMode) const
{
411
    Q_UNUSED(fileMode)
412
    return m_files;
Roberto Raggi's avatar
Roberto Raggi committed
413 414
}

Tobias Hunger's avatar
Tobias Hunger committed
415
QStringList GenericProject::buildTargets() const
Roberto Raggi's avatar
Roberto Raggi committed
416 417 418 419 420 421 422
{
    QStringList targets;
    targets.append(QLatin1String("all"));
    targets.append(QLatin1String("clean"));
    return targets;
}

423 424 425 426
bool GenericProject::fromMap(const QVariantMap &map)
{
    if (!Project::fromMap(map))
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
427

Tobias Hunger's avatar
Tobias Hunger committed
428 429 430
    Kit *defaultKit = KitManager::instance()->defaultKit();
    if (!activeTarget() && defaultKit)
        addTarget(createTarget(defaultKit));
Tobias Hunger's avatar
Tobias Hunger committed
431

432 433 434 435 436 437 438 439 440
    // 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())
441
            t->addRunConfiguration(new QtSupport::CustomExecutableRunConfiguration(t));
442 443
    }

Roberto Raggi's avatar
Roberto Raggi committed
444
    setIncludePaths(allIncludePaths());
445

Tobias Hunger's avatar
Tobias Hunger committed
446
    refresh(Everything);
447
    return true;
448 449
}

Roberto Raggi's avatar
Roberto Raggi committed
450
////////////////////////////////////////////////////////////////////////////////////
hjk's avatar
hjk committed
451
//
Roberto Raggi's avatar
Roberto Raggi committed
452
// GenericProjectFile
hjk's avatar
hjk committed
453
//
Roberto Raggi's avatar
Roberto Raggi committed
454
////////////////////////////////////////////////////////////////////////////////////
455

456
GenericProjectFile::GenericProjectFile(GenericProject *parent, QString fileName, GenericProject::RefreshOptions options)
hjk's avatar
hjk committed
457
    : IDocument(parent),
458
      m_project(parent),
459 460
      m_fileName(fileName),
      m_options(options)
Roberto Raggi's avatar
Roberto Raggi committed
461 462
{ }

463
bool GenericProjectFile::save(QString *, const QString &, bool)
Roberto Raggi's avatar
Roberto Raggi committed
464 465 466 467 468 469
{
    return false;
}

QString GenericProjectFile::fileName() const
{
470
    return m_fileName;
Roberto Raggi's avatar
Roberto Raggi committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484
}

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

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

QString GenericProjectFile::mimeType() const
{
485
    return QLatin1String(Constants::GENERICMIMETYPE);
Roberto Raggi's avatar
Roberto Raggi committed
486 487 488 489 490 491 492 493 494 495 496 497
}

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

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

dt's avatar
dt committed
498 499 500 501
void GenericProjectFile::rename(const QString &newName)
{
    // Can't happen
    Q_UNUSED(newName);
502
    QTC_CHECK(false);
dt's avatar
dt committed
503 504
}

hjk's avatar
hjk committed
505
IDocument::ReloadBehavior GenericProjectFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
Roberto Raggi's avatar
Roberto Raggi committed
506
{
507 508 509 510 511
    Q_UNUSED(state)
    Q_UNUSED(type)
    return BehaviorSilent;
}

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

} // namespace Internal
} // namespace GenericProjectManager