cmCMakePolicyCommand.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCMakePolicyCommand.h"
  14. #include "cmVersion.h"
  15. // cmCMakePolicyCommand
  16. bool cmCMakePolicyCommand
  17. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  18. {
  19. if(args.size() < 1)
  20. {
  21. this->SetError("requires at least one argument.");
  22. return false;
  23. }
  24. if(args[0] == "SET")
  25. {
  26. return this->HandleSetMode(args);
  27. }
  28. else if(args[0] == "GET")
  29. {
  30. return this->HandleGetMode(args);
  31. }
  32. else if(args[0] == "PUSH")
  33. {
  34. if(args.size() > 1)
  35. {
  36. this->SetError("PUSH may not be given additional arguments.");
  37. return false;
  38. }
  39. return this->Makefile->PushPolicy();
  40. }
  41. else if(args[0] == "POP")
  42. {
  43. if(args.size() > 1)
  44. {
  45. this->SetError("POP may not be given additional arguments.");
  46. return false;
  47. }
  48. if(this->Makefile->PopPolicy(false))
  49. {
  50. return true;
  51. }
  52. else
  53. {
  54. this->SetError("POP without matching PUSH");
  55. return false;
  56. }
  57. }
  58. else if(args[0] == "VERSION")
  59. {
  60. return this->HandleVersionMode(args);
  61. }
  62. cmOStringStream e;
  63. e << "given unknown first argument \"" << args[0] << "\"";
  64. this->SetError(e.str().c_str());
  65. return false;
  66. }
  67. //----------------------------------------------------------------------------
  68. bool cmCMakePolicyCommand::HandleSetMode(std::vector<std::string> const& args)
  69. {
  70. if(args.size() != 3)
  71. {
  72. this->SetError("SET must be given exactly 2 additional arguments.");
  73. return false;
  74. }
  75. cmPolicies::PolicyStatus status;
  76. if(args[2] == "OLD")
  77. {
  78. status = cmPolicies::OLD;
  79. }
  80. else if(args[2] == "NEW")
  81. {
  82. status = cmPolicies::NEW;
  83. }
  84. else
  85. {
  86. cmOStringStream e;
  87. e << "SET given unrecognized policy status \"" << args[2] << "\"";
  88. this->SetError(e.str().c_str());
  89. return false;
  90. }
  91. if(!this->Makefile->SetPolicy(args[1].c_str(), status))
  92. {
  93. this->SetError("SET failed to set policy.");
  94. return false;
  95. }
  96. return true;
  97. }
  98. //----------------------------------------------------------------------------
  99. bool cmCMakePolicyCommand::HandleGetMode(std::vector<std::string> const& args)
  100. {
  101. if(args.size() != 3)
  102. {
  103. this->SetError("GET must be given exactly 2 additional arguments.");
  104. return false;
  105. }
  106. // Get arguments.
  107. std::string const& id = args[1];
  108. std::string const& var = args[2];
  109. // Lookup the policy number.
  110. cmPolicies::PolicyID pid;
  111. if(!this->Makefile->GetPolicies()->GetPolicyID(id.c_str(), pid))
  112. {
  113. cmOStringStream e;
  114. e << "GET given policy \"" << id << "\" which is not known to this "
  115. << "version of CMake.";
  116. this->SetError(e.str().c_str());
  117. return false;
  118. }
  119. // Lookup the policy setting.
  120. cmPolicies::PolicyStatus status = this->Makefile->GetPolicyStatus(pid);
  121. switch (status)
  122. {
  123. case cmPolicies::OLD:
  124. // Report that the policy is set to OLD.
  125. this->Makefile->AddDefinition(var.c_str(), "OLD");
  126. break;
  127. case cmPolicies::WARN:
  128. // Report that the policy is not set.
  129. this->Makefile->AddDefinition(var.c_str(), "");
  130. break;
  131. case cmPolicies::NEW:
  132. // Report that the policy is set to NEW.
  133. this->Makefile->AddDefinition(var.c_str(), "NEW");
  134. break;
  135. case cmPolicies::REQUIRED_IF_USED:
  136. case cmPolicies::REQUIRED_ALWAYS:
  137. // The policy is required to be set before anything needs it.
  138. {
  139. cmOStringStream e;
  140. e << this->Makefile->GetPolicies()->GetRequiredPolicyError(pid)
  141. << "\n"
  142. << "The call to cmake_policy(GET " << id << " ...) at which this "
  143. << "error appears requests the policy, and this version of CMake "
  144. << "requires that the policy be set to NEW before it is checked.";
  145. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  146. }
  147. }
  148. return true;
  149. }
  150. //----------------------------------------------------------------------------
  151. bool
  152. cmCMakePolicyCommand::HandleVersionMode(std::vector<std::string> const& args)
  153. {
  154. if(args.size() <= 1)
  155. {
  156. this->SetError("VERSION not given an argument");
  157. return false;
  158. }
  159. else if(args.size() >= 3)
  160. {
  161. this->SetError("VERSION given too many arguments");
  162. return false;
  163. }
  164. this->Makefile->SetPolicyVersion(args[1].c_str());
  165. return true;
  166. }