Commit 2e3e0605 authored by Ulf Hermann's avatar Ulf Hermann
Browse files

Generalize support for extra compilers



Allow for different extra compilers which may get called to generate
additional code for the code model. The build system is expected to
know what files are generated from which source file and the extra
compilers know how to generate the content of those files, without
touching the build directory. the uic adapter is refactored to be
the first such extra compiler.

The extra compiler is run when an editor for its source document
loses focus, or after a timeout of 1s when the source document has
been changed.

Change-Id: I13c110c61120c812f02639a3684144daf8979b37
Reviewed-by: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent 13e0abf5
......@@ -52,8 +52,12 @@
#include <qtsupport/baseqtversion.h>
#include <qtsupport/customexecutablerunconfiguration.h>
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/uicodemodelsupport.h>
#include <cpptools/generatedcodemodelsupport.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/projectinfo.h>
#include <cpptools/projectpartbuilder.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
......@@ -108,7 +112,7 @@ CMakeProject::~CMakeProject()
{
setRootProjectNode(nullptr);
m_codeModelFuture.cancel();
delete m_buildDirManager;
qDeleteAll(m_extraCompilers);
}
void CMakeProject::changeActiveBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
......@@ -287,7 +291,7 @@ void CMakeProject::parseCMakeOutput()
updateApplicationAndDeploymentTargets();
createUiCodeModelSupport();
createGeneratedCodeModelSupport();
ToolChain *tc = ProjectExplorer::ToolChainKitInformation::toolChain(m_buildDirManager->kit());
if (!tc) {
......@@ -624,11 +628,11 @@ CMakeBuildTarget CMakeProject::buildTargetForTitle(const QString &title)
return CMakeBuildTarget();
}
QString CMakeProject::uiHeaderFile(const QString &uiFile)
QStringList CMakeProject::filesGeneratedFrom(const QString &sourceFile) const
{
if (!activeTarget())
return QString();
QFileInfo fi(uiFile);
return QStringList();
QFileInfo fi(sourceFile);
FileName project = projectDirectory();
FileName baseDirectory = FileName::fromString(fi.absolutePath());
......@@ -645,12 +649,17 @@ QString CMakeProject::uiHeaderFile(const QString &uiFile)
QDir srcDirRoot = QDir(project.toString());
QString relativePath = srcDirRoot.relativeFilePath(baseDirectory.toString());
QDir buildDir = QDir(activeTarget()->activeBuildConfiguration()->buildDirectory().toString());
QString uiHeaderFilePath = buildDir.absoluteFilePath(relativePath);
uiHeaderFilePath += QLatin1String("/ui_");
uiHeaderFilePath += fi.completeBaseName();
uiHeaderFilePath += QLatin1String(".h");
QString generatedFilePath = buildDir.absoluteFilePath(relativePath);
return QDir::cleanPath(uiHeaderFilePath);
if (fi.suffix() == QLatin1String("ui")) {
generatedFilePath += QLatin1String("/ui_");
generatedFilePath += fi.completeBaseName();
generatedFilePath += QLatin1String(".h");
return QStringList(QDir::cleanPath(generatedFilePath));
} else {
// TODO: Other types will be added when adapters for their compilers become available.
return QStringList();
}
}
void CMakeProject::updateRunConfigurations()
......@@ -753,17 +762,31 @@ void CMakeProject::updateApplicationAndDeploymentTargets()
t->setDeploymentData(deploymentData);
}
void CMakeProject::createUiCodeModelSupport()
{
QHash<QString, QString> uiFileHash;
// Find all ui files
foreach (const QString &uiFile, files(SourceFiles)) {
if (uiFile.endsWith(QLatin1String(".ui")))
uiFileHash.insert(uiFile, uiHeaderFile(uiFile));
void CMakeProject::createGeneratedCodeModelSupport()
{
qDeleteAll(m_extraCompilers);
m_extraCompilers.clear();
QList<ProjectExplorer::ExtraCompilerFactory *> factories =
ProjectExplorer::ExtraCompilerFactory::extraCompilerFactories();
// Find all files generated by any of the extra compilers, in a rather crude way.
foreach (const QString &file, files(SourceFiles)) {
foreach (ProjectExplorer::ExtraCompilerFactory *factory, factories) {
if (file.endsWith(QLatin1Char('.') + factory->sourceTag())) {
QStringList generated = filesGeneratedFrom(file);
if (!generated.isEmpty()) {
const FileNameList fileNames = Utils::transform(generated,
[](const QString &s) {
return FileName::fromString(s);
});
m_extraCompilers.append(factory->create(this, FileName::fromString(file),
fileNames));
}
}
}
}
QtSupport::UiCodeModelManager::update(this, uiFileHash);
CppTools::GeneratedCodeModelSupport::update(m_extraCompilers);
}
void CMakeBuildTarget::clear()
......
......@@ -29,6 +29,7 @@
#include "cmakeprojectnodes.h"
#include "configmodel.h"
#include <projectexplorer/extracompiler.h>
#include <projectexplorer/project.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/namedwidget.h>
......@@ -141,8 +142,8 @@ private:
void buildTree(Internal::CMakeProjectNode *rootNode, QList<ProjectExplorer::FileNode *> list);
void gatherFileNodes(ProjectExplorer::FolderNode *parent, QList<ProjectExplorer::FileNode *> &list) const;
ProjectExplorer::FolderNode *findOrCreateFolder(Internal::CMakeProjectNode *rootNode, QString directory);
void createUiCodeModelSupport();
QString uiHeaderFile(const QString &uiFile);
void createGeneratedCodeModelSupport();
QStringList filesGeneratedFrom(const QString &sourceFile) const override;
void updateTargetRunConfigurations(ProjectExplorer::Target *t);
void updateApplicationAndDeploymentTargets();
QStringList getCXXFlagsFor(const CMakeBuildTarget &buildTarget, QHash<QString, QStringList> &cache);
......@@ -155,6 +156,7 @@ private:
// TODO probably need a CMake specific node structure
QList<CMakeBuildTarget> m_buildTargets;
QFuture<void> m_codeModelFuture;
QList<ProjectExplorer::ExtraCompiler *> m_extraCompilers;
};
} // namespace CMakeProjectManager
......@@ -34,9 +34,15 @@
namespace CppTools {
AbstractEditorSupport::AbstractEditorSupport(CppModelManager *modelmanager) :
m_modelmanager(modelmanager), m_revision(1)
AbstractEditorSupport::AbstractEditorSupport(CppModelManager *modelmanager, QObject *parent) :
QObject(parent), m_modelmanager(modelmanager), m_revision(1)
{
modelmanager->addExtraEditorSupport(this);
}
AbstractEditorSupport::~AbstractEditorSupport()
{
m_modelmanager->removeExtraEditorSupport(this);
}
void AbstractEditorSupport::updateDocument()
......
......@@ -38,7 +38,8 @@ class CPPTOOLS_EXPORT AbstractEditorSupport : public QObject
{
Q_OBJECT
public:
explicit AbstractEditorSupport(CppModelManager *modelmanager);
explicit AbstractEditorSupport(CppModelManager *modelmanager, QObject *parent = 0);
~AbstractEditorSupport();
/// \returns the contents, encoded as UTF-8
virtual QByteArray contents() const = 0;
......
......@@ -56,6 +56,7 @@ HEADERS += \
doxygengenerator.h \
editordocumenthandle.h \
functionutils.h \
generatedcodemodelsupport.h \
includeutils.h \
indexitem.h \
insertionpointlocator.h \
......@@ -127,6 +128,7 @@ SOURCES += \
doxygengenerator.cpp \
editordocumenthandle.cpp \
functionutils.cpp \
generatedcodemodelsupport.cpp \
includeutils.cpp \
indexitem.cpp \
insertionpointlocator.cpp \
......
......@@ -83,6 +83,7 @@ QtcPlugin {
"doxygengenerator.cpp", "doxygengenerator.h",
"editordocumenthandle.cpp", "editordocumenthandle.h",
"functionutils.cpp", "functionutils.h",
"generatedcodemodelsupport.cpp", "generatedcodemodelsupport.h",
"includeutils.cpp", "includeutils.h",
"indexitem.cpp", "indexitem.h",
"insertionpointlocator.cpp", "insertionpointlocator.h",
......
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "generatedcodemodelsupport.h"
#include "cppmodelmanager.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/idocument.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <QFile>
#include <QFileInfo>
#include <QLoggingCategory>
using namespace ProjectExplorer;
using namespace CPlusPlus;
namespace CppTools {
class QObjectCache
{
public:
bool contains(QObject *object) const
{
return m_cache.contains(object);
}
void insert(QObject *object)
{
QObject::connect(object, &QObject::destroyed, [this](QObject *dead) {
m_cache.remove(dead);
});
m_cache.insert(object);
}
private:
QSet<QObject *> m_cache;
};
GeneratedCodeModelSupport::GeneratedCodeModelSupport(CppModelManager *modelmanager,
ProjectExplorer::ExtraCompiler *generator,
const Utils::FileName &generatedFile) :
CppTools::AbstractEditorSupport(modelmanager, generator), m_generatedFileName(generatedFile),
m_generator(generator)
{
QLoggingCategory log("qtc.cpptools.generatedcodemodelsupport");
qCDebug(log) << "ctor GeneratedCodeModelSupport for" << m_generator->source()
<< generatedFile;
init();
}
GeneratedCodeModelSupport::~GeneratedCodeModelSupport()
{
CppTools::CppModelManager::instance()->emitAbstractEditorSupportRemoved(
m_generatedFileName.toString());
QLoggingCategory log("qtc.cpptools.generatedcodemodelsupport");
qCDebug(log) << "dtor ~generatedcodemodelsupport for" << m_generatedFileName;
}
void GeneratedCodeModelSupport::onContentsChanged(const Utils::FileName &file)
{
if (file == m_generatedFileName) {
notifyAboutUpdatedContents();
updateDocument();
}
}
void GeneratedCodeModelSupport::init() const
{
connect(m_generator, &ProjectExplorer::ExtraCompiler::contentsChanged,
this, &GeneratedCodeModelSupport::onContentsChanged);
}
QByteArray GeneratedCodeModelSupport::contents() const
{
return m_generator->content(m_generatedFileName).toUtf8();
}
QString GeneratedCodeModelSupport::fileName() const
{
return m_generatedFileName.toString();
}
void GeneratedCodeModelSupport::update(const QList<ProjectExplorer::ExtraCompiler *> &generators)
{
static QObjectCache extraCompilerCache;
CppTools::CppModelManager *mm = CppTools::CppModelManager::instance();
foreach (ExtraCompiler *generator, generators) {
if (extraCompilerCache.contains(generator))
continue;
extraCompilerCache.insert(generator);
foreach (const Utils::FileName &generatedFile, generator->targets())
new GeneratedCodeModelSupport(mm, generator, generatedFile);
}
}
} // namespace CppTools
......@@ -23,12 +23,13 @@
**
****************************************************************************/
#ifndef UICODEMODELSUPPORT_H
#define UICODEMODELSUPPORT_H
#pragma once
#include "qtsupport_global.h"
#include "cpptools_global.h"
#include <cpptools/abstracteditorsupport.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/extracompiler.h>
#include <QDateTime>
#include <QHash>
......@@ -36,86 +37,31 @@
#include <QSet>
namespace Core { class IEditor; }
namespace CPlusPlus { class CppModelManager; }
namespace ProjectExplorer { class Project; }
namespace QtSupport {
namespace CppTools {
namespace Internal { class QtSupportPlugin; }
class UiCodeModelSupport : public CppTools::AbstractEditorSupport
class CPPTOOLS_EXPORT GeneratedCodeModelSupport : public AbstractEditorSupport
{
Q_OBJECT
public:
UiCodeModelSupport(CppTools::CppModelManager *modelmanager,
ProjectExplorer::Project *project,
const QString &sourceFile,
const QString &uiHeaderFile);
~UiCodeModelSupport();
GeneratedCodeModelSupport(CppModelManager *modelmanager,
ProjectExplorer::ExtraCompiler *generator,
const Utils::FileName &generatedFile);
~GeneratedCodeModelSupport();
void setHeaderFileName(const QString &name);
/// \returns the contents encoded in UTF-8.
QByteArray contents() const;
QString uiFileName() const; // The .ui-file
QString fileName() const; // The header file
QString headerFileName() const { return fileName(); }
void updateFromEditor(const QString &formEditorContents);
void updateFromBuild();
private:
QString uicCommand() const;
QStringList environment() const;
QString fileName() const; // The generated file
private slots:
bool finishProcess();
static void update(const QList<ProjectExplorer::ExtraCompiler *> &generators);
private:
ProjectExplorer::Project *m_project;
enum State { BARE, RUNNING, FINISHED, ABORTING };
void onContentsChanged(const Utils::FileName &file);
void init() const;
bool runUic(const QString &ui) const;
mutable QProcess m_process;
QString m_uiFileName;
QString m_headerFileName;
mutable State m_state;
mutable QByteArray m_contents;
mutable QDateTime m_cacheTime;
static QList<UiCodeModelSupport *> m_waitingForStart;
};
class QTSUPPORT_EXPORT UiCodeModelManager : public QObject
{
Q_OBJECT
public:
// This needs to be called by the project *before* the C++ code model is updated!
static void update(ProjectExplorer::Project *project,
QHash<QString, QString> uiHeaders);
private slots:
void buildStateHasChanged(ProjectExplorer::Project *project);
void projectWasRemoved(ProjectExplorer::Project *project);
void editorIsAboutToClose(Core::IEditor *editor);
void editorWasChanged(Core::IEditor *editor);
void uiDocumentContentsHasChanged();
private:
UiCodeModelManager();
~UiCodeModelManager();
static void updateContents(const QString &uiFileName, const QString &contents);
QHash<ProjectExplorer::Project *, QList<UiCodeModelSupport *> > m_projectUiSupport;
Core::IEditor *m_lastEditor;
bool m_dirty;
static UiCodeModelManager *m_instance;
friend class Internal::QtSupportPlugin;
Utils::FileName m_generatedFileName;
ProjectExplorer::ExtraCompiler *m_generator;
};
} // QtSupport
#endif // UICODEMODELSUPPORT_H
} // CppTools
......@@ -48,7 +48,9 @@ static QString generatedHeaderOf(const QString &uiFileName)
{
if (const ProjectExplorer::Project *uiProject =
ProjectExplorer::SessionManager::projectForFile(Utils::FileName::fromString(uiFileName))) {
return uiProject->generatedUiHeader(Utils::FileName::fromString(uiFileName));
QStringList files = uiProject->filesGeneratedFrom(uiFileName);
if (!files.isEmpty()) // There should be at most one header generated from a .ui
return files.front();
}
return QString();
}
......
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "extracompiler.h"
#include "buildmanager.h"
#include "session.h"
#include "target.h"
#include "buildconfiguration.h"
#include "kitinformation.h"
#include <utils/qtcassert.h>
#include <coreplugin/idocument.h>
#include <coreplugin/editormanager/editormanager.h>
#include <QDateTime>
#include <QTimer>
namespace ProjectExplorer {
Q_GLOBAL_STATIC(QList<ExtraCompilerFactory *>, factories);
class ExtraCompilerPrivate
{
public:
const Project *project;
Utils::FileName source;
Utils::FileNameList targets;
QVector<QString> contents;
QDateTime compileTime;
Core::IEditor *lastEditor = 0;
QMetaObject::Connection activeBuildConfigConnection;
QMetaObject::Connection activeEnvironmentConnection;
bool dirty = false;
QTimer timer;
};
ExtraCompiler::ExtraCompiler(const Project *project, const Utils::FileName &source,
const Utils::FileNameList &targets, QObject *parent) :
QObject(parent), d(new ExtraCompilerPrivate)
{
d->project = project;
d->source = source;
d->targets = targets;
d->contents.resize(targets.size());
d->timer.setSingleShot(true);
connect(d->project, &Project::activeTargetChanged, this, &ExtraCompiler::onActiveTargetChanged);
onActiveTargetChanged();
connect(&d->timer, &QTimer::timeout, this, [this](){
if (d->dirty && d->lastEditor) {
run(d->lastEditor->document()->contents());
d->dirty = false;
}
});
connect(BuildManager::instance(), &BuildManager::buildStateChanged,
this, &ExtraCompiler::onTargetsBuilt);
connect(SessionManager::instance(), &SessionManager::projectRemoved,
this, [this](Project *project) {
if (project == d->project)
deleteLater();
});
Core::EditorManager *editorManager = Core::EditorManager::instance();
connect(editorManager, &Core::EditorManager::currentEditorChanged,
this, &ExtraCompiler::onEditorChanged);
connect(editorManager, &Core::EditorManager::editorAboutToClose,
this, &ExtraCompiler::onEditorAboutToClose);
// Use existing target files, where possible. Otherwise run the compiler.
QDateTime sourceTime = d->source.toFileInfo().lastModified();
foreach (const Utils::FileName &target, targets) {
QFileInfo targetFileInfo(target.toFileInfo());
if (!targetFileInfo.exists()) {
d->dirty = true;
continue;
}
QDateTime lastModified = targetFileInfo.lastModified();
if (lastModified < sourceTime)
d->dirty = true;
if (!d->compileTime.isValid() || d->compileTime > lastModified)
d->compileTime = lastModified;
QFile file(target.toString());
if (file.open(QFile::ReadOnly | QFile::Text))
setContent(target, QTextStream(&file).readAll());
}
if (d->dirty) {
// Run in the next event loop, as run() is not available yet in the ctor.
QTimer::singleShot(0, this, [this](){
QFile file(d->source.toString());
if (file.open(QFile::ReadOnly | QFile::Text))
run(file.readAll());
d->dirty = false;
});
}
}
ExtraCompiler::~ExtraCompiler()
{
delete d;
}
const Project *ExtraCompiler::project() const
{
return d->project;
}
Utils::FileName ExtraCompiler::source() const
{
return d->source;
}
QString ExtraCompiler::content(const Utils::FileName &file) const
{
for (int i = 0; i < d->targets.length(); ++i) {
if (d->targets[i] == file)