Changeset View
Changeset View
Standalone View
Standalone View
plugins/meson/mesonintrospectjob.cpp
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Daniel Mensinger <daniel@mensinger-ka.de> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "mesonintrospectjob.h" | ||||
21 | #include "mesonconfig.h" | ||||
22 | #include "mesonmanager.h" | ||||
23 | #include "mesonoptions.h" | ||||
24 | | ||||
25 | #include <KProcess> | ||||
26 | #include <QDir> | ||||
27 | #include <QJsonArray> | ||||
28 | #include <QJsonDocument> | ||||
29 | #include <QJsonObject> | ||||
30 | #include <QtConcurrentRun> | ||||
31 | #include <debug.h> | ||||
32 | #include <klocalizedstring.h> | ||||
33 | | ||||
34 | using namespace Meson; | ||||
35 | using namespace KDevelop; | ||||
36 | | ||||
37 | MesonIntrospectJob::MesonIntrospectJob(KDevelop::IProject* project, QVector<MesonIntrospectJob::Type> types, | ||||
38 | MesonIntrospectJob::Mode mode, QObject* parent) | ||||
39 | : KJob(parent) | ||||
40 | , m_types(types) | ||||
41 | , m_mode(mode) | ||||
42 | { | ||||
43 | Q_ASSERT(project); | ||||
44 | | ||||
45 | if (mode == MESON_FILE) { | ||||
46 | // Since we are parsing the meson file in this mode, no build directory | ||||
47 | // is required and we have to fake a build directory | ||||
48 | m_buildDir.buildDir = project->path(); | ||||
49 | auto* bsm = project->buildSystemManager(); | ||||
50 | MesonManager* manager = dynamic_cast<MesonManager*>(bsm); | ||||
51 | if (manager) { | ||||
52 | m_buildDir.mesonExecutable = manager->findMeson(); | ||||
53 | } | ||||
54 | } else { | ||||
55 | m_buildDir = Meson::currentBuildDir(project); | ||||
56 | } | ||||
57 | | ||||
58 | m_projectPath = project->path(); | ||||
59 | connect(&m_futureWatcher, &QFutureWatcher<QString>::finished, this, &MesonIntrospectJob::finished); | ||||
60 | } | ||||
61 | | ||||
62 | MesonIntrospectJob::MesonIntrospectJob(KDevelop::Path projectPath, KDevelop::Path meson, | ||||
63 | QVector<MesonIntrospectJob::Type> types, QObject* parent) | ||||
64 | : KJob(parent) | ||||
65 | , m_types(types) | ||||
66 | , m_mode(MESON_FILE) | ||||
67 | , m_projectPath(projectPath) | ||||
68 | { | ||||
69 | // Since we are parsing the meson file in this mode, no build directory | ||||
70 | // is required and we have to fake a build directory | ||||
71 | m_buildDir.buildDir = m_projectPath; | ||||
72 | m_buildDir.mesonExecutable = meson; | ||||
73 | connect(&m_futureWatcher, &QFutureWatcher<QString>::finished, this, &MesonIntrospectJob::finished); | ||||
74 | } | ||||
75 | | ||||
76 | MesonIntrospectJob::MesonIntrospectJob(KDevelop::Path projectPath, Meson::BuildDir buildDir, | ||||
77 | QVector<MesonIntrospectJob::Type> types, MesonIntrospectJob::Mode mode, | ||||
78 | QObject* parent) | ||||
79 | : KJob(parent) | ||||
80 | , m_types(types) | ||||
81 | , m_mode(mode) | ||||
82 | , m_buildDir(buildDir) | ||||
83 | , m_projectPath(projectPath) | ||||
84 | { | ||||
85 | connect(&m_futureWatcher, &QFutureWatcher<QString>::finished, this, &MesonIntrospectJob::finished); | ||||
86 | } | ||||
87 | | ||||
88 | QString MesonIntrospectJob::getTypeString(MesonIntrospectJob::Type type) const | ||||
89 | { | ||||
90 | switch (type) { | ||||
91 | case BENCHMARKS: | ||||
92 | return QStringLiteral("benchmarks"); | ||||
93 | case BUILDOPTIONS: | ||||
94 | return QStringLiteral("buildoptions"); | ||||
95 | case BUILDSYSTEM_FILES: | ||||
96 | return QStringLiteral("buildsystem_files"); | ||||
97 | case DEPENDENCIES: | ||||
98 | return QStringLiteral("dependencies"); | ||||
99 | case INSTALLED: | ||||
100 | return QStringLiteral("installed"); | ||||
101 | case PROJECTINFO: | ||||
102 | return QStringLiteral("projectinfo"); | ||||
103 | case TARGETS: | ||||
104 | return QStringLiteral("targets"); | ||||
105 | case TESTS: | ||||
106 | return QStringLiteral("tests"); | ||||
107 | } | ||||
108 | | ||||
109 | return QStringLiteral("error"); | ||||
110 | } | ||||
111 | | ||||
112 | QString MesonIntrospectJob::importJSONFile(const BuildDir& buildDir, MesonIntrospectJob::Type type, QJsonObject* out) | ||||
113 | { | ||||
114 | QString typeStr = getTypeString(type); | ||||
115 | QString fileName = QStringLiteral("intro-") + typeStr + QStringLiteral(".json"); | ||||
116 | QString infoDir = buildDir.buildDir.toLocalFile() + QStringLiteral("/") + QStringLiteral("meson-info"); | ||||
117 | QFile introFile(infoDir + QStringLiteral("/") + fileName); | ||||
118 | | ||||
119 | if (!introFile.exists()) { | ||||
120 | return i18n("Introspection file '%1' does not exist", QFileInfo(introFile).canonicalFilePath()); | ||||
121 | } | ||||
122 | | ||||
123 | if (!introFile.open(QFile::ReadOnly | QFile::Text)) { | ||||
124 | return i18n("Failed to open introspection file '%1'", QFileInfo(introFile).canonicalFilePath()); | ||||
125 | } | ||||
126 | | ||||
127 | QJsonParseError error; | ||||
128 | QJsonDocument doc = QJsonDocument::fromJson(introFile.readAll(), &error); | ||||
129 | if (error.error) { | ||||
130 | return i18n("In %1:%2: %3", QFileInfo(introFile).canonicalFilePath(), error.offset, error.errorString()); | ||||
131 | } | ||||
132 | | ||||
133 | if (doc.isArray()) { | ||||
134 | (*out)[typeStr] = doc.array(); | ||||
135 | } else if (doc.isObject()) { | ||||
136 | (*out)[typeStr] = doc.object(); | ||||
137 | } else { | ||||
138 | return i18n("The introspection file '%1' contains neither an array nor an object", | ||||
139 | QFileInfo(introFile).canonicalFilePath()); | ||||
140 | } | ||||
141 | | ||||
142 | return QStringLiteral(""); | ||||
143 | } | ||||
144 | | ||||
145 | QString MesonIntrospectJob::importMesonAPI(const BuildDir& buildDir, MesonIntrospectJob::Type type, QJsonObject* out) | ||||
146 | { | ||||
147 | QString typeStr = getTypeString(type); | ||||
148 | QString option = QStringLiteral("--") + typeStr; | ||||
149 | option.replace(QChar::fromLatin1('_'), QChar::fromLatin1('-')); | ||||
150 | | ||||
151 | KProcess proc(this); | ||||
152 | proc.setWorkingDirectory(m_projectPath.toLocalFile()); | ||||
153 | proc.setOutputChannelMode(KProcess::SeparateChannels); | ||||
154 | proc.setProgram(buildDir.mesonExecutable.toLocalFile()); | ||||
155 | proc << QStringLiteral("introspect") << option << QStringLiteral("meson.build"); | ||||
156 | | ||||
157 | int ret = proc.execute(); | ||||
158 | if (ret != 0) { | ||||
159 | return i18n("%1 returned %2", proc.program().join(QChar::fromLatin1(' ')), ret); | ||||
160 | } | ||||
161 | | ||||
162 | QJsonParseError error; | ||||
163 | QJsonDocument doc = QJsonDocument::fromJson(proc.readAll(), &error); | ||||
164 | if (error.error) { | ||||
165 | return i18n("JSON parser error: %1", error.errorString()); | ||||
166 | } | ||||
167 | | ||||
168 | if (doc.isArray()) { | ||||
169 | (*out)[typeStr] = doc.array(); | ||||
170 | } else if (doc.isObject()) { | ||||
171 | (*out)[typeStr] = doc.object(); | ||||
172 | } else { | ||||
173 | return i18n("The introspection output of '%1' contains neither an array nor an object", | ||||
174 | proc.program().join(QChar::fromLatin1(' '))); | ||||
175 | } | ||||
176 | | ||||
177 | return QStringLiteral(""); | ||||
178 | } | ||||
179 | | ||||
180 | QString MesonIntrospectJob::import(BuildDir buildDir) | ||||
181 | { | ||||
182 | QJsonObject rawData; | ||||
183 | | ||||
184 | // First load the complete JSON data | ||||
185 | for (auto i : m_types) { | ||||
186 | QString err; | ||||
187 | switch (m_mode) { | ||||
188 | case BUILD_DIR: | ||||
189 | err = importJSONFile(buildDir, i, &rawData); | ||||
190 | break; | ||||
191 | case MESON_FILE: | ||||
192 | err = importMesonAPI(buildDir, i, &rawData); | ||||
193 | break; | ||||
194 | } | ||||
195 | | ||||
196 | if (!err.isEmpty()) { | ||||
197 | qCWarning(KDEV_Meson) << "MINTRO: " << err; | ||||
198 | setError(true); | ||||
199 | setErrorText(err); | ||||
200 | return err; | ||||
201 | } | ||||
202 | } | ||||
203 | | ||||
204 | auto buildOptionsJSON = rawData[QStringLiteral("buildoptions")]; | ||||
205 | if (buildOptionsJSON.isArray()) { | ||||
206 | m_res_options = std::make_shared<MesonOptions>(buildOptionsJSON.toArray()); | ||||
207 | if (m_res_options) { | ||||
208 | qCDebug(KDEV_Meson) << "MINTRO: Imported " << m_res_options->options().size() << " buildoptions"; | ||||
209 | } else { | ||||
210 | qCWarning(KDEV_Meson) << "MINTRO: Failed to parse buildoptions"; | ||||
211 | } | ||||
212 | } | ||||
213 | | ||||
214 | return QStringLiteral(""); | ||||
215 | } | ||||
216 | | ||||
217 | void MesonIntrospectJob::start() | ||||
218 | { | ||||
219 | qCDebug(KDEV_Meson) << "MINTRO: Starting meson introspection job"; | ||||
220 | if (!m_buildDir.isValid()) { | ||||
221 | qCWarning(KDEV_Meson) << "The current build directory is invalid"; | ||||
222 | setError(true); | ||||
223 | setErrorText(i18n("The current build directory is invalid")); | ||||
224 | emitResult(); | ||||
225 | return; | ||||
226 | } | ||||
227 | | ||||
228 | auto future = QtConcurrent::run(this, &MesonIntrospectJob::import, m_buildDir); | ||||
229 | m_futureWatcher.setFuture(future); | ||||
230 | } | ||||
231 | | ||||
232 | void MesonIntrospectJob::finished() | ||||
233 | { | ||||
234 | qCDebug(KDEV_Meson) << "MINTRO: Meson introspection job finished"; | ||||
235 | emitResult(); | ||||
236 | } | ||||
237 | | ||||
238 | bool MesonIntrospectJob::doKill() | ||||
239 | { | ||||
240 | if (m_futureWatcher.isRunning()) { | ||||
241 | m_futureWatcher.cancel(); | ||||
242 | } | ||||
243 | return true; | ||||
244 | } | ||||
245 | | ||||
246 | MESON_OPT_PTR MesonIntrospectJob::buildOptions() | ||||
247 | { | ||||
248 | return m_res_options; | ||||
249 | } |