forked from qt-creator/qt-creator
CMakePM: Add support for configure CMake presets
This patchset will add support for version 1 of the CMakePresets feature that has been implemented in CMake 3.19 https://cmake.org/cmake/help/v3.19/manual/cmake-presets.7.html The tests/manual/cmakepresets contains a manual test example for this feature. Task-number: QTCREATORBUG-24555 Change-Id: I93aba1ab4f090613d0b21d970b5b651d12c922af Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
131
src/plugins/cmakeprojectmanager/presetsmacros.cpp
Normal file
131
src/plugins/cmakeprojectmanager/presetsmacros.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
// Copyright (C) 2022 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "presetsmacros.h"
|
||||
#include "presetsparser.h"
|
||||
|
||||
#include <utils/environment.h>
|
||||
#include <utils/filepath.h>
|
||||
#include <utils/hostosinfo.h>
|
||||
|
||||
#include <QRegularExpression>
|
||||
|
||||
namespace CMakeProjectManager::Internal::CMakePresets::Macros {
|
||||
|
||||
static void expandAllButEnv(const PresetsDetails::ConfigurePreset &configurePreset,
|
||||
const Utils::FilePath &sourceDirectory,
|
||||
QString &value)
|
||||
{
|
||||
value.replace("${dollar}", "$");
|
||||
|
||||
value.replace("${sourceDir}", sourceDirectory.toString());
|
||||
value.replace("${sourceParentDir}", sourceDirectory.parentDir().toString());
|
||||
value.replace("${sourceDirName}", sourceDirectory.fileName());
|
||||
|
||||
value.replace("${presetName}", configurePreset.name);
|
||||
if (configurePreset.generator)
|
||||
value.replace("${generator}", configurePreset.generator.value());
|
||||
}
|
||||
|
||||
void expand(const PresetsDetails::ConfigurePreset &configurePreset,
|
||||
Utils::Environment &env,
|
||||
const Utils::FilePath &sourceDirectory)
|
||||
{
|
||||
const QHash<QString, QString> presetEnv = configurePreset.environment
|
||||
? configurePreset.environment.value()
|
||||
: QHash<QString, QString>();
|
||||
for (auto it = presetEnv.constKeyValueBegin(); it != presetEnv.constKeyValueEnd(); ++it) {
|
||||
const QString key = it->first;
|
||||
QString value = it->second;
|
||||
|
||||
expandAllButEnv(configurePreset, sourceDirectory, value);
|
||||
|
||||
QRegularExpression envRegex(R"((\$env\{(\w+)\}))");
|
||||
for (const QRegularExpressionMatch &match : envRegex.globalMatch(value)) {
|
||||
if (match.captured(2) != key)
|
||||
value.replace(match.captured(1), presetEnv.value(match.captured(2)));
|
||||
else
|
||||
value.replace(match.captured(1), "");
|
||||
}
|
||||
|
||||
QString sep;
|
||||
bool append = true;
|
||||
if (key.compare("PATH", Qt::CaseInsensitive) == 0) {
|
||||
sep = Utils::OsSpecificAspects::pathListSeparator(env.osType());
|
||||
const int index = value.indexOf("$penv{PATH}", 0, Qt::CaseInsensitive);
|
||||
if (index != 0)
|
||||
append = false;
|
||||
value.replace("$penv{PATH}", "", Qt::CaseInsensitive);
|
||||
}
|
||||
|
||||
QRegularExpression penvRegex(R"((\$penv\{(\w+)\}))");
|
||||
for (const QRegularExpressionMatch &match : penvRegex.globalMatch(value))
|
||||
value.replace(match.captured(1), env.value(match.captured(2)));
|
||||
|
||||
if (append)
|
||||
env.appendOrSet(key, value, sep);
|
||||
else
|
||||
env.prependOrSet(key, value, sep);
|
||||
}
|
||||
}
|
||||
|
||||
void expand(const PresetsDetails::ConfigurePreset &configurePreset,
|
||||
Utils::EnvironmentItems &envItems,
|
||||
const Utils::FilePath &sourceDirectory)
|
||||
{
|
||||
const QHash<QString, QString> presetEnv = configurePreset.environment
|
||||
? configurePreset.environment.value()
|
||||
: QHash<QString, QString>();
|
||||
|
||||
for (auto it = presetEnv.constKeyValueBegin(); it != presetEnv.constKeyValueEnd(); ++it) {
|
||||
const QString key = it->first;
|
||||
QString value = it->second;
|
||||
|
||||
expandAllButEnv(configurePreset, sourceDirectory, value);
|
||||
|
||||
QRegularExpression envRegex(R"((\$env\{(\w+)\}))");
|
||||
for (const QRegularExpressionMatch &match : envRegex.globalMatch(value)) {
|
||||
if (match.captured(2) != key)
|
||||
value.replace(match.captured(1), presetEnv.value(match.captured(2)));
|
||||
else
|
||||
value.replace(match.captured(1), "");
|
||||
}
|
||||
|
||||
auto operation = Utils::EnvironmentItem::Operation::SetEnabled;
|
||||
if (key.compare("PATH", Qt::CaseInsensitive) == 0) {
|
||||
operation = Utils::EnvironmentItem::Operation::Append;
|
||||
const int index = value.indexOf("$penv{PATH}", 0, Qt::CaseInsensitive);
|
||||
if (index != 0)
|
||||
operation = Utils::EnvironmentItem::Operation::Prepend;
|
||||
value.replace("$penv{PATH}", "", Qt::CaseInsensitive);
|
||||
}
|
||||
|
||||
QRegularExpression penvRegex(R"((\$penv\{(\w+)\}))");
|
||||
for (const QRegularExpressionMatch &match : penvRegex.globalMatch(value))
|
||||
value.replace(match.captured(1), QString("${%1}").arg(match.captured(2)));
|
||||
|
||||
envItems.emplace_back(Utils::EnvironmentItem(key, value, operation));
|
||||
}
|
||||
}
|
||||
|
||||
void expand(const PresetsDetails::ConfigurePreset &configurePreset,
|
||||
const Utils::Environment &env,
|
||||
const Utils::FilePath &sourceDirectory,
|
||||
QString &value)
|
||||
{
|
||||
expandAllButEnv(configurePreset, sourceDirectory, value);
|
||||
|
||||
const QHash<QString, QString> presetEnv = configurePreset.environment
|
||||
? configurePreset.environment.value()
|
||||
: QHash<QString, QString>();
|
||||
|
||||
QRegularExpression envRegex(R"((\$env\{(\w+)\}))");
|
||||
for (const QRegularExpressionMatch &match : envRegex.globalMatch(value))
|
||||
value.replace(match.captured(1), presetEnv.value(match.captured(2)));
|
||||
|
||||
QRegularExpression penvRegex(R"((\$penv\{(\w+)\}))");
|
||||
for (const QRegularExpressionMatch &match : penvRegex.globalMatch(value))
|
||||
value.replace(match.captured(1), env.value(match.captured(2)));
|
||||
}
|
||||
|
||||
} // namespace CMakeProjectManager::Internal::CMakePresets::Macros
|
||||
Reference in New Issue
Block a user