From 319300aa6c6db1aebdb8efeba8e17f227680c682 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Tue, 26 Jan 2021 18:17:57 +0100 Subject: 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 --- src/plugins/conan/CMakeLists.txt | 1 + src/plugins/conan/conan.pro | 1 + src/plugins/conan/conan.qbs | 1 + src/plugins/conan/conanconstants.h | 34 ++++++++++++++++++++++++ src/plugins/conan/conaninstallstep.cpp | 6 +++-- src/plugins/conan/conanplugin.cpp | 48 +++++++++++++++++++++++++++++++++- src/plugins/conan/conanplugin.h | 6 +++++ 7 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/plugins/conan/conanconstants.h (limited to 'src/plugins/conan') diff --git a/src/plugins/conan/CMakeLists.txt b/src/plugins/conan/CMakeLists.txt index e6fdf0f587..7833f6c970 100644 --- a/src/plugins/conan/CMakeLists.txt +++ b/src/plugins/conan/CMakeLists.txt @@ -1,6 +1,7 @@ add_qtc_plugin(Conan PLUGIN_DEPENDS Core ProjectExplorer SOURCES + conanconstants.h conaninstallstep.cpp conaninstallstep.h conanplugin.cpp conanplugin.h conansettings.cpp conansettings.h diff --git a/src/plugins/conan/conan.pro b/src/plugins/conan/conan.pro index af1f0b19e0..157bdb509a 100644 --- a/src/plugins/conan/conan.pro +++ b/src/plugins/conan/conan.pro @@ -5,6 +5,7 @@ SOURCES += \ conanplugin.cpp \ conansettings.cpp HEADERS += \ + conanconstants.h \ conaninstallstep.h \ conanplugin.h \ conansettings.h diff --git a/src/plugins/conan/conan.qbs b/src/plugins/conan/conan.qbs index af56a9a54b..57e2351b4f 100644 --- a/src/plugins/conan/conan.qbs +++ b/src/plugins/conan/conan.qbs @@ -10,6 +10,7 @@ QtcPlugin { Depends { name: "ProjectExplorer" } files: [ + "conanconstants.h", "conaninstallstep.h", "conaninstallstep.cpp", "conanplugin.h", diff --git a/src/plugins/conan/conanconstants.h b/src/plugins/conan/conanconstants.h new file mode 100644 index 0000000000..c9f1436ace --- /dev/null +++ b/src/plugins/conan/conanconstants.h @@ -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 diff --git a/src/plugins/conan/conaninstallstep.cpp b/src/plugins/conan/conaninstallstep.cpp index 9f3e22575b..8d7a4adf31 100644 --- a/src/plugins/conan/conaninstallstep.cpp +++ b/src/plugins/conan/conaninstallstep.cpp @@ -23,6 +23,7 @@ ** ****************************************************************************/ +#include "conanconstants.h" #include "conaninstallstep.h" #include "conanplugin.h" #include "conansettings.h" @@ -67,7 +68,8 @@ ConanInstallStep::ConanInstallStep(BuildStepList *bsl, Id id) auto conanFile = addAspect(); 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->setToolTip(tr("Enter location of conanfile.txt or conanfile.py.")); conanFile->setDisplayStyle(StringAspect::PathChooserDisplay); @@ -134,7 +136,7 @@ void ConanInstallStep::setupOutputFormatter(OutputFormatter *formatter) ConanInstallStepFactory::ConanInstallStepFactory() { - registerStep("ConanPackageManager.InstallStep"); + registerStep(Constants::INSTALL_STEP); setDisplayName(ConanInstallStep::tr("Run conan install")); } diff --git a/src/plugins/conan/conanplugin.cpp b/src/plugins/conan/conanplugin.cpp index 5bd0546223..4e548774f2 100644 --- a/src/plugins/conan/conanplugin.cpp +++ b/src/plugins/conan/conanplugin.cpp @@ -23,15 +23,24 @@ ** ****************************************************************************/ +#include "conanconstants.h" #include "conaninstallstep.h" #include "conanplugin.h" #include "conansettings.h" #include -#include +#include #include +#include +#include +#include +#include +#include +#include using namespace Core; +using namespace ProjectExplorer; +using namespace Utils; namespace ConanPackageManager { namespace Internal { @@ -58,15 +67,52 @@ bool ConanPlugin::initialize(const QStringList &arguments, QString *errorString) m_runData = new ConanPluginRunData; conanSettings()->fromSettings(ICore::settings()); + connect(SessionManager::instance(), &SessionManager::projectAdded, + this, &ConanPlugin::projectAdded); + return true; } +static void connectTarget(Project *project, Target *target) +{ + if (!ConanPlugin::conanFilePath(project).isEmpty()) { + const QList 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() { 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; +} + } // namespace Internal } // namespace ConanPackageManager diff --git a/src/plugins/conan/conanplugin.h b/src/plugins/conan/conanplugin.h index b26a738276..df68681457 100644 --- a/src/plugins/conan/conanplugin.h +++ b/src/plugins/conan/conanplugin.h @@ -26,6 +26,9 @@ #pragma once #include +#include + +namespace ProjectExplorer { class Project; } namespace ConanPackageManager { namespace Internal { @@ -39,9 +42,12 @@ class ConanPlugin final : public ExtensionSystem::IPlugin 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 = Utils::FilePath()); private: ~ConanPlugin() final; + void projectAdded(ProjectExplorer::Project *project); void extensionsInitialized() final; bool initialize(const QStringList &arguments, QString *errorString) final; -- cgit v1.2.1