diff options
author | Ulf Hermann <ulf.hermann@theqtcompany.com> | 2016-01-15 16:12:54 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@theqtcompany.com> | 2016-02-16 12:53:05 +0000 |
commit | 2e3e06052065c97fa4a2ddd2b1418bb3447387d4 (patch) | |
tree | 0628f8d1a68a3f16b66e3d61c6a26a7bb34ed21c /src/plugins/qtsupport/uicgenerator.cpp | |
parent | 13e0abf526e87179fc6b50e967902189fc42b832 (diff) |
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: Tobias Hunger <tobias.hunger@theqtcompany.com>
Diffstat (limited to 'src/plugins/qtsupport/uicgenerator.cpp')
-rw-r--r-- | src/plugins/qtsupport/uicgenerator.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/plugins/qtsupport/uicgenerator.cpp b/src/plugins/qtsupport/uicgenerator.cpp new file mode 100644 index 0000000000..a855558adc --- /dev/null +++ b/src/plugins/qtsupport/uicgenerator.cpp @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** 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 "uicgenerator.h" +#include "baseqtversion.h" +#include "qtkitinformation.h" + +#include <projectexplorer/kitmanager.h> +#include <projectexplorer/target.h> +#include <projectexplorer/buildconfiguration.h> + +#include <QFileInfo> +#include <QDir> +#include <QLoggingCategory> +#include <QProcess> +#include <QDateTime> + +namespace QtSupport { + +QLoggingCategory UicGenerator::m_log("qtc.uicgenerator"); + +UicGenerator::UicGenerator(const ProjectExplorer::Project *project, const Utils::FileName &source, + const Utils::FileNameList &targets, QObject *parent) : + ProjectExplorer::ExtraCompiler(project, source, targets, parent) +{ + connect(&m_process, static_cast<void(QProcess::*)(int)>(&QProcess::finished), + this, &UicGenerator::finishProcess); +} + +void UicGenerator::finishProcess() +{ + if (!m_process.waitForFinished(3000) + && m_process.exitStatus() != QProcess::NormalExit + && m_process.exitCode() != 0) { + + qCDebug(m_log) << "finish process: failed" << m_process.readAllStandardError(); + m_process.kill(); + return; + } + + // As far as I can discover in the UIC sources, it writes out local 8-bit encoding. The + // conversion below is to normalize both the encoding, and the line terminators. + QString normalized = QString::fromLocal8Bit(m_process.readAllStandardOutput()); + qCDebug(m_log) << "finish process: ok" << normalized.size() << "bytes."; + setCompileTime(QDateTime::currentDateTime()); + setContent(targets()[0], normalized); +} + +void UicGenerator::run(const QByteArray &sourceContent) +{ + if (m_process.state() != QProcess::NotRunning) { + m_process.kill(); + m_process.waitForFinished(3000); + } + + QtSupport::BaseQtVersion *version = 0; + ProjectExplorer::Target *target; + if ((target = project()->activeTarget())) + version = QtSupport::QtKitInformation::qtVersion(target->kit()); + else + version = QtSupport::QtKitInformation::qtVersion(ProjectExplorer::KitManager::defaultKit()); + + if (!version) + return; + + const QString generator = version->uicCommand(); + if (generator.isEmpty()) + return; + + m_process.setProcessEnvironment(buildEnvironment().toProcessEnvironment()); + + qCDebug(m_log) << " UicGenerator::run " << generator << " on " + << sourceContent.size() << " bytes"; + m_process.start(generator, QStringList(), QIODevice::ReadWrite); + if (!m_process.waitForStarted()) + return; + + m_process.write(sourceContent); + if (!m_process.waitForBytesWritten(3000)) { + qCDebug(m_log) << "failed" << m_process.readAllStandardError(); + m_process.kill(); + return; + } + + m_process.closeWriteChannel(); +} + +ProjectExplorer::FileType UicGeneratorFactory::sourceType() const +{ + return ProjectExplorer::FormType; +} + +QString UicGeneratorFactory::sourceTag() const +{ + return QLatin1String("ui"); +} + +ProjectExplorer::ExtraCompiler *UicGeneratorFactory::create(const ProjectExplorer::Project *project, + const Utils::FileName &source, + const Utils::FileNameList &targets) +{ + return new UicGenerator(project, source, targets, this); +} + +} // QtSupport |