Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [cmake] Added feature for switching cmake kit #844

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/common/project/projectinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class COMMON_EXPORT ProjectInfo
inline QString language() const {return data["Language"].toString();}
inline void setKitName(const QString &kitName) {data["KitName"] = kitName;}
inline QString kitName() const {return data["KitName"].toString();}
inline void setKitId(const QString &id) {data["KitId"] = id;}
inline QString kitId() const {return data["KitId"].toString();}
inline void setBuildFolder(const QString &buildPath) {data["BuildFolder"] = buildPath;}
inline QString buildFolder() const {return data["BuildFolder"].toString();}
inline void setWorkspaceFolder(const QString &workspaceFolder) {data["WorkspaceFolder"] = workspaceFolder;}
Expand All @@ -36,6 +38,8 @@ class COMMON_EXPORT ProjectInfo
inline QString buildType() const {return data["BuildType"].toString();}
inline void setBuildProgram(const QString &program) {data["BuildProgram"] = program;}
inline QString buildProgram() const {return data["BuildProgram"].toString();}
inline void setDebugProgram(const QString &program) {data["DebugProgram"] = program;}
inline QString debugProgram() const {return data["DebugProgram"].toString();}
inline void setConfigCustomArgs(const QStringList &args) {data["ConfigCustomArgs"] = args;}
inline QStringList configCustomArgs() const {return data["ConfigCustomArgs"].toStringList();}
inline void setSourceFiles(const QSet<QString> &files) {data["SourceFiles"] = QVariant::fromValue(files);}
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/cxx/cmake/cmakedebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "cmakedebug.h"

#include "services/option/optionmanager.h"
#include "services/project/projectservice.h"

#include <QDBusMessage>
Expand Down Expand Up @@ -32,7 +30,8 @@ CMakeDebug::~CMakeDebug()

