aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/mesonprojectmanager/mesoninfoparser.cpp
blob: 7b0434d922909c37492297b5be3d7f65ddb2e2c8 (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
// Copyright (C) 2020 Alexis Jeandet.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "mesoninfoparser.h"

#include "buildoptions.h"
#include "common.h"
#include "mesonpluginconstants.h"

#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>

#include <memory>
#include <optional>

using namespace Utils;

namespace MesonProjectManager::Internal::MesonInfoParser {

static std::unique_ptr<BuildOption> loadOption(const QJsonObject &option)
{
    const auto type = option["type"].toString();
    if (type == "string")
        return std::make_unique<StringBuildOption>(option["name"].toString(),
                                                   option["section"].toString(),
                                                   option["description"].toString(),
                                                   option["value"]);
    if (type == "boolean")
        return std::make_unique<BooleanBuildOption>(option["name"].toString(),
                                                    option["section"].toString(),
                                                    option["description"].toString(),
                                                    option["value"]);
    if (type == "combo")
        return std::make_unique<ComboBuildOption>(option["name"].toString(),
                                                  option["section"].toString(),
                                                  option["description"].toString(),
                                                  option["choices"].toVariant().toStringList(),
                                                  option["value"]);
    if (type == "integer")
        return std::make_unique<IntegerBuildOption>(option["name"].toString(),
                                                    option["section"].toString(),
                                                    option["description"].toString(),
                                                    option["value"]);
    if (type == "array")
        return std::make_unique<ArrayBuildOption>(option["name"].toString(),
                                                  option["section"].toString(),
                                                  option["description"].toString(),
                                                  option["value"].toVariant());
    if (type == "feature")
        return std::make_unique<FeatureBuildOption>(option["name"].toString(),
                                                    option["section"].toString(),
                                                    option["description"].toString(),
                                                    option["value"]);
    return std::make_unique<UnknownBuildOption>(option["name"].toString(),
                                                option["section"].toString(),
                                                option["description"].toString());
}

static std::vector<std::unique_ptr<BuildOption>> loadOptions(const QJsonArray &arr)
{
    std::vector<std::unique_ptr<BuildOption>> buildOptions;
    std::transform(std::cbegin(arr),
                   std::cend(arr),
                   std::back_inserter(buildOptions),
                   [](const auto &option) { return loadOption(option.toObject()); });
    return buildOptions;
}

static std::vector<std::unique_ptr<BuildOption>> buildOptionsList(const FilePath &buildDir)
{
    FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUIDOPTIONS;
    auto arr = load<QJsonArray>(path.toFSPathString());
    if (arr)
        return loadOptions(*arr);
    return {};
}

static std::vector<std::unique_ptr<BuildOption>> buildOptionsList(const QJsonDocument &js)
{
    auto obj = get<QJsonArray>(js.object(), "buildoptions");
    if (obj)
        return loadOptions(*obj);
    return {};
}

static Target::SourceGroup extract_source(const QJsonValue &source)
{
    const auto srcObj = source.toObject();
    return {
        srcObj["language"].toString(),
        srcObj["compiler"].toVariant().toStringList(),
        srcObj["parameters"].toVariant().toStringList(),
        srcObj["sources"].toVariant().toStringList(),
        srcObj["generated_sources"].toVariant().toStringList()
    };
}

static Target::SourceGroupList extract_sources(const QJsonArray &sources)
{
    Target::SourceGroupList res;
    std::transform(std::cbegin(sources), std::cend(sources), std::back_inserter(res), extract_source);
    return res;
}

static Target extract_target(const QJsonValue &target)
{
    auto targetObj = target.toObject();
    return {
        targetObj["type"].toString(),
        targetObj["name"].toString(),
        targetObj["id"].toString(),
        targetObj["defined_in"].toString(),
        targetObj["filename"].toVariant().toStringList(),
        targetObj["extra_files"].toVariant().toStringList(),
        targetObj["subproject"].toString(),
        extract_sources(targetObj["target_sources"].toArray()),
        targetObj["build_by_default"].toBool()
    };
}

static TargetsList load_targets(const QJsonArray &arr)
{
    TargetsList targets;
    std::transform(std::cbegin(arr), std::cend(arr), std::back_inserter(targets), extract_target);
    return targets;
}

static TargetsList targetsList(const QJsonDocument &js)
{
    if (auto obj = get<QJsonArray>(js.object(), "targets"))
        return load_targets(*obj);
    return {};
}

static TargetsList targetsList(const FilePath &buildDir)
{
    const FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_TARGETS;
    if (auto arr = load<QJsonArray>(path.toFSPathString()))
        return load_targets(*arr);
    return {};
}

static void appendFiles(const std::optional<QJsonArray> &arr, FilePaths &dest)
{
    if (!arr)
        return;
    std::transform(std::cbegin(*arr), std::cend(*arr), std::back_inserter(dest), [](const auto &file) {
        return FilePath::fromString(file.toString());
    });
}

static FilePaths files(const FilePath &buildDir)
{
    FilePaths files;
    FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUILDSYSTEM_FILES;
    auto arr = load<QJsonArray>(path.toFSPathString());
    appendFiles(arr, files);
    return files;
}

static FilePaths files(const QJsonDocument &js)
{
    FilePaths files;
    auto arr = get<QJsonArray>(js.object(), "projectinfo", "buildsystem_files");
    appendFiles(arr, files);
    const auto subprojects = get<QJsonArray>(js.object(), "projectinfo", "subprojects");
    for (const auto &subproject : *subprojects) {
        auto arr = get<QJsonArray>(subproject.toObject(), "buildsystem_files");
        appendFiles(arr, files);
    }
    return files;
}

Result parse(const FilePath &buildDir)
{
    return {targetsList(buildDir), buildOptionsList(buildDir), files(buildDir)};
}

Result parse(const QByteArray &data)
{
    const auto json = QJsonDocument::fromJson(data);
    return {targetsList(json), buildOptionsList(json), files(json)};
}

Result parse(QIODevice *introFile)
{
    if (!introFile)
        return {};
    if (!introFile->isOpen())
        introFile->open(QIODevice::ReadOnly | QIODevice::Text);
    introFile->seek(0);
    auto data = introFile->readAll();
    return parse(data);
}

} // namespace MesonProjectManager::Internal::MesonInfoParser