From 0390ece76152cc6cd2e54495855819a09f91b5d6 Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Thu, 20 Nov 2014 15:09:15 +0100 Subject: [PATCH] CMake: Further fine tune file -> target mapping Instead of requiring that the target's source directory is a parent of all source files, use a distance between the source and directory and the file. This will find the wrong CMakeLists.txt in more cases but also is much more likely to lead to using the fallback target. This makes code completion work for http://github.com/dream3d/dream3d/ Change-Id: Ic035454c5eabe361bc7c46bd943e9a9cdee730e3 Reviewed-by: Tobias Hunger --- .../cmakeprojectmanager/cmakeproject.cpp | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakeproject.cpp b/src/plugins/cmakeprojectmanager/cmakeproject.cpp index a2ec61245fe..b1e17ada637 100644 --- a/src/plugins/cmakeprojectmanager/cmakeproject.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeproject.cpp @@ -852,6 +852,15 @@ void CMakeBuildSettingsWidget::runCMake() // CMakeCbpParser //// +namespace { +int distance(const QString &targetDirectory, const Utils::FileName &fileName) +{ + const QString commonParent = Utils::commonPath(QStringList() << targetDirectory << fileName.toString()); + return targetDirectory.mid(commonParent.size()).count(QLatin1Char('/')) + + fileName.toString().mid(commonParent.size()).count(QLatin1Char('/')); +} +} + // called after everything is parsed // this function tries to figure out to which CMakeBuildTarget // each file belongs, so that it gets the appropriate defines and @@ -889,7 +898,7 @@ void CMakeCbpParser::sortFiles() // easy case, same parent directory as last file last->files.append(fileName.toString()); } else { - int bestLength = -1; + int bestDistance = std::numeric_limits::max(); int bestIndex = -1; int bestIncludeCount = -1; @@ -897,11 +906,11 @@ void CMakeCbpParser::sortFiles() const CMakeBuildTarget &target = m_buildTargets.at(i); if (target.includeFiles.isEmpty()) continue; - if (fileName.isChildOf(Utils::FileName::fromString(target.sourceDirectory)) && - (target.sourceDirectory.size() > bestLength || - (target.sourceDirectory.size() == bestLength && - target.includeFiles.count() > bestIncludeCount))) { - bestLength = target.sourceDirectory.size(); + int dist = distance(target.sourceDirectory, fileName); + if (dist < bestDistance || + (dist == bestDistance && + target.includeFiles.count() > bestIncludeCount)) { + bestDistance = dist; bestIncludeCount = target.includeFiles.count(); bestIndex = i; }