cmCMakePolicyCommand.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. return true;
  88. }
  89. //----------------------------------------------------------------------------
  90. bool cmCMakePolicyCommand::HandleGetMode(std::vector<std::string> const& args)
  91. {
  92. if(args.size() != 3)
  93. {
  94. this->SetError("GET must be given exactly 2 additional arguments.");
  95. return false;
  96. }
  97. // Get arguments.
  98. std::string const& id = args[1];
  99. std::string const& var = args[2];
  100. // Lookup the policy number.
  101. cmPolicies::PolicyID pid;
  102. if(!this->Makefile->GetPolicies()->GetPolicyID(id.c_str(), pid))
  103. {
  104. std::ostringstream e;
  105. e << "GET given policy \"" << id << "\" which is not known to this "
  106. << "version of CMake.";
  107. this->SetError(e.str());
  108. return false;
  109. }
  110. // Lookup the policy setting.
  111. cmPolicies::PolicyStatus status = this->Makefile->GetPolicyStatus(pid);
  112. switch (status)
  113. {
  114. case cmPolicies::OLD:
  115. // Report that the policy is set to OLD.
  116. this->Makefile->AddDefinition(var, "OLD");
  117. break;
  118. case cmPolicies::WARN:
  119. // Report that the policy is not set.
  120. this->Makefile->AddDefinition(var, "");
  121. break;
  122. case cmPolicies::NEW:
  123. // Report that the policy is set to NEW.
  124. this->Makefile->AddDefinition(var, "NEW");
  125. break;
  126. case cmPolicies::REQUIRED_IF_USED:
  127. case cmPolicies::REQUIRED_ALWAYS:
  128. // The policy is required to be set before anything needs it.
  129. {
  130. std::ostringstream e;
  131. e << this->Makefile->GetPolicies()->GetRequiredPolicyError(pid)
  132. << "\n"
  133. << "The call to cmake_policy(GET " << id << " ...) at which this "
  134. << "error appears requests the policy, and this version of CMake "
  135. << "requires that the policy be set to NEW before it is checked.";
  136. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  137. }
  138. }
  139. return true;
  140. }
  141. //----------------------------------------------------------------------------
  142. bool
  143. cmCMakePolicyCommand::HandleVersionMode(std::vector<std::string> const& args)
  144. {
  145. if(args.size() <= 1)
  146. {
  147. this->SetError("VERSION not given an argument");
  148. return false;
  149. }
  150. else if(args.size() >= 3)
  151. {
  152. this->SetError("VERSION given too many arguments");
  153. return false;
  154. }
  155. this->Makefile->SetPolicyVersion(args[1].c_str());
  156. return true;
  157. }