Changeset View
Changeset View
Standalone View
Standalone View
plugins/meson/mesonmanager.cpp
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | Copyright 2017 Aleix Pol Gonzalez <aleixpol@kde.org> | ||||
3 | Copyright 2018 Daniel Mensinger <daniel@mensinger-ka.de> | ||||
4 | | ||||
5 | This library is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU Library General Public | ||||
7 | License as published by the Free Software Foundation; either | ||||
8 | version 2 of the License, or (at your option) any later version. | ||||
9 | | ||||
10 | This library is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | Library General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU Library General Public License | ||||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "mesonmanager.h" | ||||
22 | #include "mesonbuilder.h" | ||||
23 | #include "mesonconfig.h" | ||||
24 | #include "mesonimportjob.h" | ||||
25 | #include "settings/mesonconfigpage.h" | ||||
26 | #include "settings/mesonnewbuilddir.h" | ||||
27 | #include <interfaces/iproject.h> | ||||
28 | #include <project/projectconfigpage.h> | ||||
29 | #include <project/projectmodel.h> | ||||
30 | #include <util/executecompositejob.h> | ||||
31 | | ||||
32 | #include <KConfigGroup> | ||||
33 | #include <KLocalizedString> | ||||
34 | #include <KPluginFactory> | ||||
35 | #include <QFileDialog> | ||||
36 | #include <QStandardPaths> | ||||
37 | | ||||
38 | #include "debug.h" | ||||
39 | | ||||
40 | using namespace KDevelop; | ||||
41 | | ||||
42 | static const QString GENERATOR_NINJA = QStringLiteral("ninja"); | ||||
43 | | ||||
44 | K_PLUGIN_FACTORY_WITH_JSON(MesonSupportFactory, "kdevmesonmanager.json", registerPlugin<MesonManager>();) | ||||
45 | | ||||
46 | MesonManager::MesonManager(QObject* parent, const QVariantList& args) | ||||
47 | : AbstractFileManagerPlugin(QStringLiteral("KDevMesonManager"), parent, args) | ||||
48 | , m_builder(new MesonBuilder(this)) | ||||
49 | { | ||||
50 | if (m_builder->hasError()) { | ||||
51 | setErrorDescription(i18n("Meson builder error: %s", m_builder->errorDescription())); | ||||
52 | } | ||||
53 | } | ||||
54 | | ||||
55 | MesonManager::~MesonManager() | ||||
56 | { | ||||
57 | delete m_builder; | ||||
58 | } | ||||
59 | | ||||
60 | // ********************************* | ||||
61 | // * AbstractFileManagerPlugin API * | ||||
62 | // ********************************* | ||||
63 | | ||||
64 | IProjectFileManager::Features MesonManager::features() const | ||||
65 | { | ||||
66 | return IProjectFileManager::Files | IProjectFileManager::Folders | IProjectFileManager::Targets; | ||||
67 | } | ||||
68 | | ||||
69 | ProjectFolderItem* MesonManager::createFolderItem(IProject* project, const Path& path, ProjectBaseItem* parent) | ||||
70 | { | ||||
71 | // TODO: Maybe use meson targets instead | ||||
72 | if (QFile::exists(path.toLocalFile() + QStringLiteral("/meson.build"))) | ||||
73 | return new ProjectBuildFolderItem(project, path, parent); | ||||
74 | else | ||||
75 | return AbstractFileManagerPlugin::createFolderItem(project, path, parent); | ||||
76 | } | ||||
77 | | ||||
78 | // *************************** | ||||
79 | // * IBuildSystemManager API * | ||||
80 | // *************************** | ||||
81 | | ||||
82 | KJob* MesonManager::createImportJob(ProjectFolderItem* item) | ||||
83 | { | ||||
84 | auto project = item->project(); | ||||
85 | auto job = new MesonImportJob(this, project, this); | ||||
86 | | ||||
87 | connect(job, &KJob::result, this, [this, job, project]() { | ||||
88 | if (job->error() != 0) { | ||||
89 | qCWarning(KDEV_Meson) << "couldn't load project successfully" << project->name(); | ||||
90 | m_projects.remove(project); | ||||
91 | } | ||||
92 | }); | ||||
93 | | ||||
94 | const QList<KJob*> jobs = { | ||||
95 | builder()->configure(project), // Make sure the project is configured | ||||
96 | job, // Import the compile_commands.json file | ||||
97 | AbstractFileManagerPlugin::createImportJob(item) // generate the file system listing | ||||
98 | }; | ||||
99 | | ||||
100 | Q_ASSERT(!jobs.contains(nullptr)); | ||||
101 | auto composite = new ExecuteCompositeJob(this, jobs); | ||||
102 | composite->setAbortOnError(false); | ||||
103 | return composite; | ||||
104 | } | ||||
105 | | ||||
106 | Path MesonManager::buildDirectory(ProjectBaseItem* item) const | ||||
107 | { | ||||
108 | Q_ASSERT(item); | ||||
109 | Meson::BuildDir buildDir = Meson::currentBuildDir(item->project()); | ||||
110 | return buildDir.buildDir; | ||||
111 | } | ||||
112 | | ||||
113 | IProjectBuilder* MesonManager::builder() const | ||||
114 | { | ||||
115 | return m_builder; | ||||
116 | } | ||||
117 | | ||||
118 | Meson::BuildDir MesonManager::newBuildDirectory(IProject* project) | ||||
119 | { | ||||
120 | Q_ASSERT(project); | ||||
121 | MesonNewBuildDir newBD(project); | ||||
122 | | ||||
123 | if (!newBD.exec() || !newBD.isConfigValid()) { | ||||
124 | qCWarning(KDEV_Meson) << "Failed to create new build directory for project " << project->name(); | ||||
125 | return Meson::BuildDir(); | ||||
126 | } | ||||
127 | | ||||
128 | Meson::BuildDir buildDir = newBD.currentConfig(); | ||||
129 | Meson::MesonConfig mesonCfg = Meson::getMesonConfig(project); | ||||
130 | buildDir.canonicalizePaths(); | ||||
131 | mesonCfg.addBuildDir(buildDir); | ||||
132 | Meson::writeMesonConfig(project, mesonCfg); | ||||
133 | | ||||
134 | return buildDir; | ||||
135 | } | ||||
136 | | ||||
137 | QStringList MesonManager::supportedMesonBackends() const | ||||
138 | { | ||||
139 | // Maybe add support for other generators | ||||
140 | return { GENERATOR_NINJA }; | ||||
141 | } | ||||
142 | | ||||
143 | QString MesonManager::defaultMesonBackend() const | ||||
144 | { | ||||
145 | return GENERATOR_NINJA; | ||||
146 | } | ||||
147 | | ||||
148 | Path MesonManager::findMeson() const | ||||
149 | { | ||||
150 | QString mesonPath; | ||||
151 | | ||||
152 | const static QStringList mesonExecutables = { QStringLiteral("meson"), QStringLiteral("meson.py") }; | ||||
153 | const static QStringList mesonPaths | ||||
154 | = { QStringLiteral("%1/.local/bin").arg(QStandardPaths::standardLocations(QStandardPaths::HomeLocation)[0]) }; | ||||
155 | | ||||
156 | for (auto const& i : mesonExecutables) { | ||||
157 | mesonPath = QStandardPaths::findExecutable(i); | ||||
158 | if (!mesonPath.isEmpty()) { | ||||
159 | break; | ||||
160 | } | ||||
161 | | ||||
162 | mesonPath = QStandardPaths::findExecutable(i, mesonPaths); | ||||
163 | if (!mesonPath.isEmpty()) { | ||||
164 | break; | ||||
165 | } | ||||
166 | } | ||||
167 | | ||||
168 | return Path(mesonPath); | ||||
169 | } | ||||
170 | | ||||
171 | void MesonManager::setProjectData(IProject* project, const QJsonObject& data) | ||||
172 | { | ||||
173 | m_projects[project] = data; | ||||
174 | } | ||||
175 | | ||||
176 | ConfigPage* MesonManager::perProjectConfigPage(int number, const ProjectConfigOptions& options, QWidget* parent) | ||||
177 | { | ||||
178 | if (number == 0) { | ||||
179 | return new MesonConfigPage(this, options.project, parent); | ||||
180 | } | ||||
181 | return nullptr; | ||||
182 | } | ||||
183 | | ||||
184 | int MesonManager::perProjectConfigPages() const | ||||
185 | { | ||||
186 | return 1; | ||||
187 | } | ||||
188 | | ||||
189 | #include "mesonmanager.moc" |