genericproject.cpp 15.3 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>
41
#include <cpptools/cppmodelmanagerinterface.h>
hjk's avatar
hjk committed
42
#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 74 75 76
    Core::Context pl(ProjectExplorer::Constants::LANG_CXX);
    pl.add(ProjectExplorer::Constants::LANG_QMLJS);
    setProjectLanguages(pl);

77

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

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

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

hjk's avatar
hjk committed
91 92 93 94
    DocumentManager::addDocument(m_creatorIDocument);
    DocumentManager::addDocument(m_filesIDocument);
    DocumentManager::addDocument(m_includesIDocument);
    DocumentManager::addDocument(m_configIDocument);
95

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

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

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

106
    delete m_rootNode;
107 108
}

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

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

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

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

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

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

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

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

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

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

181
    return saveRawFileList(newList);
182 183
}

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

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

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

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

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

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

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

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

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

241 242 243
    parseProject(options);

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

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

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

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

260
            foreach (const HeaderPath &headerPath, tc->systemHeaderPaths(cxxflags, SysRootKitInformation::sysRoot(k))) {
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

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

278 279 280
        QStringList filesToUpdate;

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

295
        pinfo.appendProjectPart(part);
296
        setProjectLanguage(ProjectExplorer::Constants::LANG_CXX, !part->files.isEmpty());
297

Roberto Raggi's avatar
Roberto Raggi committed
298
        modelManager->updateProjectInfo(pinfo);
299
        m_codeModelFuture = modelManager->updateSourceFiles(filesToUpdate);
Roberto Raggi's avatar
Roberto Raggi committed
300 301 302
    }
}

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

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

335
    QFileInfo fileInfo;
Roberto Raggi's avatar
Roberto Raggi committed
336
    QStringList absolutePaths;
337
    foreach (const QString &path, paths) {
338 339
        QString trimmedPath = path.trimmed();
        if (trimmedPath.isEmpty())
340 341
            continue;

342 343
        expandEnvironmentVariables(env, trimmedPath);

344 345
        trimmedPath = Utils::FileName::fromUserInput(trimmedPath).toString();

346 347 348 349 350 351 352
        fileInfo.setFile(projectDir, trimmedPath);
        if (fileInfo.exists()) {
            const QString absPath = fileInfo.absoluteFilePath();
            absolutePaths.append(absPath);
            if (map)
                map->insert(absPath, trimmedPath);
        }
Roberto Raggi's avatar
Roberto Raggi committed
353 354 355 356 357
    }
    absolutePaths.removeDuplicates();
    return absolutePaths;
}

Roberto Raggi's avatar
Roberto Raggi committed
358 359 360
QStringList GenericProject::allIncludePaths() const
{
    QStringList paths;
361 362
    paths += m_includePaths;
    paths += m_projectIncludePaths;
Roberto Raggi's avatar
Roberto Raggi committed
363 364 365 366 367
    paths.removeDuplicates();
    return paths;
}

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

Roberto Raggi's avatar
Roberto Raggi committed
372
QStringList GenericProject::files() const
hjk's avatar
hjk committed
373 374 375
{
    return m_files;
}
Roberto Raggi's avatar
Roberto Raggi committed
376 377

QStringList GenericProject::includePaths() const
hjk's avatar
hjk committed
378 379 380
{
    return m_includePaths;
}
Roberto Raggi's avatar
Roberto Raggi committed
381 382

void GenericProject::setIncludePaths(const QStringList &includePaths)
hjk's avatar
hjk committed
383 384 385
{
    m_includePaths = includePaths;
}
Roberto Raggi's avatar
Roberto Raggi committed
386

Roberto Raggi's avatar
Roberto Raggi committed
387
QByteArray GenericProject::defines() const
hjk's avatar
hjk committed
388 389 390
{
    return m_defines;
}
Roberto Raggi's avatar
Roberto Raggi committed
391

392
QString GenericProject::displayName() const
Roberto Raggi's avatar
Roberto Raggi committed
393
{
394
    return m_projectName;
Roberto Raggi's avatar
Roberto Raggi committed
395 396
}

hjk's avatar
hjk committed
397
Id GenericProject::id() const
Tobias Hunger's avatar
Tobias Hunger committed
398
{
hjk's avatar
hjk committed
399
    return Id(Constants::GENERICPROJECT_ID);
Tobias Hunger's avatar
Tobias Hunger committed
400 401
}

hjk's avatar
hjk committed
402
IDocument *GenericProject::document() const
Roberto Raggi's avatar
Roberto Raggi committed
403
{
404
    return m_creatorIDocument;
Roberto Raggi's avatar
Roberto Raggi committed
405 406
}

407
IProjectManager *GenericProject::projectManager() const
Roberto Raggi's avatar
Roberto Raggi committed
408
{
409
    return m_manager;
Roberto Raggi's avatar
Roberto Raggi committed
410 411
}

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

QStringList GenericProject::files(FilesMode fileMode) const
{
419
    Q_UNUSED(fileMode)
420
    return m_files;
Roberto Raggi's avatar
Roberto Raggi committed
421 422
}

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

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
452
    setIncludePaths(allIncludePaths());
453

Tobias Hunger's avatar
Tobias Hunger committed
454
    refresh(Everything);
455
    return true;
456 457
}

Roberto Raggi's avatar
Roberto Raggi committed
458
////////////////////////////////////////////////////////////////////////////////////
hjk's avatar
hjk committed
459
//
Roberto Raggi's avatar
Roberto Raggi committed
460
// GenericProjectFile
hjk's avatar
hjk committed
461
//
Roberto Raggi's avatar
Roberto Raggi committed
462
////////////////////////////////////////////////////////////////////////////////////
463

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

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

QString GenericProjectFile::fileName() const
{
478
    return m_fileName;
Roberto Raggi's avatar
Roberto Raggi committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492
}

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

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

QString GenericProjectFile::mimeType() const
{
493
    return QLatin1String(Constants::GENERICMIMETYPE);
Roberto Raggi's avatar
Roberto Raggi committed
494 495 496 497 498 499 500 501 502 503 504 505
}

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

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

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

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

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

} // namespace Internal
} // namespace GenericProjectManager