1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00

Merge topic 'set-ENV-empty'

aa5711490f set: Explicitly unset empty environment variables on Windows
723a83d8cd set: Factor out helper to set environment variables
20761cf349 set: Add test cases for setting ENV{VAR} to empty
a878d1c490 Tests: Unset the PATH environment variable more explicitly where needed

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !11290
This commit is contained in:
Brad King
2025-10-08 15:20:15 +00:00
committed by Kitware Robot
7 changed files with 42 additions and 7 deletions

View File

@@ -21,6 +21,21 @@
#include "cmSystemTools.h"
#include "cmValue.h"
namespace {
void setENV(std::string const& var, cm::string_view val)
{
#ifdef _WIN32
if (val.empty()) {
// FIXME(#27285): On Windows, PutEnv previously treated empty as unset.
// KWSys was fixed, but we need to retain the behavior for compatibility.
cmSystemTools::UnPutEnv(var);
return;
}
#endif
cmSystemTools::PutEnv(cmStrCat(var, '=', val));
}
}
// cmSetCommand
bool cmSetCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
@@ -35,7 +50,6 @@ bool cmSetCommand(std::vector<std::string> const& args,
if (cmHasLiteralPrefix(variable, "ENV{") && variable.size() > 5) {
// what is the variable name
auto const& varName = variable.substr(4, variable.size() - 5);
std::string putEnvArg = varName + "=";
// what is the current value if any
std::string currValue;
@@ -45,8 +59,7 @@ bool cmSetCommand(std::vector<std::string> const& args,
if (args.size() > 1 && !args[1].empty()) {
// but only if it is different from current value
if (!currValueSet || currValue != args[1]) {
putEnvArg += args[1];
cmSystemTools::PutEnv(putEnvArg);
setENV(varName, args[1]);
}
// if there's extra arguments, warn user
// that they are ignored by this command.
@@ -61,7 +74,7 @@ bool cmSetCommand(std::vector<std::string> const& args,
// if it will be cleared, then clear it if it isn't already clear
if (currValueSet) {
cmSystemTools::PutEnv(putEnvArg);
setENV(varName, ""_s);
}
return true;
}

View File

@@ -1,3 +1,3 @@
enable_language(C)
set(ENV{PATH} "")
unset(ENV{PATH})
enable_language(ASM)

View File

@@ -9,8 +9,8 @@ set(ENV{CMAKE_INCLUDE_PATH} )
set(ENV{CMAKE_PREFIX_PATH} )
set(ENV{CMAKE_FRAMEWORK_PATH} )
set(ENV{PATH} )
set(ENV{INCLUDE} )
unset(ENV{PATH})
unset(ENV{INCLUDE})
set(CMAKE_SYSTEM_INCLUDE_PATH )
set(CMAKE_SYSTEM_PREFIX_PATH )

View File

@@ -0,0 +1,2 @@
^-- ENV{UNSET_THEN_EMPTY} is not defined
-- ENV{SET_THEN_EMPTY} is not defined$

View File

@@ -0,0 +1,2 @@
^-- ENV{UNSET_THEN_EMPTY} is not defined
-- ENV{SET_THEN_EMPTY} is defined to ''$

View File

@@ -0,0 +1,15 @@
unset(ENV{UNSET_THEN_EMPTY})
set(ENV{UNSET_THEN_EMPTY} "")
if(DEFINED ENV{UNSET_THEN_EMPTY})
message(STATUS "ENV{UNSET_THEN_EMPTY} is defined to '$ENV{UNSET_THEN_EMPTY}'")
else()
message(STATUS "ENV{UNSET_THEN_EMPTY} is not defined")
endif()
set(ENV{SET_THEN_EMPTY} "set")
set(ENV{SET_THEN_EMPTY} "")
if(DEFINED ENV{SET_THEN_EMPTY})
message(STATUS "ENV{SET_THEN_EMPTY} is defined to '$ENV{SET_THEN_EMPTY}'")
else()
message(STATUS "ENV{SET_THEN_EMPTY} is not defined")
endif()

View File

@@ -12,3 +12,6 @@ run_cmake(CacheUnknownArguments)
run_cmake(CacheMissingArguments)
run_cmake(CacheWrongTYPE)
run_cmake(CacheSetUnset)
# set(ENV{}) syntax
run_cmake_script(Env)