cmcmd: Don't modify the current environment

Instead, pass the environment variables to the child process.
This commit is contained in:
Daniel Pfeifer
2026-03-08 22:07:49 +01:00
parent 31040ff609
commit 61072fe317

View File

@@ -17,6 +17,7 @@
#include "cmCommandLineArgument.h" #include "cmCommandLineArgument.h"
#include "cmCryptoHash.h" #include "cmCryptoHash.h"
#include "cmDuration.h" #include "cmDuration.h"
#include "cmEnvironment.h"
#include "cmGeneratedFileStream.h" #include "cmGeneratedFileStream.h"
#include "cmGlobalGenerator.h" #include "cmGlobalGenerator.h"
#include "cmList.h" #include "cmList.h"
@@ -1267,7 +1268,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args,
if (args[1] == "env") { if (args[1] == "env") {
#ifndef CMAKE_BOOTSTRAP #ifndef CMAKE_BOOTSTRAP
cmSystemTools::EnvDiff env; auto envdiff = cmEnvironmentModification{};
#endif #endif
auto ai = args.cbegin() + 2; auto ai = args.cbegin() + 2;
@@ -1285,7 +1286,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args,
#ifdef CMAKE_BOOTSTRAP #ifdef CMAKE_BOOTSTRAP
cmSystemTools::UnPutEnv(a.substr(8)); cmSystemTools::UnPutEnv(a.substr(8));
#else #else
env.UnPutEnv(a.substr(8)); envdiff.Add(a.substr(8) + "=unset:");
#endif #endif
} else if (a == "--modify") { } else if (a == "--modify") {
#ifdef CMAKE_BOOTSTRAP #ifdef CMAKE_BOOTSTRAP
@@ -1298,7 +1299,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args,
return 1; return 1;
} }
std::string const& op = *ai; std::string const& op = *ai;
if (!env.ParseOperation(op)) { if (!envdiff.Add(op)) {
std::cerr << "cmake -E env: invalid parameter to --modify: " << op std::cerr << "cmake -E env: invalid parameter to --modify: " << op
<< '\n'; << '\n';
return 1; return 1;
@@ -1314,7 +1315,10 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args,
#ifdef CMAKE_BOOTSTRAP #ifdef CMAKE_BOOTSTRAP
cmSystemTools::PutEnv(a); cmSystemTools::PutEnv(a);
#else #else
env.PutEnv(a); auto const pos = a.find('=');
std::string const& name = a.substr(0, pos);
std::string const& value = a.substr(pos + 1);
envdiff.Add(cmStrCat(name, "=set:", value));
#endif #endif
} else { } else {
// This is the beginning of the command. // This is the beginning of the command.
@@ -1327,16 +1331,19 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args,
return 1; return 1;
} }
auto env = cmEnvironment{};
#ifndef CMAKE_BOOTSTRAP #ifndef CMAKE_BOOTSTRAP
env.ApplyToCurrentEnv(); env.Update(cmSystemTools::GetEnvironmentVariables());
envdiff.ApplyTo(env);
#endif #endif
// Execute command from remaining arguments. // Execute command from remaining arguments.
std::vector<std::string> cmd(ai, ae); std::vector<std::string> cmd(ai, ae);
int retval; int retval;
if (cmSystemTools::RunSingleCommand(cmd, nullptr, nullptr, &retval, if (cmSystemTools::RunSingleCommand(
nullptr, cmd, nullptr, nullptr, &retval, nullptr,
cmSystemTools::OUTPUT_PASSTHROUGH)) { cmSystemTools::OUTPUT_PASSTHROUGH, cmDuration::zero(),
cmProcessOutput::Auto, env.GetVariables())) {
return retval; return retval;
} }
return 1; return 1;