cmCMakePolicyCommand.cxx 4.5 KB

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