cmOptionCommand.cxx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "cmOptionCommand.h"
  4. #include <sstream>
  5. #include "cmAlgorithms.h"
  6. #include "cmMakefile.h"
  7. #include "cmPolicies.h"
  8. #include "cmState.h"
  9. #include "cmStateSnapshot.h"
  10. #include "cmStateTypes.h"
  11. #include "cmSystemTools.h"
  12. #include "cmake.h"
  13. class cmExecutionStatus;
  14. // cmOptionCommand
  15. bool cmOptionCommand::InitialPass(std::vector<std::string> const& args,
  16. cmExecutionStatus&)
  17. {
  18. const bool argError = (args.size() < 2) || (args.size() > 3);
  19. if (argError) {
  20. std::string m = "called with incorrect number of arguments: ";
  21. m += cmJoin(args, " ");
  22. this->SetError(m);
  23. return false;
  24. }
  25. // Determine the state of the option policy
  26. bool checkAndWarn = false;
  27. {
  28. auto status = this->Makefile->GetPolicyStatus(cmPolicies::CMP0077);
  29. const auto* existsBeforeSet =
  30. this->Makefile->GetStateSnapshot().GetDefinition(args[0]);
  31. switch (status) {
  32. case cmPolicies::WARN:
  33. checkAndWarn = (existsBeforeSet != nullptr);
  34. break;
  35. case cmPolicies::OLD:
  36. // OLD behavior does not warn.
  37. break;
  38. case cmPolicies::REQUIRED_ALWAYS:
  39. case cmPolicies::REQUIRED_IF_USED:
  40. case cmPolicies::NEW: {
  41. // See if a local variable with this name already exists.
  42. // If so we ignore the option command.
  43. if (existsBeforeSet) {
  44. return true;
  45. }
  46. } break;
  47. }
  48. }
  49. // See if a cache variable with this name already exists
  50. // If so just make sure the doc state is correct
  51. cmState* state = this->Makefile->GetState();
  52. const char* existingValue = state->GetCacheEntryValue(args[0]);
  53. if (existingValue &&
  54. (state->GetCacheEntryType(args[0]) != cmStateEnums::UNINITIALIZED)) {
  55. state->SetCacheEntryProperty(args[0], "HELPSTRING", args[1]);
  56. return true;
  57. }
  58. // Nothing in the cache so add it
  59. std::string initialValue = existingValue ? existingValue : "Off";
  60. if (args.size() == 3) {
  61. initialValue = args[2];
  62. }
  63. bool init = cmSystemTools::IsOn(initialValue.c_str());
  64. this->Makefile->AddCacheDefinition(args[0], init ? "ON" : "OFF",
  65. args[1].c_str(), cmStateEnums::BOOL);
  66. if (checkAndWarn) {
  67. const auto* existsAfterSet =
  68. this->Makefile->GetStateSnapshot().GetDefinition(args[0]);
  69. if (!existsAfterSet) {
  70. std::ostringstream w;
  71. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0077)
  72. << "\n"
  73. "For compatibility with older versions of CMake, option "
  74. "is clearing the normal variable '"
  75. << args[0] << "'.";
  76. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  77. }
  78. }
  79. return true;
  80. }