cmUnsetCommand.cxx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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"
  4. #include "cmAlgorithms.h"
  5. #include "cmMakefile.h"
  6. #include "cmSystemTools.h"
  7. class cmExecutionStatus;
  8. // cmUnsetCommand
  9. bool cmUnsetCommand::InitialPass(std::vector<std::string> const& args,
  10. cmExecutionStatus&)
  11. {
  12. if (args.empty() || args.size() > 2) {
  13. this->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. #ifdef CMAKE_BUILD_WITH_CMAKE
  22. cmSystemTools::UnsetEnv(envVarName.c_str());
  23. #endif
  24. return true;
  25. }
  26. // unset(VAR)
  27. if (args.size() == 1) {
  28. this->Makefile->RemoveDefinition(variable);
  29. return true;
  30. }
  31. // unset(VAR CACHE)
  32. if ((args.size() == 2) && (args[1] == "CACHE")) {
  33. this->Makefile->RemoveCacheDefinition(variable);
  34. return true;
  35. }
  36. // unset(VAR PARENT_SCOPE)
  37. if ((args.size() == 2) && (args[1] == "PARENT_SCOPE")) {
  38. this->Makefile->RaiseScope(variable, nullptr);
  39. return true;
  40. }
  41. // ERROR: second argument isn't CACHE or PARENT_SCOPE
  42. this->SetError("called with an invalid second argument");
  43. return false;
  44. }