cmCMakePolicyCommand.cxx 5.1 KB

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