2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cmakeprojectmanager.h"
|
2020-04-17 15:30:05 +02:00
|
|
|
|
|
|
|
#include "cmakebuildsystem.h"
|
2016-03-10 12:01:30 +01:00
|
|
|
#include "cmakekitinformation.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cmakeproject.h"
|
2020-04-17 15:30:05 +02:00
|
|
|
#include "cmakeprojectconstants.h"
|
2019-05-26 23:48:40 +03:00
|
|
|
#include "cmakeprojectnodes.h"
|
2020-06-10 10:58:35 +02:00
|
|
|
#include "fileapiparser.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2019-08-09 11:22:49 +02:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
2011-02-23 15:52:43 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2019-05-26 23:48:40 +03:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2019-08-09 11:22:49 +02:00
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2020-06-10 10:58:35 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2019-08-09 11:22:49 +02:00
|
|
|
#include <coreplugin/messagemanager.h>
|
2015-07-14 13:10:18 +02:00
|
|
|
#include <projectexplorer/buildmanager.h>
|
2015-01-13 15:50:37 +01:00
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
2015-02-26 13:38:54 +01:00
|
|
|
#include <projectexplorer/projecttree.h>
|
2015-07-14 13:10:18 +02:00
|
|
|
#include <projectexplorer/session.h>
|
2016-01-20 12:19:16 +01:00
|
|
|
#include <projectexplorer/target.h>
|
2015-02-26 13:38:54 +01:00
|
|
|
|
2019-05-26 23:48:40 +03:00
|
|
|
#include <utils/parameteraction.h>
|
|
|
|
|
2015-02-26 13:38:54 +01:00
|
|
|
#include <QAction>
|
2020-06-10 10:58:35 +02:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2016-01-08 14:29:36 +01:00
|
|
|
using namespace ProjectExplorer;
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace CMakeProjectManager::Internal;
|
|
|
|
|
2020-06-10 10:58:35 +02:00
|
|
|
CMakeManager::CMakeManager()
|
|
|
|
: m_runCMakeAction(new QAction(QIcon(), tr("Run CMake"), this))
|
|
|
|
, m_clearCMakeCacheAction(new QAction(QIcon(), tr("Clear CMake Configuration"), this))
|
|
|
|
, m_runCMakeActionContextMenu(new QAction(QIcon(), tr("Run CMake"), this))
|
|
|
|
, m_rescanProjectAction(new QAction(QIcon(), tr("Rescan Project"), this))
|
|
|
|
, m_parseAndValidateCMakeReplyFileAction(
|
2020-07-02 09:08:26 +02:00
|
|
|
new QAction(QIcon(), tr("Parse and verify a CMake reply file."), this))
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-02-23 15:52:43 +01:00
|
|
|
Core::ActionContainer *mbuild =
|
2012-05-24 13:49:06 +02:00
|
|
|
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
|
2011-02-23 15:52:43 +01:00
|
|
|
Core::ActionContainer *mproject =
|
2012-05-24 13:49:06 +02:00
|
|
|
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
|
2011-02-23 15:52:43 +01:00
|
|
|
Core::ActionContainer *msubproject =
|
2012-05-24 13:49:06 +02:00
|
|
|
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
|
2019-05-26 23:48:40 +03:00
|
|
|
Core::ActionContainer *mfile =
|
|
|
|
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_FILECONTEXT);
|
2011-02-23 15:52:43 +01:00
|
|
|
|
2020-04-07 14:44:22 +02:00
|
|
|
const Core::Context projectContext(CMakeProjectManager::Constants::CMAKE_PROJECT_ID);
|
2016-03-10 12:01:30 +01:00
|
|
|
const Core::Context globalContext(Core::Constants::C_GLOBAL);
|
2011-04-12 12:17:19 +02:00
|
|
|
|
2012-05-24 13:49:06 +02:00
|
|
|
Core::Command *command = Core::ActionManager::registerAction(m_runCMakeAction,
|
2020-04-07 14:44:22 +02:00
|
|
|
Constants::RUN_CMAKE,
|
|
|
|
globalContext);
|
2011-02-23 15:52:43 +01:00
|
|
|
command->setAttribute(Core::Command::CA_Hide);
|
2012-04-26 19:28:59 +02:00
|
|
|
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_DEPLOY);
|
2014-09-12 14:09:40 +04:00
|
|
|
connect(m_runCMakeAction, &QAction::triggered, [this]() {
|
2019-10-25 09:55:32 +02:00
|
|
|
runCMake(SessionManager::startupBuildSystem());
|
2014-09-12 14:09:40 +04:00
|
|
|
});
|
2011-02-23 15:52:43 +01:00
|
|
|
|
2016-03-10 12:01:30 +01:00
|
|
|
command = Core::ActionManager::registerAction(m_clearCMakeCacheAction,
|
2020-04-07 14:44:22 +02:00
|
|
|
Constants::CLEAR_CMAKE_CACHE,
|
|
|
|
globalContext);
|
2016-03-10 12:01:30 +01:00
|
|
|
command->setAttribute(Core::Command::CA_Hide);
|
|
|
|
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_DEPLOY);
|
|
|
|
connect(m_clearCMakeCacheAction, &QAction::triggered, [this]() {
|
2019-10-25 09:55:32 +02:00
|
|
|
clearCMakeCache(SessionManager::startupBuildSystem());
|
2016-03-10 12:01:30 +01:00
|
|
|
});
|
|
|
|
|
2012-05-24 13:49:06 +02:00
|
|
|
command = Core::ActionManager::registerAction(m_runCMakeActionContextMenu,
|
2020-04-07 14:44:22 +02:00
|
|
|
Constants::RUN_CMAKE_CONTEXT_MENU,
|
|
|
|
projectContext);
|
2011-02-23 15:52:43 +01:00
|
|
|
command->setAttribute(Core::Command::CA_Hide);
|
|
|
|
mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
|
|
|
|
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
|
2014-09-12 14:09:40 +04:00
|
|
|
connect(m_runCMakeActionContextMenu, &QAction::triggered, [this]() {
|
2019-10-25 09:55:32 +02:00
|
|
|
runCMake(ProjectTree::currentBuildSystem());
|
2014-09-12 14:09:40 +04:00
|
|
|
});
|
2011-02-23 15:52:43 +01:00
|
|
|
|
2019-05-26 23:48:40 +03:00
|
|
|
m_buildFileContextMenu = new QAction(tr("Build"), this);
|
|
|
|
command = Core::ActionManager::registerAction(m_buildFileContextMenu,
|
2020-04-07 14:44:22 +02:00
|
|
|
Constants::BUILD_FILE_CONTEXT_MENU,
|
|
|
|
projectContext);
|
2019-05-26 23:48:40 +03:00
|
|
|
command->setAttribute(Core::Command::CA_Hide);
|
|
|
|
mfile->addAction(command, ProjectExplorer::Constants::G_FILE_OTHER);
|
|
|
|
connect(m_buildFileContextMenu, &QAction::triggered,
|
|
|
|
this, &CMakeManager::buildFileContextMenu);
|
|
|
|
|
2016-12-04 03:36:12 +10:00
|
|
|
command = Core::ActionManager::registerAction(m_rescanProjectAction,
|
2020-04-07 14:44:22 +02:00
|
|
|
Constants::RESCAN_PROJECT,
|
|
|
|
globalContext);
|
2016-12-04 03:36:12 +10:00
|
|
|
command->setAttribute(Core::Command::CA_Hide);
|
|
|
|
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_DEPLOY);
|
|
|
|
connect(m_rescanProjectAction, &QAction::triggered, [this]() {
|
2019-10-25 09:55:32 +02:00
|
|
|
rescanProject(ProjectTree::currentBuildSystem());
|
2016-12-04 03:36:12 +10:00
|
|
|
});
|
|
|
|
|
2020-04-07 14:44:22 +02:00
|
|
|
m_buildFileAction = new Utils::ParameterAction(tr("Build File"),
|
|
|
|
tr("Build File \"%1\""),
|
|
|
|
Utils::ParameterAction::AlwaysEnabled,
|
|
|
|
this);
|
|
|
|
command = Core::ActionManager::registerAction(m_buildFileAction, Constants::BUILD_FILE);
|
2019-05-26 23:48:40 +03:00
|
|
|
command->setAttribute(Core::Command::CA_Hide);
|
|
|
|
command->setAttribute(Core::Command::CA_UpdateText);
|
|
|
|
command->setDescription(m_buildFileAction->text());
|
|
|
|
command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+B")));
|
|
|
|
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
|
|
|
|
connect(m_buildFileAction, &QAction::triggered, this, [this] { buildFile(); });
|
|
|
|
|
2020-06-10 10:58:35 +02:00
|
|
|
command = Core::ActionManager::registerAction(m_parseAndValidateCMakeReplyFileAction,
|
|
|
|
"CMakeProject.Debug.ParseAndVerifyReplyFile");
|
|
|
|
connect(m_parseAndValidateCMakeReplyFileAction,
|
|
|
|
&QAction::triggered,
|
|
|
|
this,
|
|
|
|
&CMakeManager::parseAndValidateCMakeReplyFile);
|
|
|
|
|
2016-01-08 14:29:36 +01:00
|
|
|
connect(SessionManager::instance(), &SessionManager::startupProjectChanged,
|
2016-03-10 12:01:30 +01:00
|
|
|
this, &CMakeManager::updateCmakeActions);
|
2016-01-08 14:29:36 +01:00
|
|
|
connect(BuildManager::instance(), &BuildManager::buildStateChanged,
|
2016-03-10 12:01:30 +01:00
|
|
|
this, &CMakeManager::updateCmakeActions);
|
2019-05-26 23:48:40 +03:00
|
|
|
connect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
|
|
|
|
this, &CMakeManager::updateBuildFileAction);
|
|
|
|
connect(ProjectTree::instance(), &ProjectTree::currentNodeChanged,
|
|
|
|
this, &CMakeManager::updateCmakeActions);
|
2015-07-14 13:10:18 +02:00
|
|
|
|
2016-03-10 12:01:30 +01:00
|
|
|
updateCmakeActions();
|
2011-02-23 15:52:43 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 12:01:30 +01:00
|
|
|
void CMakeManager::updateCmakeActions()
|
2011-02-23 15:52:43 +01:00
|
|
|
{
|
2016-01-08 14:29:36 +01:00
|
|
|
auto project = qobject_cast<CMakeProject *>(SessionManager::startupProject());
|
2016-03-10 12:01:30 +01:00
|
|
|
const bool visible = project && !BuildManager::isBuilding(project);
|
|
|
|
m_runCMakeAction->setVisible(visible);
|
|
|
|
m_clearCMakeCacheAction->setVisible(visible);
|
2016-12-04 03:36:12 +10:00
|
|
|
m_rescanProjectAction->setVisible(visible);
|
2019-05-26 23:48:40 +03:00
|
|
|
enableBuildFileMenus(ProjectTree::currentNode());
|
2016-03-10 12:01:30 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
void CMakeManager::clearCMakeCache(BuildSystem *buildSystem)
|
2016-03-10 12:01:30 +01:00
|
|
|
{
|
2019-10-25 09:55:32 +02:00
|
|
|
auto cmakeBuildSystem = dynamic_cast<CMakeBuildSystem *>(buildSystem);
|
|
|
|
QTC_ASSERT(cmakeBuildSystem, return);
|
2016-03-10 12:01:30 +01:00
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
cmakeBuildSystem->clearCMakeCache();
|
2011-02-23 15:52:43 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
void CMakeManager::runCMake(BuildSystem *buildSystem)
|
2011-02-23 15:52:43 +01:00
|
|
|
{
|
2019-10-25 09:55:32 +02:00
|
|
|
auto cmakeBuildSystem = dynamic_cast<CMakeBuildSystem *>(buildSystem);
|
2019-12-06 11:29:36 +01:00
|
|
|
QTC_ASSERT(cmakeBuildSystem, return );
|
2011-02-23 15:52:43 +01:00
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
if (ProjectExplorerPlugin::saveModifiedFiles())
|
|
|
|
cmakeBuildSystem->runCMake();
|
2009-01-21 16:25:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
void CMakeManager::rescanProject(BuildSystem *buildSystem)
|
2016-12-04 03:36:12 +10:00
|
|
|
{
|
2019-10-25 09:55:32 +02:00
|
|
|
auto cmakeBuildSystem = dynamic_cast<CMakeBuildSystem *>(buildSystem);
|
|
|
|
QTC_ASSERT(cmakeBuildSystem, return);
|
2016-12-04 03:36:12 +10:00
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
cmakeBuildSystem->runCMakeAndScanProjectTree();// by my experience: every rescan run requires cmake run too
|
2016-12-04 03:36:12 +10:00
|
|
|
}
|
2019-05-26 23:48:40 +03:00
|
|
|
|
|
|
|
void CMakeManager::updateBuildFileAction()
|
|
|
|
{
|
|
|
|
Node *node = nullptr;
|
|
|
|
if (Core::IDocument *currentDocument = Core::EditorManager::currentDocument())
|
|
|
|
node = ProjectTree::nodeForFile(currentDocument->filePath());
|
|
|
|
enableBuildFileMenus(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMakeManager::enableBuildFileMenus(Node *node)
|
|
|
|
{
|
|
|
|
m_buildFileAction->setVisible(false);
|
|
|
|
m_buildFileAction->setEnabled(false);
|
|
|
|
m_buildFileAction->setParameter(QString());
|
|
|
|
m_buildFileContextMenu->setEnabled(false);
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
Project *project = ProjectTree::projectForNode(node);
|
|
|
|
if (!project)
|
|
|
|
return;
|
|
|
|
Target *target = project->activeTarget();
|
|
|
|
if (!target)
|
|
|
|
return;
|
|
|
|
const QString generator = CMakeGeneratorKitAspect::generator(target->kit());
|
|
|
|
if (generator != "Ninja" && !generator.contains("Makefiles"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (const FileNode *fileNode = node->asFileNode()) {
|
|
|
|
const FileType type = fileNode->fileType();
|
|
|
|
const bool visible = qobject_cast<CMakeProject *>(project)
|
|
|
|
&& dynamic_cast<CMakeTargetNode *>(node->parentProjectNode())
|
|
|
|
&& (type == FileType::Source || type == FileType::Header);
|
|
|
|
|
|
|
|
const bool enabled = visible && !BuildManager::isBuilding(project);
|
|
|
|
m_buildFileAction->setVisible(visible);
|
|
|
|
m_buildFileAction->setEnabled(enabled);
|
|
|
|
m_buildFileAction->setParameter(node->filePath().fileName());
|
|
|
|
m_buildFileContextMenu->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 10:58:35 +02:00
|
|
|
void CMakeManager::parseAndValidateCMakeReplyFile()
|
|
|
|
{
|
|
|
|
QString replyFile = QFileDialog::getOpenFileName(Core::ICore::mainWindow(),
|
2020-07-02 09:08:26 +02:00
|
|
|
tr("Select a CMake Reply File"),
|
2020-06-10 10:58:35 +02:00
|
|
|
QString(),
|
|
|
|
QString("index*.json"));
|
|
|
|
if (replyFile.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString errorMessage;
|
|
|
|
auto result = FileApiParser::parseData(QFileInfo(replyFile), errorMessage);
|
|
|
|
|
|
|
|
const QString message
|
|
|
|
= errorMessage.isEmpty()
|
|
|
|
? tr("The reply file \"%1\" and referenced data parsed OK and passed validation.")
|
|
|
|
.arg(QDir::toNativeSeparators(replyFile))
|
|
|
|
: tr("The reply file \"%1\" failed to parse or validate with error "
|
|
|
|
"message:<br><b>\"%2\"</b>")
|
|
|
|
.arg(QDir::toNativeSeparators(replyFile))
|
|
|
|
.arg(errorMessage);
|
|
|
|
QMessageBox::information(Core::ICore::mainWindow(), tr("Parsing Result"), message);
|
|
|
|
}
|
|
|
|
|
2019-05-26 23:48:40 +03:00
|
|
|
void CMakeManager::buildFile(Node *node)
|
|
|
|
{
|
|
|
|
if (!node) {
|
|
|
|
Core::IDocument *currentDocument= Core::EditorManager::currentDocument();
|
|
|
|
if (!currentDocument)
|
|
|
|
return;
|
|
|
|
const Utils::FilePath file = currentDocument->filePath();
|
|
|
|
node = ProjectTree::nodeForFile(file);
|
|
|
|
}
|
|
|
|
FileNode *fileNode = node ? node->asFileNode() : nullptr;
|
|
|
|
if (!fileNode)
|
|
|
|
return;
|
|
|
|
Project *project = ProjectTree::projectForNode(fileNode);
|
|
|
|
if (!project)
|
|
|
|
return;
|
|
|
|
CMakeTargetNode *targetNode = dynamic_cast<CMakeTargetNode *>(fileNode->parentProjectNode());
|
|
|
|
if (!targetNode)
|
|
|
|
return;
|
2019-10-25 09:55:32 +02:00
|
|
|
Target *target = project->activeTarget();
|
|
|
|
QTC_ASSERT(target, return);
|
2019-05-26 23:48:40 +03:00
|
|
|
const QString generator = CMakeGeneratorKitAspect::generator(target->kit());
|
|
|
|
const QString relativeSource = fileNode->filePath().relativeChildPath(targetNode->filePath()).toString();
|
|
|
|
const QString objExtension = Utils::HostOsInfo::isWindowsHost() ? QString(".obj") : QString(".o");
|
|
|
|
Utils::FilePath targetBase;
|
2019-10-25 09:55:32 +02:00
|
|
|
BuildConfiguration *bc = target->activeBuildConfiguration();
|
|
|
|
QTC_ASSERT(bc, return);
|
2019-05-26 23:48:40 +03:00
|
|
|
if (generator == "Ninja") {
|
|
|
|
const Utils::FilePath relativeBuildDir = targetNode->buildDirectory().relativeChildPath(
|
|
|
|
bc->buildDirectory());
|
2020-02-19 17:35:56 +01:00
|
|
|
targetBase = relativeBuildDir / "CMakeFiles" / (targetNode->displayName() + ".dir");
|
2019-05-26 23:48:40 +03:00
|
|
|
} else if (!generator.contains("Makefiles")) {
|
|
|
|
Core::MessageManager::write(tr("Build File is not supported for generator \"%1\"")
|
|
|
|
.arg(generator));
|
|
|
|
return;
|
|
|
|
}
|
2019-10-25 09:55:32 +02:00
|
|
|
|
|
|
|
static_cast<CMakeBuildSystem *>(bc->buildSystem())
|
|
|
|
->buildCMakeTarget(targetBase.pathAppended(relativeSource).toString() + objExtension);
|
2019-05-26 23:48:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMakeManager::buildFileContextMenu()
|
|
|
|
{
|
|
|
|
if (Node *node = ProjectTree::currentNode())
|
|
|
|
buildFile(node);
|
|
|
|
}
|