2012-09-06 22:38:25 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2012-09-06 22:38:25 +08:00
|
|
|
**
|
|
|
|
|
** 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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2012-09-06 22:38:25 +08:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-09-06 22:38:25 +08:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2012-09-06 22:38:25 +08:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
#include "cmaketool.h"
|
2012-09-06 22:38:25 +08:00
|
|
|
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
|
|
|
|
using namespace CMakeProjectManager::Internal;
|
|
|
|
|
|
|
|
|
|
///////////////////////////
|
2014-03-12 08:36:36 +01:00
|
|
|
// CMakeTool
|
2012-09-06 22:38:25 +08:00
|
|
|
///////////////////////////
|
2014-03-12 08:36:36 +01:00
|
|
|
CMakeTool::CMakeTool()
|
2012-09-06 22:38:25 +08:00
|
|
|
: m_state(Invalid), m_process(0), m_hasCodeBlocksMsvcGenerator(false), m_hasCodeBlocksNinjaGenerator(false)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
CMakeTool::~CMakeTool()
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
void CMakeTool::cancel()
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
if (m_process) {
|
|
|
|
|
disconnect(m_process, SIGNAL(finished(int)));
|
|
|
|
|
m_process->waitForFinished();
|
|
|
|
|
delete m_process;
|
|
|
|
|
m_process = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
void CMakeTool::setCMakeExecutable(const QString &executable)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
cancel();
|
|
|
|
|
m_process = new QProcess();
|
2015-01-29 14:32:36 +01:00
|
|
|
connect(m_process, static_cast<void (QProcess::*)(int)>(&QProcess::finished), this, &CMakeTool::finished);
|
2012-09-06 22:38:25 +08:00
|
|
|
|
|
|
|
|
m_executable = executable;
|
|
|
|
|
QFileInfo fi(m_executable);
|
|
|
|
|
if (fi.exists() && fi.isExecutable()) {
|
|
|
|
|
// Run it to find out more
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::RunningBasic;
|
2012-09-06 22:38:25 +08:00
|
|
|
if (!startProcess(QStringList(QLatin1String("--help"))))
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::Invalid;
|
2012-09-06 22:38:25 +08:00
|
|
|
} else {
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::Invalid;
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
void CMakeTool::finished(int exitCode)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
if (exitCode) {
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::Invalid;
|
2012-09-06 22:38:25 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2014-03-12 08:36:36 +01:00
|
|
|
if (m_state == CMakeTool::RunningBasic) {
|
2012-11-21 23:54:06 +02:00
|
|
|
QByteArray response = m_process->readAll();
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2012-11-21 23:54:06 +02:00
|
|
|
m_hasCodeBlocksMsvcGenerator = response.contains("CodeBlocks - NMake Makefiles");
|
|
|
|
|
m_hasCodeBlocksNinjaGenerator = response.contains("CodeBlocks - Ninja");
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2013-10-22 15:51:21 +02:00
|
|
|
if (response.isEmpty()) {
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::Invalid;
|
2012-09-06 22:38:25 +08:00
|
|
|
} else {
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::RunningFunctionList;
|
2012-09-06 22:38:25 +08:00
|
|
|
if (!startProcess(QStringList(QLatin1String("--help-command-list"))))
|
2013-02-16 22:33:36 +08:00
|
|
|
finished(0); // should never happen, just continue
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
2014-03-12 08:36:36 +01:00
|
|
|
} else if (m_state == CMakeTool::RunningFunctionList) {
|
2012-09-06 22:38:25 +08:00
|
|
|
parseFunctionOutput(m_process->readAll());
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::RunningFunctionDetails;
|
2012-09-06 22:38:25 +08:00
|
|
|
if (!startProcess(QStringList(QLatin1String("--help-commands"))))
|
2013-02-16 22:33:36 +08:00
|
|
|
finished(0); // should never happen, just continue
|
2014-03-12 08:36:36 +01:00
|
|
|
} else if (m_state == CMakeTool::RunningFunctionDetails) {
|
2012-09-06 22:38:25 +08:00
|
|
|
parseFunctionDetailsOutput(m_process->readAll());
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::RunningPropertyList;
|
2013-02-16 22:37:31 +08:00
|
|
|
if (!startProcess(QStringList(QLatin1String("--help-property-list"))))
|
|
|
|
|
finished(0); // should never happen, just continue
|
2014-03-12 08:36:36 +01:00
|
|
|
} else if (m_state == CMakeTool::RunningPropertyList) {
|
2013-02-16 22:37:31 +08:00
|
|
|
parseVariableOutput(m_process->readAll());
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::RunningVariableList;
|
2013-02-16 22:37:31 +08:00
|
|
|
if (!startProcess(QStringList(QLatin1String("--help-variable-list"))))
|
|
|
|
|
finished(0); // should never happen, just continue
|
2014-03-12 08:36:36 +01:00
|
|
|
} else if (m_state == CMakeTool::RunningVariableList) {
|
2013-02-16 22:37:31 +08:00
|
|
|
parseVariableOutput(m_process->readAll());
|
|
|
|
|
parseDone();
|
2014-03-12 08:36:36 +01:00
|
|
|
m_state = CMakeTool::RunningDone;
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
bool CMakeTool::isValid() const
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2014-03-12 08:36:36 +01:00
|
|
|
if (m_state == CMakeTool::Invalid)
|
2012-09-06 22:38:25 +08:00
|
|
|
return false;
|
2014-03-12 08:36:36 +01:00
|
|
|
if (m_state == CMakeTool::RunningBasic)
|
2012-09-06 22:38:25 +08:00
|
|
|
m_process->waitForFinished();
|
2014-03-12 08:36:36 +01:00
|
|
|
return (m_state != CMakeTool::Invalid);
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
bool CMakeTool::startProcess(const QStringList &args)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
m_process->start(m_executable, args);
|
|
|
|
|
return m_process->waitForStarted(2000);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
QString CMakeTool::cmakeExecutable() const
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
return m_executable;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
bool CMakeTool::hasCodeBlocksMsvcGenerator() const
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
if (!isValid())
|
|
|
|
|
return false;
|
|
|
|
|
return m_hasCodeBlocksMsvcGenerator;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
bool CMakeTool::hasCodeBlocksNinjaGenerator() const
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
if (!isValid())
|
|
|
|
|
return false;
|
|
|
|
|
return m_hasCodeBlocksNinjaGenerator;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
TextEditor::Keywords CMakeTool::keywords()
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2014-03-12 08:36:36 +01:00
|
|
|
while (m_state != RunningDone && m_state != CMakeTool::Invalid) {
|
2012-09-06 22:38:25 +08:00
|
|
|
m_process->waitForFinished();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
if (m_state == CMakeTool::Invalid)
|
2012-09-06 22:38:25 +08:00
|
|
|
return TextEditor::Keywords(QStringList(), QStringList(), QMap<QString, QStringList>());
|
|
|
|
|
|
|
|
|
|
return TextEditor::Keywords(m_variables, m_functions, m_functionArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void extractKeywords(const QByteArray &input, QStringList *destination)
|
|
|
|
|
{
|
|
|
|
|
if (!destination)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QString keyword;
|
|
|
|
|
int ignoreZone = 0;
|
|
|
|
|
for (int i = 0; i < input.count(); ++i) {
|
2012-11-21 23:54:06 +02:00
|
|
|
const QChar chr = QLatin1Char(input.at(i));
|
2012-09-06 22:38:25 +08:00
|
|
|
if (chr == QLatin1Char('{'))
|
|
|
|
|
++ignoreZone;
|
|
|
|
|
if (chr == QLatin1Char('}'))
|
|
|
|
|
--ignoreZone;
|
|
|
|
|
if (ignoreZone == 0) {
|
|
|
|
|
if ((chr.isLetterOrNumber() && chr.isUpper())
|
|
|
|
|
|| chr == QLatin1Char('_')) {
|
|
|
|
|
keyword += chr;
|
|
|
|
|
} else {
|
|
|
|
|
if (!keyword.isEmpty()) {
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (keyword.size() > 1)
|
2012-09-06 22:38:25 +08:00
|
|
|
*destination << keyword;
|
|
|
|
|
keyword.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (keyword.size() > 1)
|
2012-09-06 22:38:25 +08:00
|
|
|
*destination << keyword;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
void CMakeTool::parseFunctionOutput(const QByteArray &output)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
QList<QByteArray> cmakeFunctionsList = output.split('\n');
|
|
|
|
|
m_functions.clear();
|
|
|
|
|
if (!cmakeFunctionsList.isEmpty()) {
|
|
|
|
|
cmakeFunctionsList.removeFirst(); //remove version string
|
|
|
|
|
foreach (const QByteArray &function, cmakeFunctionsList)
|
2012-11-21 23:54:06 +02:00
|
|
|
m_functions << QString::fromLocal8Bit(function.trimmed());
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
QString CMakeTool::formatFunctionDetails(const QString &command, const QString &args)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2012-11-21 23:54:06 +02:00
|
|
|
return QString::fromLatin1("<table><tr><td><b>%1</b></td><td>%2</td></tr>")
|
2014-08-28 17:33:47 +02:00
|
|
|
.arg(command.toHtmlEscaped(), args.toHtmlEscaped());
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
void CMakeTool::parseFunctionDetailsOutput(const QByteArray &output)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
QStringList cmakeFunctionsList = m_functions;
|
|
|
|
|
QList<QByteArray> cmakeCommandsHelp = output.split('\n');
|
|
|
|
|
for (int i = 0; i < cmakeCommandsHelp.count(); ++i) {
|
|
|
|
|
QByteArray lineTrimmed = cmakeCommandsHelp.at(i).trimmed();
|
2013-03-13 13:31:41 +01:00
|
|
|
if (cmakeFunctionsList.isEmpty())
|
|
|
|
|
break;
|
2012-09-06 22:38:25 +08:00
|
|
|
if (cmakeFunctionsList.first().toLatin1() == lineTrimmed) {
|
|
|
|
|
QStringList commandSyntaxes;
|
|
|
|
|
QString currentCommandSyntax;
|
|
|
|
|
QString currentCommand = cmakeFunctionsList.takeFirst();
|
|
|
|
|
++i;
|
|
|
|
|
for (; i < cmakeCommandsHelp.count(); ++i) {
|
|
|
|
|
lineTrimmed = cmakeCommandsHelp.at(i).trimmed();
|
|
|
|
|
|
2013-03-13 13:31:41 +01:00
|
|
|
if (!cmakeFunctionsList.isEmpty() && cmakeFunctionsList.first().toLatin1() == lineTrimmed) {
|
2012-09-06 22:38:25 +08:00
|
|
|
//start of next function in output
|
|
|
|
|
if (!currentCommandSyntax.isEmpty())
|
2012-11-21 23:54:06 +02:00
|
|
|
commandSyntaxes << currentCommandSyntax.append(QLatin1String("</table>"));
|
2012-09-06 22:38:25 +08:00
|
|
|
--i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (lineTrimmed.startsWith(currentCommand.toLatin1() + "(")) {
|
|
|
|
|
if (!currentCommandSyntax.isEmpty())
|
2012-11-21 23:54:06 +02:00
|
|
|
commandSyntaxes << currentCommandSyntax.append(QLatin1String("</table>"));
|
2012-09-06 22:38:25 +08:00
|
|
|
|
|
|
|
|
QByteArray argLine = lineTrimmed.mid(currentCommand.length());
|
|
|
|
|
extractKeywords(argLine, &m_variables);
|
2012-11-21 23:54:06 +02:00
|
|
|
currentCommandSyntax = formatFunctionDetails(currentCommand, QString::fromUtf8(argLine));
|
2012-09-06 22:38:25 +08:00
|
|
|
} else {
|
|
|
|
|
if (!currentCommandSyntax.isEmpty()) {
|
|
|
|
|
if (lineTrimmed.isEmpty()) {
|
2012-11-21 23:54:06 +02:00
|
|
|
commandSyntaxes << currentCommandSyntax.append(QLatin1String("</table>"));
|
2012-09-06 22:38:25 +08:00
|
|
|
currentCommandSyntax.clear();
|
|
|
|
|
} else {
|
|
|
|
|
extractKeywords(lineTrimmed, &m_variables);
|
2012-11-21 23:54:06 +02:00
|
|
|
currentCommandSyntax += QString::fromLatin1("<tr><td> </td><td>%1</td></tr>")
|
2014-08-28 17:33:47 +02:00
|
|
|
.arg(QString::fromLocal8Bit(lineTrimmed).toHtmlEscaped());
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_functionArgs[currentCommand] = commandSyntaxes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_functions = m_functionArgs.keys();
|
2013-02-16 22:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
void CMakeTool::parseVariableOutput(const QByteArray &output)
|
2013-02-16 22:37:31 +08:00
|
|
|
{
|
|
|
|
|
QList<QByteArray> variableList = output.split('\n');
|
|
|
|
|
if (!variableList.isEmpty()) {
|
|
|
|
|
variableList.removeFirst(); //remove version string
|
|
|
|
|
foreach (const QByteArray &variable, variableList) {
|
|
|
|
|
if (variable.contains("_<CONFIG>")) {
|
|
|
|
|
m_variables << QString::fromLocal8Bit(variable).replace(QLatin1String("_<CONFIG>"), QLatin1String("_DEBUG"));
|
|
|
|
|
m_variables << QString::fromLocal8Bit(variable).replace(QLatin1String("_<CONFIG>"), QLatin1String("_RELEASE"));
|
|
|
|
|
m_variables << QString::fromLocal8Bit(variable).replace(QLatin1String("_<CONFIG>"), QLatin1String("_MINSIZEREL"));
|
|
|
|
|
m_variables << QString::fromLocal8Bit(variable).replace(QLatin1String("_<CONFIG>"), QLatin1String("_RELWITHDEBINFO"));
|
|
|
|
|
} else if (variable.contains("_<LANG>")) {
|
|
|
|
|
m_variables << QString::fromLocal8Bit(variable).replace(QLatin1String("_<LANG>"), QLatin1String("_C"));
|
|
|
|
|
m_variables << QString::fromLocal8Bit(variable).replace(QLatin1String("_<LANG>"), QLatin1String("_CXX"));
|
|
|
|
|
} else if (!variable.contains("_<") && !variable.contains('[')) {
|
|
|
|
|
m_variables << QString::fromLocal8Bit(variable);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
void CMakeTool::parseDone()
|
2013-02-16 22:37:31 +08:00
|
|
|
{
|
2012-09-06 22:38:25 +08:00
|
|
|
m_variables.sort();
|
|
|
|
|
m_variables.removeDuplicates();
|
|
|
|
|
}
|