1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
SPDX-FileCopyrightText: 2020 Friedrich W. H. Kossebau <kossebau@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "compileanalyzejob.h"
// lib
#include <debug.h>
// KF
#include <KLocalizedString>
// Qt
#include <QTemporaryFile>
namespace KDevelop
{
QString CompileAnalyzeJob::spaceEscapedString(const QString& s)
{
return QString(s).replace(QLatin1Char(' '), QLatin1String("\\ "));
}
CompileAnalyzeJob::CompileAnalyzeJob(QObject* parent)
: OutputExecuteJob(parent)
{
setCapabilities(KJob::Killable);
setStandardToolView(IOutputView::TestView);
setBehaviours(IOutputView::AutoScroll);
setProperties(JobProperties(DisplayStdout | DisplayStderr | PostProcessOutput));
}
CompileAnalyzeJob::~CompileAnalyzeJob()
{
doKill();
if (!m_makeFilePath.isEmpty()) {
QFile::remove(m_makeFilePath);
}
}
void CompileAnalyzeJob::setParallelJobCount(int parallelJobCount)
{
m_parallelJobCount = parallelJobCount;
}
void CompileAnalyzeJob::setBuildDirectoryRoot(const QString& buildDir)
{
m_buildDir = buildDir;
}
void CompileAnalyzeJob::setCommand(const QString& command, bool verboseOutput)
{
m_command = command;
m_verboseOutput = verboseOutput;
}
void CompileAnalyzeJob::setToolDisplayName(const QString& toolDisplayName)
{
m_toolDisplayName = toolDisplayName;
m_fileStartedRegex = QRegularExpression(m_toolDisplayName + QLatin1String(" check started for (.+)$"));
m_fileFinishedRegex = QRegularExpression(m_toolDisplayName + QLatin1String(" check finished for (.+)$"));
}
void CompileAnalyzeJob::setSources(const QStringList& sources)
{
m_sources = sources;
}
void CompileAnalyzeJob::generateMakefile()
{
QTemporaryFile makefile(m_buildDir + QLatin1String("/kdevcompileanalyzerXXXXXX.makefile"));
makefile.setAutoRemove(false);
makefile.open();
m_makeFilePath = makefile.fileName();
QTextStream scriptStream(&makefile);
scriptStream << QStringLiteral("SOURCES =");
for (const auto& source : std::as_const(m_sources)) {
scriptStream << QLatin1String(" \\\n\t") << spaceEscapedString(source);
}
scriptStream << QLatin1Char('\n');
scriptStream << QLatin1String("COMMAND = ");
if (!m_verboseOutput) {
scriptStream << QLatin1Char('@');
}
scriptStream << m_command << QLatin1Char('\n');
scriptStream << QLatin1String(".PHONY: all $(SOURCES)\n");
scriptStream << QLatin1String("all: $(SOURCES)\n");
scriptStream << QLatin1String("$(SOURCES):\n");
scriptStream << QLatin1String("\t@echo '") << m_toolDisplayName << QLatin1String(" check started for $@'\n");
// Wrap filename ($@) with quotas to handle "whitespaced" file names.
scriptStream << QLatin1String("\t$(COMMAND) \"$@\"\n");
scriptStream << QLatin1String("\t@echo '") << m_toolDisplayName << QLatin1String(" check finished for $@'\n");
makefile.close();
}
void CompileAnalyzeJob::start()
{
// TODO: check success of creation
generateMakefile();
*this << QStringList{
QStringLiteral("make"),
QStringLiteral("-j"),
QString::number(m_parallelJobCount),
QStringLiteral("-k"), // keep-going
QStringLiteral("-f"),
m_makeFilePath,
};
qCDebug(KDEV_COMPILEANALYZER) << "executing:" << commandLine().join(QLatin1Char(' '));
m_finishedCount = 0;
m_totalCount = m_sources.size();
setPercent(0);
KDevelop::OutputExecuteJob::start();
}
void CompileAnalyzeJob::parseProgress(const QStringList& lines)
{
for (const auto& line : lines) {
const auto startedMatch = m_fileStartedRegex.match(line);
if (startedMatch.hasMatch()) {
emit infoMessage(this, startedMatch.captured(1));
continue;
}
const auto finishedMatch = m_fileFinishedRegex.match(line);
if (finishedMatch.hasMatch()) {
++m_finishedCount;
setPercent(static_cast<double>(m_finishedCount)/m_totalCount * 100);
continue;
}
}
}
void CompileAnalyzeJob::postProcessStdout(const QStringList& lines)
{
parseProgress(lines);
KDevelop::OutputExecuteJob::postProcessStdout(lines);
}
void CompileAnalyzeJob::childProcessExited(int exitCode, QProcess::ExitStatus exitStatus)
{
qCDebug(KDEV_COMPILEANALYZER) << "Process Finished, exitCode" << exitCode << "process exit status" << exitStatus;
setPercent(100);
KDevelop::OutputExecuteJob::childProcessExited(exitCode, exitStatus);
}
}
#include "moc_compileanalyzejob.cpp"
|