cmCMakePolicyCommand.cxx 5.5 KB

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