| 
									
										
										
										
											2022-08-19 15:59:36 +02:00
										 |  |  | // Copyright (C) 2016 The Qt Company Ltd.
 | 
					
						
							| 
									
										
										
										
											2022-12-21 10:12:09 +01:00
										 |  |  | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2022-09-29 15:26:31 +02:00
										 |  |  | #include "cmakeprojectmanagertr.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-26 23:48:40 +03:00
										 |  |  | #include "cmakeprojectnodes.h"
 | 
					
						
							| 
									
										
										
										
											2023-05-09 18:22:47 +02:00
										 |  |  | #include "cmakespecificsettings.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>
 | 
					
						
							| 
									
										
										
										
											2023-05-09 18:22:47 +02:00
										 |  |  | #include <coreplugin/modemanager.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-14 15:47:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-12 17:23:37 +03:00
										 |  |  | #include <cppeditor/cpptoolsreuse.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-14 15:47:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2023-02-14 15:47:22 +01:00
										 |  |  | #include <projectexplorer/projectmanager.h>
 | 
					
						
							| 
									
										
										
										
											2015-02-26 13:38:54 +01:00
										 |  |  | #include <projectexplorer/projecttree.h>
 | 
					
						
							| 
									
										
										
										
											2016-01-20 12:19:16 +01:00
										 |  |  | #include <projectexplorer/target.h>
 | 
					
						
							| 
									
										
										
										
											2015-02-26 13:38:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 18:22:47 +02:00
										 |  |  | #include <utils/checkablemessagebox.h>
 | 
					
						
							|  |  |  | #include <utils/utilsicons.h>
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2022-11-24 17:00:52 +01:00
										 |  |  | using namespace Utils; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace CMakeProjectManager::Internal { | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:58:35 +02:00
										 |  |  | CMakeManager::CMakeManager() | 
					
						
							| 
									
										
										
										
											2022-09-29 15:26:31 +02:00
										 |  |  |     : m_runCMakeAction(new QAction(QIcon(), Tr::tr("Run CMake"), this)) | 
					
						
							|  |  |  |     , m_clearCMakeCacheAction(new QAction(QIcon(), Tr::tr("Clear CMake Configuration"), this)) | 
					
						
							|  |  |  |     , m_runCMakeActionContextMenu(new QAction(QIcon(), Tr::tr("Run CMake"), this)) | 
					
						
							|  |  |  |     , m_rescanProjectAction(new QAction(QIcon(), Tr::tr("Rescan Project"), this)) | 
					
						
							| 
									
										
										
										
											2023-05-09 18:22:47 +02:00
										 |  |  |     , m_reloadCMakePresetsAction( | 
					
						
							|  |  |  |           new QAction(Utils::Icons::RELOAD_TOOLBAR.icon(), Tr::tr("Reload CMake Presets"), 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); | 
					
						
							| 
									
										
										
										
											2021-03-17 16:49:15 +09:00
										 |  |  |     mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD); | 
					
						
							| 
									
										
										
										
											2022-12-07 15:37:42 +01:00
										 |  |  |     connect(m_runCMakeAction, &QAction::triggered, this, [this] { | 
					
						
							| 
									
										
										
										
											2023-02-14 15:47:22 +01:00
										 |  |  |         runCMake(ProjectManager::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); | 
					
						
							| 
									
										
										
										
											2021-03-17 16:49:15 +09:00
										 |  |  |     mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD); | 
					
						
							| 
									
										
										
										
											2022-12-07 15:37:42 +01:00
										 |  |  |     connect(m_clearCMakeCacheAction, &QAction::triggered, this, [this] { | 
					
						
							| 
									
										
										
										
											2023-02-14 15:47:22 +01:00
										 |  |  |         clearCMakeCache(ProjectManager::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); | 
					
						
							| 
									
										
										
										
											2022-12-07 15:37:42 +01:00
										 |  |  |     connect(m_runCMakeActionContextMenu, &QAction::triggered, this, [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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-29 15:26:31 +02:00
										 |  |  |     m_buildFileContextMenu = new QAction(Tr::tr("Build"), this); | 
					
						
							| 
									
										
										
										
											2019-05-26 23:48:40 +03:00
										 |  |  |     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); | 
					
						
							| 
									
										
										
										
											2021-03-17 16:49:15 +09:00
										 |  |  |     mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD); | 
					
						
							| 
									
										
										
										
											2022-12-07 15:37:42 +01:00
										 |  |  |     connect(m_rescanProjectAction, &QAction::triggered, this, [this] { | 
					
						
							| 
									
										
										
										
											2019-10-25 09:55:32 +02:00
										 |  |  |         rescanProject(ProjectTree::currentBuildSystem()); | 
					
						
							| 
									
										
										
										
											2016-12-04 03:36:12 +10:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 18:22:47 +02:00
										 |  |  |     command = Core::ActionManager::registerAction(m_reloadCMakePresetsAction, | 
					
						
							|  |  |  |                                                   Constants::RELOAD_CMAKE_PRESETS, | 
					
						
							|  |  |  |                                                   globalContext); | 
					
						
							|  |  |  |     command->setAttribute(Core::Command::CA_Hide); | 
					
						
							|  |  |  |     mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD); | 
					
						
							|  |  |  |     connect(m_reloadCMakePresetsAction, &QAction::triggered, this, [this] { | 
					
						
							|  |  |  |         reloadCMakePresets(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-29 15:26:31 +02:00
										 |  |  |     m_buildFileAction = new Utils::ParameterAction(Tr::tr("Build File"), | 
					
						
							|  |  |  |                                                    Tr::tr("Build File \"%1\""), | 
					
						
							| 
									
										
										
										
											2020-04-07 14:44:22 +02:00
										 |  |  |                                                    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()); | 
					
						
							| 
									
										
										
										
											2022-09-29 15:26:31 +02:00
										 |  |  |     command->setDefaultKeySequence(QKeySequence(Tr::tr("Ctrl+Alt+B"))); | 
					
						
							| 
									
										
										
										
											2019-05-26 23:48:40 +03:00
										 |  |  |     mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD); | 
					
						
							|  |  |  |     connect(m_buildFileAction, &QAction::triggered, this, [this] { buildFile(); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-14 15:47:22 +01:00
										 |  |  |     connect(ProjectManager::instance(), &ProjectManager::startupProjectChanged, this, [this] { | 
					
						
							| 
									
										
										
										
											2021-06-18 08:46:38 +02:00
										 |  |  |         updateCmakeActions(ProjectTree::currentNode()); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     connect(BuildManager::instance(), &BuildManager::buildStateChanged, this, [this] { | 
					
						
							|  |  |  |         updateCmakeActions(ProjectTree::currentNode()); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 08:46:38 +02:00
										 |  |  |     updateCmakeActions(ProjectTree::currentNode()); | 
					
						
							| 
									
										
										
										
											2011-02-23 15:52:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 08:46:38 +02:00
										 |  |  | void CMakeManager::updateCmakeActions(Node *node) | 
					
						
							| 
									
										
										
										
											2011-02-23 15:52:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-14 15:47:22 +01:00
										 |  |  |     auto project = qobject_cast<CMakeProject *>(ProjectManager::startupProject()); | 
					
						
							| 
									
										
										
										
											2016-03-10 12:01:30 +01:00
										 |  |  |     const bool visible = project && !BuildManager::isBuilding(project); | 
					
						
							|  |  |  |     m_runCMakeAction->setVisible(visible); | 
					
						
							| 
									
										
										
										
											2021-11-26 16:16:17 +01:00
										 |  |  |     m_runCMakeActionContextMenu->setEnabled(visible); | 
					
						
							| 
									
										
										
										
											2016-03-10 12:01:30 +01:00
										 |  |  |     m_clearCMakeCacheAction->setVisible(visible); | 
					
						
							| 
									
										
										
										
											2016-12-04 03:36:12 +10:00
										 |  |  |     m_rescanProjectAction->setVisible(visible); | 
					
						
							| 
									
										
										
										
											2023-05-09 18:22:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const bool reloadPresetsVisible = [project] { | 
					
						
							|  |  |  |         if (!project) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         const FilePath presetsPath = project->projectFilePath().parentDir().pathAppended( | 
					
						
							|  |  |  |             "CMakePresets.json"); | 
					
						
							|  |  |  |         return presetsPath.exists(); | 
					
						
							|  |  |  |     }(); | 
					
						
							|  |  |  |     m_reloadCMakePresetsAction->setVisible(reloadPresetsVisible); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 08:46:38 +02:00
										 |  |  |     enableBuildFileMenus(node); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 18:22:47 +02:00
										 |  |  | void CMakeManager::reloadCMakePresets() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto settings = CMakeSpecificSettings::instance(); | 
					
						
							|  |  |  |     bool doNotAsk = !settings->askBeforePresetsReload.value(); | 
					
						
							|  |  |  |     if (!doNotAsk) { | 
					
						
							|  |  |  |         CheckableMessageBox question(Core::ICore::dialogParent()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         question.setIcon(QMessageBox::Question); | 
					
						
							|  |  |  |         question.setWindowTitle(Tr::tr("Reload CMake Presets")); | 
					
						
							|  |  |  |         question.setText(Tr::tr("Re-generates the CMake presets kits. The manual CMake project modifications will be lost.")); | 
					
						
							|  |  |  |         question.setCheckBoxText(Tr::tr("Do not ask again")); | 
					
						
							|  |  |  |         question.setCheckBoxVisible(true); | 
					
						
							|  |  |  |         question.setChecked(doNotAsk); | 
					
						
							|  |  |  |         question.setStandardButtons(QDialogButtonBox::Cancel); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         question.addButton(Tr::tr("Reload"), QDialogButtonBox::YesRole); | 
					
						
							|  |  |  |         question.setDefaultButton(QDialogButtonBox::Yes); | 
					
						
							|  |  |  |         question.exec(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         doNotAsk = question.isChecked(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         settings->askBeforePresetsReload.setValue(!doNotAsk); | 
					
						
							|  |  |  |         settings->writeSettings(Core::ICore::settings()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (question.clickedStandardButton() == QDialogButtonBox::Cancel) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CMakeProject *project = static_cast<CMakeProject *>(ProjectTree::currentProject()); | 
					
						
							|  |  |  |     if (!project) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const QSet<QString> oldPresets = Utils::transform<QSet>(project->presetsData().configurePresets, | 
					
						
							|  |  |  |                                                             [](const auto &preset) { | 
					
						
							|  |  |  |                                                                 return preset.name; | 
					
						
							|  |  |  |                                                             }); | 
					
						
							|  |  |  |     project->readPresets(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QList<Kit*> oldKits; | 
					
						
							|  |  |  |     for (const auto &target : project->targets()) { | 
					
						
							|  |  |  |         const CMakeConfigItem presetItem = CMakeConfigurationKitAspect::cmakePresetConfigItem( | 
					
						
							|  |  |  |             target->kit()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (BuildManager::isBuilding(target)) | 
					
						
							|  |  |  |             BuildManager::cancel(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Only clear the CMake configuration for preset kits. Any manual kit configuration
 | 
					
						
							|  |  |  |         // will get the chance to get imported afterwards in the Kit selection wizard
 | 
					
						
							|  |  |  |         CMakeBuildSystem *bs = static_cast<CMakeBuildSystem *>(target->buildSystem()); | 
					
						
							|  |  |  |         if (!presetItem.isNull() && bs) | 
					
						
							|  |  |  |             bs->clearCMakeCache(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!presetItem.isNull() && oldPresets.contains(QString::fromUtf8(presetItem.value))) | 
					
						
							|  |  |  |             oldKits << target->kit(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         project->removeTarget(target); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     project->setOldPresetKits(oldKits); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Core::ModeManager::activateMode(ProjectExplorer::Constants::MODE_SESSION); | 
					
						
							|  |  |  |     Core::ModeManager::setFocusToCurrentMode(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2022-11-24 17:00:52 +01:00
										 |  |  |     FilePath filePath = fileNode->filePath(); | 
					
						
							| 
									
										
										
										
											2022-10-12 17:23:37 +03:00
										 |  |  |     if (filePath.fileName().contains(".h")) { | 
					
						
							|  |  |  |         bool wasHeader = false; | 
					
						
							| 
									
										
										
										
											2022-11-24 17:00:52 +01:00
										 |  |  |         const FilePath sourceFile = CppEditor::correspondingHeaderOrSource(filePath, &wasHeader); | 
					
						
							| 
									
										
										
										
											2022-10-12 17:23:37 +03:00
										 |  |  |         if (wasHeader && !sourceFile.isEmpty()) | 
					
						
							| 
									
										
										
										
											2022-11-24 17:00:52 +01:00
										 |  |  |             filePath = sourceFile; | 
					
						
							| 
									
										
										
										
											2022-10-12 17:23:37 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											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()); | 
					
						
							| 
									
										
										
										
											2022-10-12 17:23:37 +03:00
										 |  |  |     const QString relativeSource = filePath.relativeChildPath(targetNode->filePath()).toString(); | 
					
						
							| 
									
										
										
										
											2019-05-26 23:48:40 +03:00
										 |  |  |     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")) { | 
					
						
							| 
									
										
										
										
											2020-12-01 12:52:59 +01:00
										 |  |  |         Core::MessageManager::writeFlashing( | 
					
						
							| 
									
										
										
										
											2022-09-29 15:26:31 +02:00
										 |  |  |             Tr::tr("Build File is not supported for generator \"%1\"").arg(generator)); | 
					
						
							| 
									
										
										
										
											2019-05-26 23:48:40 +03:00
										 |  |  |         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); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-11-24 17:00:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // CMakeProjectManager::Internal
 |