cmCMakePolicyCommand.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. this->Makefile->PushPolicy();
  40. return true;
  41. }
  42. else if(args[0] == "POP")
  43. {
  44. if(args.size() > 1)
  45. {
  46. this->SetError("POP may not be given additional arguments.");
  47. return false;
  48. }
  49. this->Makefile->PopPolicy();
  50. return true;
  51. }
  52. else if(args[0] == "VERSION")
  53. {
  54. return this->HandleVersionMode(args);
  55. }
  56. cmOStringStream e;
  57. e << "given unknown first argument \"" << args[0] << "\"";
  58. this->SetError(e.str().c_str());
  59. return false;
  60. }
  61. //----------------------------------------------------------------------------
  62. bool cmCMakePolicyCommand::HandleSetMode(std::vector<std::string> const& args)
  63. {
  64. if(args.size() != 3)
  65. {
  66. this->SetError("SET must be given exactly 2 additional arguments.");
  67. return false;
  68. }
  69. cmPolicies::PolicyStatus status;
  70. if(args[2] == "OLD")
  71. {
  72. status = cmPolicies::OLD;
  73. }
  74. else if(args[2] == "NEW")
  75. {
  76. status = cmPolicies::NEW;
  77. }
  78. else
  79. {
  80. cmOStringStream e;
  81. e << "SET given unrecognized policy status \"" << args[2] << "\"";
  82. this->SetError(e.str().c_str());
  83. return false;
  84. }
  85. if(!this->Makefile->SetPolicy(args[1].c_str(), status))
  86. {
  87. this->SetError("SET failed to set policy.");
  88. return false;
  89. }
  90. return true;
  91. }
  92. //----------------------------------------------------------------------------
  93. bool cmCMakePolicyCommand::HandleGetMode(std::vector<std::string> const& args)
  94. {
  95. if(args.size() != 3)
  96. {
  97. this->SetError("GET must be given exactly 2 additional arguments.");
  98. return false;
  99. }
  100. // Get arguments.
  101. std::string const& id = args[1];
  102. std::string const& var = args[2];
  103. // Lookup the policy number.
  104. cmPolicies::PolicyID pid;
  105. if(!this->Makefile->GetPolicies()->GetPolicyID(id.c_str(), pid))
  106. {
  107. cmOStringStream e;
  108. e << "GET given policy \"" << id << "\" which is not known to this "
  109. << "version of CMake.";
  110. this->SetError(e.str().c_str());
  111. return false;
  112. }
  113. // Lookup the policy setting.
  114. cmPolicies::PolicyStatus status = this->Makefile->GetPolicyStatus(pid);
  115. switch (status)
  116. {
  117. case cmPolicies::OLD:
  118. // Report that the policy is set to OLD.
  119. this->Makefile->AddDefinition(var.c_str(), "OLD");
  120. break;
  121. case cmPolicies::WARN:
  122. // Report that the policy is not set.
  123. this->Makefile->AddDefinition(var.c_str(), "");
  124. break;
  125. case cmPolicies::NEW:
  126. // Report that the policy is set to NEW.
  127. this->Makefile->AddDefinition(var.c_str(), "NEW");
  128. break;
  129. case cmPolicies::REQUIRED_IF_USED:
  130. case cmPolicies::REQUIRED_ALWAYS:
  131. // The policy is required to be set before anything needs it.
  132. {
  133. cmOStringStream e;
  134. e << this->Makefile->GetPolicies()->GetRequiredPolicyError(pid)
  135. << "\n"
  136. << "The call to cmake_policy(GET " << id << " ...) at which this "
  137. << "error appears requests the policy, and this version of CMake "
  138. << "requires that the policy be set to NEW before it is checked.";
  139. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  140. }
  141. }
  142. return true;
  143. }
  144. //----------------------------------------------------------------------------
  145. bool
  146. cmCMakePolicyCommand::HandleVersionMode(std::vector<std::string> const& args)
  147. {
  148. if(args.size() <= 1)
  149. {
  150. this->SetError("VERSION not given an argument");
  151. return false;
  152. }
  153. else if(args.size() >= 3)
  154. {
  155. this->SetError("VERSION given too many arguments");
  156. return false;
  157. }
  158. this->Makefile->SetPolicyVersion(args[1].c_str());
  159. return true;
  160. }