CMakePM: Use Utils::Environment for Presets environment

Utils::Environment takes care of the case insesitivity of the key of
environment variables on Windows.

Change-Id: I624340d30c6b170b5d0a86791f26a4841a0b2fb7
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Cristian Adam
2022-10-23 20:06:00 +02:00
parent 83e77d71a8
commit fb94873765
4 changed files with 36 additions and 30 deletions

View File

@@ -99,22 +99,17 @@ static QString expandMacroEnv(const QString &macroPrefix,
return result; return result;
} }
static QHash<QString, QString> getEnvCombined( static Utils::Environment getEnvCombined(const std::optional<Utils::Environment> &optPresetEnv,
const std::optional<QHash<QString, QString>> &optPresetEnv, const Utils::Environment &env) const Utils::Environment &env)
{ {
QHash<QString, QString> result; Utils::Environment result = env;
for (auto it = env.constBegin(); it != env.constEnd(); ++it) {
if (it.value().second)
result.insert(it.key().name, it.value().first);
}
if (!optPresetEnv) if (!optPresetEnv)
return result; return result;
QHash<QString, QString> presetEnv = optPresetEnv.value(); Utils::Environment presetEnv = optPresetEnv.value();
for (auto it = presetEnv.constKeyValueBegin(); it != presetEnv.constKeyValueEnd(); ++it) { for (auto it = presetEnv.constBegin(); it != presetEnv.constEnd(); ++it) {
result[it->first] = it->second; result.set(it.key().name, it.value().first);
} }
return result; return result;
@@ -125,10 +120,10 @@ void expand(const PresetType &preset,
Utils::Environment &env, Utils::Environment &env,
const Utils::FilePath &sourceDirectory) const Utils::FilePath &sourceDirectory)
{ {
const QHash<QString, QString> presetEnv = getEnvCombined(preset.environment, env); const Utils::Environment presetEnv = getEnvCombined(preset.environment, env);
for (auto it = presetEnv.constKeyValueBegin(); it != presetEnv.constKeyValueEnd(); ++it) { for (auto it = presetEnv.constBegin(); it != presetEnv.constEnd(); ++it) {
const QString key = it->first; const QString key = it.key().name;
QString value = it->second; 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) {
@@ -161,15 +156,15 @@ void expand(const PresetType &preset,
Utils::EnvironmentItems &envItems, Utils::EnvironmentItems &envItems,
const Utils::FilePath &sourceDirectory) const Utils::FilePath &sourceDirectory)
{ {
const QHash<QString, QString> presetEnv = preset.environment ? preset.environment.value() const Utils::Environment presetEnv = preset.environment ? preset.environment.value()
: QHash<QString, QString>(); : Utils::Environment();
for (auto it = presetEnv.constKeyValueBegin(); it != presetEnv.constKeyValueEnd(); ++it) { for (auto it = presetEnv.constBegin(); it != presetEnv.constEnd(); ++it) {
const QString key = it->first; const QString key = it.key().name;
QString value = it->second; 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.contains(macroName)) if (presetEnv.hasKey(macroName))
return presetEnv.value(macroName); return presetEnv.value(macroName);
return QString("${%1}").arg(macroName); return QString("${%1}").arg(macroName);
}); });
@@ -199,7 +194,7 @@ void expand(const PresetType &preset,
{ {
expandAllButEnv(preset, sourceDirectory, value); expandAllButEnv(preset, sourceDirectory, value);
const QHash<QString, QString> presetEnv = getEnvCombined(preset.environment, env); const Utils::Environment presetEnv = getEnvCombined(preset.environment, env);
value = expandMacroEnv("env", value, [presetEnv](const QString &macroName) { value = expandMacroEnv("env", value, [presetEnv](const QString &macroName) {
return presetEnv.value(macroName); return presetEnv.value(macroName);
}); });

View File

@@ -208,10 +208,10 @@ bool parseConfigurePresets(const QJsonValue &jsonValue,
const QJsonObject environmentObj = object.value("environment").toObject(); const QJsonObject environmentObj = object.value("environment").toObject();
for (const QString &envKey : environmentObj.keys()) { for (const QString &envKey : environmentObj.keys()) {
if (!preset.environment) if (!preset.environment)
preset.environment = QHash<QString, QString>(); preset.environment = Utils::Environment();
QJsonValue envValue = environmentObj.value(envKey); QJsonValue envValue = environmentObj.value(envKey);
preset.environment.value().insert(envKey, envValue.toString()); preset.environment.value().set(envKey, envValue.toString());
} }
const QJsonObject warningsObj = object.value("warnings").toObject(); const QJsonObject warningsObj = object.value("warnings").toObject();
@@ -335,10 +335,10 @@ bool parseBuildPresets(const QJsonValue &jsonValue,
const QJsonObject environmentObj = object.value("environment").toObject(); const QJsonObject environmentObj = object.value("environment").toObject();
for (const QString &envKey : environmentObj.keys()) { for (const QString &envKey : environmentObj.keys()) {
if (!preset.environment) if (!preset.environment)
preset.environment = QHash<QString, QString>(); preset.environment = Utils::Environment();
QJsonValue envValue = environmentObj.value(envKey); QJsonValue envValue = environmentObj.value(envKey);
preset.environment.value().insert(envKey, envValue.toString()); preset.environment.value().set(envKey, envValue.toString());
} }
if (object.contains("configurePreset")) if (object.contains("configurePreset"))
@@ -452,6 +452,16 @@ static QHash<QString, QString> merge(const QHash<QString, QString> &first,
return result; return result;
} }
static Utils::Environment merge(const Utils::Environment &first, const Utils::Environment &second)
{
Utils::Environment result = first;
for (auto it = second.constBegin(); it != second.constEnd(); ++it) {
result.set(it.key().name, it.value().first);
}
return result;
}
static CMakeConfig merge(const CMakeConfig &first, const CMakeConfig &second) static CMakeConfig merge(const CMakeConfig &first, const CMakeConfig &second)
{ {
return Utils::setUnionMerge<CMakeConfig>( return Utils::setUnionMerge<CMakeConfig>(

View File

@@ -5,6 +5,7 @@
#include "cmakeconfigitem.h" #include "cmakeconfigitem.h"
#include <utils/environment.h>
#include <utils/filepath.h> #include <utils/filepath.h>
#include <QHash> #include <QHash>
@@ -103,7 +104,7 @@ public:
std::optional<QString> installDir; std::optional<QString> installDir;
std::optional<QString> cmakeExecutable; std::optional<QString> cmakeExecutable;
std::optional<CMakeConfig> cacheVariables; std::optional<CMakeConfig> cacheVariables;
std::optional<QHash<QString, QString>> environment; std::optional<Utils::Environment> environment;
std::optional<Warnings> warnings; std::optional<Warnings> warnings;
std::optional<Errors> errors; std::optional<Errors> errors;
std::optional<Debug> debug; std::optional<Debug> debug;
@@ -120,7 +121,7 @@ public:
std::optional<QHash<QString, QString>> vendor; std::optional<QHash<QString, QString>> vendor;
std::optional<QString> displayName; std::optional<QString> displayName;
std::optional<QString> description; std::optional<QString> description;
std::optional<QHash<QString, QString>> environment; std::optional<Utils::Environment> environment;
std::optional<QString> configurePreset; std::optional<QString> configurePreset;
std::optional<bool> inheritConfigureEnvironment = true; std::optional<bool> inheritConfigureEnvironment = true;
std::optional<int> jobs; std::optional<int> jobs;

View File

@@ -12,7 +12,7 @@
"generator": "Ninja", "generator": "Ninja",
"installDir": "../inst-${presetName}", "installDir": "../inst-${presetName}",
"cacheVariables": { "cacheVariables": {
"CMAKE_PREFIX_PATH": "$env{SystemDrive}/Qt/6.3.2/mingw_64" "CMAKE_PREFIX_PATH": "$env{SYSTEMDRIVE}/Qt/6.3.2/mingw_64"
}, },
"condition": { "condition": {
"type": "equals", "type": "equals",
@@ -20,7 +20,7 @@
"rhs": "Windows" "rhs": "Windows"
}, },
"environment": { "environment": {
"PATH": "$env{SystemDrive}/Qt/Tools/mingw1120_64/bin;$penv{PATH}" "PATH": "$env{SYSTEMDRIVE}/Qt/Tools/mingw1120_64/bin;$penv{PATH}"
}, },
"debug" : { "debug" : {
"find" : true "find" : true