cmCMakePolicyCommand.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCMakePolicyCommand.h"
  4. #include "cmExecutionStatus.h"
  5. #include "cmMakefile.h"
  6. #include "cmPolicies.h"
  7. #include "cmState.h"
  8. #include "cmStateTypes.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmValue.h"
  11. namespace {
  12. bool HandleSetMode(std::vector<std::string> const& args,
  13. cmExecutionStatus& status);
  14. bool HandleGetMode(std::vector<std::string> const& args,
  15. cmExecutionStatus& status);
  16. bool HandleVersionMode(std::vector<std::string> const& args,
  17. cmExecutionStatus& status);
  18. bool HandleGetWarningMode(std::vector<std::string> const& args,
  19. cmExecutionStatus& status);
  20. }
  21. // cmCMakePolicyCommand
  22. bool cmCMakePolicyCommand(std::vector<std::string> const& args,
  23. cmExecutionStatus& status)
  24. {
  25. if (args.empty()) {
  26. status.SetError("requires at least one argument.");
  27. return false;
  28. }
  29. if (args[0] == "SET") {
  30. return HandleSetMode(args, status);
  31. }
  32. if (args[0] == "GET") {
  33. return HandleGetMode(args, status);
  34. }
  35. if (args[0] == "PUSH") {
  36. if (args.size() > 1) {
  37. status.SetError("PUSH may not be given additional arguments.");
  38. return false;
  39. }
  40. status.GetMakefile().PushPolicy();
  41. return true;
  42. }
  43. if (args[0] == "POP") {
  44. if (args.size() > 1) {
  45. status.SetError("POP may not be given additional arguments.");
  46. return false;
  47. }
  48. status.GetMakefile().PopPolicy();
  49. return true;
  50. }
  51. if (args[0] == "VERSION") {
  52. return HandleVersionMode(args, status);
  53. }
  54. if (args[0] == "GET_WARNING") {
  55. return HandleGetWarningMode(args, status);
  56. }
  57. status.SetError(cmStrCat("given unknown first argument \"", args[0], "\""));
  58. return false;
  59. }
  60. namespace {
  61. bool HandleSetMode(std::vector<std::string> const& args,
  62. cmExecutionStatus& status)
  63. {
  64. if (args.size() != 3) {
  65. status.SetError("SET must be given exactly 2 additional arguments.");
  66. return false;
  67. }
  68. cmPolicies::PolicyStatus policyStatus;
  69. if (args[2] == "OLD") {
  70. policyStatus = cmPolicies::OLD;
  71. } else if (args[2] == "NEW") {
  72. policyStatus = cmPolicies::NEW;
  73. } else {
  74. status.SetError(
  75. cmStrCat("SET given unrecognized policy status \"", args[2], "\""));
  76. return false;
  77. }
  78. if (!status.GetMakefile().SetPolicy(args[1].c_str(), policyStatus)) {
  79. status.SetError("SET failed to set policy.");
  80. return false;
  81. }
  82. if (args[1] == "CMP0001" &&
  83. (policyStatus == cmPolicies::WARN || policyStatus == cmPolicies::OLD)) {
  84. if (!(status.GetMakefile().GetState()->GetInitializedCacheValue(
  85. "CMAKE_BACKWARDS_COMPATIBILITY"))) {
  86. // Set it to 2.4 because that is the last version where the
  87. // variable had meaning.
  88. status.GetMakefile().AddCacheDefinition(
  89. "CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
  90. "For backwards compatibility, what version of CMake "
  91. "commands and "
  92. "syntax should this version of CMake try to support.",
  93. cmStateEnums::STRING);
  94. }
  95. }
  96. return true;
  97. }
  98. bool HandleGetMode(std::vector<std::string> const& args,
  99. cmExecutionStatus& status)
  100. {
  101. bool parent_scope = false;
  102. if (args.size() == 4 && args[3] == "PARENT_SCOPE") {
  103. // Undocumented PARENT_SCOPE option for use within CMake.
  104. parent_scope = true;
  105. } else if (args.size() != 3) {
  106. status.SetError("GET must be given exactly 2 additional arguments.");
  107. return false;
  108. }
  109. // Get arguments.
  110. std::string const& id = args[1];
  111. std::string const& var = args[2];
  112. // Lookup the policy number.
  113. cmPolicies::PolicyID pid;
  114. if (!cmPolicies::GetPolicyID(id.c_str(), pid)) {
  115. status.SetError(
  116. cmStrCat("GET given policy \"", id,
  117. "\" which is not known to this version of CMake."));
  118. return false;
  119. }
  120. // Lookup the policy setting.
  121. cmPolicies::PolicyStatus policyStatus =
  122. status.GetMakefile().GetPolicyStatus(pid, parent_scope);
  123. switch (policyStatus) {
  124. case cmPolicies::OLD:
  125. // Report that the policy is set to OLD.
  126. status.GetMakefile().AddDefinition(var, "OLD");
  127. break;
  128. case cmPolicies::WARN:
  129. // Report that the policy is not set.
  130. status.GetMakefile().AddDefinition(var, "");
  131. break;
  132. case cmPolicies::NEW:
  133. // Report that the policy is set to NEW.
  134. status.GetMakefile().AddDefinition(var, "NEW");
  135. break;
  136. }
  137. return true;
  138. }
  139. bool HandleVersionMode(std::vector<std::string> const& args,
  140. cmExecutionStatus& status)
  141. {
  142. if (args.size() <= 1) {
  143. status.SetError("VERSION not given an argument");
  144. return false;
  145. }
  146. if (args.size() >= 3) {
  147. status.SetError("VERSION given too many arguments");
  148. return false;
  149. }
  150. std::string const& version_string = args[1];
  151. // Separate the <min> version and any trailing ...<max> component.
  152. std::string::size_type const dd = version_string.find("...");
  153. std::string const version_min = version_string.substr(0, dd);
  154. std::string const version_max = dd != std::string::npos
  155. ? version_string.substr(dd + 3, std::string::npos)
  156. : std::string();
  157. if (dd != std::string::npos &&
  158. (version_min.empty() || version_max.empty())) {
  159. status.SetError(
  160. cmStrCat("VERSION \"", version_string,
  161. R"(" does not have a version on both sides of "...".)"));
  162. return false;
  163. }
  164. return status.GetMakefile().SetPolicyVersion(version_min, version_max);
  165. }
  166. bool HandleGetWarningMode(std::vector<std::string> const& args,
  167. cmExecutionStatus& status)
  168. {
  169. if (args.size() != 3) {
  170. status.SetError(
  171. "GET_WARNING must be given exactly 2 additional arguments.");
  172. return false;
  173. }
  174. // Get arguments.
  175. std::string const& id = args[1];
  176. std::string const& var = args[2];
  177. // Lookup the policy number.
  178. cmPolicies::PolicyID pid;
  179. if (!cmPolicies::GetPolicyID(id.c_str(), pid)) {
  180. status.SetError(
  181. cmStrCat("GET_WARNING given policy \"", id,
  182. "\" which is not known to this version of CMake."));
  183. return false;
  184. }
  185. // Lookup the policy warning.
  186. status.GetMakefile().AddDefinition(var, cmPolicies::GetPolicyWarning(pid));
  187. return true;
  188. }
  189. }