Commit 0390ece7 authored by Daniel Teske's avatar Daniel Teske

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: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent f88ad0ce
......@@ -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<int>::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;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment