aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qbsprojectmanager/qbsnodetreebuilder.cpp
blob: 82e896e77a89a7f351cd3df92d28c39a56f9b066 (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
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qbsnodetreebuilder.h"

#include "qbsnodes.h"
#include "qbsproject.h"
#include "qbsprojectmanagertr.h"
#include "qbssession.h"

#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>

#include <utils/algorithm.h>
#include <utils/qtcassert.h>

#include <projectexplorer/projecttree.h>

using namespace ProjectExplorer;
using namespace Utils;

namespace QbsProjectManager {
namespace Internal {

static FileType fileType(const QJsonObject &artifact)
{
    const QJsonArray fileTags = artifact.value("file-tags").toArray();
    if (fileTags.contains("c")
            || fileTags.contains("cpp")
            || fileTags.contains("objc")
            || fileTags.contains("objcpp")) {
        return FileType::Source;
    }
    if (fileTags.contains("hpp"))
        return FileType::Header;
    if (fileTags.contains("qrc"))
        return FileType::Resource;
    if (fileTags.contains("ui"))
        return FileType::Form;
    if (fileTags.contains("scxml"))
        return FileType::StateChart;
    if (fileTags.contains("qt.qml.qml"))
        return FileType::QML;
    if (fileTags.contains("application"))
        return FileType::App;
    if (fileTags.contains("staticlibrary") || fileTags.contains("dynamiclibrary"))
        return FileType::Lib;
    return FileType::Unknown;
}

void setupArtifact(FolderNode *root, const QJsonObject &artifact)
{
    const FilePath path = FilePath::fromString(artifact.value("file-path").toString());
    const FileType type = fileType(artifact);
    const bool isGenerated = artifact.value("is-generated").toBool();

    // A list of human-readable file types that we can reasonably expect
    // to get generated during a build. Extend as needed.
    static const QSet<QString> sourceTags = {
        "c", "cpp", "hpp", "objc", "objcpp", "c_pch_src", "cpp_pch_src", "objc_pch_src",
        "objcpp_pch_src", "asm", "asm_cpp", "linkerscript", "qrc", "java.java"
    };
    auto node = std::make_unique<FileNode>(path, type);
    node->setIsGenerated(isGenerated);
    QSet<QString> fileTags = toSet(transform<QStringList, QJsonArray>(
                                       artifact.value("file-tags").toArray(),
                                       [](const QJsonValue &v) { return v.toString(); }));
    node->setListInProject(!isGenerated || fileTags.intersects(sourceTags));
    root->addNestedNode(std::move(node));
}

static void setupArtifactsForGroup(FolderNode *root, const QJsonObject &group)
{
    forAllArtifacts(group, [root](const QJsonObject &artifact) { setupArtifact(root, artifact); });
    root->compress();
}

static void setupGeneratedArtifacts(FolderNode *root, const QJsonObject &product)
{
    forAllArtifacts(product, ArtifactType::Generated,
                    [root](const QJsonObject &artifact) { setupArtifact(root, artifact); });
    root->compress();
}


static std::unique_ptr<QbsGroupNode> buildGroupNodeTree(const QJsonObject &grp)
{
    const Location location = locationFromObject(grp);
    FilePath baseDir = location.filePath.parentDir();
    QString prefix = grp.value("prefix").toString();
    if (prefix.endsWith('/')) {
        prefix.chop(1);
        if (QFileInfo(prefix).isAbsolute())
            baseDir = FilePath::fromString(prefix);
        else
            baseDir = baseDir.pathAppended(prefix);
    }
    auto result = std::make_unique<QbsGroupNode>(grp);
    result->setAbsoluteFilePathAndLine(baseDir, -1);
    auto fileNode = std::make_unique<FileNode>(FilePath(), FileType::Project);
    fileNode->setAbsoluteFilePathAndLine(location.filePath, location.line);
    result->addNode(std::move(fileNode));
    setupArtifactsForGroup(result.get(), grp);
    return result;
}

static std::unique_ptr<QbsProductNode> buildProductNodeTree(const QJsonObject &prd)
{
    const Location location = locationFromObject(prd);
    auto result = std::make_unique<QbsProductNode>(prd);
    result->setAbsoluteFilePathAndLine(location.filePath.parentDir(), -1);
    auto fileNode = std::make_unique<FileNode>(FilePath(), FileType::Project);
    fileNode->setAbsoluteFilePathAndLine(location.filePath, location.line);
    result->addNode(std::move(fileNode));
    for (const QJsonValue &v : prd.value("groups").toArray()) {
        const QJsonObject grp = v.toObject();
        if (grp.value("name") == prd.value("name")
                && grp.value("location") == prd.value("location")) {
            // Set implicit product group right onto this node:
            setupArtifactsForGroup(result.get(), grp);
            continue;
        }
        result->addNode(buildGroupNodeTree(grp));
    }

    // Add "Generated Files" Node:
    auto genFiles = std::make_unique<VirtualFolderNode>(
                FilePath::fromString(prd.value("build-directory").toString()));
    genFiles->setDisplayName(::QbsProjectManager::Tr::tr("Generated files"));
    setupGeneratedArtifacts(genFiles.get(), prd);
    result->addNode(std::move(genFiles));
    return result;
}

static void setupProjectNode(QbsProjectNode *node)
{
    const Location loc = locationFromObject(node->projectData());
    node->setAbsoluteFilePathAndLine(loc.filePath.parentDir(), -1);
    auto fileNode = std::make_unique<FileNode>(node->filePath(), FileType::Project);
    fileNode->setAbsoluteFilePathAndLine(loc.filePath, loc.line);
    node->addNode(std::move(fileNode));

    for (const QJsonValue &v : node->projectData().value("sub-projects").toArray()) {
        auto subProject = std::make_unique<QbsProjectNode>(v.toObject());
        setupProjectNode(subProject.get());
        node->addNode(std::move(subProject));
    }

    for (const QJsonValue &v : node->projectData().value("products").toArray())
        node->addNode(buildProductNodeTree(v.toObject()));
}

static QSet<QString> referencedBuildSystemFiles(const QJsonObject &prjData)
{
    QSet<QString> result;
    result.insert(prjData.value("location").toObject().value("file-path").toString());
    for (const QJsonValue &v : prjData.value("sub-projects").toArray())
        result.unite(referencedBuildSystemFiles(v.toObject()));
    for (const QJsonValue &v : prjData.value("products").toArray()) {
        const QJsonObject product = v.toObject();
        result.insert(product.value("location").toObject().value("file-path").toString());
        for (const QJsonValue &v : product.value("groups").toArray())
            result.insert(v.toObject().value("location").toObject().value("file-path").toString());
    }
    return result;
}

static QStringList unreferencedBuildSystemFiles(const QJsonObject &project)
{
    QStringList unreferenced = arrayToStringList(project.value("build-system-files"));
    const QList<QString> referenced = toList(referencedBuildSystemFiles(project));
    for (auto it = unreferenced.begin(); it != unreferenced.end(); ) {
        if (referenced.contains(*it))
            it = unreferenced.erase(it);
        else
            ++it;
    }
    return unreferenced;
}

QbsProjectNode *QbsNodeTreeBuilder::buildTree(const QString &projectName,
                                              const Utils::FilePath &projectFile,
                                              const Utils::FilePath &projectDir,
                                              const QJsonObject &projectData)
{
    auto root = std::make_unique<QbsProjectNode>(projectData);

    // If we have no project information at all (i.e. it could not be properly parsed),
    // create the main project file node "manually".
    if (projectData.isEmpty()) {
        auto fileNode = std::make_unique<FileNode>(projectFile, FileType::Project);
        root->addNode(std::move(fileNode));
    } else {
        setupProjectNode(root.get());
    }

    if (root->displayName().isEmpty())
        root->setDisplayName(projectName);
    if (root->displayName().isEmpty())
        root->setDisplayName(projectFile.completeBaseName());

    auto buildSystemFiles = std::make_unique<FolderNode>(projectDir);
    buildSystemFiles->setDisplayName(Tr::tr("Qbs files"));

    const FilePath buildDir = FilePath::fromString(projectData.value("build-directory").toString());
    const QStringList files = unreferencedBuildSystemFiles(projectData);
    for (const QString &f : files) {
        const FilePath filePath = FilePath::fromString(f);
        if (filePath.isChildOf(projectDir)) {
            auto fileNode = std::make_unique<FileNode>(filePath, FileType::Project);
            fileNode->setIsGenerated(filePath.isChildOf(buildDir));
            buildSystemFiles->addNestedNode(std::move(fileNode));
        }
    }
    buildSystemFiles->compress();
    root->addNode(std::move(buildSystemFiles));
    ProjectTree::applyTreeManager(root.get(), ProjectTree::AsyncPhase); // QRC nodes
    return root.release();
}

} // namespace Internal
} // namespace QbsProjectManager