From 4790b09beeae70eba2b97d9d281998cc393194c4 Mon Sep 17 00:00:00 2001 From: Cristian Adam Date: Fri, 30 Aug 2024 18:45:47 +0200 Subject: [PATCH] CMakePM: Fix Subproject build for targets using FOLDER property If a target is being moved around using the FOLDER property as: set_target_properties(mytarget PROPERTIES FOLDER "folder") then the parent project node is no longer the CMakeListsNode, but the "folder" Node, which doesn't represent the subdirectory where the target has been defined. Change-Id: Ida76be7f90ad3e2e58d480c8de4be2e0f7b352b4 Reviewed-by: Marcus Tillmanns --- .../cmakeprojectmanager.cpp | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp b/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp index 5b70c994275..805ce94e5be 100644 --- a/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp @@ -75,7 +75,7 @@ private: void enableBuildSubprojectContextMenu(Node *node); void enableBuildSubprojectMenu(); void runSubprojectOperation(const QString &clean, const QString &build); - CMakeListsNode* currentListsNodeForEditor(); + const CMakeListsNode* currentListsNodeForEditor(); QAction *m_runCMakeAction; QAction *m_clearCMakeCacheAction; @@ -551,7 +551,7 @@ void CMakeManager::runSubprojectOperation(const QString &clean, const QString &b } } -CMakeListsNode *CMakeManager::currentListsNodeForEditor() +const CMakeListsNode *CMakeManager::currentListsNodeForEditor() { Core::IDocument *currentDocument = Core::EditorManager::currentDocument(); if (!currentDocument) @@ -566,7 +566,26 @@ CMakeListsNode *CMakeManager::currentListsNodeForEditor() if (!targetNode) return nullptr; - return dynamic_cast(targetNode->parentProjectNode()); + auto bs = qobject_cast(ProjectTree::currentBuildSystem()); + if (!bs) + return nullptr; + + auto cmakeBuildTarget + = Utils::findOrDefault(bs->buildTargets(), [&targetNode](const CMakeBuildTarget &cbt) { + return targetNode->buildKey() == cbt.title; + }); + + if (cmakeBuildTarget.backtrace.isEmpty()) + return nullptr; + const FilePath targetDefinitionDir = cmakeBuildTarget.backtrace.last().path.parentDir(); + + auto projectNode = bs->project()->rootProjectNode()->findProjectNode( + [&targetDefinitionDir](const ProjectNode *node) { + if (auto cmakeListsNode = dynamic_cast(node)) + return cmakeListsNode->path() == targetDefinitionDir; + return false; + }); + return dynamic_cast(projectNode); } void CMakeManager::buildFile(Node *node)