cmPolicies.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "cmPolicies.h"
  2. #include "cmAlgorithms.h"
  3. #include "cmMakefile.h"
  4. #include "cmState.h"
  5. #include "cmStateTypes.h"
  6. #include "cmSystemTools.h"
  7. #include "cmVersion.h"
  8. #include "cmake.h"
  9. #include <assert.h>
  10. #include <ctype.h>
  11. #include <sstream>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <vector>
  15. static bool stringToId(const char* input, cmPolicies::PolicyID& pid)
  16. {
  17. assert(input);
  18. if (strlen(input) != 7) {
  19. return false;
  20. }
  21. if (!cmHasLiteralPrefix(input, "CMP")) {
  22. return false;
  23. }
  24. if (cmHasLiteralSuffix(input, "0000")) {
  25. pid = cmPolicies::CMP0000;
  26. return true;
  27. }
  28. for (int i = 3; i < 7; ++i) {
  29. if (!isdigit(*(input + i))) {
  30. return false;
  31. }
  32. }
  33. long id;
  34. if (!cmSystemTools::StringToLong(input + 3, &id)) {
  35. return false;
  36. }
  37. if (id >= cmPolicies::CMPCOUNT) {
  38. return false;
  39. }
  40. pid = cmPolicies::PolicyID(id);
  41. return true;
  42. }
  43. #define CM_SELECT_ID_VERSION(F, A1, A2, A3, A4, A5, A6) F(A1, A3, A4, A5)
  44. #define CM_FOR_EACH_POLICY_ID_VERSION(POLICY) \
  45. CM_FOR_EACH_POLICY_TABLE(POLICY, CM_SELECT_ID_VERSION)
  46. #define CM_SELECT_ID_DOC(F, A1, A2, A3, A4, A5, A6) F(A1, A2)
  47. #define CM_FOR_EACH_POLICY_ID_DOC(POLICY) \
  48. CM_FOR_EACH_POLICY_TABLE(POLICY, CM_SELECT_ID_DOC)
  49. static const char* idToString(cmPolicies::PolicyID id)
  50. {
  51. switch (id) {
  52. #define POLICY_CASE(ID) \
  53. case cmPolicies::ID: \
  54. return #ID;
  55. CM_FOR_EACH_POLICY_ID(POLICY_CASE)
  56. #undef POLICY_CASE
  57. case cmPolicies::CMPCOUNT:
  58. return nullptr;
  59. }
  60. return nullptr;
  61. }
  62. static const char* idToVersion(cmPolicies::PolicyID id)
  63. {
  64. switch (id) {
  65. #define POLICY_CASE(ID, V_MAJOR, V_MINOR, V_PATCH) \
  66. case cmPolicies::ID: \
  67. return #V_MAJOR "." #V_MINOR "." #V_PATCH;
  68. CM_FOR_EACH_POLICY_ID_VERSION(POLICY_CASE)
  69. #undef POLICY_CASE
  70. case cmPolicies::CMPCOUNT:
  71. return nullptr;
  72. }
  73. return nullptr;
  74. }
  75. static bool isPolicyNewerThan(cmPolicies::PolicyID id, unsigned int majorV,
  76. unsigned int minorV, unsigned int patchV)
  77. {
  78. switch (id) {
  79. #define POLICY_CASE(ID, V_MAJOR, V_MINOR, V_PATCH) \
  80. case cmPolicies::ID: \
  81. return (majorV < (V_MAJOR) || \
  82. (majorV == (V_MAJOR) && minorV + 1 < (V_MINOR) + 1) || \
  83. (majorV == (V_MAJOR) && minorV == (V_MINOR) && \
  84. patchV + 1 < (V_PATCH) + 1));
  85. CM_FOR_EACH_POLICY_ID_VERSION(POLICY_CASE)
  86. #undef POLICY_CASE
  87. case cmPolicies::CMPCOUNT:
  88. return false;
  89. }
  90. return false;
  91. }
  92. const char* idToShortDescription(cmPolicies::PolicyID id)
  93. {
  94. switch (id) {
  95. #define POLICY_CASE(ID, SHORT_DESCRIPTION) \
  96. case cmPolicies::ID: \
  97. return SHORT_DESCRIPTION;
  98. CM_FOR_EACH_POLICY_ID_DOC(POLICY_CASE)
  99. #undef POLICY_CASE
  100. case cmPolicies::CMPCOUNT:
  101. return nullptr;
  102. }
  103. return nullptr;
  104. }
  105. static void DiagnoseAncientPolicies(
  106. std::vector<cmPolicies::PolicyID> const& ancient, unsigned int majorVer,
  107. unsigned int minorVer, unsigned int patchVer, cmMakefile* mf)
  108. {
  109. std::ostringstream e;
  110. e << "The project requests behavior compatible with CMake version \""
  111. << majorVer << "." << minorVer << "." << patchVer
  112. << "\", which requires the OLD behavior for some policies:\n";
  113. for (cmPolicies::PolicyID i : ancient) {
  114. e << " " << idToString(i) << ": " << idToShortDescription(i) << "\n";
  115. }
  116. e << "However, this version of CMake no longer supports the OLD "
  117. << "behavior for these policies. "
  118. << "Please either update your CMakeLists.txt files to conform to "
  119. << "the new behavior or use an older version of CMake that still "
  120. << "supports the old behavior.";
  121. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  122. }
  123. static bool GetPolicyDefault(cmMakefile* mf, std::string const& policy,
  124. cmPolicies::PolicyStatus* defaultSetting)
  125. {
  126. std::string defaultVar = "CMAKE_POLICY_DEFAULT_" + policy;
  127. std::string defaultValue = mf->GetSafeDefinition(defaultVar);
  128. if (defaultValue == "NEW") {
  129. *defaultSetting = cmPolicies::NEW;
  130. } else if (defaultValue == "OLD") {
  131. *defaultSetting = cmPolicies::OLD;
  132. } else if (defaultValue.empty()) {
  133. *defaultSetting = cmPolicies::WARN;
  134. } else {
  135. std::ostringstream e;
  136. e << defaultVar << " has value \"" << defaultValue
  137. << "\" but must be \"OLD\", \"NEW\", or \"\" (empty).";
  138. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  139. return false;
  140. }
  141. return true;
  142. }
  143. bool cmPolicies::ApplyPolicyVersion(cmMakefile* mf, const char* version)
  144. {
  145. std::string ver = "2.4.0";
  146. if (version && strlen(version) > 0) {
  147. ver = version;
  148. }
  149. unsigned int majorVer = 2;
  150. unsigned int minorVer = 0;
  151. unsigned int patchVer = 0;
  152. unsigned int tweakVer = 0;
  153. // parse the string
  154. if (sscanf(ver.c_str(), "%u.%u.%u.%u", &majorVer, &minorVer, &patchVer,
  155. &tweakVer) < 2) {
  156. std::ostringstream e;
  157. e << "Invalid policy version value \"" << ver << "\". "
  158. << "A numeric major.minor[.patch[.tweak]] must be given.";
  159. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  160. return false;
  161. }
  162. // it is an error if the policy version is less than 2.4
  163. if (majorVer < 2 || (majorVer == 2 && minorVer < 4)) {
  164. mf->IssueMessage(
  165. cmake::FATAL_ERROR,
  166. "Compatibility with CMake < 2.4 is not supported by CMake >= 3.0. "
  167. "For compatibility with older versions please use any CMake 2.8.x "
  168. "release or lower.");
  169. return false;
  170. }
  171. // It is an error if the policy version is greater than the running
  172. // CMake.
  173. if (majorVer > cmVersion::GetMajorVersion() ||
  174. (majorVer == cmVersion::GetMajorVersion() &&
  175. minorVer > cmVersion::GetMinorVersion()) ||
  176. (majorVer == cmVersion::GetMajorVersion() &&
  177. minorVer == cmVersion::GetMinorVersion() &&
  178. patchVer > cmVersion::GetPatchVersion()) ||
  179. (majorVer == cmVersion::GetMajorVersion() &&
  180. minorVer == cmVersion::GetMinorVersion() &&
  181. patchVer == cmVersion::GetPatchVersion() &&
  182. tweakVer > cmVersion::GetTweakVersion())) {
  183. std::ostringstream e;
  184. e << "An attempt was made to set the policy version of CMake to \""
  185. << version << "\" which is greater than this version of CMake. "
  186. << "This is not allowed because the greater version may have new "
  187. << "policies not known to this CMake. "
  188. << "You may need a newer CMake version to build this project.";
  189. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  190. return false;
  191. }
  192. // now loop over all the policies and set them as appropriate
  193. std::vector<cmPolicies::PolicyID> ancientPolicies;
  194. for (PolicyID pid = cmPolicies::CMP0000; pid != cmPolicies::CMPCOUNT;
  195. pid = PolicyID(pid + 1)) {
  196. if (isPolicyNewerThan(pid, majorVer, minorVer, patchVer)) {
  197. if (cmPolicies::GetPolicyStatus(pid) == cmPolicies::REQUIRED_ALWAYS) {
  198. ancientPolicies.push_back(pid);
  199. } else {
  200. cmPolicies::PolicyStatus status = cmPolicies::WARN;
  201. if (!GetPolicyDefault(mf, idToString(pid), &status) ||
  202. !mf->SetPolicy(pid, status)) {
  203. return false;
  204. }
  205. if (pid == cmPolicies::CMP0001 &&
  206. (status == cmPolicies::WARN || status == cmPolicies::OLD)) {
  207. if (!(mf->GetState()->GetInitializedCacheValue(
  208. "CMAKE_BACKWARDS_COMPATIBILITY"))) {
  209. // Set it to 2.4 because that is the last version where the
  210. // variable had meaning.
  211. mf->AddCacheDefinition(
  212. "CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
  213. "For backwards compatibility, what version of CMake "
  214. "commands and "
  215. "syntax should this version of CMake try to support.",
  216. cmStateEnums::STRING);
  217. }
  218. }
  219. }
  220. } else {
  221. if (!mf->SetPolicy(pid, cmPolicies::NEW)) {
  222. return false;
  223. }
  224. }
  225. }
  226. // Make sure the project does not use any ancient policies.
  227. if (!ancientPolicies.empty()) {
  228. DiagnoseAncientPolicies(ancientPolicies, majorVer, minorVer, patchVer, mf);
  229. cmSystemTools::SetFatalErrorOccured();
  230. return false;
  231. }
  232. return true;
  233. }
  234. bool cmPolicies::GetPolicyID(const char* id, cmPolicies::PolicyID& pid)
  235. {
  236. return stringToId(id, pid);
  237. }
  238. ///! return a warning string for a given policy
  239. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  240. {
  241. std::ostringstream msg;
  242. msg << "Policy " << idToString(id) << " is not set: "
  243. ""
  244. << idToShortDescription(id) << " "
  245. "Run \"cmake --help-policy "
  246. << idToString(id) << "\" for "
  247. "policy details. "
  248. "Use the cmake_policy command to set the policy "
  249. "and suppress this warning.";
  250. return msg.str();
  251. }
  252. std::string cmPolicies::GetPolicyDeprecatedWarning(cmPolicies::PolicyID id)
  253. {
  254. std::ostringstream msg;
  255. /* clang-format off */
  256. msg <<
  257. "The OLD behavior for policy " << idToString(id) << " "
  258. "will be removed from a future version of CMake.\n"
  259. "The cmake-policies(7) manual explains that the OLD behaviors of all "
  260. "policies are deprecated and that a policy should be set to OLD only "
  261. "under specific short-term circumstances. Projects should be ported "
  262. "to the NEW behavior and not rely on setting a policy to OLD."
  263. ;
  264. /* clang-format on */
  265. return msg.str();
  266. }
  267. ///! return an error string for when a required policy is unspecified
  268. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  269. {
  270. std::ostringstream error;
  271. error << "Policy " << idToString(id) << " is not set to NEW: "
  272. ""
  273. << idToShortDescription(id) << " "
  274. "Run \"cmake --help-policy "
  275. << idToString(id)
  276. << "\" for "
  277. "policy details. "
  278. "CMake now requires this policy to be set to NEW by the project. "
  279. "The policy may be set explicitly using the code\n"
  280. " cmake_policy(SET "
  281. << idToString(id) << " NEW)\n"
  282. "or by upgrading all policies with the code\n"
  283. " cmake_policy(VERSION "
  284. << idToVersion(id)
  285. << ") # or later\n"
  286. "Run \"cmake --help-command cmake_policy\" for more information.";
  287. return error.str();
  288. }
  289. ///! Get the default status for a policy
  290. cmPolicies::PolicyStatus cmPolicies::GetPolicyStatus(
  291. cmPolicies::PolicyID /*unused*/)
  292. {
  293. return cmPolicies::WARN;
  294. }
  295. std::string cmPolicies::GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id)
  296. {
  297. std::string pid = idToString(id);
  298. std::ostringstream e;
  299. e << "Policy " << pid << " may not be set to OLD behavior because this "
  300. << "version of CMake no longer supports it. "
  301. << "The policy was introduced in "
  302. << "CMake version " << idToVersion(id)
  303. << ", and use of NEW behavior is now required."
  304. << "\n"
  305. << "Please either update your CMakeLists.txt files to conform to "
  306. << "the new behavior or use an older version of CMake that still "
  307. << "supports the old behavior. "
  308. << "Run cmake --help-policy " << pid << " for more information.";
  309. return e.str();
  310. }
  311. cmPolicies::PolicyStatus cmPolicies::PolicyMap::Get(
  312. cmPolicies::PolicyID id) const
  313. {
  314. PolicyStatus status = cmPolicies::WARN;
  315. if (this->Status[(POLICY_STATUS_COUNT * id) + OLD]) {
  316. status = cmPolicies::OLD;
  317. } else if (this->Status[(POLICY_STATUS_COUNT * id) + NEW]) {
  318. status = cmPolicies::NEW;
  319. }
  320. return status;
  321. }
  322. void cmPolicies::PolicyMap::Set(cmPolicies::PolicyID id,
  323. cmPolicies::PolicyStatus status)
  324. {
  325. this->Status[(POLICY_STATUS_COUNT * id) + OLD] = (status == OLD);
  326. this->Status[(POLICY_STATUS_COUNT * id) + WARN] = (status == WARN);
  327. this->Status[(POLICY_STATUS_COUNT * id) + NEW] = (status == NEW);
  328. }
  329. bool cmPolicies::PolicyMap::IsDefined(cmPolicies::PolicyID id) const
  330. {
  331. return this->Status[(POLICY_STATUS_COUNT * id) + OLD] ||
  332. this->Status[(POLICY_STATUS_COUNT * id) + WARN] ||
  333. this->Status[(POLICY_STATUS_COUNT * id) + NEW];
  334. }
  335. bool cmPolicies::PolicyMap::IsEmpty() const
  336. {
  337. return this->Status.none();
  338. }