From 659e7d85f1d30f8910f59bff7efe411417aa53b8 Mon Sep 17 00:00:00 2001 From: Shane Peelar Date: Thu, 4 Apr 2013 20:44:10 -0400 Subject: [PATCH] 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 --- .../cmakeprojectmanager/cmakeproject.cpp | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakeproject.cpp b/src/plugins/cmakeprojectmanager/cmakeproject.cpp index 2f119b8904a..ff0652a3ef2 100644 --- a/src/plugins/cmakeprojectmanager/cmakeproject.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeproject.cpp @@ -324,6 +324,7 @@ bool CMakeProject::parseCMakeLists() allIncludePaths.append(cbpparser.includeFiles()); QStringList cxxflags; + bool found = false; foreach (const CMakeBuildTarget &buildTarget, m_buildTargets) { QString makeCommand = QDir::fromNativeSeparators(buildTarget.makeCommand); int startIndex = makeCommand.indexOf(QLatin1Char('\"')); @@ -336,14 +337,14 @@ bool CMakeProject::parseCMakeLists() makefile.truncate(slashIndex); makefile.append(QLatin1String("/CMakeFiles/") + buildTarget.title + QLatin1String(".dir/flags.make")); QFile file(makefile); - bool found = false; if (file.exists()) { file.open(QIODevice::ReadOnly | QIODevice::Text); - QStringList lines = QString::fromLatin1(file.readAll()).split(QLatin1Char('\n')); - foreach (const QString &line, lines) { + QTextStream stream(&file); + while (!stream.atEnd()) { + QString line = stream.readLine().trimmed(); if (line.startsWith(QLatin1String("CXX_FLAGS ="))) { - int index = line.indexOf(QLatin1Char('=')) + 1; - cxxflags = line.mid(index).trimmed().split(QLatin1Char(' ')); + // Skip past = + cxxflags = line.mid(11).trimmed().split(QLatin1Char(' '), QString::SkipEmptyParts); found = true; break; } @@ -352,6 +353,30 @@ bool CMakeProject::parseCMakeLists() if (found) 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; allDefines.append(tc->predefinedMacros(cxxflags));