cmPolicies.cxx 15 KB

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