genericproject.cpp 13.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Roberto Raggi's avatar
Roberto Raggi committed
2
**
3
** Copyright (C) 2014 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
{
72
    setId(Constants::GENERICPROJECT_ID);
hjk's avatar
hjk committed
73
    setProjectContext(Context(GenericProjectManager::Constants::PROJECTCONTEXT));
74
    setProjectLanguages(Context(ProjectExplorer::Constants::LANG_CXX));
75

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

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

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

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

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

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

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

104
    delete m_rootNode;
105 106
}

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

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

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

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

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

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

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

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

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

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

179
    return saveRawFileList(newList);
180 181
}

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

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

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

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

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

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

224
        QFile configFile(configFileName());
225 226 227 228 229
        if (configFile.open(QFile::ReadOnly)) {
            // convert from local/file encoding to UTF-8
            QTextStream configStream(&configFile);
            m_defines = configStream.readAll().toUtf8();
        }
230
    }
231

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

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

242 243 244
    parseProject(options);

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

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

250
    if (modelManager) {
251
        CppTools::CppModelManagerInterface::ProjectInfo pinfo = modelManager->projectInfo(this);
252
        pinfo.clearProjectParts();
253
        CppTools::ProjectPart::Ptr part(new CppTools::ProjectPart);
254 255 256
        part->project = this;
        part->displayName = displayName();
        part->projectFile = projectFilePath();
Roberto Raggi's avatar
Roberto Raggi committed
257

258
        part->includePaths += projectIncludePaths();
259

260
        Kit *k = activeTarget() ? activeTarget()->kit() : KitManager::defaultKit();
261
        if (ToolChain *tc = ToolChainKitInformation::toolChain(k)) {
262
            QStringList cxxflags; // FIXME: Can we do better?
263 264
            part->evaluateToolchain(tc, cxxflags, cxxflags,
                                    SysRootKitInformation::sysRoot(k));
Roberto Raggi's avatar
Roberto Raggi committed
265 266
        }

267
        part->cxxVersion = CppTools::ProjectPart::CXX11; // assume C++11
268
        part->projectDefines += m_defines;
Roberto Raggi's avatar
Roberto Raggi committed
269 270

        // ### add _defines.
271 272

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

277
        m_codeModelFuture.cancel();
278

279
        pinfo.appendProjectPart(part);
280
        setProjectLanguage(ProjectExplorer::Constants::LANG_CXX, !part->files.isEmpty());
281

282
        m_codeModelFuture = modelManager->updateProjectInfo(pinfo);
Roberto Raggi's avatar
Roberto Raggi committed
283 284 285
    }
}

286
/**
287 288 289 290 291
 * Expands environment variables in the given \a string when they are written
 * like $$(VARIABLE).
 */
