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 | } | ||||
51 | | ||||
52 | MesonManager::~MesonManager() | ||||
53 | { | ||||
54 | delete m_builder; | ||||
55 | } | ||||
56 | | ||||
57 | // ********************************* | ||||
58 | // * AbstractFileManagerPlugin API * | ||||
59 | // ********************************* | ||||
60 | | ||||
61 | IProjectFileManager::Features MesonManager::features() const | ||||
62 | { | ||||
63 | return IProjectFileManager::Files | IProjectFileManager::Folders | IProjectFileManager::Targets; | ||||
64 | } | ||||
65 | | ||||
66 | ProjectFolderItem* MesonManager::createFolderItem(IProject* project, const Path& path, ProjectBaseItem* parent) | ||||
67 | { | ||||
68 | // TODO: Maybe use meson targets instead | ||||
69 | if (QFile::exists(path.toLocalFile() + QStringLiteral("/meson.build"))) | ||||
70 | return new ProjectBuildFolderItem(project, path, parent); | ||||
71 | else | ||||
72 | return AbstractFileManagerPlugin::createFolderItem(project, path, parent); | ||||
73 | } | ||||
74 | | ||||
75 | // *************************** | ||||
76 | // * IBuildSystemManager API * | ||||
77 | // *************************** | ||||
78 | | ||||
79 | KJob* MesonManager::createImportJob(ProjectFolderItem* item) | ||||
80 | { | ||||
81 | auto project = item->project(); | ||||
82 | auto job = new MesonImportJob(this, project, this); | ||||
83 | | ||||
84 | connect(job, &KJob::result, this, [this, job, project]() { | ||||
85 | if (job->error() != 0) { | ||||
86 | qCWarning(KDEV_Meson) << "couldn't load project successfully" << project->name(); | ||||
87 | m_projects.remove(project); | ||||
88 | } | ||||
89 | }); | ||||
90 | | ||||
91 | const QList<KJob*> jobs = { | ||||
92 | builder()->configure(project), // Make sure the project is configured | ||||
93 | job, // Import the compile_commands.json file | ||||
94 | AbstractFileManagerPlugin::createImportJob(item) // generate the file system listing | ||||
95 | }; | ||||
96 | | ||||
97 | Q_ASSERT(!jobs.contains(nullptr)); | ||||
98 | auto composite = new ExecuteCompositeJob(this, jobs); | ||||
99 | composite->setAbortOnError(false); | ||||
100 | return composite; | ||||
101 | } | ||||
102 | | ||||
103 | Path MesonManager::buildDirectory(ProjectBaseItem* item) const | ||||
104 | { | ||||
105 | Q_ASSERT(item); | ||||
106 | Meson::BuildDir buildDir = Meson::currentBuildDir(item->project()); | ||||
107 | return buildDir.buildDir; | ||||
108 | } | ||||
109 | | ||||
110 | IProjectBuilder* MesonManager::builder() const | ||||
111 | { | ||||
112 | return m_builder; | ||||
113 | } | ||||
114 | | ||||
115 | Meson::BuildDir MesonManager::newBuildDirectory(IProject* project) | ||||
116 | { | ||||
117 | Q_ASSERT(project); | ||||
118 | MesonNewBuildDir newBD(project); | ||||
119 | | ||||
120 | if (!newBD.exec() || !newBD.isConfigValid()) { | ||||
121 | qCWarning(KDEV_Meson) << "Failed to create new build directory for project " << project->name(); | ||||
122 | return Meson::BuildDir(); | ||||
123 | } | ||||
124 | | ||||
125 | Meson::BuildDir buildDir = newBD.currentConfig(); | ||||
126 | Meson::MesonConfig mesonCfg = Meson::getMesonConfig(project); | ||||
127 | mesonCfg.addBuildDir(buildDir); | ||||
128 | Meson::writeMesonConfig(project, mesonCfg); | ||||
129 | | ||||
130 | return buildDir; | ||||
131 | } | ||||
132 | | ||||
133 | QStringList MesonManager::supportedMesonBackends() const | ||||
134 | { | ||||
135 | // Maybe add support for other generators | ||||
136 | return { GENERATOR_NINJA }; | ||||
137 | } | ||||
138 | | ||||
139 | QString MesonManager::defaultMesonBackend() const | ||||
140 | { | ||||
141 | return GENERATOR_NINJA; | ||||
142 | } | ||||
143 | | ||||
144 | Path MesonManager::findMeson() const | ||||
145 | { | ||||
146 | QString mesonPath; | ||||
147 | | ||||
148 | const static QStringList mesonExecutables = { QStringLiteral("meson"), QStringLiteral("meson.py") }; | ||||
149 | const static QStringList mesonPaths | ||||
150 | = { QStringLiteral("%1/.local/bin").arg(QStandardPaths::standardLocations(QStandardPaths::HomeLocation)[0]) }; | ||||
151 | | ||||
152 | for (auto const& i : mesonExecutables) { | ||||
153 | mesonPath = QStandardPaths::findExecutable(i); | ||||
154 | if (!mesonPath.isEmpty()) { | ||||
155 | break; | ||||
156 | } | ||||
157 | | ||||
158 | mesonPath = QStandardPaths::findExecutable(i, mesonPaths); | ||||
159 | if (!mesonPath.isEmpty()) { | ||||
160 | break; | ||||
161 | } | ||||
162 | } | ||||
163 | | ||||
164 | return Path(mesonPath); | ||||
165 | } | ||||
166 | | ||||
167 | void MesonManager::setProjectData(IProject* project, const QJsonObject& data) | ||||
168 | { | ||||
169 | m_projects[project] = data; | ||||
170 | } | ||||
171 | | ||||
172 | ConfigPage* MesonManager::perProjectConfigPage(int number, const ProjectConfigOptions& options, QWidget* parent) | ||||
173 | { | ||||
174 | if (number == 0) { | ||||
175 | return new MesonConfigPage(this, options.project, parent); | ||||
176 | } | ||||
177 | return nullptr; | ||||
178 | } | ||||
179 | | ||||
180 | int MesonManager::perProjectConfigPages() const | ||||
181 | { | ||||
182 | return 1; | ||||
183 | } | ||||
184 | | ||||
185 | #include "mesonmanager.moc" |