Make it possible to register multiple cmake instances

This patch add the CMakeToolManager, a central repository for cmake instances.
One instance is always the currently used "default". By that its possible to
switch between different cmake installations. The next step will be adding
it to the Kits.

Change-Id: I310fdd805e0ed239077a5632303e891dbd1d9ea1
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@theqtcompany.com>
Reviewed-by: Daniel Teske <daniel.teske@theqtcompany.com>
This commit is contained in:
Benjamin Zeller
2015-02-04 17:54:46 +01:00
parent f4a3b4ca5f
commit 99e9643dea
18 changed files with 1173 additions and 187 deletions

View File

@@ -0,0 +1,41 @@
/****************************************************************************
**
** Copyright (C) 2015 Canonical Ltd.
** Contact: http://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 http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** 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.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CMAKE_GLOBAL_H
#define CMAKE_GLOBAL_H
#include <QtGlobal>
#if defined(CMAKEPROJECTMANAGER_LIBRARY)
# define CMAKE_EXPORT Q_DECL_EXPORT
#else
# define CMAKE_EXPORT Q_DECL_IMPORT
#endif
#endif // CMAKE_GLOBAL_H

View File

@@ -264,7 +264,7 @@ QString CMakeDocument::suggestedFileName() const
// CMakeEditorFactory // CMakeEditorFactory
// //
CMakeEditorFactory::CMakeEditorFactory(CMakeSettingsPage *settingsPage) CMakeEditorFactory::CMakeEditorFactory()
{ {
setId(Constants::CMAKE_EDITOR_ID); setId(Constants::CMAKE_EDITOR_ID);
setDisplayName(tr(Constants::CMAKE_EDITOR_DISPLAY_NAME)); setDisplayName(tr(Constants::CMAKE_EDITOR_DISPLAY_NAME));
@@ -278,7 +278,7 @@ CMakeEditorFactory::CMakeEditorFactory(CMakeSettingsPage *settingsPage)
setCommentStyle(Utils::CommentDefinition::HashStyle); setCommentStyle(Utils::CommentDefinition::HashStyle);
setCodeFoldingSupported(true); setCodeFoldingSupported(true);
setCompletionAssistProvider(new CMakeFileCompletionAssistProvider(settingsPage)); setCompletionAssistProvider(new CMakeFileCompletionAssistProvider);
setEditorActionHandlers(TextEditorActionHandler::UnCommentSelection setEditorActionHandlers(TextEditorActionHandler::UnCommentSelection
| TextEditorActionHandler::JumpToFileUnderCursor); | TextEditorActionHandler::JumpToFileUnderCursor);

View File

@@ -58,7 +58,7 @@ class CMakeEditorFactory : public TextEditor::TextEditorFactory
Q_OBJECT Q_OBJECT
public: public:
CMakeEditorFactory(CMakeSettingsPage *settingsPage); CMakeEditorFactory();
}; };
} // namespace Internal } // namespace Internal

View File

@@ -32,6 +32,7 @@
#include "cmakeprojectconstants.h" #include "cmakeprojectconstants.h"
#include "cmakeprojectmanager.h" #include "cmakeprojectmanager.h"
#include "cmakesettingspage.h" #include "cmakesettingspage.h"
#include "cmaketoolmanager.h"
#include <texteditor/codeassist/keywordscompletionassist.h> #include <texteditor/codeassist/keywordscompletionassist.h>
@@ -41,8 +42,7 @@ using namespace TextEditor;
// ------------------------------- // -------------------------------
// CMakeFileCompletionAssistProvider // CMakeFileCompletionAssistProvider
// ------------------------------- // -------------------------------
CMakeFileCompletionAssistProvider::CMakeFileCompletionAssistProvider(CMakeSettingsPage *settingsPage) CMakeFileCompletionAssistProvider::CMakeFileCompletionAssistProvider()
: m_settingsPage(settingsPage)
{} {}
CMakeFileCompletionAssistProvider::~CMakeFileCompletionAssistProvider() CMakeFileCompletionAssistProvider::~CMakeFileCompletionAssistProvider()
@@ -55,5 +55,10 @@ bool CMakeFileCompletionAssistProvider::supportsEditor(Core::Id editorId) const
IAssistProcessor *CMakeFileCompletionAssistProvider::createProcessor() const IAssistProcessor *CMakeFileCompletionAssistProvider::createProcessor() const
{ {
return new KeywordsCompletionAssistProcessor(m_settingsPage->keywords()); TextEditor::Keywords keywords = TextEditor::Keywords(QStringList(), QStringList(), QMap<QString, QStringList>());
CMakeTool *cmake = CMakeToolManager::defaultCMakeTool();
if (cmake && cmake->isValid())
keywords = cmake->keywords();
return new KeywordsCompletionAssistProcessor(keywords);
} }

View File

@@ -43,14 +43,11 @@ class CMakeFileCompletionAssistProvider : public TextEditor::CompletionAssistPro
Q_OBJECT Q_OBJECT
public: public:
CMakeFileCompletionAssistProvider(CMakeSettingsPage *settingsPage); CMakeFileCompletionAssistProvider();
~CMakeFileCompletionAssistProvider(); ~CMakeFileCompletionAssistProvider();
bool supportsEditor(Core::Id editorId) const; bool supportsEditor(Core::Id editorId) const;
TextEditor::IAssistProcessor *createProcessor() const; TextEditor::IAssistProcessor *createProcessor() const;
private:
CMakeSettingsPage *m_settingsPage;
}; };
} // Internal } // Internal

View File

@@ -30,6 +30,7 @@
#include "cmakeopenprojectwizard.h" #include "cmakeopenprojectwizard.h"
#include "cmakeprojectmanager.h" #include "cmakeprojectmanager.h"
#include "cmaketoolmanager.h"
#include "cmakebuildconfiguration.h" #include "cmakebuildconfiguration.h"
#include "cmakebuildinfo.h" #include "cmakebuildinfo.h"
#include "generatorinfo.h" #include "generatorinfo.h"
@@ -386,7 +387,13 @@ void ChooseCMakePage::updateErrorText()
void ChooseCMakePage::cmakeExecutableChanged() void ChooseCMakePage::cmakeExecutableChanged()
{ {
m_cmakeWizard->cmakeManager()->setCMakeExecutable(m_cmakeExecutable->path()); CMakeTool *cmake = CMakeToolManager::defaultCMakeTool();
if (!cmake) {
Core::Id id = CMakeToolManager::registerOrFindCMakeTool(m_cmakeExecutable->fileName());
CMakeToolManager::setDefaultCMakeTool(id);
} else {
cmake->setCMakeExecutable(m_cmakeExecutable->fileName());
}
updateErrorText(); updateErrorText();
emit completeChanged(); emit completeChanged();
} }

View File

@@ -33,6 +33,7 @@
#include "cmakeprojectconstants.h" #include "cmakeprojectconstants.h"
#include "cmakeproject.h" #include "cmakeproject.h"
#include "cmakesettingspage.h" #include "cmakesettingspage.h"
#include "cmaketoolmanager.h"
#include <utils/synchronousprocess.h> #include <utils/synchronousprocess.h>
@@ -47,8 +48,7 @@
using namespace CMakeProjectManager::Internal; using namespace CMakeProjectManager::Internal;
CMakeManager::CMakeManager(CMakeSettingsPage *cmakeSettingsPage) CMakeManager::CMakeManager()
: m_settingsPage(cmakeSettingsPage)
{ {
ProjectExplorer::ProjectTree *tree = ProjectExplorer::ProjectTree::instance(); ProjectExplorer::ProjectTree *tree = ProjectExplorer::ProjectTree::instance();
connect(tree, &ProjectExplorer::ProjectTree::aboutToShowContextMenu, connect(tree, &ProjectExplorer::ProjectTree::aboutToShowContextMenu,
@@ -130,32 +130,39 @@ QString CMakeManager::mimeType() const
QString CMakeManager::cmakeExecutable() const QString CMakeManager::cmakeExecutable() const
{ {
return m_settingsPage->cmakeExecutable(); CMakeTool *cmake = CMakeToolManager::defaultCMakeTool();
if (cmake)
return cmake->cmakeExecutable().toString();
return QString();
} }
bool CMakeManager::isCMakeExecutableValid() const bool CMakeManager::isCMakeExecutableValid() const
{ {
return m_settingsPage->isCMakeExecutableValid(); CMakeTool *cmake = CMakeToolManager::defaultCMakeTool();
} if (cmake)
return cmake->isValid();
void CMakeManager::setCMakeExecutable(const QString &executable) return false;
{
m_settingsPage->setCMakeExecutable(executable);
} }
bool CMakeManager::hasCodeBlocksMsvcGenerator() const bool CMakeManager::hasCodeBlocksMsvcGenerator() const
{ {
return m_settingsPage->hasCodeBlocksMsvcGenerator(); CMakeTool *cmake = CMakeToolManager::defaultCMakeTool();
if (cmake)
return cmake->hasCodeBlocksMsvcGenerator();
return false;
} }
bool CMakeManager::hasCodeBlocksNinjaGenerator() const bool CMakeManager::hasCodeBlocksNinjaGenerator() const
{ {
return m_settingsPage->hasCodeBlocksNinjaGenerator(); CMakeTool *cmake = CMakeToolManager::defaultCMakeTool();
if (cmake)
return cmake->hasCodeBlocksNinjaGenerator();
return false;
} }
bool CMakeManager::preferNinja() const bool CMakeManager::preferNinja() const
{ {
return m_settingsPage->preferNinja(); return CMakeToolManager::preferNinja();
} }
// need to refactor this out // need to refactor this out

View File

@@ -53,7 +53,7 @@ class CMakeManager : public ProjectExplorer::IProjectManager
{ {
Q_OBJECT Q_OBJECT
public: public:
CMakeManager(CMakeSettingsPage *cmakeSettingsPage); CMakeManager();
virtual ProjectExplorer::Project *openProject(const QString &fileName, QString *errorString); virtual ProjectExplorer::Project *openProject(const QString &fileName, QString *errorString);
virtual QString mimeType() const; virtual QString mimeType() const;
@@ -61,8 +61,6 @@ public:
QString cmakeExecutable() const; QString cmakeExecutable() const;
bool isCMakeExecutableValid() const; bool isCMakeExecutableValid() const;
void setCMakeExecutable(const QString &executable);
void createXmlFile(Utils::QtcProcess *process, void createXmlFile(Utils::QtcProcess *process,
const QString &arguments, const QString &arguments,
const QString &sourceDirectory, const QString &sourceDirectory,

View File

@@ -1,3 +1,4 @@
DEFINES += CMAKEPROJECTMANAGER_LIBRARY
include(../../qtcreatorplugin.pri) include(../../qtcreatorplugin.pri)
HEADERS = cmakebuildinfo.h \ HEADERS = cmakebuildinfo.h \
@@ -16,7 +17,9 @@ HEADERS = cmakebuildinfo.h \
cmaketool.h \ cmaketool.h \
cmakeparser.h \ cmakeparser.h \
generatorinfo.h \ generatorinfo.h \
cmakesettingspage.h cmakesettingspage.h \
cmaketoolmanager.h \
cmake_global.h
SOURCES = cmakeproject.cpp \ SOURCES = cmakeproject.cpp \
cmakeprojectplugin.cpp \ cmakeprojectplugin.cpp \
@@ -32,7 +35,8 @@ SOURCES = cmakeproject.cpp \
cmaketool.cpp \ cmaketool.cpp \
cmakeparser.cpp \ cmakeparser.cpp \
generatorinfo.cpp \ generatorinfo.cpp \
cmakesettingspage.cpp cmakesettingspage.cpp \
cmaketoolmanager.cpp
RESOURCES += cmakeproject.qrc RESOURCES += cmakeproject.qrc

View File

@@ -18,6 +18,7 @@ QtcPlugin {
] ]
files: [ files: [
"cmake_global.h",
"cmakebuildconfiguration.cpp", "cmakebuildconfiguration.cpp",
"cmakebuildconfiguration.h", "cmakebuildconfiguration.h",
"cmakebuildinfo.h", "cmakebuildinfo.h",
@@ -45,6 +46,8 @@ QtcPlugin {
"cmakerunconfiguration.h", "cmakerunconfiguration.h",
"cmaketool.cpp", "cmaketool.cpp",
"cmaketool.h", "cmaketool.h",
"cmaketoolmanager.cpp",
"cmaketoolmanager.h",
"makestep.cpp", "makestep.cpp",
"makestep.h", "makestep.h",
"cmakesettingspage.h", "cmakesettingspage.h",

View File

@@ -39,6 +39,7 @@
#include "cmakelocatorfilter.h" #include "cmakelocatorfilter.h"
#include "cmakefilecompletionassist.h" #include "cmakefilecompletionassist.h"
#include "cmakesettingspage.h" #include "cmakesettingspage.h"
#include "cmaketoolmanager.h"
#include <coreplugin/featureprovider.h> #include <coreplugin/featureprovider.h>
#include <utils/mimetypes/mimedatabase.h> #include <utils/mimetypes/mimedatabase.h>
@@ -62,15 +63,17 @@ bool CMakeProjectPlugin::initialize(const QStringList & /*arguments*/, QString *
Q_UNUSED(errorMessage) Q_UNUSED(errorMessage)
Utils::MimeDatabase::addMimeTypes(QLatin1String(":cmakeproject/CMakeProjectManager.mimetypes.xml")); Utils::MimeDatabase::addMimeTypes(QLatin1String(":cmakeproject/CMakeProjectManager.mimetypes.xml"));
CMakeSettingsPage *cmp = new CMakeSettingsPage(); addAutoReleasedObject(new CMakeSettingsPage);
addAutoReleasedObject(cmp); addAutoReleasedObject(new CMakeManager);
addAutoReleasedObject(new CMakeManager(cmp));
addAutoReleasedObject(new MakeStepFactory); addAutoReleasedObject(new MakeStepFactory);
addAutoReleasedObject(new CMakeRunConfigurationFactory); addAutoReleasedObject(new CMakeRunConfigurationFactory);
addAutoReleasedObject(new CMakeBuildConfigurationFactory); addAutoReleasedObject(new CMakeBuildConfigurationFactory);
addAutoReleasedObject(new CMakeEditorFactory(cmp)); addAutoReleasedObject(new CMakeEditorFactory);
addAutoReleasedObject(new CMakeLocatorFilter); addAutoReleasedObject(new CMakeLocatorFilter);
addAutoReleasedObject(new CMakeFileCompletionAssistProvider(cmp)); addAutoReleasedObject(new CMakeFileCompletionAssistProvider);
new CMakeToolManager(this);
CMakeToolManager::restoreCMakeTools();
return true; return true;
} }

