File: mesonintrospectjob.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (286 lines) | stat: -rw-r--r-- 8,941 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2019 Daniel Mensinger <daniel@mensinger-ka.de>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "mesonintrospectjob.h"

#include "mesonconfig.h"
#include "mesonmanager.h"
#include "mesonoptions.h"
#include <debug.h>

#include <KLocalizedString>
#include <KProcess>

#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QtConcurrentRun>

using namespace Meson;
using namespace KDevelop;

MesonIntrospectJob::MesonIntrospectJob(KDevelop::IProject* project, QVector<MesonIntrospectJob::Type> types,
                                       MesonIntrospectJob::Mode mode, QObject* parent)
    : KJob(parent)
    , m_types(types)
    , m_mode(mode)
    , m_project(project)
{
    Q_ASSERT(project);

    if (mode == MESON_FILE) {
        // Since we are parsing the meson file in this mode, no build directory
        // is required and we have to fake a build directory
        m_buildDir.buildDir = project->path();
        auto* bsm = project->buildSystemManager();
        auto* manager = dynamic_cast<MesonManager*>(bsm);
        if (manager) {
            m_buildDir.mesonExecutable = manager->findMeson();
        }
    } else {
        m_buildDir = Meson::currentBuildDir(project);
    }

    m_projectPath = project->path();
    connect(&m_futureWatcher, &QFutureWatcher<QString>::finished, this, &MesonIntrospectJob::finished);
}

MesonIntrospectJob::MesonIntrospectJob(KDevelop::IProject* project, KDevelop::Path meson,
                                       QVector<MesonIntrospectJob::Type> types, QObject* parent)
    : KJob(parent)
    , m_types(types)
    , m_mode(MESON_FILE)
    , m_project(project)
{
    Q_ASSERT(project);

    // Since we are parsing the meson file in this mode, no build directory
    // is required and we have to fake a build directory
    m_projectPath = project->path();
    m_buildDir.buildDir = m_projectPath;
    m_buildDir.mesonExecutable = meson;
    connect(&m_futureWatcher, &QFutureWatcher<QString>::finished, this, &MesonIntrospectJob::finished);
}

MesonIntrospectJob::MesonIntrospectJob(KDevelop::IProject* project, Meson::BuildDir buildDir,
                                       QVector<MesonIntrospectJob::Type> types, MesonIntrospectJob::Mode mode,
                                       QObject* parent)
    : KJob(parent)
    , m_types(types)
    , m_mode(mode)
    , m_buildDir(buildDir)
    , m_project(project)
{
    Q_ASSERT(project);
    m_projectPath = project->path();
    connect(&m_futureWatcher, &QFutureWatcher<QString>::finished, this, &MesonIntrospectJob::finished);
}

QString MesonIntrospectJob::getTypeString(MesonIntrospectJob::Type type) const
{
    switch (type) {
    case BENCHMARKS:
        return QStringLiteral("benchmarks");
    case BUILDOPTIONS:
        return QStringLiteral("buildoptions");
    case BUILDSYSTEM_FILES:
        return QStringLiteral("buildsystem_files");
    case DEPENDENCIES:
        return QStringLiteral("dependencies");
    case INSTALLED:
        return QStringLiteral("installed");
    case PROJECTINFO:
        return QStringLiteral("projectinfo");
    case TARGETS:
        return QStringLiteral("targets");
    case TESTS:
        return QStringLiteral("tests");
    }

    return QStringLiteral("error");
}

QString MesonIntrospectJob::importJSONFile(const BuildDir& buildDir, MesonIntrospectJob::Type type, QJsonObject* out)
{
    QString typeStr = getTypeString(type);
    QString fileName = QStringLiteral("intro-") + typeStr + QStringLiteral(".json");
    QString infoDir = buildDir.buildDir.toLocalFile() + QStringLiteral("/") + QStringLiteral("meson-info");
    QFile introFile(infoDir + QStringLiteral("/") + fileName);

    if (!introFile.exists()) {
        return i18n("Introspection file '%1' does not exist", QFileInfo(introFile).canonicalFilePath());
    }

    if (!introFile.open(QFile::ReadOnly | QFile::Text)) {
        return i18n("Failed to open introspection file '%1'", QFileInfo(introFile).canonicalFilePath());
    }

    QJsonParseError error;
    QJsonDocument doc = QJsonDocument::fromJson(introFile.readAll(), &error);
    if (error.error) {
        return i18n("In %1:%2: %3", QFileInfo(introFile).canonicalFilePath(), error.offset, error.errorString());
    }

    if (doc.isArray()) {
        (*out)[typeStr] = doc.array();
    } else if (doc.isObject()) {
        (*out)[typeStr] = doc.object();
    } else {
        return i18n("The introspection file '%1' contains neither an array nor an object",
                    QFileInfo(introFile).canonicalFilePath());
    }

    return QString();
}

