Fix CMakeProjectManager Ninja CXXFLAGS detection

In recent CMake versions, a new backend, Ninja, was added with the intention
of providing an alternative to makefiles.  However, this backend stores the
CXXFLAGS for the project in a different format than it does for the makefiles
backends.  As the CMakeProjectManager assumes a makefile backend, it fails
to correctly obtain the CXXFLAGS of the project when Ninja is used.
This small patch will attempt to read CXXFLAGS from the build.ninja file
to correct this issue.

Task-number: QTCREATORBUG-9047

Change-Id: Ic19ae3b4802c7a12b5d0a92a7f1a2254bec5a3d2
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
Shane Peelar
2013-04-04 20:44:10 -04:00
parent 2f147b4055
commit 659e7d85f1

View File

@@ -324,6 +324,7 @@ bool CMakeProject::parseCMakeLists()
allIncludePaths.append(cbpparser.includeFiles()); allIncludePaths.append(cbpparser.includeFiles());
QStringList cxxflags; QStringList cxxflags;
bool found = false;
foreach (const CMakeBuildTarget &buildTarget, m_buildTargets) { foreach (const CMakeBuildTarget &buildTarget, m_buildTargets) {
QString makeCommand = QDir::fromNativeSeparators(buildTarget.makeCommand); QString makeCommand = QDir::fromNativeSeparators(buildTarget.makeCommand);
int startIndex = makeCommand.indexOf(QLatin1Char('\"')); int startIndex = makeCommand.indexOf(QLatin1Char('\"'));
@@ -336,14 +337,14 @@ bool CMakeProject::parseCMakeLists()
makefile.truncate(slashIndex); makefile.truncate(slashIndex);
makefile.append(QLatin1String("/CMakeFiles/") + buildTarget.title + QLatin1String(".dir/flags.make")); makefile.append(QLatin1String("/CMakeFiles/") + buildTarget.title + QLatin1String(".dir/flags.make"));
QFile file(makefile); QFile file(makefile);
bool found = false;
if (file.exists()) { if (file.exists()) {
file.open(QIODevice::ReadOnly | QIODevice::Text); file.open(QIODevice::ReadOnly | QIODevice::Text);
QStringList lines = QString::fromLatin1(file.readAll()).split(QLatin1Char('\n')); QTextStream stream(&file);
foreach (const QString &line, lines) { while (!stream.atEnd()) {
QString line = stream.readLine().trimmed();
if (line.startsWith(QLatin1String("CXX_FLAGS ="))) { if (line.startsWith(QLatin1String("CXX_FLAGS ="))) {
int index = line.indexOf(QLatin1Char('=')) + 1; // Skip past =
cxxflags = line.mid(index).trimmed().split(QLatin1Char(' ')); cxxflags = line.mid(11).trimmed().split(QLatin1Char(' '), QString::SkipEmptyParts);
found = true; found = true;
break; break;
} }
@@ -352,6 +353,30 @@ bool CMakeProject::parseCMakeLists()
if (found) if (found)
break; break;
} }
// Attempt to find build.ninja file and obtain FLAGS (CXX_FLAGS) from there if no suitable flags.make were
// found
if (!found && !cbpparser.buildTargets().isEmpty()) {
// Get "all" target's working directory
QString buildNinjaFile = QDir::fromNativeSeparators(cbpparser.buildTargets().at(0).workingDirectory);
buildNinjaFile += QLatin1String("/build.ninja");
QFile buildNinja(buildNinjaFile);
if (buildNinja.exists()) {
buildNinja.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream stream(&buildNinja);
bool cxxFound = false;
while (!stream.atEnd()) {
QString line = stream.readLine().trimmed();
// Look for a build rule which invokes CXX_COMPILER
if (line.startsWith(QLatin1String("build"))) {
cxxFound = line.indexOf(QLatin1String("CXX_COMPILER")) != -1;
} else if (cxxFound && line.startsWith(QLatin1String("FLAGS ="))) {
// Skip past =
cxxflags = line.mid(7).trimmed().split(QLatin1Char(' '), QString::SkipEmptyParts);
break;
}
}
}
}
QByteArray allDefines; QByteArray allDefines;
allDefines.append(tc->predefinedMacros(cxxflags)); allDefines.append(tc->predefinedMacros(cxxflags));