From 81f64f85a054125d053bfae8ba1c66294aab03ce Mon Sep 17 00:00:00 2001 From: Claus Steuer Date: Mon, 20 Apr 2015 15:59:18 +0200 Subject: [PATCH] CMakeProject: Fix CXX Flags parsing for ninja projects For ninja projects the "build.ninja" file is parsed to extract the cxx flags for each target. This file is located in the working directory of the "all" target, however since commit "65c113bc" qtcreator searches in the build directory of the current target. I have restored the search behavior to the previous state and added some code to ensure that the parsed flags really belong to the target Change-Id: I7cc7f6dbd4f12aec698133206da889037131bb13 Reviewed-by: Eike Ziller Reviewed-by: Daniel Teske --- .../cmakeprojectmanager/cmakeproject.cpp | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakeproject.cpp b/src/plugins/cmakeprojectmanager/cmakeproject.cpp index 4ae5adf4801..a0392a8d00c 100644 --- a/src/plugins/cmakeprojectmanager/cmakeproject.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeproject.cpp @@ -204,21 +204,31 @@ QStringList CMakeProject::getCXXFlagsFor(const CMakeBuildTarget &buildTarget) // Attempt to find build.ninja file and obtain FLAGS (CXX_FLAGS) from there if no suitable flags.make were // found // Get "all" target's working directory - QString buildNinjaFile = QDir::fromNativeSeparators(buildTarget.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 = - return line.mid(7).trimmed().split(QLatin1Char(' '), QString::SkipEmptyParts); + if (!buildTargets().empty()) { + QString buildNinjaFile = QDir::fromNativeSeparators(buildTargets().at(0).workingDirectory); + buildNinjaFile += QLatin1String("/build.ninja"); + QFile buildNinja(buildNinjaFile); + if (buildNinja.exists()) { + buildNinja.open(QIODevice::ReadOnly | QIODevice::Text); + QTextStream stream(&buildNinja); + bool targetFound = false; + bool cxxFound = false; + QString targetSearchPattern = QString::fromLatin1("target %1").arg(buildTarget.title); + + while (!stream.atEnd()) { + // 1. Look for a block that refers to the current target + // 2. Look for a build rule which invokes CXX_COMPILER + // 3. Return the FLAGS definition + QString line = stream.readLine().trimmed(); + if (line.startsWith(QLatin1String("#"))) { + if (!line.startsWith(QLatin1String("# Object build statements for"))) continue; + targetFound = line.endsWith(targetSearchPattern); + } else if (targetFound && line.startsWith(QLatin1String("build"))) { + cxxFound = line.indexOf(QLatin1String("CXX_COMPILER")) != -1; + } else if (cxxFound && line.startsWith(QLatin1String("FLAGS ="))) { + // Skip past = + return line.mid(7).trimmed().split(QLatin1Char(' '), QString::SkipEmptyParts); + } } } }