cmOptionCommand.cxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "cmExecutionStatus.h"
  5. #include "cmMakefile.h"
  6. #include "cmMessageType.h"
  7. #include "cmPolicies.h"
  8. #include "cmState.h"
  9. #include "cmStateSnapshot.h"
  10. #include "cmStateTypes.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmValue.h"
  13. // cmOptionCommand
  14. bool cmOptionCommand(std::vector<std::string> const& args,
  15. cmExecutionStatus& status)
  16. {
  17. const bool argError = (args.size() < 2) || (args.size() > 3);
  18. if (argError) {
  19. std::string m = cmStrCat("called with incorrect number of arguments: ",
  20. cmJoin(args, " "));
  21. status.SetError(m);
  22. return false;
  23. }
  24. // Determine the state of the option policy
  25. bool checkAndWarn = false;
  26. {
  27. auto policyStatus =
  28. status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0077);
  29. const auto& existsBeforeSet =
  30. status.GetMakefile().GetStateSnapshot().GetDefinition(args[0]);
  31. switch (policyStatus) {
  32. case cmPolicies::WARN:
  33. checkAndWarn = (existsBeforeSet != nullptr);
  34. CM_FALLTHROUGH;
  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 = status.GetMakefile().GetState();
  52. cmValue 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 = cmIsOn(initialValue);
  64. status.GetMakefile().AddCacheDefinition(args[0], init ? "ON" : "OFF",
  65. args[1], cmStateEnums::BOOL);
  66. if (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0077) !=
  67. cmPolicies::NEW &&
  68. status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0126) ==
  69. cmPolicies::NEW) {
  70. // if there was a definition then remove it
  71. status.GetMakefile().GetStateSnapshot().RemoveDefinition(args[0]);
  72. }
  73. if (checkAndWarn) {
  74. const auto& existsAfterSet =
  75. status.GetMakefile().GetStateSnapshot().GetDefinition(args[0]);
  76. if (!existsAfterSet) {
  77. status.GetMakefile().IssueMessage(
  78. MessageType::AUTHOR_WARNING,
  79. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0077),
  80. "\n"
  81. "For compatibility with older versions of CMake, option "
  82. "is clearing the normal variable '",
  83. args[0], "'."));
  84. }
  85. }
  86. return true;
  87. }