cmOptionCommand.cxx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "cmProperty.h"
  9. #include "cmState.h"
  10. #include "cmStateSnapshot.h"
  11. #include "cmStateTypes.h"
  12. #include "cmStringAlgorithms.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. 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 = status.GetMakefile().GetState();
  52. cmProp 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].c_str(), cmStateEnums::BOOL);
  66. if (checkAndWarn) {
  67. const auto* existsAfterSet =
  68. status.GetMakefile().GetStateSnapshot().GetDefinition(args[0]);
  69. if (!existsAfterSet) {
  70. status.GetMakefile().IssueMessage(
  71. MessageType::AUTHOR_WARNING,
  72. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0077),
  73. "\n"
  74. "For compatibility with older versions of CMake, option "
  75. "is clearing the normal variable '",
  76. args[0], "'."));
  77. }
  78. }
  79. return true;
  80. }