diff --git a/src/plugins/conan/CMakeLists.txt b/src/plugins/conan/CMakeLists.txt index e6fdf0f5873..7833f6c970e 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 af1f0b19e04..157bdb509a3 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 af56a9a54b1..57e2351b4f7 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 00000000000..c9f1436acef --- /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 9f3e22575b9..8d7a4adf315 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 5bd05462234..4e548774f2a 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 b26a7382761..df68681457a 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;