1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmUnsetCommand.h"
5 #include "cmExecutionStatus.h"
6 #include "cmMakefile.h"
7 #include "cmStringAlgorithms.h"
8 #include "cmSystemTools.h"
11 bool cmUnsetCommand(std::vector<std::string> const& args,
12 cmExecutionStatus& status)
14 if (args.empty() || args.size() > 2) {
15 status.SetError("called with incorrect number of arguments");
19 auto const& variable = args[0];
22 if (cmHasLiteralPrefix(variable, "ENV{") && variable.size() > 5) {
23 // what is the variable name
24 auto const& envVarName = variable.substr(4, variable.size() - 5);
26 #ifndef CMAKE_BOOTSTRAP
27 cmSystemTools::UnsetEnv(envVarName.c_str());
32 if (args.size() == 1) {
33 status.GetMakefile().RemoveDefinition(variable);
37 if ((args.size() == 2) && (args[1] == "CACHE")) {
38 status.GetMakefile().RemoveCacheDefinition(variable);
41 // unset(VAR PARENT_SCOPE)
42 if ((args.size() == 2) && (args[1] == "PARENT_SCOPE")) {
43 status.GetMakefile().RaiseScope(variable, nullptr);
46 // ERROR: second argument isn't CACHE or PARENT_SCOPE
47 status.SetError("called with an invalid second argument");