Conan: Simplify plugin setup

Change-Id: Ic60d2626384c2d15b81614f51db447a197640156
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2023-05-15 18:52:11 +02:00
parent b0f68a916f
commit d4cf9b667b
7 changed files with 61 additions and 111 deletions

View File

@@ -3,7 +3,7 @@ add_qtc_plugin(Conan
SOURCES SOURCES
conanconstants.h conanconstants.h
conaninstallstep.cpp conaninstallstep.h conaninstallstep.cpp conaninstallstep.h
conanplugin.cpp conanplugin.h conanplugin.cpp
conansettings.cpp conansettings.h conansettings.cpp conansettings.h
conantr.h conantr.h
) )

View File

@@ -13,7 +13,6 @@ QtcPlugin {
"conanconstants.h", "conanconstants.h",
"conaninstallstep.h", "conaninstallstep.h",
"conaninstallstep.cpp", "conaninstallstep.cpp",
"conanplugin.h",
"conanplugin.cpp", "conanplugin.cpp",
"conansettings.h", "conansettings.h",
"conansettings.cpp", "conansettings.cpp",

View File

@@ -4,7 +4,6 @@
#include "conaninstallstep.h" #include "conaninstallstep.h"
#include "conanconstants.h" #include "conanconstants.h"
#include "conanplugin.h"
#include "conansettings.h" #include "conansettings.h"
#include "conantr.h" #include "conantr.h"
@@ -19,12 +18,40 @@
#include <projectexplorer/target.h> #include <projectexplorer/target.h>
#include <projectexplorer/task.h> #include <projectexplorer/task.h>
#include <projectexplorer/toolchain.h> #include <projectexplorer/toolchain.h>
#include <projectexplorer/projectmanager.h>
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils; using namespace Utils;
namespace Conan::Internal { namespace Conan::Internal {
static FilePath conanFilePath(Project *project, const FilePath &defaultFilePath = {})
{
const FilePath projectDirectory = project->projectDirectory();
// conanfile.py takes precedence over conanfile.txt when "conan install dir" is invoked
const FilePath conanPy = projectDirectory / "conanfile.py";
if (conanPy.exists())
return conanPy;
const FilePath conanTxt = projectDirectory / "conanfile.txt";
if (conanTxt.exists())
return conanTxt;
return defaultFilePath;
}
static void connectTarget(Project *project, Target *target)
{
if (!conanFilePath(project).isEmpty()) {
const QList<BuildConfiguration *> buildConfigurations = target->buildConfigurations();
for (BuildConfiguration *buildConfiguration : buildConfigurations)
buildConfiguration->buildSteps()->insertStep(0, Constants::INSTALL_STEP);
}
QObject::connect(target, &Target::addedBuildConfiguration,
target, [project] (BuildConfiguration *buildConfiguration) {
if (!conanFilePath(project).isEmpty())
buildConfiguration->buildSteps()->insertStep(0, Constants::INSTALL_STEP);
});
}
// ConanInstallStep // ConanInstallStep
class ConanInstallStep final : public AbstractProcessStep class ConanInstallStep final : public AbstractProcessStep
@@ -45,7 +72,7 @@ ConanInstallStep::ConanInstallStep(BuildStepList *bsl, Id id)
auto conanFile = addAspect<StringAspect>(); auto conanFile = addAspect<StringAspect>();
conanFile->setSettingsKey("ConanPackageManager.InstallStep.ConanFile"); conanFile->setSettingsKey("ConanPackageManager.InstallStep.ConanFile");
conanFile->setFilePath(ConanPlugin::conanFilePath(project(), conanFile->setFilePath(conanFilePath(project(),
project()->projectDirectory() / "conanfile.txt")); project()->projectDirectory() / "conanfile.txt"));
conanFile->setLabelText(Tr::tr("Conan file:")); conanFile->setLabelText(Tr::tr("Conan file:"));
conanFile->setToolTip(Tr::tr("Enter location of conanfile.txt or conanfile.py.")); conanFile->setToolTip(Tr::tr("Enter location of conanfile.txt or conanfile.py."));
@@ -68,7 +95,7 @@ ConanInstallStep::ConanInstallStep(BuildStepList *bsl, Id id)
const QString buildType = bt == BuildConfiguration::Release ? QString("Release") const QString buildType = bt == BuildConfiguration::Release ? QString("Release")
: QString("Debug"); : QString("Debug");
CommandLine cmd(ConanPlugin::conanSettings()->conanFilePath()); CommandLine cmd(settings().conanFilePath());
cmd.addArgs({"install", "-s", "build_type=" + buildType}); cmd.addArgs({"install", "-s", "build_type=" + buildType});
if (buildMissing->value()) if (buildMissing->value())
cmd.addArg("--build=missing"); cmd.addArg("--build=missing");
@@ -85,6 +112,12 @@ ConanInstallStep::ConanInstallStep(BuildStepList *bsl, Id id)
setupProcessParameters(&param); setupProcessParameters(&param);
return param.summary(displayName()); return param.summary(displayName());
}); });
connect(ProjectManager::instance(), &ProjectManager::projectAdded, this, [this](Project * project) {
connect(project, &Project::addedTarget, project, [project] (Target *target) {
connectTarget(project, target);
});
});
} }
bool ConanInstallStep::init() bool ConanInstallStep::init()

