2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2020 Alexis Jeandet.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2020-05-01 18:20:56 +02:00
|
|
|
|
|
|
|
|
#include "mesonbuildsystem.h"
|
2020-10-29 10:20:14 +01:00
|
|
|
|
2023-06-09 15:16:33 +02:00
|
|
|
#include "kitdata.h"
|
2020-05-01 18:20:56 +02:00
|
|
|
#include "mesonbuildconfiguration.h"
|
2022-10-10 09:14:04 +02:00
|
|
|
#include "mesonprojectmanagertr.h"
|
2022-10-06 16:58:18 +02:00
|
|
|
#include "mesontoolkitaspect.h"
|
|
|
|
|
#include "settings.h"
|
2020-10-29 10:20:14 +01:00
|
|
|
|
2023-06-09 15:16:33 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2023-06-13 10:56:40 +02:00
|
|
|
#include <projectexplorer/buildconfiguration.h>
|
2023-08-11 09:18:56 +02:00
|
|
|
#include <projectexplorer/kitaspects.h>
|
2023-06-09 15:16:33 +02:00
|
|
|
#include <projectexplorer/kitmanager.h>
|
2023-10-19 10:42:45 +02:00
|
|
|
#include <projectexplorer/projectupdater.h>
|
2023-06-13 10:56:40 +02:00
|
|
|
#include <projectexplorer/taskhub.h>
|
2023-06-09 15:16:33 +02:00
|
|
|
#include <projectexplorer/toolchain.h>
|
|
|
|
|
|
2023-06-13 10:56:40 +02:00
|
|
|
#include <qtsupport/qtcppkitinfo.h>
|
2023-08-11 11:23:12 +02:00
|
|
|
#include <qtsupport/qtkitaspect.h>
|
2023-06-13 10:56:40 +02:00
|
|
|
|
2023-06-09 15:16:33 +02:00
|
|
|
#include <utils/macroexpander.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
|
|
2020-10-29 10:20:14 +01:00
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
|
2020-05-01 18:20:56 +02:00
|
|
|
#define LEAVE_IF_BUSY() \
|
|
|
|
|
{ \
|
|
|
|
|
if (m_parseGuard.guardsProject()) \
|
|
|
|
|
return false; \
|
|
|
|
|
}
|
|
|
|
|
#define LOCK() \
|
|
|
|
|
{ \
|
|
|
|
|
m_parseGuard = guardParsingRun(); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define UNLOCK(success) \
|
|
|
|
|
{ \
|
|
|
|
|
if (success) \
|
|
|
|
|
m_parseGuard.markAsSuccess(); \
|
|
|
|
|
m_parseGuard = {}; \
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-01 14:57:54 +01:00
|
|
|
using namespace ProjectExplorer;
|
2023-06-09 15:16:33 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
|
|
namespace MesonProjectManager::Internal {
|
2020-12-01 14:57:54 +01:00
|
|
|
|
|
|
|
|
static Q_LOGGING_CATEGORY(mesonBuildSystemLog, "qtc.meson.buildsystem", QtWarningMsg);
|
2020-05-01 18:20:56 +02:00
|
|
|
|
2023-06-09 15:16:33 +02:00
|
|
|
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
|
|
|
|
|
|
2020-05-01 18:20:56 +02:00
|
|
|
MesonBuildSystem::MesonBuildSystem(MesonBuildConfiguration *bc)
|
2023-06-13 10:56:40 +02:00
|
|
|
: BuildSystem(bc)
|
|
|
|
|
, m_parser(MesonToolKitAspect::mesonToolId(bc->kit()), bc->environment(), project())
|
2023-10-19 10:42:45 +02:00
|
|
|
, m_cppCodeModelUpdater(ProjectUpdaterFactory::createCppProjectUpdater())
|
2020-05-01 18:20:56 +02:00
|
|
|
{
|
2023-06-13 10:56:40 +02:00
|
|
|
qCDebug(mesonBuildSystemLog) << "Init";
|
|
|
|
|
connect(bc->target(), &ProjectExplorer::Target::kitChanged, this, [this] {
|
|
|
|
|
updateKit(kit());
|
|
|
|
|
});
|
|
|
|
|
connect(bc, &MesonBuildConfiguration::buildDirectoryChanged, this, [this] {
|
|
|
|
|
updateKit(kit());
|
|
|
|
|
this->triggerParsing();
|
|
|
|
|
});
|
|
|
|
|
connect(bc, &MesonBuildConfiguration::parametersChanged, this, [this] {
|
|
|
|
|
updateKit(kit());
|
|
|
|
|
wipe();
|
|
|
|
|
});
|
|
|
|
|
connect(bc, &MesonBuildConfiguration::environmentChanged, this, [this] {
|
|
|
|
|
m_parser.setEnvironment(buildConfiguration()->environment());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(project(), &ProjectExplorer::Project::projectFileIsDirty, this, [this] {
|
|
|
|
|
if (buildConfiguration()->isActive())
|
|
|
|
|
parseProject();
|
|
|
|
|
});
|
|
|
|
|
connect(&m_parser, &MesonProjectParser::parsingCompleted, this, &MesonBuildSystem::parsingCompleted);
|
|
|
|
|
|
|
|
|
|
connect(&m_IntroWatcher, &Utils::FileSystemWatcher::fileChanged, this, [this] {
|
|
|
|
|
if (buildConfiguration()->isActive())
|
|
|
|
|
parseProject();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
updateKit(kit());
|
|
|
|
|
// as specified here https://mesonbuild.com/IDE-integration.html#ide-integration
|
|
|
|
|
// meson-info.json is the last written file, which ensure that all others introspection
|
|
|
|
|
// files are ready when a modification is detected on this one.
|
|
|
|
|
m_IntroWatcher.addFile(buildConfiguration()
|
|
|
|
|
->buildDirectory()
|
|
|
|
|
.pathAppended(Constants::MESON_INFO_DIR)
|
|
|
|
|
.pathAppended(Constants::MESON_INFO),
|
|
|
|
|
Utils::FileSystemWatcher::WatchModifiedDate);
|
2020-05-01 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MesonBuildSystem::~MesonBuildSystem()
|
|
|
|
|
{
|
|
|
|
|
qCDebug(mesonBuildSystemLog) << "dtor";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MesonBuildSystem::triggerParsing()
|
|
|
|
|
{
|
|
|
|
|
qCDebug(mesonBuildSystemLog) << "Trigger parsing";
|
|
|
|
|
parseProject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MesonBuildSystem::needsSetup()
|
|
|
|
|
{
|
|
|
|
|
const Utils::FilePath &buildDir = buildConfiguration()->buildDirectory();
|
|
|
|
|
return (!isSetup(buildDir) || !m_parser.usesSameMesonVersion(buildDir)
|
|
|
|
|
|| !m_parser.matchesKit(m_kitData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MesonBuildSystem::parsingCompleted(bool success)
|
|
|
|
|
{
|
|
|
|
|
if (success) {
|
|
|
|
|
setRootProjectNode(m_parser.takeProjectNode());
|
|
|
|
|
if (kit() && buildConfiguration()) {
|
2023-10-19 10:42:45 +02:00
|
|
|
KitInfo kitInfo{kit()};
|
|
|
|
|
m_cppCodeModelUpdater->update(
|
2020-05-01 18:20:56 +02:00
|
|
|
{project(),
|
|
|
|
|
QtSupport::CppKitInfo(kit()),
|
|
|
|
|
buildConfiguration()->environment(),
|
|
|
|
|
m_parser.buildProjectParts(kitInfo.cxxToolChain, kitInfo.cToolChain)});
|
|
|
|
|
}
|
|
|
|
|
setApplicationTargets(m_parser.appsTargets());
|
|
|
|
|
UNLOCK(true);
|
|
|
|
|
emitBuildSystemUpdated();
|
|
|
|
|
} else {
|
2022-10-10 09:14:04 +02:00
|
|
|
TaskHub::addTask(BuildSystemTask(Task::Error, Tr::tr("Meson build: Parsing failed")));
|
2020-05-01 18:20:56 +02:00
|
|
|
UNLOCK(false);
|
|
|
|
|
emitBuildSystemUpdated();
|
|
|
|
|
}
|
2023-06-13 08:39:56 +02:00
|
|
|
emitParsingFinished(success);
|
|
|
|
|
|
|
|
|
|
emit buildConfiguration()->enabledChanged(); // HACK. Should not be needed.
|
2020-05-01 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList MesonBuildSystem::configArgs(bool isSetup)
|
|
|
|
|
{
|
2023-06-13 10:56:40 +02:00
|
|
|
MesonBuildConfiguration *bc = static_cast<MesonBuildConfiguration *>(buildConfiguration());
|
|
|
|
|
|
|
|
|
|
const QString ¶ms = bc->parameters();
|
2021-01-02 20:14:56 +02:00
|
|
|
if (!isSetup || params.contains("--cross-file") || params.contains("--native-file"))
|
2023-06-13 10:56:40 +02:00
|
|
|
return m_pendingConfigArgs + bc->mesonConfigArgs();
|
|
|
|
|
|
|
|
|
|
return QStringList{
|
|
|
|
|
QString("--native-file=%1").arg(MachineFileManager::machineFile(kit()).toString())}
|
|
|
|
|
+ m_pendingConfigArgs + bc->mesonConfigArgs();
|
2020-05-01 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MesonBuildSystem::configure()
|
|
|
|
|
{
|
|
|
|
|
LEAVE_IF_BUSY();
|
|
|
|
|
qCDebug(mesonBuildSystemLog) << "Configure";
|
|
|
|
|
if (needsSetup())
|
|
|
|
|
return setup();
|
|
|
|
|
LOCK();
|
|
|
|
|
if (m_parser.configure(projectDirectory(),
|
|
|
|
|
buildConfiguration()->buildDirectory(),
|
|
|
|
|
configArgs(false))) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
UNLOCK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MesonBuildSystem::setup()
|
|
|
|
|
{
|
|
|
|
|
LEAVE_IF_BUSY();
|
|
|
|
|
LOCK();
|
|
|
|
|
qCDebug(mesonBuildSystemLog) << "Setup";
|
|
|
|
|
if (m_parser.setup(projectDirectory(), buildConfiguration()->buildDirectory(), configArgs(true)))
|
|
|
|
|
return true;
|
|
|
|
|
UNLOCK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MesonBuildSystem::wipe()
|
|
|
|
|
{
|
|
|
|
|
LEAVE_IF_BUSY();
|
|
|
|
|
LOCK();
|
|
|
|
|
qCDebug(mesonBuildSystemLog) << "Wipe";
|
|
|
|
|
if (m_parser.wipe(projectDirectory(), buildConfiguration()->buildDirectory(), configArgs(true)))
|
|
|
|
|
return true;
|
|
|
|
|
UNLOCK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MesonBuildSystem::parseProject()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(buildConfiguration(), return false);
|
2023-05-26 11:19:53 +02:00
|
|
|
if (!isSetup(buildConfiguration()->buildDirectory()) && settings().autorunMeson())
|
2020-05-01 18:20:56 +02:00
|
|
|
return configure();
|
|
|
|
|
LEAVE_IF_BUSY();
|
|
|
|
|
LOCK();
|
|
|
|
|
qCDebug(mesonBuildSystemLog) << "Starting parser";
|
|
|
|
|
if (m_parser.parse(projectDirectory(), buildConfiguration()->buildDirectory()))
|
|
|
|
|
return true;
|
|
|
|
|
UNLOCK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MesonBuildSystem::updateKit(ProjectExplorer::Kit *kit)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(kit, return );
|
2023-06-09 15:16:33 +02:00
|
|
|
m_kitData = createKitData(kit);
|
2020-05-01 18:20:56 +02:00
|
|
|
m_parser.setQtVersion(m_kitData.qtVersion);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 15:16:33 +02:00
|
|
|
} // MesonProjectManager::Internal
|