cmCMakePolicyCommand.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "cmCMakePolicyCommand.h"
  4. #include "cmVersion.h"
  5. // cmCMakePolicyCommand
  6. bool cmCMakePolicyCommand::InitialPass(std::vector<std::string> const& args,
  7. cmExecutionStatus&)
  8. {
  9. if (args.empty()) {
  10. this->SetError("requires at least one argument.");
  11. return false;
  12. }
  13. if (args[0] == "SET") {
  14. return this->HandleSetMode(args);
  15. }
  16. if (args[0] == "GET") {
  17. return this->HandleGetMode(args);
  18. }
  19. if (args[0] == "PUSH") {
  20. if (args.size() > 1) {
  21. this->SetError("PUSH may not be given additional arguments.");
  22. return false;
  23. }
  24. this->Makefile->PushPolicy();
  25. return true;
  26. }
  27. if (args[0] == "POP") {
  28. if (args.size() > 1) {
  29. this->SetError("POP may not be given additional arguments.");
  30. return false;
  31. }
  32. this->Makefile->PopPolicy();
  33. return true;
  34. }
  35. if (args[0] == "VERSION") {
  36. return this->HandleVersionMode(args);
  37. }
  38. std::ostringstream e;
  39. e << "given unknown first argument \"" << args[0] << "\"";
  40. this->SetError(e.str());
  41. return false;
  42. }
  43. bool cmCMakePolicyCommand::HandleSetMode(std::vector<std::string> const& args)
  44. {
  45. if (args.size() != 3) {
  46. this->SetError("SET must be given exactly 2 additional arguments.");
  47. return false;
  48. }
  49. cmPolicies::PolicyStatus status;
  50. if (args[2] == "OLD") {
  51. status = cmPolicies::OLD;
  52. } else if (args[2] == "NEW") {
  53. status = cmPolicies::NEW;
  54. } else {
  55. std::ostringstream e;
  56. e << "SET given unrecognized policy status \"" << args[2] << "\"";
  57. this->SetError(e.str());
  58. return false;
  59. }
  60. if (!this->Makefile->SetPolicy(args[1].c_str(), status)) {
  61. this->SetError("SET failed to set policy.");
  62. return false;
  63. }
  64. if (args[1] == "CMP0001" &&
  65. (status == cmPolicies::WARN || status == cmPolicies::OLD)) {
  66. if (!(this->Makefile->GetState()->GetInitializedCacheValue(
  67. "CMAKE_BACKWARDS_COMPATIBILITY"))) {
  68. // Set it to 2.4 because that is the last version where the
  69. // variable had meaning.
  70. this->Makefile->AddCacheDefinition(
  71. "CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
  72. "For backwards compatibility, what version of CMake "
  73. "commands and "
  74. "syntax should this version of CMake try to support.",
  75. cmStateEnums::STRING);
  76. }
  77. }
  78. return true;
  79. }
  80. bool cmCMakePolicyCommand::HandleGetMode(std::vector<std::string> const& args)
  81. {
  82. if (args.size() != 3) {
  83. this->SetError("GET must be given exactly 2 additional arguments.");
  84. return false;
  85. }
  86. // Get arguments.
  87. std::string const& id = args[1];
  88. std::string const& var = args[2];
  89. // Lookup the policy number.
  90. cmPolicies::PolicyID pid;
  91. if (!cmPolicies::GetPolicyID(id.c_str(), pid)) {
  92. std::ostringstream e;
  93. e << "GET given policy \"" << id << "\" which is not known to this "
  94. << "version of CMake.";
  95. this->SetError(e.str());
  96. return false;
  97. }
  98. // Lookup the policy setting.
  99. cmPolicies::PolicyStatus status = this->Makefile->GetPolicyStatus(pid);
  100. switch (status) {
  101. case cmPolicies::OLD:
  102. // Report that the policy is set to OLD.
  103. this->Makefile->AddDefinition(var, "OLD");
  104. break;
  105. case cmPolicies::WARN:
  106. // Report that the policy is not set.
  107. this->Makefile->AddDefinition(var, "");
  108. break;
  109. case cmPolicies::NEW:
  110. // Report that the policy is set to NEW.
  111. this->Makefile->AddDefinition(var, "NEW");
  112. break;
  113. case cmPolicies::REQUIRED_IF_USED:
  114. case cmPolicies::REQUIRED_ALWAYS:
  115. // The policy is required to be set before anything needs it.
  116. {
  117. std::ostringstream e;
  118. e << cmPolicies::GetRequiredPolicyError(pid) << "\n"
  119. << "The call to cmake_policy(GET " << id << " ...) at which this "
  120. << "error appears requests the policy, and this version of CMake "
  121. << "requires that the policy be set to NEW before it is checked.";
  122. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  123. }
  124. }
  125. return true;
  126. }
  127. bool cmCMakePolicyCommand::HandleVersionMode(
  128. std::vector<std::string> const& args)
  129. {
  130. if (args.size() <= 1) {
  131. this->SetError("VERSION not given an argument");
  132. return false;
  133. }
  134. if (args.size() >= 3) {
  135. this->SetError("VERSION given too many arguments");
  136. return false;
  137. }
  138. this->Makefile->SetPolicyVersion(args[1].c_str());
  139. return true;
  140. }