cmPolicies.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include "cmPolicies.h"
  2. #include <cassert>
  3. #include <cctype>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <sstream>
  7. #include <vector>
  8. #include "cmMakefile.h"
  9. #include "cmMessageType.h"
  10. #include "cmState.h"
  11. #include "cmStateTypes.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. #include "cmVersion.h"
  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 (!cmStrToLong(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. // NOLINTNEXTLINE(bugprone-branch-clone)
  69. CM_FOR_EACH_POLICY_ID_VERSION(POLICY_CASE)
  70. #undef POLICY_CASE
  71. case cmPolicies::CMPCOUNT:
  72. return nullptr;
  73. }
  74. return nullptr;
  75. }
  76. static bool isPolicyNewerThan(cmPolicies::PolicyID id, unsigned int majorV,
  77. unsigned int minorV, unsigned int patchV)
  78. {
  79. switch (id) {
  80. #define POLICY_CASE(ID, V_MAJOR, V_MINOR, V_PATCH) \
  81. case cmPolicies::ID: \
  82. return (majorV < (V_MAJOR) || \
  83. (majorV == (V_MAJOR) && minorV + 1 < (V_MINOR) + 1) || \
  84. (majorV == (V_MAJOR) && minorV == (V_MINOR) && \
  85. patchV + 1 < (V_PATCH) + 1));
  86. // NOLINTNEXTLINE(bugprone-branch-clone)
  87. CM_FOR_EACH_POLICY_ID_VERSION(POLICY_CASE)
  88. #undef POLICY_CASE
  89. case cmPolicies::CMPCOUNT:
  90. return false;
  91. }
  92. return false;
  93. }
  94. const char* idToShortDescription(cmPolicies::PolicyID id)
  95. {
  96. switch (id) {
  97. #define POLICY_CASE(ID, SHORT_DESCRIPTION) \
  98. case cmPolicies::ID: \
  99. return SHORT_DESCRIPTION;
  100. CM_FOR_EACH_POLICY_ID_DOC(POLICY_CASE)
  101. #undef POLICY_CASE
  102. case cmPolicies::CMPCOUNT:
  103. return nullptr;
  104. }
  105. return nullptr;
  106. }
  107. static void DiagnoseAncientPolicies(
  108. std::vector<cmPolicies::PolicyID> const& ancient, unsigned int majorVer,
  109. unsigned int minorVer, unsigned int patchVer, cmMakefile* mf)
  110. {
  111. std::ostringstream e;
  112. e << "The project requests behavior compatible with CMake version \""
  113. << majorVer << "." << minorVer << "." << patchVer
  114. << "\", which requires the OLD behavior for some policies:\n";
  115. for (cmPolicies::PolicyID i : ancient) {
  116. e << " " << idToString(i) << ": " << idToShortDescription(i) << "\n";
  117. }
  118. e << "However, this version of CMake no longer supports the OLD "
  119. << "behavior for these policies. "
  120. << "Please either update your CMakeLists.txt files to conform to "
  121. << "the new behavior or use an older version of CMake that still "
  122. << "supports the old behavior.";
  123. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  124. }
  125. static bool GetPolicyDefault(cmMakefile* mf, std::string const& policy,
  126. cmPolicies::PolicyStatus* defaultSetting)
  127. {
  128. std::string defaultVar = "CMAKE_POLICY_DEFAULT_" + policy;
  129. std::string const& defaultValue = mf->GetSafeDefinition(defaultVar);
  130. if (defaultValue == "NEW") {
  131. *defaultSetting = cmPolicies::NEW;
  132. } else if (defaultValue == "OLD") {
  133. *defaultSetting = cmPolicies::OLD;
  134. } else if (defaultValue.empty()) {
  135. *defaultSetting = cmPolicies::WARN;
  136. } else {
  137. std::ostringstream e;
  138. e << defaultVar << " has value \"" << defaultValue
  139. << R"(" but must be "OLD", "NEW", or "" (empty).)";
  140. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  141. return false;
  142. }
  143. return true;
  144. }
  145. bool cmPolicies::ApplyPolicyVersion(cmMakefile* mf,
  146. std::string const& version_min,
  147. std::string const& version_max)
  148. {
  149. // Parse components of the minimum version.
  150. unsigned int minMajor = 2;
  151. unsigned int minMinor = 0;
  152. unsigned int minPatch = 0;
  153. unsigned int minTweak = 0;
  154. if (sscanf(version_min.c_str(), "%u.%u.%u.%u", &minMajor, &minMinor,
  155. &minPatch, &minTweak) < 2) {
  156. std::ostringstream e;
  157. e << "Invalid policy version value \"" << version_min << "\". "
  158. << "A numeric major.minor[.patch[.tweak]] must be given.";
  159. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  160. return false;
  161. }
  162. // it is an error if the policy version is less than 2.4
  163. if (minMajor < 2 || (minMajor == 2 && minMinor < 4)) {
  164. mf->IssueMessage(
  165. MessageType::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 (minMajor > cmVersion::GetMajorVersion() ||
  174. (minMajor == cmVersion::GetMajorVersion() &&
  175. minMinor > cmVersion::GetMinorVersion()) ||
  176. (minMajor == cmVersion::GetMajorVersion() &&
  177. minMinor == cmVersion::GetMinorVersion() &&
  178. minPatch > cmVersion::GetPatchVersion()) ||
  179. (minMajor == cmVersion::GetMajorVersion() &&
  180. minMinor == cmVersion::GetMinorVersion() &&
  181. minPatch == cmVersion::GetPatchVersion() &&
  182. minTweak > cmVersion::GetTweakVersion())) {
  183. std::ostringstream e;
  184. e << "An attempt was made to set the policy version of CMake to \""
  185. << version_min << "\" 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(MessageType::FATAL_ERROR, e.str());
  190. return false;
  191. }
  192. unsigned int polMajor = minMajor;
  193. unsigned int polMinor = minMinor;
  194. unsigned int polPatch = minPatch;
  195. if (!version_max.empty()) {
  196. // Parse components of the maximum version.
  197. unsigned int maxMajor = 0;
  198. unsigned int maxMinor = 0;
  199. unsigned int maxPatch = 0;
  200. unsigned int maxTweak = 0;
  201. if (sscanf(version_max.c_str(), "%u.%u.%u.%u", &maxMajor, &maxMinor,
  202. &maxPatch, &maxTweak) < 2) {
  203. std::ostringstream e;
  204. e << "Invalid policy max version value \"" << version_max << "\". "
  205. << "A numeric major.minor[.patch[.tweak]] must be given.";
  206. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  207. return false;
  208. }
  209. // It is an error if the min version is greater than the max version.
  210. if (minMajor > maxMajor || (minMajor == maxMajor && minMinor > maxMinor) ||
  211. (minMajor == maxMajor && minMinor == maxMinor &&
  212. minPatch > maxPatch) ||
  213. (minMajor == maxMajor && minMinor == maxMinor &&
  214. minPatch == maxPatch && minTweak > maxTweak)) {
  215. std::ostringstream e;
  216. e << "Policy VERSION range \"" << version_min << "..." << version_max
  217. << "\""
  218. << " specifies a larger minimum than maximum.";
  219. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  220. return false;
  221. }
  222. // Use the max version as the policy version.
  223. polMajor = maxMajor;
  224. polMinor = maxMinor;
  225. polPatch = maxPatch;
  226. }
  227. return cmPolicies::ApplyPolicyVersion(mf, polMajor, polMinor, polPatch);
  228. }
  229. bool cmPolicies::ApplyPolicyVersion(cmMakefile* mf, unsigned int majorVer,
  230. unsigned int minorVer,
  231. unsigned int patchVer)
  232. {
  233. // now loop over all the policies and set them as appropriate
  234. std::vector<cmPolicies::PolicyID> ancientPolicies;
  235. for (PolicyID pid = cmPolicies::CMP0000; pid != cmPolicies::CMPCOUNT;
  236. pid = PolicyID(pid + 1)) {
  237. if (isPolicyNewerThan(pid, majorVer, minorVer, patchVer)) {
  238. if (cmPolicies::GetPolicyStatus(pid) == cmPolicies::REQUIRED_ALWAYS) {
  239. ancientPolicies.push_back(pid);
  240. } else {
  241. cmPolicies::PolicyStatus status = cmPolicies::WARN;
  242. if (!GetPolicyDefault(mf, idToString(pid), &status) ||
  243. !mf->SetPolicy(pid, status)) {
  244. return false;
  245. }
  246. if (pid == cmPolicies::CMP0001 &&
  247. (status == cmPolicies::WARN || status == cmPolicies::OLD)) {
  248. if (!(mf->GetState()->GetInitializedCacheValue(
  249. "CMAKE_BACKWARDS_COMPATIBILITY"))) {
  250. // Set it to 2.4 because that is the last version where the
  251. // variable had meaning.
  252. mf->AddCacheDefinition(
  253. "CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
  254. "For backwards compatibility, what version of CMake "
  255. "commands and "
  256. "syntax should this version of CMake try to support.",
  257. cmStateEnums::STRING);
  258. }
  259. }
  260. }
  261. } else {
  262. if (!mf->SetPolicy(pid, cmPolicies::NEW)) {
  263. return false;
  264. }
  265. }
  266. }
  267. // Make sure the project does not use any ancient policies.
  268. if (!ancientPolicies.empty()) {
  269. DiagnoseAncientPolicies(ancientPolicies, majorVer, minorVer, patchVer, mf);
  270. cmSystemTools::SetFatalErrorOccured();
  271. return false;
  272. }
  273. return true;
  274. }
  275. bool cmPolicies::GetPolicyID(const char* id, cmPolicies::PolicyID& pid)
  276. {
  277. return stringToId(id, pid);
  278. }
  279. //! return a warning string for a given policy
  280. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  281. {
  282. std::ostringstream msg;
  283. msg << "Policy " << idToString(id)
  284. << " is not set: "
  285. ""
  286. << idToShortDescription(id)
  287. << " "
  288. "Run \"cmake --help-policy "
  289. << idToString(id)
  290. << "\" for "
  291. "policy details. "
  292. "Use the cmake_policy command to set the policy "
  293. "and suppress this warning.";
  294. return msg.str();
  295. }
  296. std::string cmPolicies::GetPolicyDeprecatedWarning(cmPolicies::PolicyID id)
  297. {
  298. std::ostringstream msg;
  299. /* clang-format off */
  300. msg <<
  301. "The OLD behavior for policy " << idToString(id) << " "
  302. "will be removed from a future version of CMake.\n"
  303. "The cmake-policies(7) manual explains that the OLD behaviors of all "
  304. "policies are deprecated and that a policy should be set to OLD only "
  305. "under specific short-term circumstances. Projects should be ported "
  306. "to the NEW behavior and not rely on setting a policy to OLD."
  307. ;
  308. /* clang-format on */
  309. return msg.str();
  310. }
  311. //! return an error string for when a required policy is unspecified
  312. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  313. {
  314. std::ostringstream error;
  315. error << "Policy " << idToString(id)
  316. << " is not set to NEW: "
  317. ""
  318. << idToShortDescription(id)
  319. << " "
  320. "Run \"cmake --help-policy "
  321. << idToString(id)
  322. << "\" for "
  323. "policy details. "
  324. "CMake now requires this policy to be set to NEW by the project. "
  325. "The policy may be set explicitly using the code\n"
  326. " cmake_policy(SET "
  327. << idToString(id)
  328. << " NEW)\n"
  329. "or by upgrading all policies with the code\n"
  330. " cmake_policy(VERSION "
  331. << idToVersion(id)
  332. << ") # or later\n"
  333. "Run \"cmake --help-command cmake_policy\" for more information.";
  334. return error.str();
  335. }
  336. //! Get the default status for a policy
  337. cmPolicies::PolicyStatus cmPolicies::GetPolicyStatus(
  338. cmPolicies::PolicyID /*unused*/)
  339. {
  340. return cmPolicies::WARN;
  341. }
  342. std::string cmPolicies::GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id)
  343. {
  344. std::string pid = idToString(id);
  345. std::ostringstream e;
  346. e << "Policy " << pid << " may not be set to OLD behavior because this "
  347. << "version of CMake no longer supports it. "
  348. << "The policy was introduced in "
  349. << "CMake version " << idToVersion(id)
  350. << ", and use of NEW behavior is now required."
  351. << "\n"
  352. << "Please either update your CMakeLists.txt files to conform to "
  353. << "the new behavior or use an older version of CMake that still "
  354. << "supports the old behavior. "
  355. << "Run cmake --help-policy " << pid << " for more information.";
  356. return e.str();
  357. }
  358. cmPolicies::PolicyStatus cmPolicies::PolicyMap::Get(
  359. cmPolicies::PolicyID id) const
  360. {
  361. PolicyStatus status = cmPolicies::WARN;
  362. if (this->Status[(POLICY_STATUS_COUNT * id) + OLD]) {
  363. status = cmPolicies::OLD;
  364. } else if (this->Status[(POLICY_STATUS_COUNT * id) + NEW]) {
  365. status = cmPolicies::NEW;
  366. }
  367. return status;
  368. }
  369. void cmPolicies::PolicyMap::Set(cmPolicies::PolicyID id,
  370. cmPolicies::PolicyStatus status)
  371. {
  372. this->Status[(POLICY_STATUS_COUNT * id) + OLD] = (status == OLD);
  373. this->Status[(POLICY_STATUS_COUNT * id) + WARN] = (status == WARN);
  374. this->Status[(POLICY_STATUS_COUNT * id) + NEW] = (status == NEW);
  375. }
  376. bool cmPolicies::PolicyMap::IsDefined(cmPolicies::PolicyID id) const
  377. {
  378. return this->Status[(POLICY_STATUS_COUNT * id) + OLD] ||
  379. this->Status[(POLICY_STATUS_COUNT * id) + WARN] ||
  380. this->Status[(POLICY_STATUS_COUNT * id) + NEW];
  381. }
  382. bool cmPolicies::PolicyMap::IsEmpty() const
  383. {
  384. return this->Status.none();
  385. }