From b3c4aa99da1970045b920b15ccff92b33ba92522 Mon Sep 17 00:00:00 2001 From: Peter Kuemmel <syntheticpp@gmx.net> Date: Sat, 26 May 2012 21:05:30 +0200 Subject: [PATCH] CMake: Read macro definitions The CodeBlocks generator writes for each macro definition <Add option=-D... /> (also make getters const) Task-number: QTCREATORBUG-3922 Change-Id: I93e10397e76ad4f34126a76c4c36e4529d48d0ad Reviewed-by: Daniel Teske <daniel.teske@nokia.com> --- .../cmakeprojectmanager/cmakeproject.cpp | 39 +++++++++++++++++-- .../cmakeprojectmanager/cmakeproject.h | 4 ++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakeproject.cpp b/src/plugins/cmakeprojectmanager/cmakeproject.cpp index 2845b74ed00..e0129942eb1 100644 --- a/src/plugins/cmakeprojectmanager/cmakeproject.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeproject.cpp @@ -293,6 +293,10 @@ bool CMakeProject::parseCMakeLists() allIncludePaths.append(projectDirectory()); allIncludePaths.append(cbpparser.includeFiles()); + QByteArray allDefines; + allDefines.append(activeBC->toolChain()->predefinedMacros(QStringList())); + allDefines.append(cbpparser.defines()); + QStringList allFrameworkPaths; QList<ProjectExplorer::HeaderPath> allHeaderPaths; if (activeBC->toolChain()) @@ -310,7 +314,7 @@ bool CMakeProject::parseCMakeLists() CPlusPlus::CppModelManagerInterface::ProjectInfo pinfo = modelmanager->projectInfo(this); if (pinfo.includePaths() != allIncludePaths || pinfo.sourceFiles() != m_files - || pinfo.defines() != (activeBC->toolChain() ? activeBC->toolChain()->predefinedMacros(QStringList()) : QByteArray()) + || pinfo.defines() != allDefines || pinfo.frameworkPaths() != allFrameworkPaths) { pinfo.clearProjectParts(); CPlusPlus::CppModelManagerInterface::ProjectPart::Ptr part( @@ -318,7 +322,7 @@ bool CMakeProject::parseCMakeLists() part->includePaths = allIncludePaths; // TODO we only want C++ files, not all other stuff that might be in the project part->sourceFiles = m_files; - part->defines = (activeBC->toolChain() ? activeBC->toolChain()->predefinedMacros(QStringList()) : QByteArray()); // TODO this is to simplistic + part->defines = allDefines; part->frameworkPaths = allFrameworkPaths; part->language = CPlusPlus::CppModelManagerInterface::CXX; pinfo.appendProjectPart(part); @@ -1085,7 +1089,31 @@ void CMakeCbpParser::parseCompiler() void CMakeCbpParser::parseAdd() { - m_includeFiles.append(attributes().value("directory").toString()); + // CMake only supports <Add option=\> and <Add directory=\> + const QXmlStreamAttributes addAttributes = attributes(); + + const QString includeDirectory = addAttributes.value("directory").toString(); + // allow adding multiple times because order happens + if (!includeDirectory.isEmpty()) { + m_includeFiles.append(includeDirectory); + } + + QString compilerOption = addAttributes.value("option").toString(); + // defining multiple times a macro to the same value makes no sense + if (!compilerOption.isEmpty() && !m_compilerOptions.contains(compilerOption)) { + m_compilerOptions.append(compilerOption); + int macroNameIndex = compilerOption.indexOf("-D") + 2; + if (macroNameIndex != 1) { + int assignIndex = compilerOption.indexOf('=', macroNameIndex); + if (assignIndex != -1) { + compilerOption[assignIndex] = ' '; + } + m_defines.append("#define "); + m_defines.append(compilerOption.mid(macroNameIndex).toAscii()); + m_defines.append('\n'); + } + } + while (!atEnd()) { readNext(); if (isEndElement()) { @@ -1183,6 +1211,11 @@ QStringList CMakeCbpParser::includeFiles() return m_includeFiles; } +QByteArray CMakeCbpParser::defines() const +{ + return m_defines; +} + QList<CMakeBuildTarget> CMakeCbpParser::buildTargets() { return m_buildTargets; diff --git a/src/plugins/cmakeprojectmanager/cmakeproject.h b/src/plugins/cmakeprojectmanager/cmakeproject.h index 52183e905b0..6166a348b24 100644 --- a/src/plugins/cmakeprojectmanager/cmakeproject.h +++ b/src/plugins/cmakeprojectmanager/cmakeproject.h @@ -162,9 +162,11 @@ public: QList<ProjectExplorer::FileNode *> cmakeFileList(); QStringList includeFiles(); QList<CMakeBuildTarget> buildTargets(); + QByteArray defines() const; QString projectName() const; QString compilerName() const; bool hasCMakeFiles(); + private: void parseCodeBlocks_project_file(); void parseProject(); @@ -186,6 +188,8 @@ private: QSet<QString> m_processedUnits; bool m_parsingCmakeUnit; QStringList m_includeFiles; + QStringList m_compilerOptions; + QByteArray m_defines; CMakeBuildTarget m_buildTarget; bool m_buildTargetType; -- GitLab