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 "mesontools.h"
|
2020-10-29 10:20:14 +01:00
|
|
|
|
2024-07-23 08:26:37 +02:00
|
|
|
#include "mesonpluginconstants.h"
|
|
|
|
|
|
2024-07-22 18:10:51 +02:00
|
|
|
#include <utils/algorithm.h>
|
2024-07-22 17:14:09 +02:00
|
|
|
#include <utils/environment.h>
|
2024-07-23 08:26:37 +02:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/qtcprocess.h>
|
|
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QTemporaryFile>
|
2024-07-22 17:14:09 +02:00
|
|
|
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
2024-07-22 18:10:51 +02:00
|
|
|
namespace MesonProjectManager::Internal {
|
2020-05-01 18:20:56 +02:00
|
|
|
|
2024-07-23 08:26:37 +02:00
|
|
|
static ToolType typeFromId(const QString &id)
|
|
|
|
|
{
|
|
|
|
|
if (id == Constants::ToolsSettings::TOOL_TYPE_NINJA)
|
|
|
|
|
return ToolType::Ninja;
|
|
|
|
|
if (id == Constants::ToolsSettings::TOOL_TYPE_MESON)
|
|
|
|
|
return ToolType::Meson;
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return ToolType::Meson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ToolWrapper::ToolWrapper(const Store &data)
|
|
|
|
|
{
|
|
|
|
|
m_toolType = typeFromId(data.value(Constants::ToolsSettings::TOOL_TYPE_KEY).toString());
|
|
|
|
|
m_name = data[Constants::ToolsSettings::NAME_KEY].toString();
|
|
|
|
|
m_exe = FilePath::fromSettings(data[Constants::ToolsSettings::EXE_KEY]);
|
|
|
|
|
m_id = Id::fromSetting(data[Constants::ToolsSettings::ID_KEY]);
|
|
|
|
|
m_autoDetected = data[Constants::ToolsSettings::AUTO_DETECTED_KEY].toBool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ToolWrapper::ToolWrapper(ToolType toolType,
|
|
|
|
|
const QString &name,
|
|
|
|
|
const FilePath &path,
|
|
|
|
|
bool autoDetected)
|
|
|
|
|
: m_toolType(toolType)
|
|
|
|
|
, m_version(read_version(path))
|
|
|
|
|
, m_isValid{path.exists() && m_version.isValid}
|
|
|
|
|
, m_autoDetected{autoDetected}
|
2024-07-23 08:54:48 +02:00
|
|
|
, m_id{Id::generate()}
|
2024-07-23 08:26:37 +02:00
|
|
|
, m_exe{path}
|
|
|
|
|
, m_name{name}
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
ToolWrapper::ToolWrapper(ToolType toolType,
|
|
|
|
|
const QString &name,
|
|
|
|
|
const FilePath &path,
|
|
|
|
|
const Id &id,
|
|
|
|
|
bool autoDetected)
|
|
|
|
|
: m_toolType(toolType)
|
|
|
|
|
, m_version(read_version(path))
|
|
|
|
|
, m_isValid{path.exists() && m_version.isValid}
|
|
|
|
|
, m_autoDetected{autoDetected}
|
|
|
|
|
, m_id{id}
|
|
|
|
|
, m_exe{path}
|
|
|
|
|
, m_name{name}
|
|
|
|
|
{
|
2024-07-23 08:54:48 +02:00
|
|
|
QTC_ASSERT(m_id.isValid(), m_id = Id::generate());
|
2024-07-23 08:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ToolWrapper::~ToolWrapper() = default;
|
|
|
|
|
|
2024-07-23 08:54:48 +02:00
|
|
|
void ToolWrapper::setExe(const FilePath &newExe)
|
2024-07-23 08:26:37 +02:00
|
|
|
{
|
|
|
|
|
m_exe = newExe;
|
|
|
|
|
m_version = read_version(m_exe);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 08:54:48 +02:00
|
|
|
Version ToolWrapper::read_version(const FilePath &toolPath)
|
2024-07-23 08:26:37 +02:00
|
|
|
{
|
|
|
|
|
if (toolPath.toFileInfo().isExecutable()) {
|
2024-07-23 08:54:48 +02:00
|
|
|
Process process;
|
2024-07-23 08:26:37 +02:00
|
|
|
process.setCommand({ toolPath, { "--version" } });
|
|
|
|
|
process.start();
|
|
|
|
|
if (process.waitForFinished())
|
|
|
|
|
return Version::fromString(process.cleanedStdOut());
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Store ToolWrapper::toVariantMap() const
|
|
|
|
|
{
|
2024-07-23 08:54:48 +02:00
|
|
|
Store data;
|
2024-07-23 08:26:37 +02:00
|
|
|
data.insert(Constants::ToolsSettings::NAME_KEY, m_name);
|
|
|
|
|
data.insert(Constants::ToolsSettings::EXE_KEY, m_exe.toSettings());
|
|
|
|
|
data.insert(Constants::ToolsSettings::AUTO_DETECTED_KEY, m_autoDetected);
|
|
|
|
|
data.insert(Constants::ToolsSettings::ID_KEY, m_id.toSetting());
|
|
|
|
|
if (m_toolType == ToolType::Meson)
|
|
|
|
|
data.insert(Constants::ToolsSettings::TOOL_TYPE_KEY, Constants::ToolsSettings::TOOL_TYPE_MESON);
|
|
|
|
|
else
|
|
|
|
|
data.insert(Constants::ToolsSettings::TOOL_TYPE_KEY, Constants::ToolsSettings::TOOL_TYPE_NINJA);
|
|
|
|
|
;
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First>
|
|
|
|
|
void impl_option_cat(QStringList &list, const First &first)
|
|
|
|
|
{
|
|
|
|
|
list.append(first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First, typename... T>
|
|
|
|
|
void impl_option_cat(QStringList &list, const First &first, const T &...args)
|
|
|
|
|
{
|
|
|
|
|
impl_option_cat(list, first);
|
|
|
|
|
impl_option_cat(list, args...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename... T>
|
|
|
|
|
QStringList options_cat(const T &...args)
|
|
|
|
|
{
|
|
|
|
|
QStringList result;
|
|
|
|
|
impl_option_cat(result, args...);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command ToolWrapper::setup(const FilePath &sourceDirectory,
|
|
|
|
|
const FilePath &buildDirectory,
|
|
|
|
|
const QStringList &options) const
|
|
|
|
|
{
|
|
|
|
|
return {{m_exe, options_cat("setup", options, sourceDirectory.path(), buildDirectory.path())},
|
|
|
|
|
sourceDirectory};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command ToolWrapper::configure(const FilePath &sourceDirectory,
|
|
|
|
|
const FilePath &buildDirectory,
|
|
|
|
|
const QStringList &options) const
|
|
|
|
|
{
|
|
|
|
|
if (!isSetup(buildDirectory))
|
|
|
|
|
return setup(sourceDirectory, buildDirectory, options);
|
|
|
|
|
return {{m_exe, options_cat("configure", options, buildDirectory.path())},
|
|
|
|
|
buildDirectory};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command ToolWrapper::regenerate(const FilePath &sourceDirectory,
|
|
|
|
|
const FilePath &buildDirectory) const
|
|
|
|
|
{
|
|
|
|
|
return {{m_exe,
|
|
|
|
|
options_cat("--internal",
|
|
|
|
|
"regenerate",
|
|
|
|
|
sourceDirectory.path(),
|
|
|
|
|
buildDirectory.path(),
|
|
|
|
|
"--backend",
|
|
|
|
|
"ninja")},
|
|
|
|
|
buildDirectory};
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 08:54:48 +02:00
|
|
|
Command ToolWrapper::introspect(const FilePath &sourceDirectory) const
|
2024-07-23 08:26:37 +02:00
|
|
|
{
|
|
|
|
|
return {{m_exe,
|
|
|
|
|
{"introspect", "--all", QString("%1/meson.build").arg(sourceDirectory.path())}},
|
|
|
|
|
sourceDirectory};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename File_t>
|
|
|
|
|
bool containsFiles(const QString &path, const File_t &file)
|
|
|
|
|
{
|
|
|
|
|
return QFileInfo::exists(QString("%1/%2").arg(path).arg(file));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename File_t, typename... T>
|
|
|
|
|
bool containsFiles(const QString &path, const File_t &file, const T &...files)
|
|
|
|
|
{
|
|
|
|
|
return containsFiles(path, file) && containsFiles(path, files...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool run_meson(const Command &command, QIODevice *output)
|
|
|
|
|
{
|
2024-07-23 08:54:48 +02:00
|
|
|
Process process;
|
2024-07-23 08:26:37 +02:00
|
|
|
process.setWorkingDirectory(command.workDir);
|
|
|
|
|
process.setCommand(command.cmdLine);
|
|
|
|
|
process.start();
|
|
|
|
|
if (!process.waitForFinished())
|
|
|
|
|
return false;
|
|
|
|
|
if (output) {
|
|
|
|
|
output->write(process.rawStdOut());
|
|
|
|
|
}
|
|
|
|
|
return process.exitCode() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 08:54:48 +02:00
|
|
|
bool isSetup(const FilePath &buildPath)
|
2024-07-23 08:26:37 +02:00
|
|
|
{
|
|
|
|
|
return containsFiles(buildPath.pathAppended(Constants::MESON_INFO_DIR).toString(),
|
|
|
|
|
Constants::MESON_INTRO_TESTS,
|
|
|
|
|
Constants::MESON_INTRO_TARGETS,
|
|
|
|
|
Constants::MESON_INTRO_INSTALLED,
|
|
|
|
|
Constants::MESON_INTRO_BENCHMARKS,
|
|
|
|
|
Constants::MESON_INTRO_BUIDOPTIONS,
|
|
|
|
|
Constants::MESON_INTRO_PROJECTINFO,
|
|
|
|
|
Constants::MESON_INTRO_DEPENDENCIES,
|
|
|
|
|
Constants::MESON_INTRO_BUILDSYSTEM_FILES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::optional<FilePath> findToolHelper(const QStringList &exeNames)
|
|
|
|
|
{
|
|
|
|
|
Environment systemEnvironment = Environment::systemEnvironment();
|
|
|
|
|
for (const auto &exe : exeNames) {
|
|
|
|
|
const FilePath exe_path = systemEnvironment.searchInPath(exe);
|
|
|
|
|
if (exe_path.exists())
|
|
|
|
|
return exe_path;
|
|
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<FilePath> findTool(ToolType toolType)
|
|
|
|
|
{
|
|
|
|
|
if (toolType == ToolType::Meson)
|
|
|
|
|
return findToolHelper({"meson.py", "meson"});
|
|
|
|
|
if (toolType == ToolType::Ninja)
|
|
|
|
|
return findToolHelper({"ninja", "ninja-build"});
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-07-22 18:10:51 +02:00
|
|
|
std::vector<MesonTools::Tool_t> s_tools;
|
|
|
|
|
|
|
|
|
|
static MesonTools::Tool_t findTool(const Id &id, ToolType toolType)
|
2020-05-01 18:20:56 +02:00
|
|
|
{
|
2024-07-22 18:10:51 +02:00
|
|
|
const auto tool = std::find_if(std::cbegin(s_tools),
|
|
|
|
|
std::cend(s_tools),
|
2020-05-01 18:20:56 +02:00
|
|
|
[&id](const MesonTools::Tool_t &tool) {
|
|
|
|
|
return tool->id() == id;
|
|
|
|
|
});
|
2024-07-22 18:10:51 +02:00
|
|
|
if (tool != std::cend(s_tools) && (*tool)->toolType() == toolType)
|
2024-07-22 17:27:50 +02:00
|
|
|
return *tool;
|
2020-05-01 18:20:56 +02:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 10:26:05 +02:00
|
|
|
MesonTools::Tool_t MesonTools::autoDetectedTool(ToolType toolType)
|
2020-05-01 18:20:56 +02:00
|
|
|
{
|
2024-07-22 18:10:51 +02:00
|
|
|
for (const auto &tool : s_tools) {
|
2024-07-22 17:14:09 +02:00
|
|
|
if (tool->autoDetected() && tool->toolType() == toolType)
|
|
|
|
|
return tool;
|
2020-05-01 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-22 18:10:51 +02:00
|
|
|
static void fixAutoDetected(ToolType toolType)
|
2020-05-01 18:20:56 +02:00
|
|
|
{
|
2024-07-24 10:26:05 +02:00
|
|
|
MesonTools::Tool_t autoDetected = MesonTools::autoDetectedTool(toolType);
|
2024-07-22 18:10:51 +02:00
|
|
|
if (!autoDetected) {
|
2024-07-22 17:14:09 +02:00
|
|
|
QStringList exeNames;
|
|
|
|
|
QString toolName;
|
|
|
|
|
if (toolType == ToolType::Meson) {
|
2024-07-22 18:10:51 +02:00
|
|
|
if (std::optional<FilePath> path = findTool(toolType)) {
|
|
|
|
|
s_tools.emplace_back(
|
2024-07-22 17:14:09 +02:00
|
|
|
std::make_shared<ToolWrapper>(toolType,
|
|
|
|
|
QString("System %1 at %2").arg("Meson").arg(path->toString()), *path, true));
|
|
|
|
|
}
|
|
|
|
|
} else if (toolType == ToolType::Ninja) {
|
2024-07-22 18:10:51 +02:00
|
|
|
if (std::optional<FilePath> path = findTool(toolType)) {
|
|
|
|
|
s_tools.emplace_back(
|
2024-07-22 17:14:09 +02:00
|
|
|
std::make_shared<ToolWrapper>(toolType,
|
|
|
|
|
QString("System %1 at %2").arg("Ninja").arg(path->toString()), *path, true));
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-01 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MesonTools::setTools(std::vector<MesonTools::Tool_t> &&tools)
|
|
|
|
|
{
|
2024-07-22 18:10:51 +02:00
|
|
|
std::swap(s_tools, tools);
|
|
|
|
|
fixAutoDetected(ToolType::Meson);
|
|
|
|
|
fixAutoDetected(ToolType::Ninja);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::vector<MesonTools::Tool_t> &MesonTools::tools()
|
|
|
|
|
{
|
|
|
|
|
return s_tools;
|
2020-05-01 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 17:49:58 +02:00
|
|
|
void MesonTools::updateTool(const Id &itemId, const QString &name, const FilePath &exe)
|
|
|
|
|
{
|
2024-07-22 18:10:51 +02:00
|
|
|
auto item = std::find_if(std::begin(s_tools),
|
|
|
|
|
std::end(s_tools),
|
2024-07-22 17:49:58 +02:00
|
|
|
[&itemId](const Tool_t &tool) { return tool->id() == itemId; });
|
2024-07-22 18:10:51 +02:00
|
|
|
if (item != std::end(s_tools)) {
|
2024-07-22 17:49:58 +02:00
|
|
|
(*item)->setExe(exe);
|
|
|
|
|
(*item)->setName(name);
|
|
|
|
|
} else {
|
2024-07-23 08:54:48 +02:00
|
|
|
// TODO improve this
|
|
|
|
|
const ToolType toolType = exe.fileName().contains("ninja") ? ToolType::Ninja : ToolType::Meson;
|
|
|
|
|
s_tools.emplace_back(std::make_shared<ToolWrapper>(toolType, name, exe, itemId));
|
|
|
|
|
emit instance()->toolAdded(s_tools.back());
|
2024-07-22 17:49:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MesonTools::removeTool(const Id &id)
|
|
|
|
|
{
|
2024-07-22 18:10:51 +02:00
|
|
|
auto item = Utils::take(s_tools, [&id](const auto &item) { return item->id() == id; });
|
2024-07-22 17:49:58 +02:00
|
|
|
QTC_ASSERT(item, return );
|
2024-07-22 18:10:51 +02:00
|
|
|
emit instance()->toolRemoved(*item);
|
2020-05-01 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 18:10:51 +02:00
|
|
|
std::shared_ptr<ToolWrapper> MesonTools::toolById(const Id &id, ToolType toolType)
|
2020-05-01 18:20:56 +02:00
|
|
|
{
|
2024-07-22 18:10:51 +02:00
|
|
|
return findTool(id, toolType);
|
2020-05-01 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 18:10:51 +02:00
|
|
|
MesonTools *MesonTools::instance()
|
2020-05-01 18:20:56 +02:00
|
|
|
{
|
2024-07-22 18:10:51 +02:00
|
|
|
static MesonTools inst;
|
|
|
|
|
return &inst;
|
2020-05-01 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 18:10:51 +02:00
|
|
|
} // MesonProjectManager::Internal
|