summaryrefslogtreecommitdiff
path: root/src/plugins/boostbuildprojectmanager/b2project.cpp
blob: 1b4fc99e6ebc45efd28ad48f11a940bcda2e2042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//
// Copyright (C) 2013 Mateusz Łoskot <mateusz@loskot.net>
// Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
//
// This file is part of Qt Creator Boost.Build plugin project.
//
// This is free software; you can redistribute and/or modify it under
// the terms of the  GNU Lesser General Public License, Version 2.1
// as published by the Free Software Foundation.
// See accompanying file LICENSE.txt or copy at
// http://www.gnu.org/licenses/lgpl-2.1-standalone.html.
//
#include "b2buildconfiguration.h"
#include "b2buildstep.h"
#include "b2openprojectwizard.h"
#include "b2project.h"
#include "b2projectfile.h"
#include "b2projectmanager.h"
#include "b2projectmanagerconstants.h"
#include "b2projectnode.h"
#include "b2utility.h"

#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <coreplugin/generatedfile.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/cppprojects.h>
#include <cpptools/cpptoolsconstants.h>
#include <projectexplorer/kit.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/target.h>
#include <qtsupport/customexecutablerunconfiguration.h>
#include <utils/fileutils.h>
#include <utils/QtConcurrentTools>

#include <QDir>
#include <QFileInfo>
#include <QMessageBox>

