cmPolicies.cxx 12 KB

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