CMake: Cleanup CMakeCbpParser

Change-Id: I7fa193297e02ae8e3c5de88583f39409aeecf01f
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Tobias Hunger
2016-10-17 13:12:02 +02:00
parent b1433e2a05
commit 567db27e57

View File

@@ -48,10 +48,10 @@ namespace {
int distance(const QString &targetDirectory, const FileName &fileName)
{
const QString commonParent = commonPath(QStringList() << targetDirectory << fileName.toString());
return targetDirectory.mid(commonParent.size()).count(QLatin1Char('/'))
+ fileName.toString().mid(commonParent.size()).count(QLatin1Char('/'));
}
return targetDirectory.mid(commonParent.size()).count('/')
+ fileName.toString().mid(commonParent.size()).count('/');
}
} // namespace
// called after everything is parsed
// this function tries to figure out to which CMakeBuildTarget
@@ -64,7 +64,6 @@ void CMakeCbpParser::sortFiles()
sort(fileNames);
CMakeBuildTarget *last = 0;
FileName parentDirectory;
@@ -170,7 +169,7 @@ bool CMakeCbpParser::parseCbpFile(CMakeTool::PathMapper mapper, const QString &f
while (!atEnd()) {
readNext();
if (name() == QLatin1String("CodeBlocks_project_file"))
if (name() == "CodeBlocks_project_file")
parseCodeBlocks_project_file();
else if (isStartElement())
parseUnknownElement();
@@ -182,7 +181,7 @@ bool CMakeCbpParser::parseCbpFile(CMakeTool::PathMapper mapper, const QString &f
// There is always a clean target:
CMakeBuildTarget cleanTarget;
cleanTarget.title = QLatin1String("clean");
cleanTarget.title = "clean";
cleanTarget.targetType = UtilityType;
cleanTarget.workingDirectory = m_buildDirectory;
cleanTarget.sourceDirectory = m_sourceDirectory;
@@ -200,7 +199,7 @@ void CMakeCbpParser::parseCodeBlocks_project_file()
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Project"))
else if (name() == "Project")
parseProject();
else if (isStartElement())
parseUnknownElement();
@@ -213,11 +212,11 @@ void CMakeCbpParser::parseProject()
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Option"))
else if (name() == "Option")
parseOption();
else if (name() == QLatin1String("Unit"))
else if (name() == "Unit")
parseUnit();
else if (name() == QLatin1String("Build"))
else if (name() == "Build")
parseBuild();
else if (isStartElement())
parseUnknownElement();
@@ -230,7 +229,7 @@ void CMakeCbpParser::parseBuild()
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Target"))
else if (name() == "Target")
parseBuildTarget();
else if (isStartElement())
parseUnknownElement();
@@ -241,23 +240,23 @@ void CMakeCbpParser::parseBuildTarget()
{
m_buildTarget.clear();
if (attributes().hasAttribute(QLatin1String("title")))
m_buildTarget.title = attributes().value(QLatin1String("title")).toString();
if (attributes().hasAttribute("title"))
m_buildTarget.title = attributes().value("title").toString();
while (!atEnd()) {
readNext();
if (isEndElement()) {
if (!m_buildTarget.title.endsWith(QLatin1String("/fast"))
&& !m_buildTarget.title.endsWith(QLatin1String("_automoc"))) {
if (!m_buildTarget.title.endsWith("/fast")
&& !m_buildTarget.title.endsWith("_automoc")) {
if (m_buildTarget.executable.isEmpty() && m_buildTarget.targetType == ExecutableType)
m_buildTarget.targetType = UtilityType;
m_buildTargets.append(m_buildTarget);
}
return;
} else if (name() == QLatin1String("Compiler")) {
} else if (name() == "Compiler") {
parseCompiler();
} else if (name() == QLatin1String("Option")) {
} else if (name() == "Option") {
parseBuildTargetOption();
} else if (name() == QLatin1String("MakeCommands")) {
} else if (name() == "MakeCommands") {
parseMakeCommands();
} else if (isStartElement()) {
parseUnknownElement();
@@ -267,10 +266,10 @@ void CMakeCbpParser::parseBuildTarget()
void CMakeCbpParser::parseBuildTargetOption()
{
if (attributes().hasAttribute(QLatin1String("output"))) {
m_buildTarget.executable = m_pathMapper(attributes().value(QLatin1String("output")).toString());
} else if (attributes().hasAttribute(QLatin1String("type"))) {
const QStringRef value = attributes().value(QLatin1String("type"));
if (attributes().hasAttribute("output")) {
m_buildTarget.executable = m_pathMapper(attributes().value("output").toString());
} else if (attributes().hasAttribute("type")) {
const QStringRef value = attributes().value("type");
if (value == "0" || value == "1")
m_buildTarget.targetType = ExecutableType;
else if (value == "2")
@@ -279,8 +278,8 @@ void CMakeCbpParser::parseBuildTargetOption()
m_buildTarget.targetType = DynamicLibraryType;
else
m_buildTarget.targetType = UtilityType;
} else if (attributes().hasAttribute(QLatin1String("working_dir"))) {
m_buildTarget.workingDirectory = attributes().value(QLatin1String("working_dir")).toString();
} else if (attributes().hasAttribute("working_dir")) {
m_buildTarget.workingDirectory = attributes().value("working_dir").toString();
QFile cmakeSourceInfoFile(m_buildTarget.workingDirectory
+ QStringLiteral("/CMakeFiles/CMakeDirectoryInformation.cmake"));
@@ -320,11 +319,11 @@ QString CMakeCbpParser::projectName() const
void CMakeCbpParser::parseOption()
{
if (attributes().hasAttribute(QLatin1String("title")))
m_projectName = attributes().value(QLatin1String("title")).toString();
if (attributes().hasAttribute("title"))
m_projectName = attributes().value("title").toString();
if (attributes().hasAttribute(QLatin1String("compiler")))
m_compiler = attributes().value(QLatin1String("compiler")).toString();
if (attributes().hasAttribute("compiler"))
m_compiler = attributes().value("compiler").toString();
while (!atEnd()) {
readNext();
@@ -341,9 +340,9 @@ void CMakeCbpParser::parseMakeCommands()
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Build"))
else if (name() == "Build")
parseBuildTargetBuild();
else if (name() == QLatin1String("Clean"))
else if (name() == "Clean")
parseBuildTargetClean();
else if (isStartElement())
parseUnknownElement();
@@ -352,8 +351,8 @@ void CMakeCbpParser::parseMakeCommands()
void CMakeCbpParser::parseBuildTargetBuild()
{
if (attributes().hasAttribute(QLatin1String("command")))
m_buildTarget.makeCommand = m_pathMapper(attributes().value(QLatin1String("command")).toString());
if (attributes().hasAttribute("command"))
m_buildTarget.makeCommand = m_pathMapper(attributes().value("command").toString());
while (!atEnd()) {
readNext();
if (isEndElement())
@@ -380,7 +379,7 @@ void CMakeCbpParser::parseCompiler()
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Add"))
else if (name() == "Add")
parseAdd();
else if (isStartElement())
parseUnknownElement();
@@ -392,19 +391,19 @@ void CMakeCbpParser::parseAdd()
// CMake only supports <Add option=\> and <Add directory=\>
const QXmlStreamAttributes addAttributes = attributes();
QString includeDirectory = m_pathMapper(addAttributes.value(QLatin1String("directory")).toString());
QString includeDirectory = m_pathMapper(addAttributes.value("directory").toString());
// allow adding multiple times because order happens
if (!includeDirectory.isEmpty())
m_buildTarget.includeFiles.append(includeDirectory);
QString compilerOption = addAttributes.value(QLatin1String("option")).toString();
QString compilerOption = addAttributes.value("option").toString();
// defining multiple times a macro to the same value makes no sense
if (!compilerOption.isEmpty() && !m_buildTarget.compilerOptions.contains(compilerOption)) {
m_buildTarget.compilerOptions.append(compilerOption);
int macroNameIndex = compilerOption.indexOf(QLatin1String("-D")) + 2;
int macroNameIndex = compilerOption.indexOf("-D") + 2;
if (macroNameIndex != 1) {
int assignIndex = compilerOption.indexOf(QLatin1Char('='), macroNameIndex);
int assignIndex = compilerOption.indexOf('=', macroNameIndex);
if (assignIndex != -1)
compilerOption[assignIndex] = ' ';
m_buildTarget.defines.append("#define ");
@@ -424,10 +423,8 @@ void CMakeCbpParser::parseAdd()
void CMakeCbpParser::parseUnit()
{
//qDebug()<<stream.attributes().value("filename");
FileName fileName =
FileName::fromString(m_pathMapper(FileName::fromUserInput(
attributes().value(QLatin1String("filename"))
FileName::fromString(m_pathMapper(FileName::fromUserInput(attributes().value("filename")
.toString()).toString()));
m_parsingCMakeUnit = false;
@@ -435,19 +432,19 @@ void CMakeCbpParser::parseUnit()
while (!atEnd()) {
readNext();
if (isEndElement()) {
if (!fileName.endsWith(QLatin1String(".rule")) && !m_processedUnits.contains(fileName)) {
if (!fileName.endsWith(".rule") && !m_processedUnits.contains(fileName)) {
// Now check whether we found a virtual element beneath
if (m_parsingCMakeUnit) {
m_cmakeFileList.append( new FileNode(fileName, ProjectFileType, false));
} else {
bool generated = false;
QString onlyFileName = fileName.fileName();
if ( (onlyFileName.startsWith(QLatin1String("moc_")) && onlyFileName.endsWith(QLatin1String(".cxx")))
|| (onlyFileName.startsWith(QLatin1String("ui_")) && onlyFileName.endsWith(QLatin1String(".h")))
|| (onlyFileName.startsWith(QLatin1String("qrc_")) && onlyFileName.endsWith(QLatin1String(".cxx"))))
if ( (onlyFileName.startsWith("moc_") && onlyFileName.endsWith(".cxx"))
|| (onlyFileName.startsWith("ui_") && onlyFileName.endsWith(".h"))
|| (onlyFileName.startsWith("qrc_") && onlyFileName.endsWith(".cxx")))
generated = true;
if (fileName.endsWith(QLatin1String(".qrc")))
if (fileName.endsWith(".qrc"))
m_fileList.append( new FileNode(fileName, ResourceType, generated));
else
m_fileList.append( new FileNode(fileName, SourceType, generated));
@@ -456,7 +453,7 @@ void CMakeCbpParser::parseUnit()
m_processedUnits.insert(fileName);
}
return;
} else if (name() == QLatin1String("Option")) {
} else if (name() == "Option") {
parseUnitOption();
} else if (isStartElement()) {
parseUnknownElement();
@@ -467,8 +464,8 @@ void CMakeCbpParser::parseUnit()
void CMakeCbpParser::parseUnitOption()
{
const QXmlStreamAttributes optionAttributes = attributes();
m_parsingCMakeUnit = optionAttributes.hasAttribute(QLatin1String("virtualFolder"));
const QString target = optionAttributes.value(QLatin1String("target")).toString();
m_parsingCMakeUnit = optionAttributes.hasAttribute("virtualFolder");
const QString target = optionAttributes.value("target").toString();
if (!target.isEmpty())
m_unitTargets.append(target);
@@ -523,6 +520,5 @@ QString CMakeCbpParser::compilerName() const
return m_compiler;
}
} // namespace Internal
} // namespace CMakeProjectManager