2012-09-06 22:38:25 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2012-09-06 22:38:25 +08:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2012-09-06 22:38:25 +08:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
#include "cmaketool.h"
|
2015-02-24 21:57:00 +01:00
|
|
|
#include "cmaketoolmanager.h"
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/environment.h>
|
2015-02-04 17:54:46 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-09-06 22:38:25 +08:00
|
|
|
#include <QFileInfo>
|
2016-01-22 13:50:58 +01:00
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QSet>
|
2012-09-06 22:38:25 +08:00
|
|
|
#include <QTextDocument>
|
2015-02-04 17:54:46 +01:00
|
|
|
#include <QUuid>
|
2016-01-22 13:50:58 +01:00
|
|
|
#include <QVariantMap>
|
2015-02-04 17:54:46 +01:00
|
|
|
|
|
|
|
|
using namespace CMakeProjectManager;
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2015-02-04 17:54:46 +01:00
|
|
|
const char CMAKE_INFORMATION_ID[] = "Id";
|
|
|
|
|
const char CMAKE_INFORMATION_COMMAND[] = "Binary";
|
|
|
|
|
const char CMAKE_INFORMATION_DISPLAYNAME[] = "DisplayName";
|
|
|
|
|
const char CMAKE_INFORMATION_AUTODETECTED[] = "AutoDetected";
|
2012-09-06 22:38:25 +08:00
|
|
|
|
|
|
|
|
///////////////////////////
|
2014-03-12 08:36:36 +01:00
|
|
|
// CMakeTool
|
2012-09-06 22:38:25 +08:00
|
|
|
///////////////////////////
|
2016-01-07 15:22:53 +01:00
|
|
|
CMakeTool::CMakeTool(Detection d, const Core::Id &id) :
|
2016-01-22 13:50:58 +01:00
|
|
|
m_id(id), m_isAutoDetected(d == AutoDetection)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
QTC_ASSERT(m_id.isValid(), m_id = Core::Id::fromString(QUuid::createUuid().toString()));
|
2015-02-04 17:54:46 +01:00
|
|
|
}
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
CMakeTool::CMakeTool(const QVariantMap &map, bool fromSdk) : m_isAutoDetected(fromSdk)
|
2015-02-04 17:54:46 +01:00
|
|
|
{
|
|
|
|
|
m_id = Core::Id::fromSetting(map.value(QLatin1String(CMAKE_INFORMATION_ID)));
|
|
|
|
|
m_displayName = map.value(QLatin1String(CMAKE_INFORMATION_DISPLAYNAME)).toString();
|
|
|
|
|
|
|
|
|
|
//loading a CMakeTool from SDK is always autodetection
|
|
|
|
|
if (!fromSdk)
|
|
|
|
|
m_isAutoDetected = map.value(QLatin1String(CMAKE_INFORMATION_AUTODETECTED), false).toBool();
|
|
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
setCMakeExecutable(Utils::FileName::fromString(map.value(QLatin1String(CMAKE_INFORMATION_COMMAND)).toString()));
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
Core::Id CMakeTool::createId()
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
return Core::Id::fromString(QUuid::createUuid().toString());
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:54:46 +01:00
|
|
|
void CMakeTool::setCMakeExecutable(const Utils::FileName &executable)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
if (m_executable == executable)
|
|
|
|
|
return;
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
m_didRun = false;
|
|
|
|
|
m_didAttemptToRun = false;
|
2015-02-24 21:57:00 +01:00
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
m_executable = executable;
|
2015-02-24 21:57:00 +01:00
|
|
|
CMakeToolManager::notifyAboutUpdate(this);
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
bool CMakeTool::isValid() const
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
if (!m_id.isValid())
|
|
|
|
|
return false;
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
if (!m_didAttemptToRun)
|
|
|
|
|
supportedGenerators();
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
return m_didRun;
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
Utils::SynchronousProcessResponse CMakeTool::run(const QString &arg) const
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
if (m_didAttemptToRun && !m_didRun) {
|
|
|
|
|
Utils::SynchronousProcessResponse response;
|
|
|
|
|
response.result = Utils::SynchronousProcessResponse::StartFailed;
|
|
|
|
|
return response;
|
2015-02-04 17:54:46 +01:00
|
|
|
}
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
Utils::SynchronousProcess cmake;
|
|
|
|
|
cmake.setTimeoutS(1);
|
|
|
|
|
cmake.setFlags(Utils::SynchronousProcess::UnixTerminalDisabled);
|
|
|
|
|
Utils::Environment env = Utils::Environment::systemEnvironment();
|
|
|
|
|
env.set(QLatin1String("LC_ALL"), QLatin1String("C"));
|
|
|
|
|
cmake.setProcessEnvironment(env.toProcessEnvironment());
|
|
|
|
|
cmake.setTimeOutMessageBoxEnabled(false);
|
|
|
|
|
|
|
|
|
|
Utils::SynchronousProcessResponse response = cmake.run(m_executable.toString(), QStringList() << arg);
|
|
|
|
|
m_didAttemptToRun = true;
|
|
|
|
|
m_didRun = (response.result == Utils::SynchronousProcessResponse::Finished);
|
|
|
|
|
return response;
|
2015-02-04 17:54:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariantMap CMakeTool::toMap() const
|
|
|
|
|
{
|
|
|
|
|
QVariantMap data;
|
|
|
|
|
data.insert(QLatin1String(CMAKE_INFORMATION_DISPLAYNAME), m_displayName);
|
|
|
|
|
data.insert(QLatin1String(CMAKE_INFORMATION_ID), m_id.toSetting());
|
|
|
|
|
data.insert(QLatin1String(CMAKE_INFORMATION_COMMAND), m_executable.toString());
|
|
|
|
|
data.insert(QLatin1String(CMAKE_INFORMATION_AUTODETECTED), m_isAutoDetected);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::FileName 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;
|
2016-01-22 13:50:58 +01:00
|
|
|
return supportedGenerators().contains(QLatin1String("CodeBlocks - NMake Makefiles"));
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2014-03-12 08:36:36 +01:00
|
|
|
bool CMakeTool::hasCodeBlocksNinjaGenerator() const
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
return supportedGenerators().contains(QLatin1String("CodeBlocks - Ninja"));
|
2015-02-04 17:54:46 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
QStringList CMakeTool::supportedGenerators() const
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
if (m_generators.isEmpty()) {
|
|
|
|
|
Utils::SynchronousProcessResponse response = run(QLatin1String("--help"));
|
|
|
|
|
if (response.result == Utils::SynchronousProcessResponse::Finished) {
|
|
|
|
|
bool inGeneratorSection = false;
|
|
|
|
|
const QStringList lines = response.stdOut.split(QLatin1Char('\n'));
|
|
|
|
|
foreach (const QString &line, lines) {
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
if (line == QLatin1String("Generators"))
|
|
|
|
|
inGeneratorSection = true;
|
|
|
|
|
if (!inGeneratorSection)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (line.startsWith(QLatin1String(" "))) {
|
|
|
|
|
int pos = line.indexOf(QLatin1Char('='));
|
|
|
|
|
if (pos < 0)
|
|
|
|
|
pos = line.length();
|
|
|
|
|
if (pos >= 0) {
|
|
|
|
|
--pos;
|
|
|
|
|
while (pos > 2 && line.at(pos).isSpace())
|
|
|
|
|
--pos;
|
|
|
|
|
}
|
|
|
|
|
if (pos > 2)
|
|
|
|
|
m_generators.append(line.mid(2, pos - 1));
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-22 13:50:58 +01:00
|
|
|
return m_generators;
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
TextEditor::Keywords CMakeTool::keywords()
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
if (m_functions.isEmpty()) {
|
|
|
|
|
Utils::SynchronousProcessResponse response;
|
|
|
|
|
response = run(QLatin1String("--help-command-list"));
|
|
|
|
|
if (response.result == Utils::SynchronousProcessResponse::Finished)
|
|
|
|
|
m_functions = response.stdOut.split(QLatin1Char('\n'));
|
|
|
|
|
|
|
|
|
|
response = run(QLatin1String("--help-commands"));
|
|
|
|
|
if (response.result == Utils::SynchronousProcessResponse::Finished)
|
|
|
|
|
parseFunctionDetailsOutput(response.stdOut);
|
|
|
|
|
|
|
|
|
|
response = run(QLatin1String("--help-property-list"));
|
|
|
|
|
if (response.result == Utils::SynchronousProcessResponse::Finished)
|
|
|
|
|
m_variables = parseVariableOutput(response.stdOut);
|
|
|
|
|
|
|
|
|
|
response = run(QLatin1String("--help-variable-list"));
|
|
|
|
|
if (response.result == Utils::SynchronousProcessResponse::Finished) {
|
|
|
|
|
m_variables.append(parseVariableOutput(response.stdOut));
|
|
|
|
|
m_variables = Utils::filteredUnique(m_variables);
|
|
|
|
|
Utils::sort(m_variables);
|
|
|
|
|
}
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
2016-01-22 13:50:58 +01:00
|
|
|
|
|
|
|
|
return TextEditor::Keywords(m_variables, m_functions, m_functionArgs);
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
bool CMakeTool::isAutoDetected() const
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
return m_isAutoDetected;
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:54:46 +01:00
|
|
|
QString CMakeTool::displayName() const
|
|
|
|
|
{
|
|
|
|
|
return m_displayName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CMakeTool::setDisplayName(const QString &displayName)
|
|
|
|
|
{
|
|
|
|
|
m_displayName = displayName;
|
2015-02-24 21:57:00 +01:00
|
|
|
CMakeToolManager::notifyAboutUpdate(this);
|
2015-02-04 17:54:46 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-10 15:47:09 +01:00
|
|
|
void CMakeTool::setPathMapper(const CMakeTool::PathMapper &pathMapper)
|
|
|
|
|
{
|
|
|
|
|
m_pathMapper = pathMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CMakeTool::mapAllPaths(ProjectExplorer::Kit *kit, const QString &in) const
|
|
|
|
|
{
|
|
|
|
|
if (m_pathMapper)
|
|
|
|
|
return m_pathMapper(kit, in);
|
|
|
|
|
return in;
|
|
|
|
|
}
|
2015-02-04 17:54:46 +01:00
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
static QStringList parseDefinition(const QString &definition)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
QStringList result;
|
|
|
|
|
QString word;
|
|
|
|
|
bool ignoreWord = false;
|
|
|
|
|
QVector<QChar> braceStack;
|
|
|
|
|
|
|
|
|
|
foreach (const QChar &c, definition) {
|
|
|
|
|
if (c == QLatin1Char('[') || c == QLatin1Char('<') || c == QLatin1Char('(')) {
|
|
|
|
|
braceStack.append(c);
|
|
|
|
|
ignoreWord = false;
|
|
|
|
|
} else if (c == QLatin1Char(']') || c == QLatin1Char('>') || c == QLatin1Char(')')) {
|
|
|
|
|
if (braceStack.isEmpty() || braceStack.takeLast() == QLatin1Char('<'))
|
|
|
|
|
ignoreWord = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c == QLatin1Char(' ') || c == QLatin1Char('[') || c == QLatin1Char('<') || c == QLatin1Char('(')
|
|
|
|
|
|| c == QLatin1Char(']') || c == QLatin1Char('>') || c == QLatin1Char(')')) {
|
|
|
|
|
if (!ignoreWord && !word.isEmpty()) {
|
|
|
|
|
if (result.isEmpty() || Utils::allOf(word, [](const QChar &c) { return c.isUpper() || c == QLatin1Char('_'); }))
|
|
|
|
|
result.append(word);
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
2016-01-22 13:50:58 +01:00
|
|
|
word.clear();
|
|
|
|
|
ignoreWord = false;
|
|
|
|
|
} else {
|
|
|
|
|
word.append(c);
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-22 13:50:58 +01:00
|
|
|
return result;
|
2013-02-16 22:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
void CMakeTool::parseFunctionDetailsOutput(const QString &output)
|
2013-02-16 22:37:31 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
QSet<QString> functionSet;
|
|
|
|
|
functionSet.fromList(m_functions);
|
|
|
|
|
|
|
|
|
|
bool expectDefinition = false;
|
|
|
|
|
QString currentDefinition;
|
|
|
|
|
|
|
|
|
|
const QStringList lines = output.split(QLatin1Char('\n'));
|
|
|
|
|
for (int i = 0; i < lines.count(); ++i) {
|
|
|
|
|
const QString line = lines.at(i);
|
|
|
|
|
|
|
|
|
|
if (line == QLatin1String("::")) {
|
|
|
|
|
expectDefinition = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (expectDefinition) {
|
|
|
|
|
if (!line.startsWith(QLatin1Char(' ')) && !line.isEmpty()) {
|
|
|
|
|
expectDefinition = false;
|
|
|
|
|
QStringList words = parseDefinition(currentDefinition);
|
|
|
|
|
if (!words.isEmpty()) {
|
|
|
|
|
const QString command = words.takeFirst();
|
|
|
|
|
if (functionSet.contains(command)) {
|
|
|
|
|
QStringList tmp = words + m_functionArgs[command];
|
|
|
|
|
Utils::sort(tmp);
|
|
|
|
|
m_functionArgs[command] = Utils::filteredUnique(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!words.isEmpty() && functionSet.contains(words.at(0)))
|
|
|
|
|
m_functionArgs[words.at(0)];
|
|
|
|
|
currentDefinition.clear();
|
|
|
|
|
} else {
|
|
|
|
|
currentDefinition.append(line.trimmed() + QLatin1Char(' '));
|
2013-02-16 22:37:31 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 13:50:58 +01:00
|
|
|
QStringList CMakeTool::parseVariableOutput(const QString &output)
|
2013-02-16 22:37:31 +08:00
|
|
|
{
|
2016-01-22 13:50:58 +01:00
|
|
|
const QStringList variableList = output.split(QLatin1Char('\n'));
|
|
|
|
|
QStringList result;
|
|
|
|
|
foreach (const QString &v, variableList) {
|
|
|
|
|
if (v.contains(QLatin1String("<CONFIG>"))) {
|
|
|
|
|
const QString tmp = QString(v).replace(QLatin1String("<CONFIG>"), QLatin1String("%1"));
|
|
|
|
|
result << tmp.arg(QLatin1String("DEBUG")) << tmp.arg(QLatin1String("RELEASE"))
|
|
|
|
|
<< tmp.arg(QLatin1String("MINSIZEREL")) << tmp.arg(QLatin1String("RELWITHDEBINFO"));
|
|
|
|
|
} else if (v.contains(QLatin1String("<LANG>"))) {
|
|
|
|
|
const QString tmp = QString(v).replace(QLatin1String("<LANG>"), QLatin1String("%1"));
|
|
|
|
|
result << tmp.arg(QLatin1String("C")) << tmp.arg(QLatin1String("CXX"));
|
|
|
|
|
} else if (!v.contains(QLatin1Char('<')) && !v.contains(QLatin1Char('['))) {
|
|
|
|
|
result << v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|