cmPolicies.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include "cmPolicies.h"
  2. #include "cmake.h"
  3. #include "cmMakefile.h"
  4. #include "cmSourceFile.h"
  5. #include "cmVersion.h"
  6. #include <map>
  7. #include <set>
  8. #include <queue>
  9. #include <assert.h>
  10. const char* cmPolicies::PolicyStatusNames[] = {
  11. "OLD", "WARN", "NEW", "REQUIRED_IF_USED", "REQUIRED_ALWAYS"
  12. };
  13. class cmPolicy
  14. {
  15. public:
  16. cmPolicy(cmPolicies::PolicyID iD,
  17. const char *idString,
  18. const char *shortDescription,
  19. const char *longDescription,
  20. unsigned int majorVersionIntroduced,
  21. unsigned int minorVersionIntroduced,
  22. unsigned int patchVersionIntroduced,
  23. cmPolicies::PolicyStatus status)
  24. {
  25. if (!idString || !shortDescription || ! longDescription)
  26. {
  27. cmSystemTools::Error("Attempt to define a policy without "
  28. "all parameters being specified!");
  29. return;
  30. }
  31. this->ID = iD;
  32. this->IDString = idString;
  33. this->ShortDescription = shortDescription;
  34. this->LongDescription = longDescription;
  35. this->MajorVersionIntroduced = majorVersionIntroduced;
  36. this->MinorVersionIntroduced = minorVersionIntroduced;
  37. this->PatchVersionIntroduced = patchVersionIntroduced;
  38. this->Status = status;
  39. }
  40. std::string GetVersionString()
  41. {
  42. cmOStringStream error;
  43. error << this->MajorVersionIntroduced << "." <<
  44. this->MinorVersionIntroduced << "." <<
  45. this->PatchVersionIntroduced;
  46. return error.str();
  47. }
  48. bool IsPolicyNewerThan(unsigned int majorV,
  49. unsigned int minorV,
  50. unsigned int patchV)
  51. {
  52. if (majorV < this->MajorVersionIntroduced)
  53. {
  54. return true;
  55. }
  56. if (majorV > this->MajorVersionIntroduced)
  57. {
  58. return false;
  59. }
  60. if (minorV < this->MinorVersionIntroduced)
  61. {
  62. return true;
  63. }
  64. if (minorV > this->MinorVersionIntroduced)
  65. {
  66. return false;
  67. }
  68. return (patchV < this->PatchVersionIntroduced);
  69. }
  70. cmPolicies::PolicyID ID;
  71. std::string IDString;
  72. std::string ShortDescription;
  73. std::string LongDescription;
  74. unsigned int MajorVersionIntroduced;
  75. unsigned int MinorVersionIntroduced;
  76. unsigned int PatchVersionIntroduced;
  77. cmPolicies::PolicyStatus Status;
  78. };
  79. cmPolicies::cmPolicies()
  80. {
  81. // define all the policies
  82. this->DefinePolicy(
  83. CMP0000, "CMP0000",
  84. "A policy version number must be specified.",
  85. "CMake requires that projects specify the version of CMake to which "
  86. "they have been written. "
  87. "This policy has been put in place to help CMake maintain backwards "
  88. "compatibility with existing projects while allowing it to evolve "
  89. "more rapidly.\n"
  90. "The easiest way to specify a policy version number is to "
  91. "call the cmake_policy command at the top of your CMakeLists file:\n"
  92. " cmake_policy(VERSION <major>.<minor>)\n"
  93. "where <major>.<minor> is the version of CMake you want to support. "
  94. "The cmake_minimum_required command may also be used; see its "
  95. "documentation for details.",
  96. 2,6,0, cmPolicies::WARN
  97. );
  98. this->DefinePolicy(
  99. CMP0001, "CMP0001",
  100. "CMAKE_BACKWARDS_COMPATIBILITY should no longer be used.",
  101. "The OLD behavior is to check CMAKE_BACKWARDS_COMPATIBILITY and present "
  102. "it to the user. "
  103. "The NEW behavior is to ignore CMAKE_BACKWARDS_COMPATIBILITY "
  104. "completely.\n"
  105. "In CMake 2.4 and below the variable CMAKE_BACKWARDS_COMPATIBILITY was "
  106. "used to request compatibility with earlier versions of CMake. "
  107. "In CMake 2.6 and above all compatibility issues are handled by policies "
  108. "and the cmake_policy command. "
  109. "However, CMake must still check CMAKE_BACKWARDS_COMPATIBILITY for "
  110. "projects written for CMake 2.4 and below.",
  111. 2,6,0, cmPolicies::WARN
  112. );
  113. this->DefinePolicy(
  114. CMP0002, "CMP0002",
  115. "Logical target names must be globally unique.",
  116. "Targets names created with "
  117. "add_executable, add_library, or add_custom_target "
  118. "are logical build target names. "
  119. "Logical target names must be globally unique because:\n"
  120. " - Unique names may be referenced unambiguously both in CMake\n"
  121. " code and on make tool command lines.\n"
  122. " - Logical names are used by Xcode and VS IDE generators\n"
  123. " to produce meaningful project names for the targets.\n"
  124. "The logical name of executable and library targets does not "
  125. "have to correspond to the physical file names built. "
  126. "Consider using the OUTPUT_NAME target property to create two "
  127. "targets with the same physical name while keeping logical "
  128. "names distinct. "
  129. "Custom targets must simply have globally unique names (unless one "
  130. "uses the global property ALLOW_DUPLICATE_CUSTOM_TARGETS with a "
  131. "Makefiles generator).",
  132. 2,6,0, cmPolicies::WARN
  133. );
  134. }
  135. cmPolicies::~cmPolicies()
  136. {
  137. // free the policies
  138. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  139. = this->Policies.begin();
  140. for (;i != this->Policies.end(); ++i)
  141. {
  142. delete i->second;
  143. }
  144. }
  145. void cmPolicies::DefinePolicy(cmPolicies::PolicyID iD,
  146. const char *idString,
  147. const char *shortDescription,
  148. const char *longDescription,
  149. unsigned int majorVersionIntroduced,
  150. unsigned int minorVersionIntroduced,
  151. unsigned int patchVersionIntroduced,
  152. cmPolicies::PolicyStatus status)
  153. {
  154. // a policy must be unique and can only be defined once
  155. if (this->Policies.find(iD) != this->Policies.end())
  156. {
  157. cmSystemTools::Error("Attempt to redefine a CMake policy for policy "
  158. "ID ", this->GetPolicyIDString(iD).c_str());
  159. return;
  160. }
  161. this->Policies[iD] = new cmPolicy(iD, idString,
  162. shortDescription,
  163. longDescription,
  164. majorVersionIntroduced,
  165. minorVersionIntroduced,
  166. patchVersionIntroduced,
  167. status);
  168. this->PolicyStringMap[idString] = iD;
  169. }
  170. bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
  171. const char *version)
  172. {
  173. std::string ver = "2.4.0";
  174. if (version && strlen(version) > 0)
  175. {
  176. ver = version;
  177. }
  178. unsigned int majorVer = 2;
  179. unsigned int minorVer = 0;
  180. unsigned int patchVer = 0;
  181. // parse the string
  182. if(sscanf(ver.c_str(), "%u.%u.%u",
  183. &majorVer, &minorVer, &patchVer) < 2)
  184. {
  185. return false;
  186. }
  187. // it is an error if the policy version is less than 2.4
  188. if (majorVer < 2 || majorVer == 2 && minorVer < 4)
  189. {
  190. mf->IssueMessage(cmake::FATAL_ERROR,
  191. "An attempt was made to set the policy version of CMake to something "
  192. "earlier than \"2.4\". "
  193. "In CMake 2.4 and below backwards compatibility was handled with the "
  194. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  195. "In order to get compatibility features supporting versions earlier "
  196. "than 2.4 set policy CMP0001 to OLD to tell CMake to check the "
  197. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  198. "One way to so this is to set the policy version to 2.4 exactly."
  199. );
  200. }
  201. // now loop over all the policies and set them as appropriate
  202. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  203. = this->Policies.begin();
  204. for (;i != this->Policies.end(); ++i)
  205. {
  206. if (i->second->IsPolicyNewerThan(majorVer,minorVer,patchVer))
  207. {
  208. if (!mf->SetPolicy(i->second->ID, cmPolicies::WARN))
  209. {
  210. return false;
  211. }
  212. }
  213. else
  214. {
  215. if (!mf->SetPolicy(i->second->ID, cmPolicies::NEW))
  216. {
  217. return false;
  218. }
  219. }
  220. }
  221. return true;
  222. }
  223. // is this a valid status the listfile can set this policy to?
  224. bool cmPolicies::IsValidPolicyStatus(cmPolicies::PolicyID id,
  225. cmPolicies::PolicyStatus status)
  226. {
  227. // if they are setting a feature to anything other than OLD or WARN and the
  228. // feature is not known about then that is an error
  229. if (this->Policies.find(id) == this->Policies.end())
  230. {
  231. if (status == cmPolicies::WARN ||
  232. status == cmPolicies::OLD)
  233. {
  234. return true;
  235. }
  236. cmOStringStream error;
  237. error <<
  238. "Error: an attempt was made to enable the new behavior for " <<
  239. "a new feature that is in a later version of CMake than "
  240. "what you are runing, please upgrade to a newer version "
  241. "of CMake.";
  242. cmSystemTools::Error(error.str().c_str());
  243. return false;
  244. }
  245. // now we know the feature is defined, so the only issue is if someone is
  246. // setting it to WARN or OLD when the feature is REQUIRED_ALWAYS
  247. if ((status == cmPolicies::WARN ||
  248. status == cmPolicies::OLD) &&
  249. this->Policies[id]->Status == cmPolicies::REQUIRED_ALWAYS)
  250. {
  251. cmOStringStream error;
  252. error <<
  253. "Error: an attempt was made to enable the old behavior for " <<
  254. "a feature that is no longer supported. The feature in " <<
  255. "question is feature " <<
  256. id <<
  257. " which had new behavior introduced in CMake version " <<
  258. this->Policies[id]->GetVersionString() <<
  259. " please either update your CMakeLists files to conform to " <<
  260. "the new behavior " <<
  261. "or use an older version of CMake that still supports " <<
  262. "the old behavior. Run cmake --help-policies " <<
  263. id << " for more information.";
  264. cmSystemTools::Error(error.str().c_str());
  265. return false;
  266. }
  267. return true;
  268. }
  269. // is this a valid status the listfile can set this policy to?
  270. bool cmPolicies::IsValidUsedPolicyStatus(cmPolicies::PolicyID id,
  271. cmPolicies::PolicyStatus status)
  272. {
  273. // if they are setting a feature to anything other than OLD or WARN and the
  274. // feature is not known about then that is an error
  275. if (this->Policies.find(id) == this->Policies.end())
  276. {
  277. if (status == cmPolicies::WARN ||
  278. status == cmPolicies::OLD)
  279. {
  280. return true;
  281. }
  282. cmOStringStream error;
  283. error <<
  284. "Error: an attempt was made to enable the new behavior for " <<
  285. "a new feature that is in a later version of CMake than "
  286. "what you are runing, please upgrade to a newer version "
  287. "of CMake.";
  288. cmSystemTools::Error(error.str().c_str());
  289. return false;
  290. }
  291. // now we know the feature is defined, so the only issue is if someone is
  292. // setting it to WARN or OLD when the feature is REQUIRED_ALWAYS
  293. if ((status == cmPolicies::WARN ||
  294. status == cmPolicies::OLD) &&
  295. (this->Policies[id]->Status == cmPolicies::REQUIRED_ALWAYS ||
  296. this->Policies[id]->Status == cmPolicies::REQUIRED_IF_USED))
  297. {
  298. cmOStringStream error;
  299. error <<
  300. "Error: an attempt was made to enable the old behavior for " <<
  301. "a feature that is no longer supported. The feature in " <<
  302. "question is feature " <<
  303. id <<
  304. " which had new behavior introduced in CMake version " <<
  305. this->Policies[id]->GetVersionString() <<
  306. " please either update your CMakeLists files to conform to " <<
  307. "the new behavior " <<
  308. "or use an older version of CMake that still supports " <<
  309. "the old behavior. Run cmake --help-policies " <<
  310. id << " for more information.";
  311. cmSystemTools::Error(error.str().c_str());
  312. return false;
  313. }
  314. return true;
  315. }
  316. bool cmPolicies::GetPolicyID(const char *id, cmPolicies::PolicyID &pid)
  317. {
  318. if (!id || strlen(id) < 1)
  319. {
  320. return false;
  321. }
  322. std::map<std::string,cmPolicies::PolicyID>::iterator pos =
  323. this->PolicyStringMap.find(id);
  324. if (pos == this->PolicyStringMap.end())
  325. {
  326. return false;
  327. }
  328. pid = pos->second;
  329. return true;
  330. }
  331. std::string cmPolicies::GetPolicyIDString(cmPolicies::PolicyID pid)
  332. {
  333. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  334. this->Policies.find(pid);
  335. if (pos == this->Policies.end())
  336. {
  337. return "";
  338. }
  339. return pos->second->IDString;
  340. }
  341. ///! return a warning string for a given policy
  342. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  343. {
  344. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  345. this->Policies.find(id);
  346. if (pos == this->Policies.end())
  347. {
  348. cmSystemTools::Error(
  349. "Request for warning text for undefined policy!");
  350. return "Request for warning text for undefined policy!";
  351. }
  352. cmOStringStream msg;
  353. msg <<
  354. "Policy " << pos->second->IDString << " is not set: "
  355. "" << pos->second->ShortDescription << "\n"
  356. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  357. "policy details. "
  358. "Use the cmake_policy command to set the policy "
  359. "and suppress this warning.";
  360. return msg.str();
  361. }
  362. ///! return an error string for when a required policy is unspecified
  363. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  364. {
  365. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  366. this->Policies.find(id);
  367. if (pos == this->Policies.end())
  368. {
  369. cmSystemTools::Error(
  370. "Request for error text for undefined policy!");
  371. return "Request for warning text for undefined policy!";
  372. }
  373. cmOStringStream error;
  374. error <<
  375. "Policy " << pos->second->IDString << " is not set to NEW: "
  376. "" << pos->second->ShortDescription << "\n"
  377. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  378. "policy details. "
  379. "CMake now requires this policy to be set to NEW by the project. "
  380. "The policy may be set explicitly using the code\n"
  381. " cmake_policy(SET " << pos->second->IDString << " NEW)\n"
  382. "or by upgrading all policies with the code\n"
  383. " cmake_policy(VERSION " << pos->second->GetVersionString() <<
  384. ") # or later\n"
  385. "Run \"cmake --help-command cmake_policy\" for more information.";
  386. return error.str();
  387. }
  388. ///! Get the default status for a policy
  389. cmPolicies::PolicyStatus
  390. cmPolicies::GetPolicyStatus(cmPolicies::PolicyID id)
  391. {
  392. // if the policy is not know then what?
  393. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  394. this->Policies.find(id);
  395. if (pos == this->Policies.end())
  396. {
  397. // TODO is this right?
  398. return cmPolicies::WARN;
  399. }
  400. return pos->second->Status;
  401. }
  402. void cmPolicies::GetDocumentation(std::vector<cmDocumentationEntry>& v)
  403. {
  404. // now loop over all the policies and set them as appropriate
  405. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  406. = this->Policies.begin();
  407. for (;i != this->Policies.end(); ++i)
  408. {
  409. cmOStringStream full;
  410. full << i->second->LongDescription;
  411. full << "\nThis policy was introduced in CMake version ";
  412. full << i->second->GetVersionString() << ". ";
  413. full << "CMake version " << cmVersion::GetMajorVersion()
  414. << "." << cmVersion::GetMinorVersion() << " ";
  415. // add in some more text here based on status
  416. switch (i->second->Status)
  417. {
  418. case cmPolicies::WARN:
  419. full << "defaults to WARN for this policy. "
  420. << "Use the cmake_policy command to set it to OLD or NEW.";
  421. break;
  422. case cmPolicies::OLD:
  423. full << "defaults to the OLD behavior for this policy.";
  424. break;
  425. case cmPolicies::NEW:
  426. full << "defaults to the NEW behavior for this policy.";
  427. break;
  428. case cmPolicies::REQUIRED_IF_USED:
  429. full << "requires the policy to be set to NEW if you use it. "
  430. << "Use the cmake_policy command to set it to NEW.";
  431. break;
  432. case cmPolicies::REQUIRED_ALWAYS:
  433. full << "requires the policy to be set to NEW. "
  434. << "Use the cmake_policy command to set it to NEW.";
  435. break;
  436. }
  437. cmDocumentationEntry e(i->second->IDString.c_str(),
  438. i->second->ShortDescription.c_str(),
  439. full.str().c_str());
  440. v.push_back(e);
  441. }
  442. }