mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-15 20:46:37 +08:00
set: Factor out helper to set environment variables
This commit is contained in:
@@ -21,6 +21,13 @@
|
|||||||
#include "cmSystemTools.h"
|
#include "cmSystemTools.h"
|
||||||
#include "cmValue.h"
|
#include "cmValue.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
void setENV(std::string const& var, cm::string_view val)
|
||||||
|
{
|
||||||
|
cmSystemTools::PutEnv(cmStrCat(var, '=', val));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// cmSetCommand
|
// cmSetCommand
|
||||||
bool cmSetCommand(std::vector<std::string> const& args,
|
bool cmSetCommand(std::vector<std::string> const& args,
|
||||||
cmExecutionStatus& status)
|
cmExecutionStatus& status)
|
||||||
@@ -35,7 +42,6 @@ bool cmSetCommand(std::vector<std::string> const& args,
|
|||||||
if (cmHasLiteralPrefix(variable, "ENV{") && variable.size() > 5) {
|
if (cmHasLiteralPrefix(variable, "ENV{") && variable.size() > 5) {
|
||||||
// what is the variable name
|
// what is the variable name
|
||||||
auto const& varName = variable.substr(4, variable.size() - 5);
|
auto const& varName = variable.substr(4, variable.size() - 5);
|
||||||
std::string putEnvArg = varName + "=";
|
|
||||||
|
|
||||||
// what is the current value if any
|
// what is the current value if any
|
||||||
std::string currValue;
|
std::string currValue;
|
||||||
@@ -45,8 +51,7 @@ bool cmSetCommand(std::vector<std::string> const& args,
|
|||||||
if (args.size() > 1 && !args[1].empty()) {
|
if (args.size() > 1 && !args[1].empty()) {
|
||||||
// but only if it is different from current value
|
// but only if it is different from current value
|
||||||
if (!currValueSet || currValue != args[1]) {
|
if (!currValueSet || currValue != args[1]) {
|
||||||
putEnvArg += args[1];
|
setENV(varName, args[1]);
|
||||||
cmSystemTools::PutEnv(putEnvArg);
|
|
||||||
}
|
}
|
||||||
// if there's extra arguments, warn user
|
// if there's extra arguments, warn user
|
||||||
// that they are ignored by this command.
|
// that they are ignored by this command.
|
||||||
@@ -61,7 +66,7 @@ bool cmSetCommand(std::vector<std::string> const& args,
|
|||||||
|
|
||||||
// if it will be cleared, then clear it if it isn't already clear
|
// if it will be cleared, then clear it if it isn't already clear
|
||||||
if (currValueSet) {
|
if (currValueSet) {
|
||||||
cmSystemTools::PutEnv(putEnvArg);
|
setENV(varName, ""_s);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user