From 1f540c6c9deb5aabd2f3c7e97bbd9251b98279a6 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Fri, 10 Feb 2017 13:50:10 +0300 Subject: [PATCH] CMakeProjectManager: Fix findCMakeNode function Via CMake project we can add subdirectory like: > add_subdirectory("subdir/subsubdir" "enotherdir") Where "subdir" my not contain "CMakeLists.txt" file. And intermediate directory can contain CMakeLists.txt file that is not a part of the root node. This change fix it. Task-number: QTCREATORBUG-17721 Change-Id: Ice9fba1ca5a979955ec8f44324f75f3bc16ee198 Reviewed-by: hjk --- .../cmakeprojectmanager/servermodereader.cpp | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/servermodereader.cpp b/src/plugins/cmakeprojectmanager/servermodereader.cpp index 39e899ea6ab..f1063c8d396 100644 --- a/src/plugins/cmakeprojectmanager/servermodereader.cpp +++ b/src/plugins/cmakeprojectmanager/servermodereader.cpp @@ -573,17 +573,27 @@ static CMakeListsNode *findCMakeNode(CMakeListsNode *root, const Utils::FileName const Utils::FileName stepDir = dir; const Utils::FileName topDir = root->filePath().parentDir(); - QStringList relative = stepDir.relativeChildPath(topDir).toString().split('/'); + QStringList relative = stepDir.relativeChildPath(topDir).toString().split('/', QString::SkipEmptyParts); CMakeListsNode *result = root; + QString relativePathElement; while (!relative.isEmpty()) { - const QString nextPathElement = relative.takeFirst(); - if (nextPathElement.isEmpty()) - continue; - const QString nextFullPath - = result->filePath().parentDir().toString() + '/' + nextPathElement + "/CMakeLists.txt"; - result = static_cast(result->projectNode(Utils::FileName::fromString(nextFullPath))); + const QString nextDirPath = result->filePath().parentDir().toString(); + Utils::FileName nextFullPath; + // Some directory may not contain CMakeLists.txt file, skip it: + do { + relativePathElement += '/' + relative.takeFirst(); + nextFullPath = Utils::FileName::fromString(nextDirPath + relativePathElement + "/CMakeLists.txt"); + } while (!nextFullPath.exists() && !relative.isEmpty()); + result = static_cast(result->projectNode(nextFullPath)); + // Intermediate directory can contain CMakeLists.txt file + // that is not a part of the root node, skip it: + if (!result && !relative.isEmpty()) { + result = root; + } else { + relativePathElement.clear(); + } QTC_ASSERT(result, return nullptr); } return result;