Conan: Detect automatically conan file when adding a new build config

Add a conan install step to all build configurations for all
targets in case the conan file is present in the root of the
project source tree. Do this detection only once, when configuring
the project, but not when configured project is being opened / loaded.
In addition, perform this detection also when a new build
configuration is being added.

Task-number: QTCREATORBUG-25081
Task-number: QTCREATORBUG-21785
Change-Id: I872814a33234e20104046c848b95cdfca577ed7e
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
This commit is contained in:
Jarek Kobus
2021-01-26 18:17:57 +01:00
parent a3318e33db
commit 319300aa6c
7 changed files with 94 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
add_qtc_plugin(Conan add_qtc_plugin(Conan
PLUGIN_DEPENDS Core ProjectExplorer PLUGIN_DEPENDS Core ProjectExplorer
SOURCES SOURCES
conanconstants.h
conaninstallstep.cpp conaninstallstep.h conaninstallstep.cpp conaninstallstep.h
conanplugin.cpp conanplugin.h conanplugin.cpp conanplugin.h
conansettings.cpp conansettings.h conansettings.cpp conansettings.h

View File

@@ -5,6 +5,7 @@ SOURCES += \
conanplugin.cpp \ conanplugin.cpp \
conansettings.cpp conansettings.cpp
HEADERS += \ HEADERS += \
conanconstants.h \
conaninstallstep.h \ conaninstallstep.h \
conanplugin.h \ conanplugin.h \
conansettings.h conansettings.h

View File

@@ -10,6 +10,7 @@ QtcPlugin {
Depends { name: "ProjectExplorer" } Depends { name: "ProjectExplorer" }
files: [ files: [
"conanconstants.h",
"conaninstallstep.h", "conaninstallstep.h",
"conaninstallstep.cpp", "conaninstallstep.cpp",
"conanplugin.h", "conanplugin.h",

View File

@@ -0,0 +1,34 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** 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.
**
****************************************************************************/
#pragma once
namespace ConanPackageManager {
namespace Constants {
const char INSTALL_STEP[] = "ConanPackageManager.InstallStep";
} // namespace Constants
} // namespace ConanPackageManager

View File

@@ -23,6 +23,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include "conanconstants.h"
#include "conaninstallstep.h" #include "conaninstallstep.h"
#include "conanplugin.h" #include "conanplugin.h"
#include "conansettings.h" #include "conansettings.h"
@@ -67,7 +68,8 @@ 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(project()->projectDirectory() / "conanfile.txt"); conanFile->setFilePath(ConanPlugin::conanFilePath(project(),
project()->projectDirectory() / "conanfile.txt"));
conanFile->setLabelText(tr("Conan file:")); conanFile->setLabelText(tr("Conan file:"));
conanFile->setToolTip(tr("Enter location of conanfile.txt or conanfile.py.")); conanFile->setToolTip(tr("Enter location of conanfile.txt or conanfile.py."));
conanFile->setDisplayStyle(StringAspect::PathChooserDisplay); conanFile->setDisplayStyle(StringAspect::PathChooserDisplay);
@@ -134,7 +136,7 @@ void ConanInstallStep::setupOutputFormatter(OutputFormatter *formatter)
ConanInstallStepFactory::ConanInstallStepFactory() ConanInstallStepFactory::ConanInstallStepFactory()
{ {
registerStep<ConanInstallStep>("ConanPackageManager.InstallStep"); registerStep<ConanInstallStep>(Constants::INSTALL_STEP);
setDisplayName(ConanInstallStep::tr("Run conan install")); setDisplayName(ConanInstallStep::tr("Run conan install"));
} }

View File

@@ -23,15 +23,24 @@
** **
****************************************************************************/ ****************************************************************************/
#include "conanconstants.h"
#include "conaninstallstep.h" #include "conaninstallstep.h"
#include "conanplugin.h" #include "conanplugin.h"
#include "conansettings.h" #include "conansettings.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <projectexplorer/projectmanager.h> #include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildmanager.h> #include <projectexplorer/buildmanager.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
using namespace Core; using namespace Core;
using namespace ProjectExplorer;
using namespace Utils;
namespace ConanPackageManager { namespace ConanPackageManager {
namespace Internal { namespace Internal {
@@ -58,15 +67,52 @@ bool ConanPlugin::initialize(const QStringList &arguments, QString *errorString)
m_runData = new ConanPluginRunData; m_runData = new ConanPluginRunData;
conanSettings()->fromSettings(ICore::settings()); conanSettings()->fromSettings(ICore::settings());
connect(SessionManager::instance(), &SessionManager::projectAdded,
this, &ConanPlugin::projectAdded);
return true; return true;
} }
static void connectTarget(Project *project, Target *target)
{
if (!ConanPlugin::conanFilePath(project).isEmpty()) {
const QList<BuildConfiguration *> buildConfigurations = target->buildConfigurations();
for (BuildConfiguration *buildConfiguration : buildConfigurations)
buildConfiguration->buildSteps()->appendStep(Constants::INSTALL_STEP);
}
QObject::connect(target, &Target::addedBuildConfiguration,
target, [project] (BuildConfiguration *buildConfiguration) {
if (!ConanPlugin::conanFilePath(project).isEmpty())
buildConfiguration->buildSteps()->appendStep(Constants::INSTALL_STEP);
});
}
void ConanPlugin::projectAdded(Project *project)
{
connect(project, &Project::addedTarget, project, [project] (Target *target) {
connectTarget(project, target);
});
}
ConanSettings *ConanPlugin::conanSettings() ConanSettings *ConanPlugin::conanSettings()
{ {
static ConanSettings theSettings; static ConanSettings theSettings;
return &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;
}
} // namespace Internal } // namespace Internal
} // namespace ConanPackageManager } // namespace ConanPackageManager

View File

@@ -26,6 +26,9 @@
#pragma once #pragma once
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
#include <utils/fileutils.h>
namespace ProjectExplorer { class Project; }
namespace ConanPackageManager { namespace ConanPackageManager {
namespace Internal { namespace Internal {
@@ -39,9 +42,12 @@ class ConanPlugin final : public ExtensionSystem::IPlugin
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Conan.json") Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Conan.json")
public: public:
static ConanSettings *conanSettings(); static ConanSettings *conanSettings();
static Utils::FilePath conanFilePath(ProjectExplorer::Project *project,
const Utils::FilePath &defaultFilePath = Utils::FilePath());
private: private:
~ConanPlugin() final; ~ConanPlugin() final;
void projectAdded(ProjectExplorer::Project *project);
void extensionsInitialized() final; void extensionsInitialized() final;
bool initialize(const QStringList &arguments, QString *errorString) final; bool initialize(const QStringList &arguments, QString *errorString) final;