bool CMakeDebug::prepareDebug(QString &retMsg)
{
QString debuggerTool = OptionManager::getInstance()->getCxxDebuggerToolPath();
auto prjInfo = dpfGetService(ProjectService)->getActiveProjectInfo();
QString debuggerTool = prjInfo.debugProgram();
if (!debuggerTool.contains("gdb")) {
retMsg = tr("The gdb is required, please install it in console with \"sudo apt install gdb\", "
"and then restart the tool, reselect the CMake Debugger in Options Dialog...");
Expand Down
1 change: 1 addition & 0 deletions src/plugins/cxx/cmake/model/kitlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ QModelIndex KitListModel::addItem()
{
Kit item;
item.setKitName(tr("Unnamed"));
item.setId(QUuid::createUuid().toString());

int pos = kitItemList.size();
beginInsertRows(QModelIndex(), pos, pos);
Expand Down
19 changes: 8 additions & 11 deletions src/plugins/cxx/cmake/option/kit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@

class KitPrivate
{
public:
explicit KitPrivate(QString _id)
: id(_id)
{
if (id.isEmpty())
id = QUuid::createUuid().toString();
}

public:
QString id;
QString kitName;
Expand All @@ -26,14 +18,14 @@ class KitPrivate
QString cmakeGenerator;
};

Kit::Kit(QString id, QObject *parent)
Kit::Kit(QObject *parent)
: QObject(parent),
d(new KitPrivate(id))
d(new KitPrivate())
{
}

Kit::Kit(const Kit &other)
: d(new KitPrivate(""))
: d(new KitPrivate())
{
copyFrom(other);
}
Expand Down Expand Up @@ -113,6 +105,11 @@ void Kit::setCMakeGenerator(const QString &cg)
d->cmakeGenerator = cg;
}

bool Kit::isValid() const
{
return !d->id.isEmpty();
}

void Kit::copyFrom(const Kit &other)
{
d->id = other.d->id;
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/cxx/cmake/option/kit.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Kit : public QObject
{
Q_OBJECT
public:
explicit Kit(QString id = "", QObject *parent = nullptr);
explicit Kit(QObject *parent = nullptr);
Kit(const Kit &other);
~Kit();

Expand All @@ -31,6 +31,7 @@ class Kit : public QObject
QString cmakeGenerator() const;
void setCMakeGenerator(const QString &cg);

bool isValid() const;
void copyFrom(const Kit &other);
QVariantMap toVariantMap();
static QVariantMap toVariantMap(const Kit &kit);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/cxx/cmake/option/kitmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ QList<Kit> KitManager::kitList() const

Kit KitManager::findKit(const QString &id)
{
auto iter = std::find_if(allKit.cbegin(), allKit.cbegin(),
auto iter = std::find_if(allKit.cbegin(), allKit.cend(),
[&id](const Kit &kit) {
return kit.id() == id;
});
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/cxx/cmake/project/cmakeprojectgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "properties/bulidCfgWidget/buildpropertypage.h"
#include "properties/runCfgWidget/runpropertypage.h"
#include "properties/configWidget/configpropertywidget.h"
#include "properties/kitpage/kitpage.h"
#include "properties/configutil.h"
#include "properties/targetsmanager.h"
#include "services/builder/builderservice.h"
Expand Down Expand Up @@ -530,6 +531,9 @@ void CmakeProjectGenerator::runCMake(QStandardItem *root, const QPair<QString, Q

removeWatcher(root);

ProjectConfigure *param = ConfigUtil::instance()->getConfigureParamPointer();
ConfigUtil::instance()->updateProjectInfo(proInfo, param);

// reconfigure project info
configure(proInfo);
}
Expand All @@ -545,9 +549,11 @@ void CmakeProjectGenerator::actionProperties(const dpfservice::ProjectInfo &info

BuildPropertyPage *buildWidget = new BuildPropertyPage(info);
RunPropertyPage *runWidget = new RunPropertyPage(info, item);
KitPage *kitPage = new KitPage(info, item);

dlg.insertPropertyPanel(tr("Build"), buildWidget);
dlg.insertPropertyPanel(tr("Run"), runWidget);
dlg.insertPropertyPanel(tr("Kit"), kitPage);

connect(buildWidget, &BuildPropertyPage::cacheFileUpdated, this, [=](){
runCMake(this->rootItem, {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "environmentwidget.h"
#include "stepspane.h"
#include "targetsmanager.h"
#include "cmake/option/kitmanager.h"
#include "services/project/projectservice.h"
#include "cmakeCfgWidget/cmakepropertypage.h"

Expand Down
8 changes: 8 additions & 0 deletions src/plugins/cxx/cmake/project/properties/configutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
info.setBuildType(ConfigUtil::instance()->getNameFromType(iter->type));
info.setBuildFolder(iter->directory);
info.setBuildProgram(kit.cmakeTool().path);
info.setDebugProgram(kit.debugger().path);
info.setKitId(kit.id());

QStringList configArguments;
configArguments << "-S";
Expand All @@ -100,6 +102,8 @@
configArguments << CDT_PROJECT_KIT::get()->CBP_GENERATOR;
configArguments << "-DCMAKE_BUILD_TYPE=" + info.buildType();
configArguments << "-DCMAKE_EXPORT_COMPILE_COMMANDS=1";
configArguments << "-DCMAKE_C_COMPILER=" + kit.ccompiler().path;
configArguments << "-DCMAKE_CXX_COMPILER=" + kit.cxxcompiler().path;
info.setConfigCustomArgs(configArguments);

QStringList buildArguments;
Expand Down Expand Up @@ -187,8 +191,10 @@
info.setBuildType(ConfigUtil::instance()->getNameFromType(iter->type));
info.setBuildFolder(iter->directory);
info.setBuildProgram(kit.cmakeTool().path);
info.setDebugProgram(kit.debugger().path);
info.setKitId(kit.id());

QStringList arguments;

Check warning on line 197 in src/plugins/cxx/cmake/project/properties/configutil.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Local variable 'arguments' shadows outer variable

Check warning on line 197 in src/plugins/cxx/cmake/project/properties/configutil.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Local variable 'arguments' shadows outer variable
arguments << "-S";
arguments << info.workspaceFolder();
arguments << "-B";
Expand All @@ -197,6 +203,8 @@
arguments << CDT_PROJECT_KIT::get()->CBP_GENERATOR;
arguments << "-DCMAKE_BUILD_TYPE=" + info.buildType();
arguments << "-DCMAKE_EXPORT_COMPILE_COMMANDS=1";
arguments << "-DCMAKE_C_COMPILER=" + kit.ccompiler().path;
arguments << "-DCMAKE_CXX_COMPILER=" + kit.cxxcompiler().path;
info.setConfigCustomArgs(arguments);

for (auto iterStep = iter->buildConfigure.steps.begin(); iterStep != iter->buildConfigure.steps.end(); ++iterStep) {
Expand Down
65 changes: 65 additions & 0 deletions src/plugins/cxx/cmake/project/properties/kitpage/kitpage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "kitpage.h"
#include "configutil.h"
#include "cmake/option/kitmanager.h"

#include <QFormLayout>

using namespace config;
DWIDGET_USE_NAMESPACE

KitPage::KitPage(const dpfservice::ProjectInfo &projectInfo, QStandardItem *item, QWidget *parent)
: PageWidget(parent)
{
this->projectInfo = projectInfo;
this->item = item;
initUI();
}

void KitPage::initUI()
{
kitComboBox = new DComboBox(this);
kitComboBox->setFixedWidth(220);

QFormLayout *layout = new QFormLayout(this);
layout->addRow(tr("Kit:"), kitComboBox);
}

void KitPage::readConfig()
{
kitComboBox->clear();

kitComboBox->addItem(tr("None"));
const auto &kitList = KitManager::instance()->kitList();
for (const auto &kit : kitList) {
kitComboBox->addItem(kit.kitName(), kit.id());
}

const auto &id = projectInfo.kitId();
const auto &kit = KitManager::instance()->findKit(id);
if (!kit.isValid()) {
kitComboBox->setCurrentIndex(0);
} else {
int index = kitComboBox->findData(id);
kitComboBox->setCurrentIndex(index);
}
}

void KitPage::saveConfig()
{
ProjectConfigure *param = ConfigUtil::instance()->getConfigureParamPointer();
const QString id = kitComboBox->currentData().toString();
if (id == param->kitId)
return;

param->kit = kitComboBox->currentText();
param->kitId = id;
QString filePath = ConfigUtil::instance()->getConfigPath(param->workspace);
ConfigUtil::instance()->saveConfig(filePath, *param);

ConfigUtil::instance()->updateProjectInfo(projectInfo, param);
dpfservice::ProjectInfo::set(item, projectInfo);
}
29 changes: 29 additions & 0 deletions src/plugins/cxx/cmake/project/properties/kitpage/kitpage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef KITPAGE_H
#define KITPAGE_H

#include "common/widget/pagewidget.h"
#include "common/project/projectinfo.h"

#include <DComboBox>

class KitPage : public PageWidget
{
public:
explicit KitPage(const dpfservice::ProjectInfo &projectInfo, QStandardItem *item, QWidget *parent = nullptr);

void readConfig() override;
void saveConfig() override;

private:
void initUI();

DTK_WIDGET_NAMESPACE::DComboBox *kitComboBox { nullptr };
dpfservice::ProjectInfo projectInfo;
QStandardItem *item { nullptr };
};

#endif // KITPAGE_H
3 changes: 1 addition & 2 deletions src/plugins/cxx/cmake/settings/kitoptionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ void KitOptionWidget::setConfig(const QVariantMap &config)
d->parseKitParamsV1(config);
else
d->parseKitParamsV2(config);

KitManager::instance()->setKitList(d->listModel.itemList());
}

QVariantMap KitOptionWidget::getConfig() const
Expand All @@ -329,5 +327,6 @@ QVariantMap KitOptionWidget::getConfig() const
map.insert(item.kitName(), params);
}

KitManager::instance()->setKitList(d->listModel.itemList());
return map;
}
6 changes: 4 additions & 2 deletions src/plugins/debugger/dap/dapdebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,13 @@ void DAPDebugger::attachDebug(const QString &processId)

// only support gdb for now
updateRunState(kStart);
QString debuggerTool = OptionManager::getInstance()->getCxxDebuggerToolPath();
auto prjInfo = dpfGetService(ProjectService)->getActiveProjectInfo();
QString debuggerTool = prjInfo.debugProgram();
if (!debuggerTool.contains("gdb")) {
auto msg = tr("The gdb is required, please install it in console with \"sudo apt install gdb\", "
"and then restart the tool, reselect the CMake Debugger in Options Dialog...");
printOutput(msg, OutputPane::OutputFormat::ErrorMessage);
return;
}

//todo : change signal to other debuger
Expand All @@ -273,7 +275,7 @@ void DAPDebugger::attachDebug(const QString &processId)
"getDebugPort");
d->requestDAPPortPpid = QString(getpid());
msg << d->requestDAPPortPpid
<< "gdb"
<< debuggerTool
<< processId
<< QStringList();
bool ret = QDBusConnection::sessionBus().send(msg);
Expand Down
Loading