From 430ff621aa203bb13242e91a8892c757480a3a64 Mon Sep 17 00:00:00 2001 From: Cristian Adam Date: Tue, 18 Oct 2022 17:56:26 +0200 Subject: [PATCH] CMakePM: Use parent environment for $env{macro} expansion Now the code takes into consideration the parent environment for $env{macro} alongside the "environment" presets values. Task-number: QTCREATORBUG-24555 Change-Id: I644618b0a6f866fd65a4109ee63b1f5bfcd4164d Reviewed-by: Alessandro Portale Reviewed-by: --- .../cmakeprojectmanager/presetsmacros.cpp | 33 +++++++++++++++---- tests/manual/cmakepresets/CMakePresets.json | 6 ++-- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/presetsmacros.cpp b/src/plugins/cmakeprojectmanager/presetsmacros.cpp index 1220444cbfb..1a7b884ca9d 100644 --- a/src/plugins/cmakeprojectmanager/presetsmacros.cpp +++ b/src/plugins/cmakeprojectmanager/presetsmacros.cpp @@ -99,13 +99,33 @@ static QString expandMacroEnv(const QString ¯oPrefix, return result; } +static QHash getEnvCombined( + const std::optional> &optPresetEnv, const Utils::Environment &env) +{ + QHash result; + + for (auto it = env.constBegin(); it != env.constEnd(); ++it) { + if (it.value().second) + result.insert(it.key().name, it.value().first); + } + + if (!optPresetEnv) + return result; + + QHash presetEnv = optPresetEnv.value(); + for (auto it = presetEnv.constKeyValueBegin(); it != presetEnv.constKeyValueEnd(); ++it) { + result[it->first] = it->second; + } + + return result; +} + template void expand(const PresetType &preset, Utils::Environment &env, const Utils::FilePath &sourceDirectory) { - const QHash presetEnv = preset.environment ? preset.environment.value() - : QHash(); + const QHash presetEnv = getEnvCombined(preset.environment, env); for (auto it = presetEnv.constKeyValueBegin(); it != presetEnv.constKeyValueEnd(); ++it) { const QString key = it->first; QString value = it->second; @@ -143,14 +163,15 @@ void expand(const PresetType &preset, { const QHash presetEnv = preset.environment ? preset.environment.value() : QHash(); - for (auto it = presetEnv.constKeyValueBegin(); it != presetEnv.constKeyValueEnd(); ++it) { const QString key = it->first; QString value = it->second; expandAllButEnv(preset, sourceDirectory, value); value = expandMacroEnv("env", value, [presetEnv](const QString ¯oName) { - return presetEnv.value(macroName); + if (presetEnv.contains(macroName)) + return presetEnv.value(macroName); + return QString("${%1}").arg(macroName); }); auto operation = Utils::EnvironmentItem::Operation::SetEnabled; @@ -178,9 +199,7 @@ void expand(const PresetType &preset, { expandAllButEnv(preset, sourceDirectory, value); - const QHash presetEnv = preset.environment ? preset.environment.value() - : QHash(); - + const QHash presetEnv = getEnvCombined(preset.environment, env); value = expandMacroEnv("env", value, [presetEnv](const QString ¯oName) { return presetEnv.value(macroName); }); diff --git a/tests/manual/cmakepresets/CMakePresets.json b/tests/manual/cmakepresets/CMakePresets.json index e4fd4feeae9..8f0ef76f8fc 100644 --- a/tests/manual/cmakepresets/CMakePresets.json +++ b/tests/manual/cmakepresets/CMakePresets.json @@ -75,8 +75,8 @@ "VCToolsVersion": "14.29.30133", "WindowsSDKVersion" : "10.0.22000.0", "VCArch": "x64", - "VCToolsInstallDir": "$penv{ProgramFiles(x86)}/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/$env{VCToolsVersion}", - "WindowsSdkDir" : "$penv{ProgramFiles(x86)}/Windows Kits/10", + "VCToolsInstallDir": "$env{ProgramFiles(x86)}/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/$env{VCToolsVersion}", + "WindowsSdkDir" : "$env{ProgramFiles(x86)}/Windows Kits/10", "WindowsSdkIncVerDir": "$env{WindowsSdkDir}/Include/$env{WindowsSDKVersion}", "WindowsSdkLibVerDir": "$env{WindowsSdkDir}/Lib/$env{WindowsSDKVersion}", @@ -90,7 +90,7 @@ "displayName": "Linux GCC", "generator": "Ninja", "binaryDir": "${sourceDir}/build-${presetName}", - "toolchainFile" : "$penv{HOME}/Qt/6.3.2/gcc_64/lib/cmake/Qt6/qt.toolchain.cmake", + "toolchainFile" : "$env{HOME}/Qt/6.3.2/gcc_64/lib/cmake/Qt6/qt.toolchain.cmake", "condition" : { "type": "equals", "lhs": "${hostSystemName}",