cmUnsetCommand.cxx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 "cmSystemTools.h"
  5. // cmUnsetCommand
  6. bool cmUnsetCommand::InitialPass(std::vector<std::string> const& args,
  7. cmExecutionStatus&)
  8. {
  9. if (args.empty() || args.size() > 2) {
  10. this->SetError("called with incorrect number of arguments");
  11. return false;
  12. }
  13. const char* variable = args[0].c_str();
  14. // unset(ENV{VAR})
  15. if (cmHasLiteralPrefix(variable, "ENV{") && strlen(variable) > 5) {
  16. // what is the variable name
  17. char* envVarName = new char[strlen(variable)];
  18. strncpy(envVarName, variable + 4, strlen(variable) - 5);
  19. envVarName[strlen(variable) - 5] = '\0';
  20. #ifdef CMAKE_BUILD_WITH_CMAKE
  21. cmSystemTools::UnsetEnv(envVarName);
  22. #endif
  23. delete[] envVarName;
  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, CM_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. }