cmPolicies.cxx 15 KB

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