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

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();
}
......
This diff is collapsed.
/****************************************************************************
**
** 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.
**
****************************************************************************/
#pragma once
#include "projectnodes.h"
#include "project.h"
#include <coreplugin/editormanager/ieditor.h>
#include <utils/fileutils.h>
#include <utils/environment.h>
namespace ProjectExplorer {
class ExtraCompilerPrivate;
class PROJECTEXPLORER_EXPORT ExtraCompiler : public QObject
{
Q_OBJECT
public:
ExtraCompiler(const Project *project, const Utils::FileName &source,
const Utils::FileNameList &targets, QObject *parent = 0);
virtual ~ExtraCompiler() override;
const Project *project() const;
Utils::FileName source() const;
// You can set the contents from the outside. This is done if the file has been (re)created by
// the regular build process.
void setContent(const Utils::FileName &file, const QString &content);
QString content(const Utils::FileName &file) const;
Utils::FileNameList targets() const;
void setCompileTime(const QDateTime &time);
QDateTime compileTime() const;
signals:
void contentsChanged(const Utils::FileName &file);
protected:
Utils::Environment buildEnvironment() const;
private:
void onTargetsBuilt(Project *project);
void onEditorChanged(Core::IEditor *editor);
void onEditorAboutToClose(Core::IEditor *editor);
void onActiveTargetChanged();
void onActiveBuildConfigurationChanged();
void setDirty();
virtual void run(const QByteArray &sourceContent) = 0;
ExtraCompilerPrivate *const d;
};
class PROJECTEXPLORER_EXPORT ExtraCompilerFactory : public QObject
{
Q_OBJECT
public:
explicit ExtraCompilerFactory(QObject *parent = 0);
virtual FileType sourceType() const = 0;
virtual QString sourceTag() const = 0;
virtual ExtraCompiler *create(const Project *project, const Utils::FileName &source,
const Utils::FileNameList &targets) = 0;
static void registerExtraCompilerFactory(ExtraCompilerFactory *factory);
static QList<ExtraCompilerFactory *> extraCompilerFactories();
};
} // namespace ProjectExplorer
......@@ -577,9 +577,10 @@ EditorConfiguration *Project::editorConfiguration() const
return &d->m_editorConfiguration;
}
QString Project::generatedUiHeader(const Utils::FileName & /* formFile */) const
QStringList Project::filesGeneratedFrom(const QString &file) const
{
return QString();
Q_UNUSED(file);
return QStringList();
}
void Project::setProjectContext(Core::Context context)
......
......@@ -114,8 +114,7 @@ public:
AllFiles = SourceFiles | GeneratedFiles
};
virtual QStringList files(FilesMode fileMode) const = 0;
// TODO: generalize to find source(s) of generated files?
virtual QString generatedUiHeader(const Utils::FileName &formFile) const;
virtual QStringList filesGeneratedFrom(const QString &sourceFile) const;
static QString makeUnique(const QString &preferredName, const QStringList &usedNames);
......
......@@ -154,7 +154,8 @@ HEADERS += projectexplorer.h \
expanddata.h \
waitforstopdialog.h \
projectexplorericons.h \
projectexplorer_global.h
projectexplorer_global.h \
extracompiler.h
SOURCES += projectexplorer.cpp \
abi.cpp \
......@@ -293,7 +294,8 @@ SOURCES += projectexplorer.cpp \
projectpanelfactory.cpp \
projecttree.cpp \
expanddata.cpp \
waitforstopdialog.cpp
waitforstopdialog.cpp \
extracompiler.cpp
FORMS += processstep.ui \
editorsettingspropertiespage.ui \
......
......@@ -70,6 +70,7 @@ QtcPlugin {
"environmentitemswidget.cpp", "environmentitemswidget.h",
"environmentwidget.cpp", "environmentwidget.h",
"expanddata.cpp", "expanddata.h",
"extracompiler.cpp", "extracompiler.h",
"foldernavigationwidget.cpp", "foldernavigationwidget.h",
"gccparser.cpp", "gccparser.h",
"gcctoolchain.cpp", "gcctoolchain.h",
......
......@@ -43,6 +43,7 @@
#include <coreplugin/progressmanager/progressmanager.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/projectpartbuilder.h>
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/buildenvironmentwidget.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/buildtargetinfo.h>
......@@ -56,7 +57,7 @@
#include <projectexplorer/toolchain.h>
#include <projectexplorer/headerpath.h>
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/uicodemodelsupport.h>
#include <cpptools/generatedcodemodelsupport.h>
#include <qmljstools/qmljsmodelmanager.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <utils/hostosinfo.h>
......@@ -133,6 +134,7 @@ QbsProject::~QbsProject()
delete m_qbsUpdateFutureInterface;
m_qbsUpdateFutureInterface = 0;
}
qDeleteAll(m_extraCompilers);
}
QString QbsProject::displayName() const
......@@ -693,7 +695,13 @@ void QbsProject::updateCppCodeModel()
ppBuilder.setQtVersion(CppTools::ProjectPart::NoQt);