cmPolicies.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 const& 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,
  144. std::string const& version_min,
  145. std::string const& version_max)
  146. {
  147. // Parse components of the minimum version.
  148. unsigned int minMajor = 2;
  149. unsigned int minMinor = 0;
  150. unsigned int minPatch = 0;
  151. unsigned int minTweak = 0;
  152. if (sscanf(version_min.c_str(), "%u.%u.%u.%u", &minMajor, &minMinor,
  153. &minPatch, &minTweak) < 2) {
  154. std::ostringstream e;
  155. e << "Invalid policy version value \"" << version_min << "\". "
  156. << "A numeric major.minor[.patch[.tweak]] must be given.";
  157. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  158. return false;
  159. }
  160. // it is an error if the policy version is less than 2.4
  161. if (minMajor < 2 || (minMajor == 2 && minMinor < 4)) {
  162. mf->IssueMessage(
  163. cmake::FATAL_ERROR,
  164. "Compatibility with CMake < 2.4 is not supported by CMake >= 3.0. "
  165. "For compatibility with older versions please use any CMake 2.8.x "
  166. "release or lower.");
  167. return false;
  168. }
  169. // It is an error if the policy version is greater than the running
  170. // CMake.
  171. if (minMajor > cmVersion::GetMajorVersion() ||
  172. (minMajor == cmVersion::GetMajorVersion() &&
  173. minMinor > cmVersion::GetMinorVersion()) ||
  174. (minMajor == cmVersion::GetMajorVersion() &&
  175. minMinor == cmVersion::GetMinorVersion() &&
  176. minPatch > cmVersion::GetPatchVersion()) ||
  177. (minMajor == cmVersion::GetMajorVersion() &&
  178. minMinor == cmVersion::GetMinorVersion() &&
  179. minPatch == cmVersion::GetPatchVersion() &&
  180. minTweak > cmVersion::GetTweakVersion())) {
  181. std::ostringstream e;
  182. e << "An attempt was made to set the policy version of CMake to \""
  183. << version_min << "\" which is greater than this version of CMake. "
  184. << "This is not allowed because the greater version may have new "
  185. << "policies not known to this CMake. "
  186. << "You may need a newer CMake version to build this project.";
  187. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  188. return false;
  189. }
  190. unsigned int polMajor = minMajor;
  191. unsigned int polMinor = minMinor;
  192. unsigned int polPatch = minPatch;
  193. if (!version_max.empty()) {
  194. // Parse components of the maximum version.
  195. unsigned int maxMajor = 0;
  196. unsigned int maxMinor = 0;
  197. unsigned int maxPatch = 0;
  198. unsigned int maxTweak = 0;
  199. if (sscanf(version_max.c_str(), "%u.%u.%u.%u", &maxMajor, &maxMinor,
  200. &maxPatch, &maxTweak) < 2) {
  201. std::ostringstream e;
  202. e << "Invalid policy max version value \"" << version_max << "\". "
  203. << "A numeric major.minor[.patch[.tweak]] must be given.";
  204. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  205. return false;
  206. }
  207. // It is an error if the min version is greater than the max version.
  208. if (minMajor > maxMajor || (minMajor == maxMajor && minMinor > maxMinor) ||
  209. (minMajor == maxMajor && minMinor == maxMinor &&
  210. minPatch > maxPatch) ||
  211. (minMajor == maxMajor && minMinor == maxMinor &&
  212. minPatch == maxPatch && minTweak > maxTweak)) {
  213. std::ostringstream e;
  214. e << "Policy VERSION range \"" << version_min << "..." << version_max
  215. << "\""
  216. << " specifies a larger minimum than maximum.";
  217. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  218. return false;
  219. }
  220. // Use the max version as the policy version.
  221. polMajor = maxMajor;
  222. polMinor = maxMinor;
  223. polPatch = maxPatch;
  224. }
  225. return cmPolicies::ApplyPolicyVersion(mf, polMajor, polMinor, polPatch);
  226. }
  227. bool cmPolicies::ApplyPolicyVersion(cmMakefile* mf, unsigned int majorVer,
  228. unsigned int minorVer,
  229. unsigned int patchVer)
  230. {
  231. // now loop over all the policies and set them as appropriate
  232. std::vector<cmPolicies::PolicyID> ancientPolicies;
  233. for (PolicyID pid = cmPolicies::CMP0000; pid != cmPolicies::CMPCOUNT;
  234. pid = PolicyID(pid + 1)) {
  235. if (isPolicyNewerThan(pid, majorVer, minorVer, patchVer)) {
  236. if (cmPolicies::GetPolicyStatus(pid) == cmPolicies::REQUIRED_ALWAYS) {
  237. ancientPolicies.push_back(pid);
  238. } else {
  239. cmPolicies::PolicyStatus status = cmPolicies::WARN;
  240. if (!GetPolicyDefault(mf, idToString(pid), &status) ||
  241. !mf->SetPolicy(pid, status)) {
  242. return false;
  243. }
  244. if (pid == cmPolicies::CMP0001 &&
  245. (status == cmPolicies::WARN || status == cmPolicies::OLD)) {
  246. if (!(mf->GetState()->GetInitializedCacheValue(
  247. "CMAKE_BACKWARDS_COMPATIBILITY"))) {
  248. // Set it to 2.4 because that is the last version where the
  249. // variable had meaning.
  250. mf->AddCacheDefinition(
  251. "CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
  252. "For backwards compatibility, what version of CMake "
  253. "commands and "
  254. "syntax should this version of CMake try to support.",
  255. cmStateEnums::STRING);
  256. }
  257. }
  258. }
  259. } else {
  260. if (!mf->SetPolicy(pid, cmPolicies::NEW)) {
  261. return false;
  262. }
  263. }
  264. }
  265. // Make sure the project does not use any ancient policies.
  266. if (!ancientPolicies.empty()) {
  267. DiagnoseAncientPolicies(ancientPolicies, majorVer, minorVer, patchVer, mf);
  268. cmSystemTools::SetFatalErrorOccured();
  269. return false;
  270. }
  271. return true;
  272. }
  273. bool cmPolicies::GetPolicyID(const char* id, cmPolicies::PolicyID& pid)
  274. {
  275. return stringToId(id, pid);
  276. }
  277. ///! return a warning string for a given policy
  278. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  279. {
  280. std::ostringstream msg;
  281. msg << "Policy " << idToString(id)
  282. << " is not set: "
  283. ""
  284. << idToShortDescription(id)
  285. << " "
  286. "Run \"cmake --help-policy "
  287. << idToString(id)
  288. << "\" for "
  289. "policy details. "
  290. "Use the cmake_policy command to set the policy "
  291. "and suppress this warning.";
  292. return msg.str();
  293. }
  294. std::string cmPolicies::GetPolicyDeprecatedWarning(cmPolicies::PolicyID id)
  295. {
  296. std::ostringstream msg;
  297. /* clang-format off */
  298. msg <<
  299. "The OLD behavior for policy " << idToString(id) << " "
  300. "will be removed from a future version of CMake.\n"
  301. "The cmake-policies(7) manual explains that the OLD behaviors of all "
  302. "policies are deprecated and that a policy should be set to OLD only "
  303. "under specific short-term circumstances. Projects should be ported "
  304. "to the NEW behavior and not rely on setting a policy to OLD."
  305. ;
  306. /* clang-format on */
  307. return msg.str();
  308. }
  309. ///! return an error string for when a required policy is unspecified
  310. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  311. {
  312. std::ostringstream error;
  313. error << "Policy " << idToString(id)
  314. << " is not set to NEW: "
  315. ""
  316. << idToShortDescription(id)
  317. << " "
  318. "Run \"cmake --help-policy "
  319. << idToString(id)
  320. << "\" for "
  321. "policy details. "
  322. "CMake now requires this policy to be set to NEW by the project. "
  323. "The policy may be set explicitly using the code\n"
  324. " cmake_policy(SET "
  325. << idToString(id)
  326. << " NEW)\n"
  327. "or by upgrading all policies with the code\n"
  328. " cmake_policy(VERSION "
  329. << idToVersion(id)
  330. << ") # or later\n"
  331. "Run \"cmake --help-command cmake_policy\" for more information.";
  332. return error.str();
  333. }
  334. ///! Get the default status for a policy
  335. cmPolicies::PolicyStatus cmPolicies::GetPolicyStatus(
  336. cmPolicies::PolicyID /*unused*/)
  337. {
  338. return cmPolicies::WARN;
  339. }
  340. std::string cmPolicies::GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id)
  341. {
  342. std::string pid = idToString(id);
  343. std::ostringstream e;
  344. e << "Policy " << pid << " may not be set to OLD behavior because this "
  345. << "version of CMake no longer supports it. "
  346. << "The policy was introduced in "
  347. << "CMake version " << idToVersion(id)
  348. << ", and use of NEW behavior is now required."
  349. << "\n"
  350. << "Please either update your CMakeLists.txt files to conform to "
  351. << "the new behavior or use an older version of CMake that still "
  352. << "supports the old behavior. "
  353. << "Run cmake --help-policy " << pid << " for more information.";
  354. return e.str();
  355. }
  356. cmPolicies::PolicyStatus cmPolicies::PolicyMap::Get(
  357. cmPolicies::PolicyID id) const
  358. {
  359. PolicyStatus status = cmPolicies::WARN;
  360. if (this->Status[(POLICY_STATUS_COUNT * id) + OLD]) {
  361. status = cmPolicies::OLD;
  362. } else if (this->Status[(POLICY_STATUS_COUNT * id) + NEW]) {
  363. status = cmPolicies::NEW;
  364. }
  365. return status;
  366. }
  367. void cmPolicies::PolicyMap::Set(cmPolicies::PolicyID id,
  368. cmPolicies::PolicyStatus status)
  369. {
  370. this->Status[(POLICY_STATUS_COUNT * id) + OLD] = (status == OLD);
  371. this->Status[(POLICY_STATUS_COUNT * id) + WARN] = (status == WARN);
  372. this->Status[(POLICY_STATUS_COUNT * id) + NEW] = (status == NEW);
  373. }
  374. bool cmPolicies::PolicyMap::IsDefined(cmPolicies::PolicyID id) const
  375. {
  376. return this->Status[(POLICY_STATUS_COUNT * id) + OLD] ||
  377. this->Status[(POLICY_STATUS_COUNT * id) + WARN] ||
  378. this->Status[(POLICY_STATUS_COUNT * id) + NEW];
  379. }
  380. bool cmPolicies::PolicyMap::IsEmpty() const
  381. {
  382. return this->Status.none();
  383. }