View File

@@ -1,86 +1,26 @@
// Copyright (C) 2018 Jochen Seemann // Copyright (C) 2018 Jochen Seemann
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "conanplugin.h"
#include "conanconstants.h"
#include "conaninstallstep.h" #include "conaninstallstep.h"
#include "conansettings.h" #include "conansettings.h"
#include <coreplugin/icore.h> #include <extensionsystem/iplugin.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/target.h>
using namespace Core;
using namespace ProjectExplorer;
using namespace Utils;
namespace Conan::Internal { namespace Conan::Internal {
class ConanPluginPrivate class ConanPlugin final : public ExtensionSystem::IPlugin
{ {
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Conan.json")
public: public:
ConanInstallStepFactory installStepFactory; ConanPlugin()
{
addManaged<ConanSettings>();
addManaged<ConanInstallStepFactory>();
}
}; };
ConanPlugin::~ConanPlugin()
{
delete d;
}
void ConanPlugin::initialize()
{
d = new ConanPluginPrivate;
conanSettings()->readSettings(ICore::settings());
connect(ProjectManager::instance(), &ProjectManager::projectAdded,
this, &ConanPlugin::projectAdded);
}
static void connectTarget(Project *project, Target *target)
{
if (!ConanPlugin::conanFilePath(project).isEmpty()) {
const QList<BuildConfiguration *> buildConfigurations = target->buildConfigurations();
for (BuildConfiguration *buildConfiguration : buildConfigurations)
buildConfiguration->buildSteps()->insertStep(0, Constants::INSTALL_STEP);
}
QObject::connect(target, &Target::addedBuildConfiguration,
target, [project] (BuildConfiguration *buildConfiguration) {
if (!ConanPlugin::conanFilePath(project).isEmpty())
buildConfiguration->buildSteps()->insertStep(0, Constants::INSTALL_STEP);
});
}
void ConanPlugin::projectAdded(Project *project)
{
connect(project, &Project::addedTarget, project, [project] (Target *target) {
connectTarget(project, target);
});
}
ConanSettings *ConanPlugin::conanSettings()
{
static ConanSettings theSettings;
return &theSettings;
}
FilePath ConanPlugin::conanFilePath(Project *project, const FilePath &defaultFilePath)
{
const FilePath projectDirectory = project->projectDirectory();
// conanfile.py takes precedence over conanfile.txt when "conan install dir" is invoked
const FilePath conanPy = projectDirectory / "conanfile.py";
if (conanPy.exists())
return conanPy;
const FilePath conanTxt = projectDirectory / "conanfile.txt";
if (conanTxt.exists())
return conanTxt;
return defaultFilePath;
}
} // Conan::Internal } // Conan::Internal
#include "conanplugin.moc"

View File

@@ -1,34 +0,0 @@
// Copyright (C) 2018 Jochen Seemann
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <extensionsystem/iplugin.h>
#include <utils/filepath.h>
namespace ProjectExplorer { class Project; }
namespace Conan::Internal {
class ConanSettings;
class ConanPlugin final : public ExtensionSystem::IPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Conan.json")
public:
static ConanSettings *conanSettings();
static Utils::FilePath conanFilePath(ProjectExplorer::Project *project,
const Utils::FilePath &defaultFilePath = {});
private:
~ConanPlugin() final;
void projectAdded(ProjectExplorer::Project *project);
void initialize() final;
class ConanPluginPrivate *d = nullptr;
};
} // Conan::Internal

View File

@@ -3,14 +3,22 @@
#include "conansettings.h" #include "conansettings.h"
#include <coreplugin/icore.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
using namespace Utils; using namespace Utils;
namespace Conan::Internal { namespace Conan::Internal {
static ConanSettings *theSettings;
ConanSettings &settings() { return *theSettings; }
ConanSettings::ConanSettings() ConanSettings::ConanSettings()
{ {
theSettings = this;
setSettingsGroup("ConanSettings"); setSettingsGroup("ConanSettings");
setAutoApply(false); setAutoApply(false);
@@ -18,6 +26,8 @@ ConanSettings::ConanSettings()
conanFilePath.setSettingsKey("ConanFilePath"); conanFilePath.setSettingsKey("ConanFilePath");
conanFilePath.setExpectedKind(PathChooser::ExistingCommand); conanFilePath.setExpectedKind(PathChooser::ExistingCommand);
conanFilePath.setDefaultValue(HostOsInfo::withExecutableSuffix("conan")); conanFilePath.setDefaultValue(HostOsInfo::withExecutableSuffix("conan"));
readSettings(Core::ICore::settings());
} }
} // Conan::Internal } // Conan::Internal

View File

@@ -15,4 +15,6 @@ public:
Utils::FilePathAspect conanFilePath; Utils::FilePathAspect conanFilePath;
}; };
ConanSettings &settings();
} // Conan::Internal } // Conan::Internal