static void expandEnvironmentVariables(const QProcessEnvironment &env, QString &string)
{
292
    static QRegExp candidate(QLatin1String("\\$\\$\\((.+)\\)"));
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

    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.
 *
309
 * The \a map variable is an optional argument that will map the returned
310
 * absolute paths back to their original \a entries.
311
 */
312 313
QStringList GenericProject::processEntries(const QStringList &paths,
                                           QHash<QString, QString> *map) const
Roberto Raggi's avatar
Roberto Raggi committed
314
{
315
    const QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
316
    const QDir projectDir(QFileInfo(m_fileName).dir());
317

318
    QFileInfo fileInfo;
Roberto Raggi's avatar
Roberto Raggi committed
319
    QStringList absolutePaths;
320
    foreach (const QString &path, paths) {
321 322
        QString trimmedPath = path.trimmed();
        if (trimmedPath.isEmpty())
323 324
            continue;

325 326
        expandEnvironmentVariables(env, trimmedPath);

327 328
        trimmedPath = Utils::FileName::fromUserInput(trimmedPath).toString();

329 330 331 332 333 334 335
        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
336 337 338 339 340
    }
    absolutePaths.removeDuplicates();
    return absolutePaths;
}

Roberto Raggi's avatar
Roberto Raggi committed
341
QStringList GenericProject::projectIncludePaths() const
hjk's avatar
hjk committed
342 343 344
{
    return m_projectIncludePaths;
}
Roberto Raggi's avatar
Roberto Raggi committed
345

Roberto Raggi's avatar
Roberto Raggi committed
346
QStringList GenericProject::files() const
hjk's avatar
hjk committed
347 348 349
{
    return m_files;
}
Roberto Raggi's avatar
Roberto Raggi committed
350

Roberto Raggi's avatar
Roberto Raggi committed
351
QByteArray GenericProject::defines() const
hjk's avatar
hjk committed
352 353 354
{
    return m_defines;
}
Roberto Raggi's avatar
Roberto Raggi committed
355

356
QString GenericProject::displayName() const
Roberto Raggi's avatar
Roberto Raggi committed
357
{
358
    return m_projectName;
Roberto Raggi's avatar
Roberto Raggi committed
359 360
}

hjk's avatar
hjk committed
361
IDocument *GenericProject::document() const
Roberto Raggi's avatar
Roberto Raggi committed
362
{
363
    return m_creatorIDocument;
Roberto Raggi's avatar
Roberto Raggi committed
364 365
}

366
IProjectManager *GenericProject::projectManager() const
Roberto Raggi's avatar
Roberto Raggi committed
367
{
368
    return m_manager;
Roberto Raggi's avatar
Roberto Raggi committed
369 370
}

371
GenericProjectNode *GenericProject::rootProjectNode() const
Roberto Raggi's avatar
Roberto Raggi committed
372
{
373
    return m_rootNode;
Roberto Raggi's avatar
Roberto Raggi committed
374 375 376 377
}

QStringList GenericProject::files(FilesMode fileMode) const
{
378
    Q_UNUSED(fileMode)
379
    return m_files;
Roberto Raggi's avatar
Roberto Raggi committed
380 381
}

Tobias Hunger's avatar
Tobias Hunger committed
382
QStringList GenericProject::buildTargets() const
Roberto Raggi's avatar
Roberto Raggi committed
383 384 385 386 387 388 389
{
    QStringList targets;
    targets.append(QLatin1String("all"));
    targets.append(QLatin1String("clean"));
    return targets;
}

390 391 392 393
bool GenericProject::fromMap(const QVariantMap &map)
{
    if (!Project::fromMap(map))
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
394

395
    Kit *defaultKit = KitManager::defaultKit();
Tobias Hunger's avatar
Tobias Hunger committed
396 397
    if (!activeTarget() && defaultKit)
        addTarget(createTarget(defaultKit));
Tobias Hunger's avatar
Tobias Hunger committed
398

399 400 401 402 403 404 405 406
    // Sanity check: We need both a buildconfiguration and a runconfiguration!
    QList<Target *> targetList = targets();
    foreach (Target *t, targetList) {
        if (!t->activeBuildConfiguration()) {
            removeTarget(t);
            continue;
        }
        if (!t->activeRunConfiguration())
407
            t->addRunConfiguration(new QtSupport::CustomExecutableRunConfiguration(t));
408 409
    }

Tobias Hunger's avatar
Tobias Hunger committed
410
    refresh(Everything);
411
    return true;
412 413
}

Roberto Raggi's avatar
Roberto Raggi committed
414
////////////////////////////////////////////////////////////////////////////////////
hjk's avatar
hjk committed
415
//
Roberto Raggi's avatar
Roberto Raggi committed
416
// GenericProjectFile
hjk's avatar
hjk committed
417
//
Roberto Raggi's avatar
Roberto Raggi committed
418
////////////////////////////////////////////////////////////////////////////////////
419

420
GenericProjectFile::GenericProjectFile(GenericProject *parent, QString fileName, GenericProject::RefreshOptions options)
hjk's avatar
hjk committed
421
    : IDocument(parent),
422
      m_project(parent),
423
      m_options(options)
Roberto Raggi's avatar
Roberto Raggi committed
424
{
425
    setFilePath(fileName);
Roberto Raggi's avatar
Roberto Raggi committed
426 427
}

428
bool GenericProjectFile::save(QString *, const QString &, bool)
Roberto Raggi's avatar
Roberto Raggi committed
429
{
430
    return false;
Roberto Raggi's avatar
Roberto Raggi committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444
}

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

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

QString GenericProjectFile::mimeType() const
{
445
    return QLatin1String(Constants::GENERICMIMETYPE);
Roberto Raggi's avatar
Roberto Raggi committed
446 447 448 449 450 451 452 453 454 455 456 457
}

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

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

hjk's avatar
hjk committed
458
IDocument::ReloadBehavior GenericProjectFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
Roberto Raggi's avatar
Roberto Raggi committed
459
{
460 461 462 463 464
    Q_UNUSED(state)
    Q_UNUSED(type)
    return BehaviorSilent;
}

465
bool GenericProjectFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
466
{
467
    Q_UNUSED(errorString)
468
    Q_UNUSED(flag)
469 470 471
    if (type == TypePermissions)
        return true;
    m_project->refresh(m_options);
472
    return true;
Roberto Raggi's avatar
Roberto Raggi committed
473
}
hjk's avatar
hjk committed
474 475 476

} // namespace Internal
} // namespace GenericProjectManager