cmUnsetCommand.cxx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include "cmExecutionStatus.h"
  5. #include "cmMakefile.h"
  6. #include "cmStringAlgorithms.h"
  7. #include "cmSystemTools.h"
  8. // cmUnsetCommand
  9. bool cmUnsetCommand(std::vector<std::string> const& args,
  10. cmExecutionStatus& status)
  11. {
  12. if (args.empty() || args.size() > 2) {
  13. status.SetError("called with incorrect number of arguments");
  14. return false;
  15. }
  16. auto const& variable = args[0];
  17. // unset(ENV{VAR})
  18. if (cmHasLiteralPrefix(variable, "ENV{") && variable.size() > 5) {
  19. // what is the variable name
  20. auto const& envVarName = variable.substr(4, variable.size() - 5);
  21. #ifndef CMAKE_BOOTSTRAP
  22. cmSystemTools::UnsetEnv(envVarName.c_str());
  23. #endif
  24. return true;
  25. }
  26. // unset(CACHE{VAR})
  27. if (cmHasLiteralPrefix(variable, "CACHE{") && variable.size() > 7 &&
  28. cmHasLiteralSuffix(variable, "}")) {
  29. // get the variable name
  30. auto const& varName = variable.substr(6, variable.size() - 7);
  31. status.GetMakefile().RemoveCacheDefinition(varName);
  32. return true;
  33. }
  34. // unset(VAR)
  35. if (args.size() == 1) {
  36. status.GetMakefile().RemoveDefinition(variable);
  37. return true;
  38. }
  39. // unset(VAR CACHE)
  40. if ((args.size() == 2) && (args[1] == "CACHE")) {
  41. status.GetMakefile().RemoveCacheDefinition(variable);
  42. return true;
  43. }
  44. // unset(VAR PARENT_SCOPE)
  45. if ((args.size() == 2) && (args[1] == "PARENT_SCOPE")) {
  46. status.GetMakefile().RaiseScope(variable, nullptr);
  47. return true;
  48. }
  49. // ERROR: second argument isn't CACHE or PARENT_SCOPE
  50. status.SetError("called with an invalid second argument");
  51. return false;
  52. }