/src/CMake/Source/cmUnsetCommand.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmUnsetCommand.h" |
4 | | |
5 | | #include "cmExecutionStatus.h" |
6 | | #include "cmMakefile.h" |
7 | | #include "cmStringAlgorithms.h" |
8 | | #include "cmSystemTools.h" |
9 | | |
10 | | // cmUnsetCommand |
11 | | bool cmUnsetCommand(std::vector<std::string> const& args, |
12 | | cmExecutionStatus& status) |
13 | 0 | { |
14 | 0 | if (args.empty() || args.size() > 2) { |
15 | 0 | status.SetError("called with incorrect number of arguments"); |
16 | 0 | return false; |
17 | 0 | } |
18 | | |
19 | 0 | auto const& variable = args[0]; |
20 | | |
21 | | // unset(ENV{VAR}) |
22 | 0 | if (cmHasLiteralPrefix(variable, "ENV{") && variable.size() > 5) { |
23 | | // what is the variable name |
24 | 0 | auto const& envVarName = variable.substr(4, variable.size() - 5); |
25 | |
|
26 | 0 | #ifndef CMAKE_BOOTSTRAP |
27 | 0 | cmSystemTools::UnsetEnv(envVarName.c_str()); |
28 | 0 | #endif |
29 | 0 | return true; |
30 | 0 | } |
31 | | // unset(CACHE{VAR}) |
32 | 0 | if (cmHasLiteralPrefix(variable, "CACHE{") && variable.size() > 7 && |
33 | 0 | cmHasSuffix(variable, '}')) { |
34 | | // get the variable name |
35 | 0 | auto const& varName = variable.substr(6, variable.size() - 7); |
36 | 0 | status.GetMakefile().RemoveCacheDefinition(varName); |
37 | 0 | return true; |
38 | 0 | } |
39 | | // unset(VAR) |
40 | 0 | if (args.size() == 1) { |
41 | 0 | status.GetMakefile().RemoveDefinition(variable); |
42 | 0 | return true; |
43 | 0 | } |
44 | | // unset(VAR CACHE) |
45 | 0 | if ((args.size() == 2) && (args[1] == "CACHE")) { |
46 | 0 | status.GetMakefile().RemoveCacheDefinition(variable); |
47 | 0 | return true; |
48 | 0 | } |
49 | | // unset(VAR PARENT_SCOPE) |
50 | 0 | if ((args.size() == 2) && (args[1] == "PARENT_SCOPE")) { |
51 | 0 | status.GetMakefile().RaiseScope(variable, nullptr); |
52 | 0 | return true; |
53 | 0 | } |
54 | | // ERROR: second argument isn't CACHE or PARENT_SCOPE |
55 | 0 | status.SetError("called with an invalid second argument"); |
56 | 0 | return false; |
57 | 0 | } |