QString MesonIntrospectJob::importMesonAPI(const BuildDir& buildDir, MesonIntrospectJob::Type type, QJsonObject* out)
{
    QString typeStr = getTypeString(type);
    QString option = QStringLiteral("--") + typeStr;
    option.replace(QLatin1Char('_'), QLatin1Char('-'));

    KProcess proc;
    proc.setWorkingDirectory(m_projectPath.toLocalFile());
    proc.setOutputChannelMode(KProcess::SeparateChannels);
    proc.setProgram(buildDir.mesonExecutable.toLocalFile());
    proc << QStringLiteral("introspect") << option << QStringLiteral("meson.build");

    int ret = proc.execute();
    if (ret != 0) {
        return i18n("%1 returned %2", proc.program().join(QLatin1Char(' ')), ret);
    }

    QJsonParseError error;
    QJsonDocument doc = QJsonDocument::fromJson(proc.readAll(), &error);
    if (error.error) {
        return i18n("JSON parser error: %1", error.errorString());
    }

    if (doc.isArray()) {
        (*out)[typeStr] = doc.array();
    } else if (doc.isObject()) {
        (*out)[typeStr] = doc.object();
    } else {
        return i18n("The introspection output of '%1' contains neither an array nor an object",
                    proc.program().join(QLatin1Char(' ')));
    }

    return QString();
}

QString MesonIntrospectJob::import(BuildDir buildDir)
{
    QJsonObject rawData;

    // First load the complete JSON data
    for (auto i : m_types) {
        QString err;
        switch (m_mode) {
        case BUILD_DIR:
            err = importJSONFile(buildDir, i, &rawData);
            break;
        case MESON_FILE:
            err = importMesonAPI(buildDir, i, &rawData);
            break;
        }

        if (!err.isEmpty()) {
            qCWarning(KDEV_Meson) << "MINTRO: " << err;
            setError(true);
            setErrorText(err);
            return err;
        }
    }

    auto buildOptionsJSON = rawData[QStringLiteral("buildoptions")];
    if (buildOptionsJSON.isArray()) {
        m_res_options = std::make_shared<MesonOptions>(buildOptionsJSON.toArray());
        if (m_res_options) {
            qCDebug(KDEV_Meson) << "MINTRO: Imported " << m_res_options->options().size() << " buildoptions";
        } else {
            qCWarning(KDEV_Meson) << "MINTRO: Failed to parse buildoptions";
        }
    }

    auto projectInfoJSON = rawData[QStringLiteral("projectinfo")];
    if (projectInfoJSON.isObject()) {
        m_res_projectInfo = std::make_shared<MesonProjectInfo>(projectInfoJSON.toObject());
        if (!m_res_projectInfo) {
            qCWarning(KDEV_Meson) << "MINTRO: Failed to parse projectinfo";
        }
    }

    auto targetsJSON = rawData[QStringLiteral("targets")];
    if (targetsJSON.isArray()) {
        m_res_targets = std::make_shared<MesonTargets>(targetsJSON.toArray());
    }

    auto testsJSON = rawData[QStringLiteral("tests")];
    if (testsJSON.isArray()) {
        m_res_tests = std::make_shared<MesonTestSuites>(testsJSON.toArray(), m_project);
        if (m_res_tests) {
            qCDebug(KDEV_Meson) << "MINTRO: Imported " << m_res_tests->testSuites().size() << " test suites";
        } else {
            qCWarning(KDEV_Meson) << "MINTRO: Failed to parse tests";
        }
    }

    return QString();
}

void MesonIntrospectJob::start()
{
    qCDebug(KDEV_Meson) << "MINTRO: Starting meson introspection job";
    if (!m_buildDir.isValid()) {
        qCWarning(KDEV_Meson) << "The current build directory is invalid";
        setError(true);
        setErrorText(i18n("The current build directory is invalid"));
        emitResult();
        return;
    }

    auto future = QtConcurrent::run([this] {
        return import(m_buildDir);
    });
    m_futureWatcher.setFuture(future);
}

void MesonIntrospectJob::finished()
{
    qCDebug(KDEV_Meson) << "MINTRO: Meson introspection job finished";
    emitResult();
}

bool MesonIntrospectJob::doKill()
{
    if (m_futureWatcher.isRunning()) {
        m_futureWatcher.cancel();
    }
    return true;
}

MesonOptsPtr MesonIntrospectJob::buildOptions()
{
    return m_res_options;
}

MesonProjectInfoPtr MesonIntrospectJob::projectInfo()
{
    return m_res_projectInfo;
}

MesonTargetsPtr MesonIntrospectJob::targets()
{
    return m_res_targets;
}

MesonTestSuitesPtr MesonIntrospectJob::tests()
{
    return m_res_tests;
}

#include "moc_mesonintrospectjob.cpp"