namespace BoostBuildProjectManager {
namespace Internal {

Project::Project(ProjectManager* manager, QString const& fileName)
    : m_manager(manager)
    , m_filePath(fileName)
    , m_projectFile(new ProjectFile(this, m_filePath)) // enables projectDirectory()
    , m_projectNode(new ProjectNode(this, m_projectFile))
{
    Q_ASSERT(m_manager);
    Q_ASSERT(!m_filePath.isEmpty());

    setProjectContext(Core::Context(Constants::PROJECT_CONTEXT));
    setProjectLanguages(Core::Context(ProjectExplorer::Constants::LANG_CXX));
#if defined(IDE_VERSION_MAJOR) && (IDE_VERSION_MAJOR == 3 && IDE_VERSION_MINOR > 0)
    setId(Constants::PROJECT_ID);
#endif

    QFileInfo const projectFileInfo(m_filePath);
    QDir const projectDir(projectFileInfo.dir());
    m_projectName = defaultProjectName(m_filePath);
    m_filesFilePath = QFileInfo(projectDir
        , m_filePath + QLatin1String(Constants::EXT_JAMFILE_FILES)).absoluteFilePath();
    m_includesFilePath = QFileInfo(projectDir
        , m_filePath + QLatin1String(Constants::EXT_JAMFILE_INCLUDES)).absoluteFilePath();

    m_projectNode->setDisplayName(displayName());

    m_manager->registerProject(this);

    // TODO: Add file watchers
    //projectFileWatcher_->addPath(projectFilePath);
    //connect(projectFileWatcher_, SIGNAL(fileChanged(QString)), this, SLOT(refresh()));

    BBPM_QDEBUG("created project: " << displayName() << " in " << projectDirectory());
}

Project::~Project()
{
    m_manager->unregisterProject(this);
    delete m_projectNode;
}

QString Project::displayName() const
{
    return m_projectName;
}

#if defined(IDE_VERSION_MAJOR) && (IDE_VERSION_MAJOR == 3 && IDE_VERSION_MINOR == 0)
Core::Id Project::id() const
{
    return Core::Id(Constants::PROJECT_ID);
}
#endif

Core::IDocument* Project::document() const
{
    return m_projectFile;
}

ProjectExplorer::IProjectManager* Project::projectManager() const
{
    return m_manager;
}

ProjectExplorer::ProjectNode* Project::rootProjectNode() const
{
    return m_projectNode;
}

QStringList Project::files(FilesMode fileMode) const
{
    // TODO: handle ExcludeGeneratedFiles, but what files exactly?
    //       *.qtcreator.files, *.qtcreator.includes and *.user?
    Q_UNUSED(fileMode);
    return m_files;
}

QStringList Project::files() const
{
    return files(FilesMode::AllFiles);
}

QString Project::filesFilePath() const
{
    Q_ASSERT(!m_filesFilePath.isEmpty());
    return m_filesFilePath;
}

QString Project::includesFilePath() const
{
    Q_ASSERT(!m_includesFilePath.isEmpty());
    return m_includesFilePath;
}

bool Project::needsConfiguration() const
{
    // TODO: Does Boost.Build project require any configuration on loading?
    // - Kit selection
    // - build/stage directory
    // - targets listing
    // CMakeProjectManager seems to request configuration in fromMap()

    return false;
}

/*static*/
QString Project::defaultProjectName(QString const& fileName)
{
    QFileInfo const fileInfo(fileName);
    return fileInfo.absoluteDir().dirName();
}

/*static*/
QString Project::defaultBuildDirectory(QString const& top)
{
    Utils::FileName fn(Utils::FileName::fromString(defaultWorkingDirectory(top)));
    fn.appendPath(BBPM_C(BUILD_DIR_NAME));
    return fn.toString();
}

/*static*/
QString Project::defaultWorkingDirectory(QString const& top)
{
    // Accepts both, project file or project directory, as top.
    return ProjectExplorer::Project::projectDirectory(Utils::FileName::fromString(top)).toString();
}

void Project::setProjectName(QString const& name)
{
    if (m_projectName != name) {
        m_projectName = name;
        m_projectNode->setDisplayName(m_projectName);
        // TODO: signal?
    }
}

QVariantMap Project::toMap() const
{
    QVariantMap map(ProjectExplorer::Project::toMap());
    map.insert(QLatin1String(Constants::P_KEY_PROJECTNAME), m_projectName);
    return map;
}

// This function is called at the very beginning to restore the settings
// from .user file, if there is any with previous settings stored.
bool Project::fromMap(QVariantMap const& map)
{
    BBPM_QDEBUG(displayName());
    QTC_ASSERT(m_projectNode, return false);

    if (!ProjectExplorer::Project::fromMap(map))
        return false;

    QVariantMap extraValues(map);
    if (!extraValues.contains(BBPM_C(P_KEY_PROJECTNAME)))
        extraValues.insert(BBPM_C(P_KEY_PROJECTNAME), m_projectName);
    setProjectName(map.value(BBPM_C(P_KEY_PROJECTNAME)).toString());

    // Check required auxiliary files, run wizard of necessary
    if (!QFileInfo(filesFilePath()).exists() || !QFileInfo(includesFilePath()).exists()) {
        ProjectExplorer::Kit* defaultKit = ProjectExplorer::KitManager::defaultKit();
        Q_ASSERT(defaultKit);

        OpenProjectWizard wizard(this);
        if (!wizard.run(defaultKit->displayName(), extraValues))
            return false;

        QVariantMap outputValues = wizard.outputValues();
        setProjectName(outputValues.value(BBPM_C(P_KEY_PROJECTNAME)).toString());
    }

    // Set up active ProjectConfiguration (aka Target).
    // NOTE: Call setActiveBuildConfiguration when creating new build configurations.
    if (!activeTarget()) {
        // Create project configuration from scratch

        // TODO: Map the Kit to Boost.Build toolset option value
        ProjectExplorer::Kit* defaultKit = ProjectExplorer::KitManager::defaultKit();
        Q_ASSERT(defaultKit);

        // Creates as many {Build|Run|Deploy}Configurations for as corresponding
        // factories report as available.
        // For example, BuildConfigurationFactory::availableBuilds => Debug and Release
        ProjectExplorer::Target* target = createTarget(defaultKit);
        QTC_ASSERT(target, return false);

        addTarget(target);
    } else {
        // Configure project from settings sorced from .user file
        // TODO: Do we need to handle anything from .user here? Do we need this case?
        BBPM_QDEBUG(displayName() << "has user file");
    }

    // Sanity check (taken from GenericProjectManager):
    // We need both a BuildConfiguration and a RunConfiguration!
    QList<ProjectExplorer::Target*> targetList = targets();
    foreach (ProjectExplorer::Target* t, targetList) {
        if (!t->activeBuildConfiguration()) {
            removeTarget(t);
            continue;
        }
        if (!t->activeRunConfiguration())
            t->addRunConfiguration(new QtSupport::CustomExecutableRunConfiguration(t));
    }

    QTC_ASSERT(hasActiveBuildSettings(), return false);
    QTC_ASSERT(activeTarget() != 0, return false);

    // Trigger loading project tree and parsing sources
    refresh();

    return true;
}

void Project::refresh()
{
    QTC_ASSERT(QFileInfo(filesFilePath()).exists(), return);
    QTC_ASSERT(QFileInfo(includesFilePath()).exists(), return);

    QSet<QString> oldFileList;
    oldFileList = m_files.toSet();

    // Parse project:
    // The manager does not parse Jamfile files.
    // Only generates and parses list of source files in Jamfile.${JAMFILE_FILES_EXT}
    QString const projectPath(projectDirectory().toString());
    m_filesRaw = Utility::readLines(filesFilePath());
    m_files = Utility::makeAbsolutePaths(projectPath, m_filesRaw);

    QStringList includePaths =
        Utility::makeAbsolutePaths(projectPath,
            Utility::readLines(includesFilePath()));

    emit fileListChanged();

    m_projectNode->refresh(oldFileList);

    // TODO: Does it make sense to move this to separate asynchronous task?
    // TODO: extract updateCppCodeModel
    CppTools::CppModelManager *modelmanager =
        CppTools::CppModelManager::instance();
    if (modelmanager) {
        CppTools::ProjectInfo pinfo(this);

        CppTools::ProjectPartBuilder builder(pinfo);
        //builder.setDefines(); // TODO: waiting for Jamfile parser
        builder.setIncludePaths(QStringList() << projectDirectory().toString() << includePaths);

        const QList<Core::Id> languages = builder.createProjectPartsForFiles(m_files);
        foreach (Core::Id language, languages)
            setProjectLanguage(language, true);

        m_cppModelFuture.cancel();
        pinfo.finish();
        m_cppModelFuture = modelmanager->updateProjectInfo(pinfo);
    }
}

} // namespace Internal
} // namespace BoostBuildProjectManager