View File

@@ -36,6 +36,9 @@
#include <QObject> #include <QObject>
namespace CMakeProjectManager { namespace CMakeProjectManager {
class CMakeToolManager;
namespace Internal { namespace Internal {
class CMakeProjectPlugin class CMakeProjectPlugin

View File

@@ -28,28 +28,516 @@
** **
****************************************************************************/ ****************************************************************************/
#include "cmakesettingspage.h" #include "cmakesettingspage.h"
#include "cmaketoolmanager.h"
#include <projectexplorer/projectexplorerconstants.h> #include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/project.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <utils/environment.h> #include <utils/environment.h>
#include <utils/detailswidget.h>
#include <utils/pathchooser.h>
#include <utils/qtcassert.h>
#include <utils/treemodel.h>
#include <QSettings>
#include <QGroupBox>
#include <QSpacerItem>
#include <QFormLayout> #include <QFormLayout>
#include <QBoxLayout> #include <QHeaderView>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTreeView>
#include <QWidget>
#include <QUuid>
#include <QCheckBox> #include <QCheckBox>
using namespace Utils;
namespace CMakeProjectManager { namespace CMakeProjectManager {
namespace Internal { namespace Internal {
class CMakeToolTreeItem;
// --------------------------------------------------------------------------
// CMakeToolItemModel
// --------------------------------------------------------------------------
class CMakeToolItemModel : public TreeModel
{
Q_DECLARE_TR_FUNCTIONS(CMakeProjectManager::CMakeSettingsPage)
public:
CMakeToolItemModel();
CMakeToolTreeItem *cmakeToolItem(const Core::Id &id) const;
CMakeToolTreeItem *cmakeToolItem(const QModelIndex &index) const;
QModelIndex addCMakeTool(const QString &name, const FileName &executable, const bool isAutoDetected);
QModelIndex addCMakeTool(const CMakeTool *item, bool changed);
TreeItem *autoGroupItem() const;
TreeItem *manualGroupItem() const;
void reevaluateChangedFlag(CMakeToolTreeItem *item) const;
void updateCMakeTool(const Core::Id &id, const QString &displayName, const FileName &executable);
void removeCMakeTool(const Core::Id &id);
void apply();
Core::Id defaultItemId() const;
void setDefaultItemId(const Core::Id &id);
QString uniqueDisplayName(const QString &base) const;
private:
Core::Id m_defaultItemId;
QList<Core::Id> m_removedItems;
};
class CMakeToolTreeItem : public TreeItem
{
Q_DECLARE_TR_FUNCTIONS(CMakeProjectManager::CMakeSettingsPage)
public:
CMakeToolTreeItem(const CMakeTool *item, bool changed) :
m_id(item->id()),
m_name(item->displayName()),
m_executable(item->cmakeExecutable()),
m_autodetected(item->isAutoDetected()),
m_changed(changed)
{}
CMakeToolTreeItem(const QString &name, const Utils::FileName &executable, bool autodetected) :
m_id(Core::Id::fromString(QUuid::createUuid().toString())),
m_name(name),
m_executable(executable),
m_autodetected(autodetected),
m_changed(true)
{}
CMakeToolTreeItem() : m_autodetected(false), m_changed(true) {}
CMakeToolItemModel *model() const { return static_cast<CMakeToolItemModel *>(TreeItem::model()); }
QVariant data(int column, int role) const
{
switch (role) {
case Qt::DisplayRole:
switch (column) {
case 0: {
QString name = m_name;
if (model()->defaultItemId() == m_id)
name += tr(" (Default)");
return name;
}
case 1: return m_executable.toUserOutput();
}
case Qt::FontRole: {
QFont font;
font.setBold(m_changed);
font.setItalic(model()->defaultItemId() == m_id);
return font;
}
}
return QVariant();
}
Core::Id m_id;
QString m_name;
FileName m_executable;
bool m_autodetected;
bool m_changed;
};
CMakeToolItemModel::CMakeToolItemModel()
{
setHeader(QStringList() << tr("Name") << tr("Location"));
rootItem()->appendChild(new TreeItem(QStringList() << tr("Auto-detected") << QString() << QString()));
rootItem()->appendChild(new TreeItem(QStringList() << tr("Manual") << QString() << QString()));
foreach (const CMakeTool *item, CMakeToolManager::cmakeTools())
addCMakeTool(item, false);
CMakeTool *defTool = CMakeToolManager::defaultCMakeTool();
m_defaultItemId = defTool ? defTool->id() : Core::Id();
}
QModelIndex CMakeToolItemModel::addCMakeTool(const QString &name, const FileName &executable, const bool isAutoDetected)
{
CMakeToolTreeItem *item = new CMakeToolTreeItem(name, executable, isAutoDetected);
if (isAutoDetected)
autoGroupItem()->appendChild(item);
else
manualGroupItem()->appendChild(item);
return item->index();
}
QModelIndex CMakeToolItemModel::addCMakeTool(const CMakeTool *item, bool changed)
{
CMakeToolTreeItem *treeItem = new CMakeToolTreeItem(item, changed);
if (item->isAutoDetected())
autoGroupItem()->appendChild(treeItem);
else
manualGroupItem()->appendChild(treeItem);
return treeItem->index();
}
TreeItem *CMakeToolItemModel::autoGroupItem() const
{
return rootItem()->child(0);
}
TreeItem *CMakeToolItemModel::manualGroupItem() const
{
return rootItem()->child(1);
}
void CMakeToolItemModel::reevaluateChangedFlag(CMakeToolTreeItem *item) const
{
CMakeTool *orig = CMakeToolManager::findById(item->m_id);
item->m_changed = !orig || orig->displayName() != item->m_name
|| orig->cmakeExecutable() != item->m_executable;
//make sure the item is marked as changed when the default cmake was changed
CMakeTool *origDefTool = CMakeToolManager::defaultCMakeTool();
Core::Id origDefault = origDefTool ? origDefTool->id() : Core::Id();
if (origDefault != m_defaultItemId) {
if (item->m_id == origDefault || item->m_id == m_defaultItemId)
item->m_changed = true;
}
item->update(); // Notify views.
}
void CMakeToolItemModel::updateCMakeTool(const Core::Id &id, const QString &displayName,
const FileName &executable)
{
CMakeToolTreeItem *treeItem = cmakeToolItem(id);
QTC_ASSERT(treeItem, return);
treeItem->m_name = displayName;
treeItem->m_executable = executable;
reevaluateChangedFlag(treeItem);
}
CMakeToolTreeItem *CMakeToolItemModel::cmakeToolItem(const Core::Id &id) const
{
auto matcher = [id](CMakeToolTreeItem *n) { return n->m_id == id; };
return findItemAtLevel<CMakeToolTreeItem *>(2, matcher);
}
CMakeToolTreeItem *CMakeToolItemModel::cmakeToolItem(const QModelIndex &index) const
{
TreeItem *item = itemFromIndex(index);
if (item->level() == 2)
return static_cast<CMakeToolTreeItem *>(item);
return 0;
}
void CMakeToolItemModel::removeCMakeTool(const Core::Id &id)
{
CMakeToolTreeItem *treeItem = cmakeToolItem(id);
QTC_ASSERT(treeItem, return);
removeItem(treeItem);
m_removedItems.append(id);
delete treeItem;
}
void CMakeToolItemModel::apply()
{
foreach (const Core::Id &id, m_removedItems)
CMakeToolManager::deregisterCMakeTool(id);
foreach (auto item, treeLevelItems<CMakeToolTreeItem *>(2)) {
item->m_changed = false;
bool isNew = false;
CMakeTool *cmake = CMakeToolManager::findById(item->m_id);
if (!cmake) {
isNew = true;
CMakeTool::Detection detection = item->m_autodetected ? CMakeTool::AutoDetection
: CMakeTool::ManualDetection;
cmake = new CMakeTool(detection, item->m_id);
}
cmake->setDisplayName(item->m_name);
cmake->setCMakeExecutable(item->m_executable);
if (isNew) {
if (!CMakeToolManager::registerCMakeTool(cmake)) {
delete cmake;
item->m_changed = true;
}
}
}
CMakeToolManager::setDefaultCMakeTool(defaultItemId());
}
Core::Id CMakeToolItemModel::defaultItemId() const
{
return m_defaultItemId;
}
void CMakeToolItemModel::setDefaultItemId(const Core::Id &id)
{
if (m_defaultItemId == id)
return;
Core::Id oldDefaultId = m_defaultItemId;
m_defaultItemId = id;
CMakeToolTreeItem *newDefault = cmakeToolItem(id);
if (newDefault)
reevaluateChangedFlag(newDefault);
CMakeToolTreeItem *oldDefault = cmakeToolItem(oldDefaultId);
if (oldDefault)
reevaluateChangedFlag(oldDefault);
}
QString CMakeToolItemModel::uniqueDisplayName(const QString &base) const
{
QStringList names;
foreach (CMakeToolTreeItem *item, treeLevelItems<CMakeToolTreeItem *>(2))
names << item->m_name;
return ProjectExplorer::Project::makeUnique(base, names);
}
// -----------------------------------------------------------------------
// CMakeToolItemConfigWidget
// -----------------------------------------------------------------------
class CMakeToolItemConfigWidget : public QWidget
{
Q_DECLARE_TR_FUNCTIONS(CMakeProjectManager::CMakeSettingsPage)
public:
explicit CMakeToolItemConfigWidget(CMakeToolItemModel *model);
void load(const CMakeToolTreeItem *item);
void store() const;
private:
CMakeToolItemModel *m_model;
QLineEdit *m_displayNameLineEdit;
PathChooser *m_binaryChooser;
bool m_autodetected;
Core::Id m_id;
bool m_loadingItem;
};
CMakeToolItemConfigWidget::CMakeToolItemConfigWidget(CMakeToolItemModel *model)
: m_model(model), m_loadingItem(false)
{
m_displayNameLineEdit = new QLineEdit(this);
m_binaryChooser = new PathChooser(this);
m_binaryChooser->setExpectedKind(PathChooser::ExistingCommand);
m_binaryChooser->setMinimumWidth(400);
m_binaryChooser->setHistoryCompleter(QLatin1String("Cmake.Command.History"));
QFormLayout *formLayout = new QFormLayout(this);
formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
formLayout->addRow(new QLabel(tr("Name:")), m_displayNameLineEdit);
formLayout->addRow(new QLabel(tr("Path:")), m_binaryChooser);
connect(m_binaryChooser, &PathChooser::changed,
this, &CMakeToolItemConfigWidget::store);
connect(m_displayNameLineEdit, &QLineEdit::textChanged,
this, &CMakeToolItemConfigWidget::store);
}
void CMakeToolItemConfigWidget::store() const
{
if (!m_loadingItem && m_id.isValid())
m_model->updateCMakeTool(m_id, m_displayNameLineEdit->text(), m_binaryChooser->fileName());
}
void CMakeToolItemConfigWidget::load(const CMakeToolTreeItem *item)
{
m_loadingItem = true; // avoid intermediate signal handling
m_id = Core::Id();
if (!item) {
m_loadingItem = false;
return;
}
// Set values:
m_displayNameLineEdit->setEnabled(!item->m_autodetected);
m_displayNameLineEdit->setText(item->m_name);
m_binaryChooser->setReadOnly(item->m_autodetected);
m_binaryChooser->setFileName(item->m_executable);
m_id = item->m_id;
m_loadingItem = false;
}
// --------------------------------------------------------------------------
// CMakeToolConfigWidget
// --------------------------------------------------------------------------
class CMakeToolConfigWidget : public QWidget
{
public:
CMakeToolConfigWidget() : m_currentItem(0)
{
m_addButton = new QPushButton(tr("Add"), this);
m_cloneButton = new QPushButton(tr("Clone"), this);
m_cloneButton->setEnabled(false);
m_delButton = new QPushButton(tr("Remove"), this);
m_delButton->setEnabled(false);
m_makeDefButton = new QPushButton(tr("Make Default"), this);
m_makeDefButton->setEnabled(false);
m_preferNinjaCheckBox = new QCheckBox(tr("Prefer Ninja generator (CMake 2.8.9 or higher required)"));
m_preferNinjaCheckBox->setChecked(CMakeToolManager::preferNinja());
m_container = new DetailsWidget(this);
m_container->setState(DetailsWidget::NoSummary);
m_container->setVisible(false);
m_cmakeToolsView = new QTreeView(this);
m_cmakeToolsView->setModel(&m_model);
m_cmakeToolsView->setUniformRowHeights(true);
m_cmakeToolsView->setRootIsDecorated(false);
m_cmakeToolsView->setSelectionMode(QAbstractItemView::SingleSelection);
m_cmakeToolsView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_cmakeToolsView->expandAll();
QHeaderView *header = m_cmakeToolsView->header();
header->setStretchLastSection(false);
header->setSectionResizeMode(0, QHeaderView::ResizeToContents);
header->setSectionResizeMode(1, QHeaderView::Stretch);
QVBoxLayout *buttonLayout = new QVBoxLayout();
buttonLayout->setContentsMargins(0, 0, 0, 0);
buttonLayout->addWidget(m_addButton);
buttonLayout->addWidget(m_cloneButton);
buttonLayout->addWidget(m_delButton);
buttonLayout->addWidget(m_makeDefButton);
buttonLayout->addItem(new QSpacerItem(10, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
QVBoxLayout *verticalLayout = new QVBoxLayout();
verticalLayout->addWidget(m_cmakeToolsView);
verticalLayout->addWidget(m_container);
verticalLayout->addWidget(m_preferNinjaCheckBox);
QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
horizontalLayout->addLayout(verticalLayout);
horizontalLayout->addLayout(buttonLayout);
connect(m_cmakeToolsView->selectionModel(), &QItemSelectionModel::currentChanged,
this, &CMakeToolConfigWidget::currentCMakeToolChanged, Qt::QueuedConnection);
connect(m_addButton, &QAbstractButton::clicked,
this, &CMakeToolConfigWidget::addCMakeTool);
connect(m_cloneButton, &QAbstractButton::clicked,
this, &CMakeToolConfigWidget::cloneCMakeTool);
connect(m_delButton, &QAbstractButton::clicked,
this, &CMakeToolConfigWidget::removeCMakeTool);
connect(m_makeDefButton, &QAbstractButton::clicked,
this, &CMakeToolConfigWidget::setDefaultCMakeTool);
m_itemConfigWidget = new CMakeToolItemConfigWidget(&m_model);
m_container->setWidget(m_itemConfigWidget);
}
void apply();
void cloneCMakeTool();
void addCMakeTool();
void removeCMakeTool();
void setDefaultCMakeTool();
void currentCMakeToolChanged(const QModelIndex &newCurrent);
CMakeToolItemModel m_model;
QTreeView *m_cmakeToolsView;
QPushButton *m_addButton;
QPushButton *m_cloneButton;
QPushButton *m_delButton;
QPushButton *m_makeDefButton;
QCheckBox *m_preferNinjaCheckBox;
DetailsWidget *m_container;
CMakeToolItemConfigWidget *m_itemConfigWidget;
CMakeToolTreeItem *m_currentItem;
};
void CMakeToolConfigWidget::apply()
{
m_model.apply();
CMakeToolManager::setPreferNinja(m_preferNinjaCheckBox->checkState() == Qt::Checked);
}
void CMakeToolConfigWidget::cloneCMakeTool()
{
if (!m_currentItem)
return;
QModelIndex newItem = m_model.addCMakeTool(tr("Clone of %1").arg(m_currentItem->m_name),
m_currentItem->m_executable,
false);
m_cmakeToolsView->setCurrentIndex(newItem);
}
void CMakeToolConfigWidget::addCMakeTool()
{
QModelIndex newItem = m_model.addCMakeTool(m_model.uniqueDisplayName(tr("New CMake")),
FileName(), false);
m_cmakeToolsView->setCurrentIndex(newItem);
}
void CMakeToolConfigWidget::removeCMakeTool()
{
bool delDef = m_model.defaultItemId() == m_currentItem->m_id;
m_model.removeCMakeTool(m_currentItem->m_id);
m_currentItem = 0;
if (delDef) {
CMakeToolTreeItem *it = static_cast<CMakeToolTreeItem *>(m_model.autoGroupItem()->firstChild());
if (!it)
it = static_cast<CMakeToolTreeItem *>(m_model.manualGroupItem()->firstChild());
if (it)
m_model.setDefaultItemId(it->m_id);
}
TreeItem *newCurrent = m_model.manualGroupItem()->lastChild();
if (!newCurrent)
newCurrent = m_model.autoGroupItem()->lastChild();
if (newCurrent)
m_cmakeToolsView->setCurrentIndex(newCurrent->index());
}
void CMakeToolConfigWidget::setDefaultCMakeTool()
{
if (!m_currentItem)
return;
m_model.setDefaultItemId(m_currentItem->m_id);
m_makeDefButton->setEnabled(false);
}
void CMakeToolConfigWidget::currentCMakeToolChanged(const QModelIndex &newCurrent)
{
m_currentItem = m_model.cmakeToolItem(newCurrent);
m_itemConfigWidget->load(m_currentItem);
m_container->setVisible(m_currentItem);
m_cloneButton->setEnabled(m_currentItem);
m_delButton->setEnabled(m_currentItem && !m_currentItem->m_autodetected);
m_makeDefButton->setEnabled(m_currentItem && (!m_model.defaultItemId().isValid() || m_currentItem->m_id != m_model.defaultItemId()));
}
///// /////
// CMakeSettingsPage // CMakeSettingsPage
//// ////
CMakeSettingsPage::CMakeSettingsPage() : m_widget(0)
CMakeSettingsPage::CMakeSettingsPage()
: m_pathchooser(0), m_preferNinja(0)
{ {
setId("Z.CMake"); setId("Z.CMake");
setDisplayName(tr("CMake")); setDisplayName(tr("CMake"));
@@ -57,132 +545,30 @@ CMakeSettingsPage::CMakeSettingsPage()
setDisplayCategory(QCoreApplication::translate("ProjectExplorer", setDisplayCategory(QCoreApplication::translate("ProjectExplorer",
ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY)); ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY));
setCategoryIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON)); setCategoryIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));
QSettings *settings = Core::ICore::settings();
settings->beginGroup(QLatin1String("CMakeSettings"));
m_cmakeValidatorForUser.setCMakeExecutable(settings->value(QLatin1String("cmakeExecutable")).toString());
settings->endGroup();
m_cmakeValidatorForSystem.setCMakeExecutable(findCmakeExecutable());
}
bool CMakeSettingsPage::isCMakeExecutableValid() const
{
if (m_cmakeValidatorForUser.isValid())
return true;
return m_cmakeValidatorForSystem.isValid();
} }
CMakeSettingsPage::~CMakeSettingsPage() CMakeSettingsPage::~CMakeSettingsPage()
{ {
m_cmakeValidatorForUser.cancel();
m_cmakeValidatorForSystem.cancel();
}
QString CMakeSettingsPage::findCmakeExecutable() const
{
return Utils::Environment::systemEnvironment().searchInPath(QLatin1String("cmake")).toString();
} }
QWidget *CMakeSettingsPage::widget() QWidget *CMakeSettingsPage::widget()
{ {
if (!m_widget) { if (!m_widget)
m_widget = new QWidget; m_widget = new CMakeToolConfigWidget;
QFormLayout *formLayout = new QFormLayout(m_widget);
formLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
m_pathchooser = new Utils::PathChooser;
m_pathchooser->setExpectedKind(Utils::PathChooser::ExistingCommand);
m_pathchooser->setHistoryCompleter(QLatin1String("Cmake.Command.History"));
formLayout->addRow(tr("Executable:"), m_pathchooser);
formLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
m_preferNinja = new QCheckBox(tr("Prefer Ninja generator (CMake 2.8.9 or higher required)"));
formLayout->addRow(m_preferNinja);
}
m_pathchooser->setPath(m_cmakeValidatorForUser.cmakeExecutable());
m_preferNinja->setChecked(preferNinja());
return m_widget; return m_widget;
} }
void CMakeSettingsPage::saveSettings() const
{
QSettings *settings = Core::ICore::settings();
settings->beginGroup(QLatin1String("CMakeSettings"));
settings->setValue(QLatin1String("cmakeExecutable"), m_cmakeValidatorForUser.cmakeExecutable());
settings->setValue(QLatin1String("preferNinja"), m_preferNinja->isChecked());
settings->endGroup();
}
void CMakeSettingsPage::apply() void CMakeSettingsPage::apply()
{ {
if (!m_pathchooser) // page was never shown QTC_ASSERT(m_widget, return);
return; m_widget->m_itemConfigWidget->store();
if (m_cmakeValidatorForUser.cmakeExecutable() != m_pathchooser->path()) m_widget->m_model.apply();
m_cmakeValidatorForUser.setCMakeExecutable(m_pathchooser->path());
saveSettings();
} }
void CMakeSettingsPage::finish() void CMakeSettingsPage::finish()
{ {
delete m_widget; delete m_widget;
} m_widget = 0;
QString CMakeSettingsPage::cmakeExecutable() const
{
if (!isCMakeExecutableValid())
return QString();
if (m_cmakeValidatorForUser.isValid())
return m_cmakeValidatorForUser.cmakeExecutable();
if (m_cmakeValidatorForSystem.isValid())
return m_cmakeValidatorForSystem.cmakeExecutable();
return QString();
}
void CMakeSettingsPage::setCMakeExecutable(const QString &executable)
{
if (m_cmakeValidatorForUser.cmakeExecutable() == executable)
return;
m_cmakeValidatorForUser.setCMakeExecutable(executable);
}
bool CMakeSettingsPage::hasCodeBlocksMsvcGenerator() const
{
if (m_cmakeValidatorForUser.isValid())
return m_cmakeValidatorForUser.hasCodeBlocksMsvcGenerator();
if (m_cmakeValidatorForSystem.isValid())
return m_cmakeValidatorForSystem.hasCodeBlocksMsvcGenerator();
return false;
}
bool CMakeSettingsPage::hasCodeBlocksNinjaGenerator() const
{
if (m_cmakeValidatorForUser.isValid())
return m_cmakeValidatorForUser.hasCodeBlocksNinjaGenerator();
if (m_cmakeValidatorForSystem.isValid())
return m_cmakeValidatorForSystem.hasCodeBlocksNinjaGenerator();
return false;
}
bool CMakeSettingsPage::preferNinja() const
{
QSettings *settings = Core::ICore::settings();
settings->beginGroup(QLatin1String("CMakeSettings"));
const bool r = settings->value(QLatin1String("preferNinja"), false).toBool();
settings->endGroup();
return r;
}
TextEditor::Keywords CMakeSettingsPage::keywords()
{
if (m_cmakeValidatorForUser.isValid())
return m_cmakeValidatorForUser.keywords();
if (m_cmakeValidatorForSystem.isValid())
return m_cmakeValidatorForSystem.keywords();
return TextEditor::Keywords(QStringList(), QStringList(), QMap<QString, QStringList>());
} }
} // namespace Internal } // namespace Internal

View File

@@ -32,18 +32,16 @@
#include <coreplugin/dialogs/ioptionspage.h> #include <coreplugin/dialogs/ioptionspage.h>
#include <utils/pathchooser.h> #include <utils/pathchooser.h>
#include <texteditor/codeassist/keywordscompletionassist.h>
#include <QPointer> #include <QPointer>
#include "cmaketool.h" #include "cmaketool.h"
QT_FORWARD_DECLARE_CLASS(QLabel)
QT_FORWARD_DECLARE_CLASS(QCheckBox)
namespace CMakeProjectManager { namespace CMakeProjectManager {
namespace Internal { namespace Internal {
class CMakeToolConfigWidget;
class CMakeSettingsPage : public Core::IOptionsPage class CMakeSettingsPage : public Core::IOptionsPage
{ {
Q_OBJECT Q_OBJECT
@@ -56,24 +54,8 @@ public:
void apply(); void apply();
void finish(); void finish();
QString cmakeExecutable() const;
void setCMakeExecutable(const QString &executable);
bool isCMakeExecutableValid() const;
bool hasCodeBlocksMsvcGenerator() const;
bool hasCodeBlocksNinjaGenerator() const;
bool preferNinja() const;
TextEditor::Keywords keywords();
private: private:
void saveSettings() const; CMakeToolConfigWidget *m_widget;
QString findCmakeExecutable() const;
QPointer<QWidget> m_widget;
Utils::PathChooser *m_pathchooser;
QCheckBox *m_preferNinja;
CMakeTool m_cmakeValidatorForUser;
CMakeTool m_cmakeValidatorForSystem;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -30,19 +30,49 @@
#include "cmaketool.h" #include "cmaketool.h"
#include <utils/qtcassert.h>
#include <QProcess> #include <QProcess>
#include <QFileInfo> #include <QFileInfo>
#include <QTextDocument> #include <QTextDocument>
#include <QUuid>
using namespace CMakeProjectManager::Internal; using namespace CMakeProjectManager;
const char CMAKE_INFORMATION_ID[] = "Id";
const char CMAKE_INFORMATION_COMMAND[] = "Binary";
const char CMAKE_INFORMATION_DISPLAYNAME[] = "DisplayName";
const char CMAKE_INFORMATION_AUTODETECTED[] = "AutoDetected";
/////////////////////////// ///////////////////////////
// CMakeTool // CMakeTool
/////////////////////////// ///////////////////////////
CMakeTool::CMakeTool() CMakeTool::CMakeTool(Detection d, const Core::Id &id)
: m_state(Invalid), m_process(0), m_hasCodeBlocksMsvcGenerator(false), m_hasCodeBlocksNinjaGenerator(false) : m_state(Invalid), m_process(0),
m_isAutoDetected(d == AutoDetection),
m_hasCodeBlocksMsvcGenerator(false),
m_hasCodeBlocksNinjaGenerator(false),
m_id(id)
{ {
//make sure every CMakeTool has a valid ID
if (!m_id.isValid())
createId();
}
CMakeTool::CMakeTool(const QVariantMap &map, bool fromSdk)
: m_state(Invalid), m_process(0),
m_isAutoDetected(fromSdk),
m_hasCodeBlocksMsvcGenerator(false),
m_hasCodeBlocksNinjaGenerator(false)
{
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();
setCMakeExecutable(Utils::FileName::fromUserInput(map.value(QLatin1String(CMAKE_INFORMATION_COMMAND)).toString()));
} }
CMakeTool::~CMakeTool() CMakeTool::~CMakeTool()
@@ -60,14 +90,14 @@ void CMakeTool::cancel()
} }
} }
void CMakeTool::setCMakeExecutable(const QString &executable) void CMakeTool::setCMakeExecutable(const Utils::FileName &executable)
{ {
cancel(); cancel();
m_process = new QProcess(); m_process = new QProcess();
connect(m_process, static_cast<void (QProcess::*)(int)>(&QProcess::finished), this, &CMakeTool::finished); connect(m_process, static_cast<void (QProcess::*)(int)>(&QProcess::finished), this, &CMakeTool::finished);
m_executable = executable; m_executable = executable;
QFileInfo fi(m_executable); QFileInfo fi = m_executable.toFileInfo();
if (fi.exists() && fi.isExecutable()) { if (fi.exists() && fi.isExecutable()) {
// Run it to find out more // Run it to find out more
m_state = CMakeTool::RunningBasic; m_state = CMakeTool::RunningBasic;
@@ -121,20 +151,39 @@ void CMakeTool::finished(int exitCode)
bool CMakeTool::isValid() const bool CMakeTool::isValid() const
{ {
if (m_state == CMakeTool::Invalid) if (m_state == CMakeTool::Invalid || !m_id.isValid())
return false; return false;
if (m_state == CMakeTool::RunningBasic) if (m_state == CMakeTool::RunningBasic) {
m_process->waitForFinished(); if (!m_process->waitForFinished(10000)) {
return false;
}
}
return (m_state != CMakeTool::Invalid); return (m_state != CMakeTool::Invalid);
} }
void CMakeTool::createId()
{
QTC_ASSERT(!m_id.isValid(), return);
m_id = Core::Id::fromString(QUuid::createUuid().toString());
}
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;
}
bool CMakeTool::startProcess(const QStringList &args) bool CMakeTool::startProcess(const QStringList &args)
{ {
m_process->start(m_executable, args); m_process->start(m_executable.toString(), args);
return m_process->waitForStarted(2000); return m_process->waitForStarted(2000);
} }
QString CMakeTool::cmakeExecutable() const Utils::FileName CMakeTool::cmakeExecutable() const
{ {
return m_executable; return m_executable;
} }
@@ -165,6 +214,11 @@ TextEditor::Keywords CMakeTool::keywords()
return TextEditor::Keywords(m_variables, m_functions, m_functionArgs); return TextEditor::Keywords(m_variables, m_functions, m_functionArgs);
} }
bool CMakeTool::isAutoDetected() const
{
return m_isAutoDetected;
}
static void extractKeywords(const QByteArray &input, QStringList *destination) static void extractKeywords(const QByteArray &input, QStringList *destination)
{ {
if (!destination) if (!destination)
@@ -212,6 +266,17 @@ QString CMakeTool::formatFunctionDetails(const QString &command, const QString &
.arg(command.toHtmlEscaped(), args.toHtmlEscaped()); .arg(command.toHtmlEscaped(), args.toHtmlEscaped());
} }
QString CMakeTool::displayName() const
{
return m_displayName;
}
void CMakeTool::setDisplayName(const QString &displayName)
{
m_displayName = displayName;
}
void CMakeTool::parseFunctionDetailsOutput(const QByteArray &output) void CMakeTool::parseFunctionDetailsOutput(const QByteArray &output)
{ {
QStringList cmakeFunctionsList = m_functions; QStringList cmakeFunctionsList = m_functions;

View File

@@ -31,21 +31,31 @@
#ifndef CMAKEVALIDATOR_H #ifndef CMAKEVALIDATOR_H
#define CMAKEVALIDATOR_H #define CMAKEVALIDATOR_H
#include "cmake_global.h"
#include <texteditor/codeassist/keywordscompletionassist.h>
#include <utils/fileutils.h>
#include <coreplugin/id.h>
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <texteditor/codeassist/keywordscompletionassist.h>
QT_FORWARD_DECLARE_CLASS(QProcess) QT_FORWARD_DECLARE_CLASS(QProcess)
namespace CMakeProjectManager { namespace CMakeProjectManager {
namespace Internal {
class CMakeTool : public QObject class CMAKE_EXPORT CMakeTool : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
CMakeTool(); enum Detection {
ManualDetection,
AutoDetection
};
explicit CMakeTool(Detection d, const Core::Id &id = Core::Id());
explicit CMakeTool(const QVariantMap &map, bool fromSdk);
~CMakeTool(); ~CMakeTool();
enum State { Invalid, RunningBasic, RunningFunctionList, RunningFunctionDetails, enum State { Invalid, RunningBasic, RunningFunctionList, RunningFunctionDetails,
@@ -53,15 +63,23 @@ public:
void cancel(); void cancel();
bool isValid() const; bool isValid() const;
void setCMakeExecutable(const QString &executable); Core::Id id() const { return m_id; }
QString cmakeExecutable() const; QVariantMap toMap () const;
void setCMakeExecutable(const Utils::FileName &executable);
Utils::FileName cmakeExecutable() const;
bool hasCodeBlocksMsvcGenerator() const; bool hasCodeBlocksMsvcGenerator() const;
bool hasCodeBlocksNinjaGenerator() const; bool hasCodeBlocksNinjaGenerator() const;
TextEditor::Keywords keywords(); TextEditor::Keywords keywords();
bool isAutoDetected() const;
QString displayName() const;
void setDisplayName(const QString &displayName);
private slots: private slots:
void finished(int exitCode); void finished(int exitCode);
private: private:
void createId();
void finishStep(); void finishStep();
void startNextStep(); void startNextStep();
bool startProcess(const QStringList &args); bool startProcess(const QStringList &args);
@@ -73,16 +91,20 @@ private:
State m_state; State m_state;
QProcess *m_process; QProcess *m_process;
Utils::FileName m_executable;
bool m_isAutoDetected;
bool m_hasCodeBlocksMsvcGenerator; bool m_hasCodeBlocksMsvcGenerator;
bool m_hasCodeBlocksNinjaGenerator; bool m_hasCodeBlocksNinjaGenerator;
QString m_executable;
QMap<QString, QStringList> m_functionArgs; QMap<QString, QStringList> m_functionArgs;
QStringList m_variables; QStringList m_variables;
QStringList m_functions; QStringList m_functions;
Core::Id m_id;
QString m_displayName;
}; };
} // namespace Internal
} // namespace CMakeProjectManager } // namespace CMakeProjectManager
#endif // CMAKEVALIDATOR_H #endif // CMAKEVALIDATOR_H

View File

@@ -0,0 +1,392 @@
/****************************************************************************
**
** Copyright (C) 2015 Canonical Ltd.
** Contact: http://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 http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** 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.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "cmaketoolmanager.h"
#include <coreplugin/icore.h>
#include <utils/persistentsettings.h>
#include <utils/qtcassert.h>
#include <utils/environment.h>
#include <utils/algorithm.h>
#include <QFileInfo>
#include <QDebug>
#include <QDir>
using namespace Core;
using namespace Utils;
namespace CMakeProjectManager {
const char CMAKETOOL_COUNT_KEY[] = "CMakeTools.Count";
const char CMAKETOOL_DEFAULT_KEY[] = "CMakeTools.Default";
const char CMAKETOOL_PREFER_NINJA_KEY[] = "CMakeTools.PreferNinja";
const char CMAKETOOL_DATA_KEY[] = "CMakeTools.";
const char CMAKETOOL_FILE_VERSION_KEY[] = "Version";
const char CMAKETOOL_FILENAME[] = "/qtcreator/cmaketools.xml";
class CMakeToolManagerPrivate
{
public:
CMakeToolManagerPrivate() :
m_preferNinja(false),
m_writer(0)
{}
bool m_preferNinja;
Id m_defaultCMake;
QList<CMakeTool *> m_cmakeTools;
PersistentSettingsWriter *m_writer;
};
static CMakeToolManagerPrivate *d = 0;
static void addCMakeTool(CMakeTool *item)
{
QTC_ASSERT(item->id().isValid(), return);
//set the first registered cmake tool as default if there is not already one
if (!d->m_defaultCMake.isValid())
d->m_defaultCMake = item->id();
d->m_cmakeTools.append(item);
}
static FileName userSettingsFileName()
{
QFileInfo settingsLocation(ICore::settings()->fileName());
return FileName::fromString(settingsLocation.absolutePath() + QLatin1String(CMAKETOOL_FILENAME));
}
static QList<CMakeTool *> readCMakeTools(const FileName &fileName, Core::Id *defaultId, bool fromSDK)
{
PersistentSettingsReader reader;
if (!reader.load(fileName))
return QList<CMakeTool *>();
QVariantMap data = reader.restoreValues();
// Check version
int version = data.value(QLatin1String(CMAKETOOL_FILE_VERSION_KEY), 0).toInt();
if (version < 1)
return QList<CMakeTool *>();
QList<CMakeTool *> loaded;
int count = data.value(QLatin1String(CMAKETOOL_COUNT_KEY), 0).toInt();
for (int i = 0; i < count; ++i) {
const QString key = QString::fromLatin1(CMAKETOOL_DATA_KEY) + QString::number(i);
if (!data.contains(key))
continue;
const QVariantMap dbMap = data.value(key).toMap();
CMakeTool *item = new CMakeTool(dbMap,fromSDK);
if (item->isAutoDetected()) {
if (!item->cmakeExecutable().toFileInfo().isExecutable()) {
qWarning() << QString::fromLatin1("CMakeTool \"%1\" (%2) read from \"%3\" dropped since the command is not executable.")
.arg(item->cmakeExecutable().toUserOutput(), item->id().toString(), fileName.toUserOutput());
delete item;
continue;
}
}
loaded.append(item);
}
*defaultId = Id::fromSetting(data.value(QLatin1String(CMAKETOOL_DEFAULT_KEY), defaultId->toSetting()));
d->m_preferNinja= data.value(QLatin1String(CMAKETOOL_PREFER_NINJA_KEY), d->m_preferNinja).toBool();
return loaded;
}
static void readAndDeleteLegacyCMakeSettings ()
{
// restore the legacy cmake
QSettings *settings = ICore::settings();
settings->beginGroup(QLatin1String("CMakeSettings"));
FileName exec = FileName::fromUserInput(settings->value(QLatin1String("cmakeExecutable")).toString());
if (!exec.toFileInfo().isExecutable())
return;
CMakeTool *item = CMakeToolManager::findByCommand(exec);
if (!item) {
item = new CMakeTool(CMakeTool::ManualDetection);
item->setCMakeExecutable(exec);
item->setDisplayName(CMakeToolManager::tr("CMake at %1").arg(item->cmakeExecutable().toUserOutput()));
if (!CMakeToolManager::registerCMakeTool(item)) {
delete item;
item = 0;
}
}
//this setting used to be the default cmake, make sure it is again
if (item)
d->m_defaultCMake = item->id();
//read the legacy ninja setting, if its not available use the current value
d->m_preferNinja = settings->value(QLatin1String("preferNinja"), d->m_preferNinja).toBool();
settings->remove(QString());
settings->endGroup();
}
static QList<CMakeTool *> autoDetectCMakeTools()
{
QStringList filters;
filters.append(QStringLiteral("cmake"));
QList<FileName> suspects;
QStringList path = Environment::systemEnvironment().path();
path.removeDuplicates();
QDir dir;
dir.setNameFilters(filters);
dir.setFilter(QDir::Files | QDir::Executable);
foreach (const QString &base, path) {
dir.setPath(base);
foreach (const QString &entry, dir.entryList())
suspects.append(FileName::fromString(dir.absoluteFilePath(entry)));
}
QList<CMakeTool *> found;
foreach (const FileName &command, suspects) {
CMakeTool *item = new CMakeTool(CMakeTool::AutoDetection);
item->setCMakeExecutable(command);
item->setDisplayName(CMakeToolManager::tr("System CMake at %1").arg(command.toUserOutput()));
found.append(item);
}
return found;
}
CMakeToolManager::CMakeToolManager(QObject *parent) : QObject(parent)
{
d = new CMakeToolManagerPrivate;
d->m_writer = new PersistentSettingsWriter(userSettingsFileName(), QStringLiteral("QtCreatorCMakeTools"));
connect(ICore::instance(), &ICore::saveSettingsRequested,
this, &CMakeToolManager::saveCMakeTools);
}
CMakeToolManager::~CMakeToolManager()
{
delete d->m_writer;
delete d;
d = 0;
}
QList<CMakeTool *> CMakeToolManager::cmakeTools()
{
return d->m_cmakeTools;
}
void CMakeToolManager::setPreferNinja(bool set)
{
d->m_preferNinja = set;
}
bool CMakeToolManager::preferNinja()
{
return d->m_preferNinja;
}
Id CMakeToolManager::registerOrFindCMakeTool(const FileName &command)
{
CMakeTool *cmake = findByCommand(command);
if (cmake)
return cmake->id();
cmake = new CMakeTool(CMakeTool::ManualDetection);
cmake->setCMakeExecutable(command);
cmake->setDisplayName(tr("CMake at %1").arg(command.toUserOutput()));
addCMakeTool(cmake);
return cmake->id();
}
bool CMakeToolManager::registerCMakeTool(CMakeTool *tool)
{
if (!tool || d->m_cmakeTools.contains(tool))
return true;
QTC_ASSERT(tool->id().isValid(),return false);
//make sure the same id was not used before
foreach (CMakeTool *current, d->m_cmakeTools) {
if (tool->id() == current->id())
return false;
}
addCMakeTool(tool);
return true;
}
void CMakeToolManager::deregisterCMakeTool(const Id &id)
{
int idx = Utils::indexOf(d->m_cmakeTools, Utils::equal(&CMakeTool::id, id));
if (idx >= 0) {
CMakeTool *toRemove = d->m_cmakeTools.takeAt(idx);
if (toRemove->id() == d->m_defaultCMake) {
if (d->m_cmakeTools.isEmpty())
d->m_defaultCMake = Id();
else
d->m_defaultCMake = d->m_cmakeTools.first()->id();
}
delete toRemove;
}
}
CMakeTool *CMakeToolManager::defaultCMakeTool()
{
CMakeTool *tool = findById(d->m_defaultCMake);
if (!tool) {
//if the id is not valid, we set the firstly registered one as default
if (!d->m_cmakeTools.isEmpty()) {
d->m_defaultCMake = d->m_cmakeTools.first()->id();
return d->m_cmakeTools.first();
}
}
return tool;
}
void CMakeToolManager::setDefaultCMakeTool(const Id &id)
{
if (d->m_defaultCMake == id)
return;
if (findById(id))
d->m_defaultCMake = id;
}
CMakeTool *CMakeToolManager::findByCommand(const FileName &command)
{
return Utils::findOrDefault(d->m_cmakeTools, Utils::equal(&CMakeTool::cmakeExecutable, command));
}
CMakeTool *CMakeToolManager::findById(const Id &id)
{
return Utils::findOrDefault(d->m_cmakeTools, Utils::equal(&CMakeTool::id, id));
}
void CMakeToolManager::restoreCMakeTools()
{
Core::Id defaultId;
QFileInfo systemSettingsFile(ICore::settings(QSettings::SystemScope)->fileName());
FileName sdkSettingsFile = FileName::fromString(systemSettingsFile.absolutePath()
+ QLatin1String(CMAKETOOL_FILENAME));
QList<CMakeTool *> toolsToRegister = readCMakeTools(sdkSettingsFile, &defaultId, true);
//read the tools from the user settings file
QList<CMakeTool *> readTools = readCMakeTools(userSettingsFileName(), &defaultId, false);
//autodetect tools
QList<CMakeTool *> autoDetected = autoDetectCMakeTools();
//filter out the tools that were stored in SDK
for (int i = readTools.size() - 1; i >= 0; i--) {
CMakeTool *currTool = readTools.takeAt(i);
if (Utils::anyOf(toolsToRegister, Utils::equal(&CMakeTool::id, currTool->id()))) {
delete currTool;
} else {
//if the current tool is marked as autodetected and NOT in the autodetected list,
//it is a leftover SDK provided tool. The user will not be able to edit it,
//so we automatically drop it
if (currTool->isAutoDetected()) {
if (!Utils::anyOf(autoDetected,
Utils::equal(&CMakeTool::cmakeExecutable, currTool->cmakeExecutable()))) {
qWarning() << QString::fromLatin1("Previously SDK provided CMakeTool \"%1\" (%2) dropped.")
.arg(currTool->cmakeExecutable().toUserOutput(), currTool->id().toString());
delete currTool;
continue;
}
}
toolsToRegister.append(currTool);
}
}
//filter out the tools that are already known
for (int i = autoDetected.size() - 1; i >= 0; i--) {
CMakeTool *currTool = autoDetected.takeAt(i);
if (Utils::anyOf(toolsToRegister,
Utils::equal(&CMakeTool::cmakeExecutable, currTool->cmakeExecutable())))
delete currTool;
else
toolsToRegister.append(currTool);
}
// Store all tools
foreach (CMakeTool *current, toolsToRegister) {
if (!registerCMakeTool(current)) {
//this should never happen, but lets make sure we do not leak memory
qWarning() << QString::fromLatin1("CMakeTool \"%1\" (%2) dropped.")
.arg(current->cmakeExecutable().toUserOutput(), current->id().toString());
delete current;
}
}
if (CMakeToolManager::findById(defaultId))
d->m_defaultCMake = defaultId;
// restore the legacy cmake settings only once and keep them around
readAndDeleteLegacyCMakeSettings();
}
void CMakeToolManager::saveCMakeTools()
{
QTC_ASSERT(d->m_writer, return);
QVariantMap data;
data.insert(QLatin1String(CMAKETOOL_FILE_VERSION_KEY), 1);
data.insert(QLatin1String(CMAKETOOL_DEFAULT_KEY), d->m_defaultCMake.toSetting());
data.insert(QLatin1String(CMAKETOOL_PREFER_NINJA_KEY), d->m_preferNinja);
int count = 0;
foreach (CMakeTool *item, d->m_cmakeTools) {
QFileInfo fi = item->cmakeExecutable().toFileInfo();
if (fi.isExecutable()) {
QVariantMap tmp = item->toMap();
if (tmp.isEmpty())
continue;
data.insert(QString::fromLatin1(CMAKETOOL_DATA_KEY) + QString::number(count), tmp);
++count;
}
}
data.insert(QLatin1String(CMAKETOOL_COUNT_KEY), count);
d->m_writer->save(data, ICore::mainWindow());
}
} // namespace CMakeProjectManager

View File

@@ -0,0 +1,71 @@
/****************************************************************************
**
** Copyright (C) 2015 Canonical Ltd.
** Contact: http://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 http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** 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.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CMAKEPROJECTMANAGER_CMAKETOOLMANAGER_H
#define CMAKEPROJECTMANAGER_CMAKETOOLMANAGER_H
#include "cmake_global.h"
#include "cmaketool.h"
#include <utils/fileutils.h>
#include <texteditor/codeassist/keywordscompletionassist.h>
#include <QObject>
namespace CMakeProjectManager {
class CMAKE_EXPORT CMakeToolManager : public QObject
{
Q_OBJECT
public:
CMakeToolManager(QObject *parent);
~CMakeToolManager();
static QList<CMakeTool *> cmakeTools();
static void setPreferNinja(bool set);
static bool preferNinja();
static Core::Id registerOrFindCMakeTool(const Utils::FileName &command);
static bool registerCMakeTool(CMakeTool *tool);
static void deregisterCMakeTool(const Core::Id &id);
static CMakeTool *defaultCMakeTool();
static void setDefaultCMakeTool(const Core::Id &id);
static CMakeTool *findByCommand(const Utils::FileName &command);
static CMakeTool *findById(const Core::Id &id);
static void restoreCMakeTools();
private:
static void saveCMakeTools();
};
} // namespace CMakeProjectManager
#endif // CMAKEPROJECTMANAGER_CMAKETOOLMANAGER_H