CMake: Use callback-based environment iteration

Hides underlying data structure.

Task-number: QTCREATORBUG-28357
Change-Id: Ib0d7fb70afa820b1bd28d23e12b9379a6de6546b
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2023-03-01 16:27:47 +01:00
parent 9feef11b5d
commit 34fd28327d
2 changed files with 26 additions and 35 deletions

View File

@@ -8,6 +8,8 @@
#include <utils/filepath.h> #include <utils/filepath.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
using namespace Utils;
namespace CMakeProjectManager::Internal::CMakePresets::Macros { namespace CMakeProjectManager::Internal::CMakePresets::Macros {
static QString getHostSystemName(Utils::OsType osType) static QString getHostSystemName(Utils::OsType osType)
@@ -99,32 +101,26 @@ static QString expandMacroEnv(const QString &macroPrefix,
return result; return result;
} }
static Utils::Environment getEnvCombined(const std::optional<Utils::Environment> &optPresetEnv, static Environment getEnvCombined(const std::optional<Environment> &optPresetEnv,
const Utils::Environment &env) const Environment &env)
{ {
Utils::Environment result = env; Environment result = env;
if (!optPresetEnv) if (optPresetEnv) {
return result; optPresetEnv->forEachEntry([&result](const QString &key, const QString &value, bool) {
result.set(key, value);
Utils::Environment presetEnv = optPresetEnv.value(); });
for (auto it = presetEnv.constBegin(); it != presetEnv.constEnd(); ++it) {
result.set(it.key().name, it.value().first);
} }
return result; return result;
} }
template<class PresetType> template<class PresetType>
void expand(const PresetType &preset, void expand(const PresetType &preset, Environment &env, const FilePath &sourceDirectory)
Utils::Environment &env,
const Utils::FilePath &sourceDirectory)
{ {
const Utils::Environment presetEnv = getEnvCombined(preset.environment, env); const Environment presetEnv = getEnvCombined(preset.environment, env);
for (auto it = presetEnv.constBegin(); it != presetEnv.constEnd(); ++it) { presetEnv.forEachEntry([&](const QString &key, const QString &value_, bool) {
const QString key = it.key().name; QString value = value_;
QString value = it.value().first;
expandAllButEnv(preset, sourceDirectory, value); expandAllButEnv(preset, sourceDirectory, value);
value = expandMacroEnv("env", value, [presetEnv](const QString &macroName) { value = expandMacroEnv("env", value, [presetEnv](const QString &macroName) {
return presetEnv.value(macroName); return presetEnv.value(macroName);
@@ -133,7 +129,7 @@ void expand(const PresetType &preset,
QString sep; QString sep;
bool append = true; bool append = true;
if (key.compare("PATH", Qt::CaseInsensitive) == 0) { if (key.compare("PATH", Qt::CaseInsensitive) == 0) {
sep = Utils::OsSpecificAspects::pathListSeparator(env.osType()); sep = OsSpecificAspects::pathListSeparator(env.osType());
const int index = value.indexOf("$penv{PATH}", 0, Qt::CaseInsensitive); const int index = value.indexOf("$penv{PATH}", 0, Qt::CaseInsensitive);
if (index != 0) if (index != 0)
append = false; append = false;
@@ -148,20 +144,15 @@ void expand(const PresetType &preset,
env.appendOrSet(key, value, sep); env.appendOrSet(key, value, sep);
else else
env.prependOrSet(key, value, sep); env.prependOrSet(key, value, sep);
} });
} }
template<class PresetType> template<class PresetType>
void expand(const PresetType &preset, void expand(const PresetType &preset, EnvironmentItems &envItems, const FilePath &sourceDirectory)
Utils::EnvironmentItems &envItems,
const Utils::FilePath &sourceDirectory)
{ {
const Utils::Environment presetEnv = preset.environment ? preset.environment.value() const Environment presetEnv = preset.environment ? *preset.environment : Environment();
: Utils::Environment(); presetEnv.forEachEntry([&](const QString &key, const QString &value_, bool) {
for (auto it = presetEnv.constBegin(); it != presetEnv.constEnd(); ++it) { QString value = value_;
const QString key = it.key().name;
QString value = it.value().first;
expandAllButEnv(preset, sourceDirectory, value); expandAllButEnv(preset, sourceDirectory, value);
value = expandMacroEnv("env", value, [presetEnv](const QString &macroName) { value = expandMacroEnv("env", value, [presetEnv](const QString &macroName) {
if (presetEnv.hasKey(macroName)) if (presetEnv.hasKey(macroName))
@@ -169,12 +160,12 @@ void expand(const PresetType &preset,
return QString("${%1}").arg(macroName); return QString("${%1}").arg(macroName);
}); });
auto operation = Utils::EnvironmentItem::Operation::SetEnabled; auto operation = EnvironmentItem::Operation::SetEnabled;
if (key.compare("PATH", Qt::CaseInsensitive) == 0) { if (key.compare("PATH", Qt::CaseInsensitive) == 0) {
operation = Utils::EnvironmentItem::Operation::Append; operation = EnvironmentItem::Operation::Append;
const int index = value.indexOf("$penv{PATH}", 0, Qt::CaseInsensitive); const int index = value.indexOf("$penv{PATH}", 0, Qt::CaseInsensitive);
if (index != 0) if (index != 0)
operation = Utils::EnvironmentItem::Operation::Prepend; operation = EnvironmentItem::Operation::Prepend;
value.replace("$penv{PATH}", "", Qt::CaseInsensitive); value.replace("$penv{PATH}", "", Qt::CaseInsensitive);
} }
@@ -183,7 +174,7 @@ void expand(const PresetType &preset,
}); });
envItems.emplace_back(Utils::EnvironmentItem(key, value, operation)); envItems.emplace_back(Utils::EnvironmentItem(key, value, operation));
} });
} }
template<class PresetType> template<class PresetType>

View File

@@ -456,9 +456,9 @@ static QHash<QString, QString> merge(const QHash<QString, QString> &first,
static Utils::Environment merge(const Utils::Environment &first, const Utils::Environment &second) static Utils::Environment merge(const Utils::Environment &first, const Utils::Environment &second)
{ {
Utils::Environment result = first; Utils::Environment result = first;
for (auto it = second.constBegin(); it != second.constEnd(); ++it) { second.forEachEntry([&](const QString &key, const QString &value, bool) {
result.set(it.key().name, it.value().first); result.set(key, value);
} });
return result; return result;
} }