cmOptionCommand.cxx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "cmExecutionStatus.h"
  6. #include "cmMakefile.h"
  7. #include "cmMessageType.h"
  8. #include "cmPolicies.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 = "called with incorrect number of arguments: ";
  20. m += 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. 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 = 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. 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. status.GetMakefile().IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  77. }
  78. }
  79. return true;
  80. }