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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
/* KDevelop QMake Support
*
* Copyright 2006 Andreas Pakulat <apaku@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "qmakemanager.h"
#include <QAction>
#include <QDir>
#include <QIcon>
#include <QFileInfo>
#include <QHash>
#include <QList>
#include <QUrl>
#include <KIO/Global>
#include <KConfigGroup>
#include <KDirWatch>
#include <KLocalizedString>
#include <KPluginFactory>
#include <interfaces/icore.h>
#include <interfaces/iproject.h>
#include <interfaces/contextmenuextension.h>
#include <interfaces/context.h>
#include <interfaces/iruncontroller.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iplugincontroller.h>
#include <project/projectmodel.h>
#include <serialization/indexedstring.h>
#include <qmakebuilder/iqmakebuilder.h>
#include "qmakemodelitems.h"
#include "qmakeprojectfile.h"
#include "qmakecache.h"
#include "qmakemkspecs.h"
#include "qmakejob.h"
#include "qmakebuilddirchooserdialog.h"
#include "qmakeconfig.h"
#include "qmakeutils.h"
#include <debug.h>
using namespace KDevelop;
// BEGIN Helpers
QMakeFolderItem* findQMakeFolderParent(ProjectBaseItem* item)
{
QMakeFolderItem* p = nullptr;
while (!p && item) {
p = dynamic_cast<QMakeFolderItem*>(item);
item = item->parent();
}
return p;
}
// END Helpers
K_PLUGIN_FACTORY_WITH_JSON(QMakeSupportFactory, "kdevqmakemanager.json", registerPlugin<QMakeProjectManager>();)
QMakeProjectManager::QMakeProjectManager(QObject* parent, const QVariantList&)
: AbstractFileManagerPlugin(QStringLiteral("kdevqmakemanager"), parent)
, IBuildSystemManager()
{
IPlugin* i = core()->pluginController()->pluginForExtension(QStringLiteral("org.kdevelop.IQMakeBuilder"));
Q_ASSERT(i);
m_builder = i->extension<IQMakeBuilder>();
Q_ASSERT(m_builder);
connect(this, SIGNAL(folderAdded(KDevelop::ProjectFolderItem*)), this,
SLOT(slotFolderAdded(KDevelop::ProjectFolderItem*)));
m_runQMake = new QAction(QIcon::fromTheme(QStringLiteral("qtlogo")), i18n("Run QMake"), this);
connect(m_runQMake, &QAction::triggered, this, &QMakeProjectManager::slotRunQMake);
}
QMakeProjectManager::~QMakeProjectManager()
{
}
IProjectFileManager::Features QMakeProjectManager::features() const
{
return Features(Folders | Targets | Files);
}
bool QMakeProjectManager::isValid(const Path& path, const bool isFolder, IProject* project) const
{
if (!isFolder && path.lastPathSegment().startsWith(QLatin1String("Makefile"))) {
return false;
}
return AbstractFileManagerPlugin::isValid(path, isFolder, project);
}
Path QMakeProjectManager::buildDirectory(ProjectBaseItem* item) const
{
/// TODO: support includes by some other parent or sibling in a different file-tree-branch
QMakeFolderItem* qmakeItem = findQMakeFolderParent(item);
Path dir;
if (qmakeItem) {
if (!qmakeItem->parent()) {
// build root item
dir = QMakeConfig::buildDirFromSrc(qmakeItem->project(), qmakeItem->path());
} else {
// build sub-item
foreach (QMakeProjectFile* pro, qmakeItem->projectFiles()) {
if (QDir(pro->absoluteDir()) == QFileInfo(qmakeItem->path().toUrl().toLocalFile() + QLatin1Char('/')).absoluteDir()
|| pro->hasSubProject(qmakeItem->path().toUrl().toLocalFile())) {
// get path from project root and it to buildDir
dir = QMakeConfig::buildDirFromSrc(qmakeItem->project(), Path(pro->absoluteDir()));
break;
}
}
}
}
qCDebug(KDEV_QMAKE) << "build dir for" << item->text() << item->path() << "is:" << dir;
return dir;
}
ProjectFolderItem* QMakeProjectManager::createFolderItem(IProject* project, const Path& path, ProjectBaseItem* parent)
{
if (!parent) {
return projectRootItem(project, path);
} else if (ProjectFolderItem* buildFolder = buildFolderItem(project, path, parent)) {
// child folder in a qmake folder
return buildFolder;
} else {
return AbstractFileManagerPlugin::createFolderItem(project, path, parent);
}
}
ProjectFolderItem* QMakeProjectManager::projectRootItem(IProject* project, const Path& path)
{
QDir dir(path.toLocalFile());
auto item = new QMakeFolderItem(project, path);
QHash<QString, QString> qmvars = QMakeUtils::queryQMake(project);
const QString mkSpecFile = QMakeConfig::findBasicMkSpec(qmvars);
Q_ASSERT(!mkSpecFile.isEmpty());
QMakeMkSpecs* mkspecs = new QMakeMkSpecs(mkSpecFile, qmvars);
mkspecs->setProject(project);
mkspecs->read();
QMakeCache* cache = findQMakeCache(project);
if (cache) {
cache->setMkSpecs(mkspecs);
cache->read();
}
QStringList projectfiles = dir.entryList(QStringList() << QStringLiteral("*.pro"));
for (const auto& projectfile : projectfiles) {
Path proPath(path, projectfile);
/// TODO: use Path in QMakeProjectFile
QMakeProjectFile* scope = new QMakeProjectFile(proPath.toLocalFile());
scope->setProject(project);
scope->setMkSpecs(mkspecs);
if (cache) {
scope->setQMakeCache(cache);
}
scope->read();
qCDebug(KDEV_QMAKE) << "top-level scope with variables:" << scope->variables();
item->addProjectFile(scope);
}
return item;
}
ProjectFolderItem* QMakeProjectManager::buildFolderItem(IProject* project, const Path& path, ProjectBaseItem* parent)
{
// find .pro or .pri files in dir
QDir dir(path.toLocalFile());
const QStringList projectFiles = dir.entryList(QStringList{QStringLiteral("*.pro"), QStringLiteral("*.pri")},
QDir::Files);
if (projectFiles.isEmpty()) {
return nullptr;
}
auto folderItem = new QMakeFolderItem(project, path, parent);
// TODO: included by not-parent file (in a nother file-tree-branch).
QMakeFolderItem* qmakeParent = findQMakeFolderParent(parent);
if (!qmakeParent) {
// happens for bad qmake configurations
return nullptr;
}
for (const QString& file : projectFiles) {
const QString absFile = dir.absoluteFilePath(file);
// TODO: multiple includes by different .pro's
QMakeProjectFile* parentPro = nullptr;
foreach (QMakeProjectFile* p, qmakeParent->projectFiles()) {
if (p->hasSubProject(absFile)) {
parentPro = p;
break;
}
}
if (!parentPro && file.endsWith(QLatin1String(".pri"))) {
continue;
}
qCDebug(KDEV_QMAKE) << "add project file:" << absFile;
if (parentPro) {
qCDebug(KDEV_QMAKE) << "parent:" << parentPro->absoluteFile();
} else {
qCDebug(KDEV_QMAKE) << "no parent, assume project root";
}
auto qmscope = new QMakeProjectFile(absFile);
qmscope->setProject(project);
const QFileInfo info(absFile);
const QDir d = info.dir();
/// TODO: cleanup
if (parentPro) {
// subdir
if (QMakeCache* cache = findQMakeCache(project, Path(d.canonicalPath()))) {
cache->setMkSpecs(parentPro->mkSpecs());
cache->read();
qmscope->setQMakeCache(cache);
} else {
qmscope->setQMakeCache(parentPro->qmakeCache());
}
qmscope->setMkSpecs(parentPro->mkSpecs());
} else {
// new project
QMakeFolderItem* root = dynamic_cast<QMakeFolderItem*>(project->projectItem());
Q_ASSERT(root);
qmscope->setMkSpecs(root->projectFiles().first()->mkSpecs());
if (root->projectFiles().first()->qmakeCache()) {
qmscope->setQMakeCache(root->projectFiles().first()->qmakeCache());
}
}
if (qmscope->read()) {
// TODO: only on read?
folderItem->addProjectFile(qmscope);
} else {
delete qmscope;
return nullptr;
}
}
return folderItem;
}
void QMakeProjectManager::slotFolderAdded(ProjectFolderItem* folder)
{
QMakeFolderItem* qmakeParent = dynamic_cast<QMakeFolderItem*>(folder);
if (!qmakeParent) {
return;
}
qCDebug(KDEV_QMAKE) << "adding targets for" << folder->path();
foreach (QMakeProjectFile* pro, qmakeParent->projectFiles()) {
foreach (const QString& s, pro->targets()) {
if (!isValid(Path(folder->path(), s), false, folder->project())) {
continue;
}
qCDebug(KDEV_QMAKE) << "adding target:" << s;
Q_ASSERT(!s.isEmpty());
auto target = new QMakeTargetItem(pro, folder->project(), s, folder);
foreach (const QString& path, pro->filesForTarget(s)) {
new ProjectFileItem(folder->project(), Path(path), target);
/// TODO: signal?
}
}
}
}
ProjectFolderItem* QMakeProjectManager::import(IProject* project)
{
const Path dirName = project->path();
if (dirName.isRemote()) {
// FIXME turn this into a real warning
qCWarning(KDEV_QMAKE) << "not a local file. QMake support doesn't handle remote projects";
return nullptr;
}
QMakeUtils::checkForNeedingConfigure(project);
ProjectFolderItem* ret = AbstractFileManagerPlugin::import(project);
connect(projectWatcher(project), &KDirWatch::dirty, this, &QMakeProjectManager::slotDirty);
return ret;
}
void QMakeProjectManager::slotDirty(const QString& path)
{
if (!path.endsWith(QLatin1String(".pro")) && !path.endsWith(QLatin1String(".pri"))) {
return;
}
QFileInfo info(path);
if (!info.isFile()) {
return;
}
const QUrl url = QUrl::fromLocalFile(path);
if (!isValid(Path(url), false, nullptr)) {
return;
}
IProject* project = ICore::self()->projectController()->findProjectForUrl(url);
if (!project) {
// this can happen when we create/remove lots of files in a
// sub dir of a project - ignore such cases for now
return;
}
bool finished = false;
foreach (ProjectFolderItem* folder, project->foldersForPath(IndexedString(KIO::upUrl(url)))) {
if (QMakeFolderItem* qmakeFolder = dynamic_cast<QMakeFolderItem*>(folder)) {
foreach (QMakeProjectFile* pro, qmakeFolder->projectFiles()) {
if (pro->absoluteFile() == path) {
// TODO: children
// TODO: cache added
qCDebug(KDEV_QMAKE) << "reloading" << pro << path;
pro->read();
}
}
finished = true;
} else if (ProjectFolderItem* newFolder = buildFolderItem(project, folder->path(), folder->parent())) {
qCDebug(KDEV_QMAKE) << "changing from normal folder to qmake project folder:" << folder->path().toUrl();
// .pro / .pri file did not exist before
while (folder->rowCount()) {
newFolder->appendRow(folder->takeRow(0));
}
folder->parent()->removeRow(folder->row());
folder = newFolder;
finished = true;
}
if (finished) {
// remove existing targets and readd them
for (int i = 0; i < folder->rowCount(); ++i) {
if (folder->child(i)->target()) {
folder->removeRow(i);
}
}
/// TODO: put into it's own function once we add more stuff to that slot
slotFolderAdded(folder);
break;
}
}
}
QList<ProjectTargetItem*> QMakeProjectManager::targets(ProjectFolderItem* item) const
{
Q_UNUSED(item)
return QList<ProjectTargetItem*>();
}
IProjectBuilder* QMakeProjectManager::builder() const
{
Q_ASSERT(m_builder);
return m_builder;
}
Path::List QMakeProjectManager::collectDirectories(ProjectBaseItem* item, const bool collectIncludes) const
{
Path::List list;
QMakeFolderItem* folder = findQMakeFolderParent(item);
if (folder) {
foreach (QMakeProjectFile* pro, folder->projectFiles()) {
if (pro->files().contains(item->path().toLocalFile())) {
const QStringList directories = collectIncludes ? pro->includeDirectories() : pro->frameworkDirectories();
for (const QString& dir : directories) {
Path path(dir);
if (!list.contains(path)) {
list << path;
}
}
}
}
if (list.isEmpty()) {
// fallback for new files, use all possible include dirs
foreach (QMakeProjectFile* pro, folder->projectFiles()) {
const QStringList directories = collectIncludes ? pro->includeDirectories() : pro->frameworkDirectories();
for (const QString& dir : directories) {
Path path(dir);
if (!list.contains(path)) {
list << path;
}
}
}
}
// make sure the base dir is included
if (!list.contains(folder->path())) {
list << folder->path();
}
// qCDebug(KDEV_QMAKE) << "include dirs for" << item->path() << ":" << list;
}
return list;
}
Path::List QMakeProjectManager::includeDirectories(ProjectBaseItem* item) const
{
return collectDirectories(item);
}
Path::List QMakeProjectManager::frameworkDirectories(ProjectBaseItem* item) const
{
return collectDirectories(item, false);
}
QHash<QString, QString> QMakeProjectManager::defines(ProjectBaseItem* item) const
{
QHash<QString, QString> d;
QMakeFolderItem* folder = findQMakeFolderParent(item);
if (!folder) {
// happens for bad qmake configurations
return d;
}
foreach (QMakeProjectFile* pro, folder->projectFiles()) {
foreach (QMakeProjectFile::DefinePair def, pro->defines()) {
d.insert(def.first, def.second);
}
}
return d;
}
QString QMakeProjectManager::extraArguments(KDevelop::ProjectBaseItem *item) const
{
QMakeFolderItem* folder = findQMakeFolderParent(item);
if (!folder) {
// happens for bad qmake configurations
return {};
}
QStringList d;
foreach (QMakeProjectFile* pro, folder->projectFiles()) {
d << pro->extraArguments();
}
return d.join(QLatin1Char(' '));
}
bool QMakeProjectManager::hasBuildInfo(KDevelop::ProjectBaseItem* item) const
{
return findQMakeFolderParent(item);
}
QMakeCache* QMakeProjectManager::findQMakeCache(IProject* project, const Path& path) const
{
QDir curdir(QMakeConfig::buildDirFromSrc(project, !path.isValid() ? project->path() : path).toLocalFile());
curdir.makeAbsolute();
while (!curdir.exists(QStringLiteral(".qmake.cache")) && !curdir.isRoot() && curdir.cdUp()) {
qCDebug(KDEV_QMAKE) << curdir;
}
if (curdir.exists(QStringLiteral(".qmake.cache"))) {
qCDebug(KDEV_QMAKE) << "Found QMake cache in " << curdir.absolutePath();
return new QMakeCache(curdir.canonicalPath() + QLatin1String("/.qmake.cache"));
}
return nullptr;
}
ContextMenuExtension QMakeProjectManager::contextMenuExtension(Context* context, QWidget* parent)
{
Q_UNUSED(parent);
ContextMenuExtension ext;
if (context->hasType(Context::ProjectItemContext)) {
ProjectItemContext* pic = dynamic_cast<ProjectItemContext*>(context);
Q_ASSERT(pic);
if (pic->items().isEmpty()) {
return ext;
}
m_actionItem = dynamic_cast<QMakeFolderItem*>(pic->items().first());
if (m_actionItem) {
ext.addAction(ContextMenuExtension::ProjectGroup, m_runQMake);
}
}
return ext;
}
void QMakeProjectManager::slotRunQMake()
{
Q_ASSERT(m_actionItem);
Path srcDir = m_actionItem->path();
Path buildDir = QMakeConfig::buildDirFromSrc(m_actionItem->project(), srcDir);
QMakeJob* job = new QMakeJob(srcDir.toLocalFile(), buildDir.toLocalFile(), this);
job->setQMakePath(QMakeConfig::qmakeExecutable(m_actionItem->project()));
KConfigGroup cg(m_actionItem->project()->projectConfiguration(), QMakeConfig::CONFIG_GROUP);
QString installPrefix = cg.readEntry(QMakeConfig::INSTALL_PREFIX, QString());
if (!installPrefix.isEmpty())
job->setInstallPrefix(installPrefix);
job->setBuildType(cg.readEntry<int>(QMakeConfig::BUILD_TYPE, 0));
job->setExtraArguments(cg.readEntry(QMakeConfig::EXTRA_ARGUMENTS, QString()));
KDevelop::ICore::self()->runController()->registerJob(job);
}
#include "qmakemanager.moc"
|