CMakeProjectManager: Split up cmakeproject source files

Preparing for exporting the CMakeProject symbol. Removing all APIs
from the header file that will continue to be interal API.

Change-Id: I820ea0efb909e6a75be70ccb3b419f841a15cfb3
Reviewed-by: Daniel Teske <daniel.teske@theqtcompany.com>
This commit is contained in:
Benjamin Zeller
2015-03-10 10:22:38 +01:00
parent bebcf69ce9
commit 7ca9f1f277
11 changed files with 928 additions and 644 deletions

View File

@@ -36,9 +36,11 @@
#include "cmakerunconfiguration.h"
#include "makestep.h"
#include "cmakeopenprojectwizard.h"
#include "cmakecbpparser.h"
#include "cmakefile.h"
#include "cmakeprojectmanager.h"
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/headerpath.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/buildmanager.h>
@@ -46,7 +48,7 @@
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/toolchain.h>
#include <projectexplorer/target.h>
#include <projectexplorer/deployconfiguration.h>
#include <projectexplorer/deploymentdata.h>
#include <qtsupport/customexecutablerunconfiguration.h>
@@ -65,7 +67,6 @@
#include <QDebug>
#include <QDir>
#include <QFormLayout>
#include <QFileSystemWatcher>
using namespace CMakeProjectManager;
@@ -731,564 +732,6 @@ void CMakeProject::createUiCodeModelSupport()
QtSupport::UiCodeModelManager::update(this, uiFileHash);
}
// CMakeFile
CMakeFile::CMakeFile(CMakeProject *parent, const FileName &fileName)
: Core::IDocument(parent), m_project(parent)
{
setId("Cmake.ProjectFile");
setMimeType(QLatin1String(Constants::CMAKEPROJECTMIMETYPE));
setFilePath(fileName);
}
bool CMakeFile::save(QString *errorString, const QString &fileName, bool autoSave)
{
// Once we have an texteditor open for this file, we probably do
// need to implement this, don't we.
Q_UNUSED(errorString)
Q_UNUSED(fileName)
Q_UNUSED(autoSave)
return false;
}
QString CMakeFile::defaultPath() const
{
return QString();
}
QString CMakeFile::suggestedFileName() const
{
return QString();
}
bool CMakeFile::isModified() const
{
return false;
}
bool CMakeFile::isSaveAsAllowed() const
{
return false;
}
Core::IDocument::ReloadBehavior CMakeFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
{
Q_UNUSED(state)
Q_UNUSED(type)
return BehaviorSilent;
}
bool CMakeFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
{
Q_UNUSED(errorString)
Q_UNUSED(flag)
Q_UNUSED(type)
return true;
}
CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc) : m_buildConfiguration(0)
{
QFormLayout *fl = new QFormLayout(this);
fl->setContentsMargins(20, -1, 0, -1);
fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
setLayout(fl);
QPushButton *runCmakeButton = new QPushButton(tr("Run CMake..."));
connect(runCmakeButton, SIGNAL(clicked()),
this, SLOT(runCMake()));
fl->addRow(tr("Reconfigure project:"), runCmakeButton);
m_pathLineEdit = new QLineEdit(this);
m_pathLineEdit->setReadOnly(true);
QHBoxLayout *hbox = new QHBoxLayout();
hbox->addWidget(m_pathLineEdit);
m_changeButton = new QPushButton(this);
m_changeButton->setText(tr("&Change"));
connect(m_changeButton, SIGNAL(clicked()), this, SLOT(openChangeBuildDirectoryDialog()));
hbox->addWidget(m_changeButton);
fl->addRow(tr("Build directory:"), hbox);
m_buildConfiguration = bc;
m_pathLineEdit->setText(m_buildConfiguration->rawBuildDirectory().toString());
if (m_buildConfiguration->buildDirectory() == bc->target()->project()->projectDirectory())
m_changeButton->setEnabled(false);
else
m_changeButton->setEnabled(true);
setDisplayName(tr("CMake"));
}
void CMakeBuildSettingsWidget::openChangeBuildDirectoryDialog()
{
CMakeProject *project = static_cast<CMakeProject *>(m_buildConfiguration->target()->project());
CMakeBuildInfo info(m_buildConfiguration);
CMakeOpenProjectWizard copw(Core::ICore::mainWindow(),
project->projectManager(), CMakeOpenProjectWizard::ChangeDirectory,
&info);
if (copw.exec() == QDialog::Accepted) {
project->changeBuildDirectory(m_buildConfiguration, copw.buildDirectory());
m_buildConfiguration->setUseNinja(copw.useNinja());
m_pathLineEdit->setText(m_buildConfiguration->rawBuildDirectory().toString());
}
}
void CMakeBuildSettingsWidget::runCMake()
{
if (!ProjectExplorer::ProjectExplorerPlugin::saveModifiedFiles())
return;
CMakeProject *project = static_cast<CMakeProject *>(m_buildConfiguration->target()->project());
CMakeBuildInfo info(m_buildConfiguration);
CMakeOpenProjectWizard copw(Core::ICore::mainWindow(),
project->projectManager(),
CMakeOpenProjectWizard::WantToUpdate, &info);
if (copw.exec() == QDialog::Accepted)
project->parseCMakeLists();
}
/////
// CMakeCbpParser
////
namespace {
int distance(const QString &targetDirectory, const FileName &fileName)
{
const QString commonParent = Utils::commonPath(QStringList() << targetDirectory << fileName.toString());
return targetDirectory.mid(commonParent.size()).count(QLatin1Char('/'))
+ fileName.toString().mid(commonParent.size()).count(QLatin1Char('/'));
}
}
// called after everything is parsed
// this function tries to figure out to which CMakeBuildTarget
// each file belongs, so that it gets the appropriate defines and
// compiler flags
void CMakeCbpParser::sortFiles()
{
QLoggingCategory log("qtc.cmakeprojectmanager.filetargetmapping");
QList<FileName> fileNames = Utils::transform(m_fileList, [] (FileNode *node) {
return node->path();
});
Utils::sort(fileNames);
CMakeBuildTarget *last = 0;
FileName parentDirectory;
qCDebug(log) << "###############";
qCDebug(log) << "# Pre Dump #";
qCDebug(log) << "###############";
foreach (const CMakeBuildTarget &target, m_buildTargets)
qCDebug(log) << target.title << target.sourceDirectory <<
target.includeFiles << target.defines << target.files << "\n";
// find a good build target to fall back
int fallbackIndex = 0;
{
int bestIncludeCount = -1;
for (int i = 0; i < m_buildTargets.size(); ++i) {
const CMakeBuildTarget &target = m_buildTargets.at(i);
if (target.includeFiles.isEmpty())
continue;
if (target.sourceDirectory == m_sourceDirectory
&& target.includeFiles.count() > bestIncludeCount) {
bestIncludeCount = target.includeFiles.count();
fallbackIndex = i;
}
}
}
qCDebug(log) << "###############";
qCDebug(log) << "# Sorting #";
qCDebug(log) << "###############";
foreach (const FileName &fileName, fileNames) {
qCDebug(log) << fileName;
if (fileName.parentDir() == parentDirectory && last) {
// easy case, same parent directory as last file
last->files.append(fileName.toString());
qCDebug(log) << " into" << last->title;
} else {
int bestDistance = std::numeric_limits<int>::max();
int bestIndex = -1;
int bestIncludeCount = -1;
for (int i = 0; i < m_buildTargets.size(); ++i) {
const CMakeBuildTarget &target = m_buildTargets.at(i);
if (target.includeFiles.isEmpty())
continue;
int dist = distance(target.sourceDirectory, fileName);
qCDebug(log) << "distance to target" << target.title << dist;
if (dist < bestDistance ||
(dist == bestDistance &&
target.includeFiles.count() > bestIncludeCount)) {
bestDistance = dist;
bestIncludeCount = target.includeFiles.count();
bestIndex = i;
}
}
if (bestIndex == -1 && !m_buildTargets.isEmpty()) {
bestIndex = fallbackIndex;
qCDebug(log) << " using fallbackIndex";
}
if (bestIndex != -1) {
m_buildTargets[bestIndex].files.append(fileName.toString());
last = &m_buildTargets[bestIndex];
parentDirectory = fileName.parentDir();
qCDebug(log) << " into" << last->title;
}
}
}
qCDebug(log) << "###############";
qCDebug(log) << "# After Dump #";
qCDebug(log) << "###############";
foreach (const CMakeBuildTarget &target, m_buildTargets)
qCDebug(log) << target.title << target.sourceDirectory << target.includeFiles << target.defines << target.files << "\n";
}
bool CMakeCbpParser::parseCbpFile(const QString &fileName, const QString &sourceDirectory)
{
m_buildDirectory = QFileInfo(fileName).absolutePath();
m_sourceDirectory = sourceDirectory;
QFile fi(fileName);
if (fi.exists() && fi.open(QFile::ReadOnly)) {
setDevice(&fi);
while (!atEnd()) {
readNext();
if (name() == QLatin1String("CodeBlocks_project_file"))
parseCodeBlocks_project_file();
else if (isStartElement())
parseUnknownElement();
}
sortFiles();
fi.close();
return true;
}
return false;
}
void CMakeCbpParser::parseCodeBlocks_project_file()
{
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Project"))
parseProject();
else if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseProject()
{
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Option"))
parseOption();
else if (name() == QLatin1String("Unit"))
parseUnit();
else if (name() == QLatin1String("Build"))
parseBuild();
else if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseBuild()
{
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Target"))
parseBuildTarget();
else if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseBuildTarget()
{
m_buildTarget.clear();
if (attributes().hasAttribute(QLatin1String("title")))
m_buildTarget.title = attributes().value(QLatin1String("title")).toString();
while (!atEnd()) {
readNext();
if (isEndElement()) {
if (!m_buildTarget.title.endsWith(QLatin1String("/fast")))
m_buildTargets.append(m_buildTarget);
return;
} else if (name() == QLatin1String("Compiler")) {
parseCompiler();
} else if (name() == QLatin1String("Option")) {
parseBuildTargetOption();
} else if (name() == QLatin1String("MakeCommands")) {
parseMakeCommands();
} else if (isStartElement()) {
parseUnknownElement();
}
}
}
void CMakeCbpParser::parseBuildTargetOption()
{
if (attributes().hasAttribute(QLatin1String("output"))) {
m_buildTarget.executable = attributes().value(QLatin1String("output")).toString();
} else if (attributes().hasAttribute(QLatin1String("type"))) {
const QStringRef value = attributes().value(QLatin1String("type"));
if (value == QLatin1String("2") || value == QLatin1String("3"))
m_buildTarget.targetType = TargetType(value.toInt());
} else if (attributes().hasAttribute(QLatin1String("working_dir"))) {
m_buildTarget.workingDirectory = attributes().value(QLatin1String("working_dir")).toString();
QFile cmakeSourceInfoFile(m_buildTarget.workingDirectory
+ QStringLiteral("/CMakeFiles/CMakeDirectoryInformation.cmake"));
if (cmakeSourceInfoFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&cmakeSourceInfoFile);
const QLatin1String searchSource("SET(CMAKE_RELATIVE_PATH_TOP_SOURCE \"");
while (!stream.atEnd()) {
const QString lineTopSource = stream.readLine().trimmed();
if (lineTopSource.startsWith(searchSource)) {
m_buildTarget.sourceDirectory = lineTopSource.mid(searchSource.size());
m_buildTarget.sourceDirectory.chop(2); // cut off ")
break;
}
}
}
if (m_buildTarget.sourceDirectory.isEmpty()) {
QDir dir(m_buildDirectory);
const QString relative = dir.relativeFilePath(m_buildTarget.workingDirectory);
m_buildTarget.sourceDirectory
= FileName::fromString(m_sourceDirectory).appendPath(relative).toString();
}
}
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (isStartElement())
parseUnknownElement();
}
}
QString CMakeCbpParser::projectName() const
{
return m_projectName;
}
void CMakeCbpParser::parseOption()
{
if (attributes().hasAttribute(QLatin1String("title")))
m_projectName = attributes().value(QLatin1String("title")).toString();
if (attributes().hasAttribute(QLatin1String("compiler")))
m_compiler = attributes().value(QLatin1String("compiler")).toString();
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseMakeCommands()
{
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Build"))
parseBuildTargetBuild();
else if (name() == QLatin1String("Clean"))
parseBuildTargetClean();
else if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseBuildTargetBuild()
{
if (attributes().hasAttribute(QLatin1String("command")))
m_buildTarget.makeCommand = attributes().value(QLatin1String("command")).toString();
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseBuildTargetClean()
{
if (attributes().hasAttribute(QLatin1String("command")))
m_buildTarget.makeCleanCommand = attributes().value(QLatin1String("command")).toString();
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseCompiler()
{
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (name() == QLatin1String("Add"))
parseAdd();
else if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseAdd()
{
// CMake only supports <Add option=\> and <Add directory=\>
const QXmlStreamAttributes addAttributes = attributes();
const QString includeDirectory = addAttributes.value(QLatin1String("directory")).toString();
// allow adding multiple times because order happens
if (!includeDirectory.isEmpty())
m_buildTarget.includeFiles.append(includeDirectory);
QString compilerOption = addAttributes.value(QLatin1String("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;
if (macroNameIndex != 1) {
int assignIndex = compilerOption.indexOf(QLatin1Char('='), macroNameIndex);
if (assignIndex != -1)
compilerOption[assignIndex] = ' ';
m_buildTarget.defines.append("#define ");
m_buildTarget.defines.append(compilerOption.mid(macroNameIndex).toUtf8());
m_buildTarget.defines.append('\n');
}
}
while (!atEnd()) {
readNext();
if (isEndElement())
return;
else if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseUnit()
{
//qDebug()<<stream.attributes().value("filename");
FileName fileName =
FileName::fromUserInput(attributes().value(QLatin1String("filename")).toString());
m_parsingCmakeUnit = false;
while (!atEnd()) {
readNext();
if (isEndElement()) {
if (!fileName.endsWith(QLatin1String(".rule")) && !m_processedUnits.contains(fileName)) {
// Now check whether we found a virtual element beneath
if (m_parsingCmakeUnit) {
m_cmakeFileList.append( new ProjectExplorer::FileNode(fileName, ProjectExplorer::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"))))
generated = true;
if (fileName.endsWith(QLatin1String(".qrc")))
m_fileList.append( new ProjectExplorer::FileNode(fileName, ProjectExplorer::ResourceType, generated));
else
m_fileList.append( new ProjectExplorer::FileNode(fileName, ProjectExplorer::SourceType, generated));
}
m_processedUnits.insert(fileName);
}
return;
} else if (name() == QLatin1String("Option")) {
parseUnitOption();
} else if (isStartElement()) {
parseUnknownElement();
}
}
}
void CMakeCbpParser::parseUnitOption()
{
if (attributes().hasAttribute(QLatin1String("virtualFolder")))
m_parsingCmakeUnit = true;
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement())
parseUnknownElement();
}
}
void CMakeCbpParser::parseUnknownElement()
{
Q_ASSERT(isStartElement());
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement())
parseUnknownElement();
}
}
QList<ProjectExplorer::FileNode *> CMakeCbpParser::fileList()
{
return m_fileList;
}
QList<ProjectExplorer::FileNode *> CMakeCbpParser::cmakeFileList()
{
return m_cmakeFileList;
}
bool CMakeCbpParser::hasCMakeFiles()
{
return !m_cmakeFileList.isEmpty();
}
QList<CMakeBuildTarget> CMakeCbpParser::buildTargets()
{
return m_buildTargets;
}
QString CMakeCbpParser::compilerName() const
{
return m_compiler;
}
void CMakeBuildTarget::clear()
{
executable.clear();