cmCMakePolicyCommand.cxx 4.8 KB

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