Meson: Move MachineFileManager near build system

Closely related

Change-Id: Id561dccd909bb4dc53e1a55d6ee77d008e5c4078
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
hjk
2023-06-09 15:16:33 +02:00
parent ae01e13d32
commit 38c3abb774
8 changed files with 162 additions and 231 deletions

View File

@@ -14,8 +14,6 @@ add_qtc_plugin(MesonProjectManager
infoparser.h infoparser.h
kitdata.h kitdata.h
kithelper.h kithelper.h
machinefilemanager.cpp
machinefilemanager.h
mesonactionsmanager.cpp mesonactionsmanager.cpp
mesonactionsmanager.h mesonactionsmanager.h
mesonbuildconfiguration.cpp mesonbuildconfiguration.cpp

View File

@@ -12,77 +12,3 @@
#include <utils/macroexpander.h> #include <utils/macroexpander.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
namespace MesonProjectManager {
namespace Internal {
namespace KitHelper {
namespace details {
inline QString expand(const ProjectExplorer::Kit *kit, const QString &macro)
{
return kit->macroExpander()->expand(macro);
}
} // namespace details
inline QString cCompilerPath(const ProjectExplorer::Kit *kit)
{
QTC_ASSERT(kit, return {});
return details::expand(kit, "%{Compiler:Executable:C}");
}
inline QString cxxCompilerPath(const ProjectExplorer::Kit *kit)
{
QTC_ASSERT(kit, return {});
return details::expand(kit, "%{Compiler:Executable:Cxx}");
}
inline QString qmakePath(const ProjectExplorer::Kit *kit)
{
return details::expand(kit, "%{Qt:qmakeExecutable}");
}
inline QString cmakePath(const ProjectExplorer::Kit *kit)
{
return details::expand(kit, "%{CMake:Executable:FilePath}");
}
inline QString qtVersion(const ProjectExplorer::Kit *kit)
{
QTC_ASSERT(kit, return {});
return details::expand(kit, "%{Qt:Version}");
}
inline KitData kitData(const ProjectExplorer::Kit *kit)
{
QTC_ASSERT(kit, return {});
KitData data;
data.cCompilerPath = cCompilerPath(kit);
data.cxxCompilerPath = cxxCompilerPath(kit);
data.cmakePath = cmakePath(kit);
data.qmakePath = qmakePath(kit);
data.qtVersionStr = qtVersion(kit);
data.qtVersion = Utils::QtMajorVersion::None;
auto version = Version::fromString(data.qtVersionStr);
if (version.isValid) {
switch (version.major) {
case 4:
data.qtVersion = Utils::QtMajorVersion::Qt4;
break;
case 5:
data.qtVersion = Utils::QtMajorVersion::Qt5;
break;
case 6:
data.qtVersion = Utils::QtMajorVersion::Qt6;
break;
default:
data.qtVersion = Utils::QtMajorVersion::Unknown;
}
}
return data;
}
} // namespace KitHelper
} // namespace Internal
} // namespace MesonProjectManager

View File

@@ -1,114 +0,0 @@
// Copyright (C) 2020 Alexis Jeandet.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "machinefilemanager.h"
#include "kitdata.h"
#include "kithelper.h"
#include <coreplugin/icore.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/toolchain.h>
#include <utils/macroexpander.h>
#include <utils/qtcassert.h>
#include <optional>
using namespace ProjectExplorer;
using namespace Utils;
namespace MesonProjectManager::Internal {
const char MACHINE_FILE_PREFIX[] = "Meson-MachineFile-";
const char MACHINE_FILE_EXT[] = ".ini";
static FilePath machineFilesDir()
{
return Core::ICore::userResourcePath("Meson-machine-files");
}
MachineFileManager::MachineFileManager()
{
connect(KitManager::instance(), &KitManager::kitAdded,
this, &MachineFileManager::addMachineFile);
connect(KitManager::instance(), &KitManager::kitUpdated,
this, &MachineFileManager::updateMachineFile);
connect(KitManager::instance(), &KitManager::kitRemoved,
this, &MachineFileManager::removeMachineFile);
connect(KitManager::instance(), &KitManager::kitsLoaded,
this, &MachineFileManager::cleanupMachineFiles);
}
FilePath MachineFileManager::machineFile(const Kit *kit)
{
QTC_ASSERT(kit, return {});
auto baseName
= QString("%1%2%3").arg(MACHINE_FILE_PREFIX).arg(kit->id().toString()).arg(MACHINE_FILE_EXT);
baseName = baseName.remove('{').remove('}');
return machineFilesDir().pathAppended(baseName);
}
void MachineFileManager::addMachineFile(const Kit *kit)
{
FilePath filePath = machineFile(kit);
QTC_ASSERT(!filePath.isEmpty(), return );
auto kitData = KitHelper::kitData(kit);
auto entry = [](const QString &key, const QString &value) {
return QString("%1 = '%2'\n").arg(key).arg(value).toUtf8();
};
QByteArray ba = "[binaries]\n";
ba += entry("c", kitData.cCompilerPath);
ba += entry("cpp", kitData.cxxCompilerPath);
ba += entry("qmake", kitData.qmakePath);
if (kitData.qtVersion == QtMajorVersion::Qt4)
ba += entry("qmake-qt4", kitData.qmakePath);
else if (kitData.qtVersion == QtMajorVersion::Qt5)
ba += entry("qmake-qt5", kitData.qmakePath);
else if (kitData.qtVersion == QtMajorVersion::Qt6)
ba += entry("qmake-qt6", kitData.qmakePath);
ba += entry("cmake", kitData.cmakePath);
filePath.writeFileContents(ba);
}
void MachineFileManager::removeMachineFile(const Kit *kit)
{
FilePath filePath = machineFile(kit);
if (filePath.exists())
filePath.removeFile();
}
void MachineFileManager::updateMachineFile(const Kit *kit)
{
addMachineFile(kit);
}
void MachineFileManager::cleanupMachineFiles()
{
FilePath dir = machineFilesDir();
dir.ensureWritableDir();
const FileFilter filter = {{QString("%1*%2").arg(MACHINE_FILE_PREFIX).arg(MACHINE_FILE_EXT)}};
const FilePaths machineFiles = dir.dirEntries(filter);
FilePaths expected;
for (Kit const *kit : KitManager::kits()) {
const FilePath fname = machineFile(kit);
expected.push_back(fname);
if (!machineFiles.contains(fname))
addMachineFile(kit);
}
for (const FilePath &file : machineFiles) {
if (!expected.contains(file))
file.removeFile();
}
}
} // MesonProjectManager::Internal

View File

@@ -1,26 +0,0 @@
// Copyright (C) 2020 Alexis Jeandet.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <utils/filepath.h>
namespace ProjectExplorer { class Kit; }
namespace MesonProjectManager::Internal {
class MachineFileManager final : public QObject
{
public:
MachineFileManager();
static Utils::FilePath machineFile(const ProjectExplorer::Kit *kit);
private:
void addMachineFile(const ProjectExplorer::Kit *kit);
void removeMachineFile(const ProjectExplorer::Kit *kit);
void updateMachineFile(const ProjectExplorer::Kit *kit);
void cleanupMachineFiles();
};
} // MesonProjectManager::Internal

View File

@@ -4,7 +4,8 @@
#include "mesonbuildsystem.h" #include "mesonbuildsystem.h"
#include "kithelper.h" #include "kithelper.h"
#include "machinefilemanager.h" #include "kitdata.h"
#include "kithelper.h"
#include "mesonbuildconfiguration.h" #include "mesonbuildconfiguration.h"
#include "mesonprojectmanagertr.h" #include "mesonprojectmanagertr.h"
#include "mesontoolkitaspect.h" #include "mesontoolkitaspect.h"
@@ -16,6 +17,18 @@
#include <qtsupport/qtcppkitinfo.h> #include <qtsupport/qtcppkitinfo.h>
#include <qtsupport/qtkitinformation.h> #include <qtsupport/qtkitinformation.h>
#include <coreplugin/icore.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/toolchain.h>
#include <utils/macroexpander.h>
#include <utils/qtcassert.h>
#include <optional>
#include <QDir> #include <QDir>
#include <QLoggingCategory> #include <QLoggingCategory>
@@ -37,13 +50,136 @@
}; };
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils;
namespace MesonProjectManager::Internal {
namespace MesonProjectManager {
namespace Internal {
static Q_LOGGING_CATEGORY(mesonBuildSystemLog, "qtc.meson.buildsystem", QtWarningMsg); static Q_LOGGING_CATEGORY(mesonBuildSystemLog, "qtc.meson.buildsystem", QtWarningMsg);
const char MACHINE_FILE_PREFIX[] = "Meson-MachineFile-";
const char MACHINE_FILE_EXT[] = ".ini";
static KitData createKitData(const Kit *kit)
{
QTC_ASSERT(kit, return {});
MacroExpander *expander = kit->macroExpander();
KitData data;
data.cCompilerPath = expander->expand(QString("%{Compiler:Executable:C}"));
data.cxxCompilerPath = expander->expand(QString("%{Compiler:Executable:Cxx}"));
data.cmakePath = expander->expand(QString("%{CMake:Executable:FilePath}"));
data.qmakePath = expander->expand(QString("%{Qt:qmakeExecutable}"));
data.qtVersionStr = expander->expand(QString("%{Qt:Version}"));
data.qtVersion = Utils::QtMajorVersion::None;
auto version = Version::fromString(data.qtVersionStr);
if (version.isValid) {
switch (version.major) {
case 4:
data.qtVersion = Utils::QtMajorVersion::Qt4;
break;
case 5:
data.qtVersion = Utils::QtMajorVersion::Qt5;
break;
case 6:
data.qtVersion = Utils::QtMajorVersion::Qt6;
break;
default:
data.qtVersion = Utils::QtMajorVersion::Unknown;
}
}
return data;
}
static FilePath machineFilesDir()
{
return Core::ICore::userResourcePath("Meson-machine-files");
}
FilePath MachineFileManager::machineFile(const Kit *kit)
{
QTC_ASSERT(kit, return {});
auto baseName
= QString("%1%2%3").arg(MACHINE_FILE_PREFIX).arg(kit->id().toString()).arg(MACHINE_FILE_EXT);
baseName = baseName.remove('{').remove('}');
return machineFilesDir().pathAppended(baseName);
}
MachineFileManager::MachineFileManager()
{
connect(KitManager::instance(), &KitManager::kitAdded,
this, &MachineFileManager::addMachineFile);
connect(KitManager::instance(), &KitManager::kitUpdated,
this, &MachineFileManager::updateMachineFile);
connect(KitManager::instance(), &KitManager::kitRemoved,
this, &MachineFileManager::removeMachineFile);
connect(KitManager::instance(), &KitManager::kitsLoaded,
this, &MachineFileManager::cleanupMachineFiles);
}
void MachineFileManager::addMachineFile(const Kit *kit)
{
FilePath filePath = machineFile(kit);
QTC_ASSERT(!filePath.isEmpty(), return );
auto kitData = createKitData(kit);
auto entry = [](const QString &key, const QString &value) {
return QString("%1 = '%2'\n").arg(key).arg(value).toUtf8();
};
QByteArray ba = "[binaries]\n";
ba += entry("c", kitData.cCompilerPath);
ba += entry("cpp", kitData.cxxCompilerPath);
ba += entry("qmake", kitData.qmakePath);
if (kitData.qtVersion == QtMajorVersion::Qt4)
ba += entry("qmake-qt4", kitData.qmakePath);
else if (kitData.qtVersion == QtMajorVersion::Qt5)
ba += entry("qmake-qt5", kitData.qmakePath);
else if (kitData.qtVersion == QtMajorVersion::Qt6)
ba += entry("qmake-qt6", kitData.qmakePath);
ba += entry("cmake", kitData.cmakePath);
filePath.writeFileContents(ba);
}
void MachineFileManager::removeMachineFile(const Kit *kit)
{
FilePath filePath = machineFile(kit);
if (filePath.exists())
filePath.removeFile();
}
void MachineFileManager::updateMachineFile(const Kit *kit)
{
addMachineFile(kit);
}
void MachineFileManager::cleanupMachineFiles()
{
FilePath dir = machineFilesDir();
dir.ensureWritableDir();
const FileFilter filter = {{QString("%1*%2").arg(MACHINE_FILE_PREFIX).arg(MACHINE_FILE_EXT)}};
const FilePaths machineFiles = dir.dirEntries(filter);
FilePaths expected;
for (Kit const *kit : KitManager::kits()) {
const FilePath fname = machineFile(kit);
expected.push_back(fname);
if (!machineFiles.contains(fname))
addMachineFile(kit);
}
for (const FilePath &file : machineFiles) {
if (!expected.contains(file))
file.removeFile();
}
}
// MesonBuildSystem
MesonBuildSystem::MesonBuildSystem(MesonBuildConfiguration *bc) MesonBuildSystem::MesonBuildSystem(MesonBuildConfiguration *bc)
: ProjectExplorer::BuildSystem{bc} : BuildSystem{bc}
, m_parser{MesonToolKitAspect::mesonToolId(bc->kit()), bc->environment(), project()} , m_parser{MesonToolKitAspect::mesonToolId(bc->kit()), bc->environment(), project()}
{ {
init(); init();
@@ -92,7 +228,7 @@ void MesonBuildSystem::parsingCompleted(bool success)
emit buildConfiguration()->enabledChanged(); // HACK. Should not be needed. emit buildConfiguration()->enabledChanged(); // HACK. Should not be needed.
} }
ProjectExplorer::Kit *MesonBuildSystem::MesonBuildSystem::kit() Kit *MesonBuildSystem::MesonBuildSystem::kit()
{ {
return buildConfiguration()->kit(); return buildConfiguration()->kit();
} }
@@ -209,9 +345,8 @@ bool MesonBuildSystem::parseProject()
void MesonBuildSystem::updateKit(ProjectExplorer::Kit *kit) void MesonBuildSystem::updateKit(ProjectExplorer::Kit *kit)
{ {
QTC_ASSERT(kit, return ); QTC_ASSERT(kit, return );
m_kitData = KitHelper::kitData(kit); m_kitData = createKitData(kit);
m_parser.setQtVersion(m_kitData.qtVersion); m_parser.setQtVersion(m_kitData.qtVersion);
} }
} // namespace Internal } // MesonProjectManager::Internal
} // namespace MesonProjectManager

View File

@@ -13,13 +13,28 @@
#include <utils/filesystemwatcher.h> #include <utils/filesystemwatcher.h>
namespace MesonProjectManager { namespace MesonProjectManager::Internal {
namespace Internal {
class MesonBuildConfiguration; class MesonBuildConfiguration;
class MachineFileManager final : public QObject
{
public:
MachineFileManager();
static Utils::FilePath machineFile(const ProjectExplorer::Kit *kit);
private:
void addMachineFile(const ProjectExplorer::Kit *kit);
void removeMachineFile(const ProjectExplorer::Kit *kit);
void updateMachineFile(const ProjectExplorer::Kit *kit);
void cleanupMachineFiles();
};
class MesonBuildSystem final : public ProjectExplorer::BuildSystem class MesonBuildSystem final : public ProjectExplorer::BuildSystem
{ {
Q_OBJECT Q_OBJECT
public: public:
MesonBuildSystem(MesonBuildConfiguration *bc); MesonBuildSystem(MesonBuildConfiguration *bc);
~MesonBuildSystem() final; ~MesonBuildSystem() final;
@@ -56,5 +71,4 @@ private:
KitData m_kitData; KitData m_kitData;
}; };
} // namespace Internal } // MesonProjectManager::Internal
} // namespace MesonProjectManager

View File

@@ -28,8 +28,6 @@ Project {
"toolwrapper.h", "toolwrapper.h",
"kitdata.h", "kitdata.h",
"kithelper.h", "kithelper.h",
"machinefilemanager.cpp",
"machinefilemanager.h",
"mesonactionsmanager.cpp", "mesonactionsmanager.cpp",
"mesonactionsmanager.h", "mesonactionsmanager.h",
"buildoptions.h", "buildoptions.h",

View File

@@ -3,9 +3,9 @@
#include "mesonprojectplugin.h" #include "mesonprojectplugin.h"
#include "machinefilemanager.h"
#include "mesonactionsmanager.h" #include "mesonactionsmanager.h"
#include "mesonbuildconfiguration.h" #include "mesonbuildconfiguration.h"
#include "mesonbuildsystem.h"
#include "mesonproject.h" #include "mesonproject.h"
#include "mesonrunconfiguration.h" #include "mesonrunconfiguration.h"
#include "mesontoolkitaspect.h" #include "mesontoolkitaspect.h"