cmPolicies.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 minimum required CMake version 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 so users trying to build the project "
  88. "may be told when they need to update their CMake. "
  89. "Specifying a version also helps the project build with CMake versions "
  90. "newer than that specified. "
  91. "Use the cmake_minimum_required command at the top of your main "
  92. " CMakeLists.txt file:\n"
  93. " cmake_minimum_required(VERSION <major>.<minor>)\n"
  94. "where \"<major>.<minor>\" is the version of CMake you want to support "
  95. "(such as \"2.6\"). "
  96. "The command will ensure that at least the given version of CMake is "
  97. "running and help newer versions be compatible with the project. "
  98. "See documentation of cmake_minimum_required for details.",
  99. 2,6,0, cmPolicies::WARN
  100. );
  101. this->DefinePolicy(
  102. CMP0001, "CMP0001",
  103. "CMAKE_BACKWARDS_COMPATIBILITY should no longer be used.",
  104. "The OLD behavior is to check CMAKE_BACKWARDS_COMPATIBILITY and present "
  105. "it to the user. "
  106. "The NEW behavior is to ignore CMAKE_BACKWARDS_COMPATIBILITY "
  107. "completely.\n"
  108. "In CMake 2.4 and below the variable CMAKE_BACKWARDS_COMPATIBILITY was "
  109. "used to request compatibility with earlier versions of CMake. "
  110. "In CMake 2.6 and above all compatibility issues are handled by policies "
  111. "and the cmake_policy command. "
  112. "However, CMake must still check CMAKE_BACKWARDS_COMPATIBILITY for "
  113. "projects written for CMake 2.4 and below.",
  114. 2,6,0, cmPolicies::WARN
  115. );
  116. this->DefinePolicy(
  117. CMP0002, "CMP0002",
  118. "Logical target names must be globally unique.",
  119. "Targets names created with "
  120. "add_executable, add_library, or add_custom_target "
  121. "are logical build target names. "
  122. "Logical target names must be globally unique because:\n"
  123. " - Unique names may be referenced unambiguously both in CMake\n"
  124. " code and on make tool command lines.\n"
  125. " - Logical names are used by Xcode and VS IDE generators\n"
  126. " to produce meaningful project names for the targets.\n"
  127. "The logical name of executable and library targets does not "
  128. "have to correspond to the physical file names built. "
  129. "Consider using the OUTPUT_NAME target property to create two "
  130. "targets with the same physical name while keeping logical "
  131. "names distinct. "
  132. "Custom targets must simply have globally unique names (unless one "
  133. "uses the global property ALLOW_DUPLICATE_CUSTOM_TARGETS with a "
  134. "Makefiles generator).",
  135. 2,6,0, cmPolicies::WARN
  136. );
  137. this->DefinePolicy(
  138. CMP0003, "CMP0003",
  139. "Libraries linked via full path no longer produce linker search paths.",
  140. "This policy affects how libraries whose full paths are NOT known "
  141. "are found at link time, but was created due to a change in how CMake "
  142. "deals with libraries whose full paths are known. "
  143. "Consider the code\n"
  144. " target_link_libraries(myexe /path/to/libA.so)\n"
  145. "CMake 2.4 and below implemented linking to libraries whose full paths "
  146. "are known by splitting them on the link line into separate components "
  147. "consisting of the linker search path and the library name. "
  148. "The example code might have produced something like\n"
  149. " ... -L/path/to -lA ...\n"
  150. "in order to link to library A. "
  151. "An analysis was performed to order multiple link directories such that "
  152. "the linker would find library A in the desired location, but there "
  153. "are cases in which this does not work. "
  154. "CMake versions 2.6 and above use the more reliable approach of passing "
  155. "the full path to libraries directly to the linker in most cases. "
  156. "The example code now produces something like\n"
  157. " ... /path/to/libA.so ....\n"
  158. "Unfortunately this change can break code like\n"
  159. " target_link_libraries(myexe /path/to/libA.so B)\n"
  160. "where \"B\" is meant to find \"/path/to/libB.so\". "
  161. "This code is wrong because the user is asking the linker to find "
  162. "library B but has not provided a linker search path (which may be "
  163. "added with the link_directories command). "
  164. "However, with the old linking implementation the code would work "
  165. "accidentally because the linker search path added for library A "
  166. "allowed library B to be found."
  167. "\n"
  168. "In order to support projects depending on linker search paths "
  169. "added by linking to libraries with known full paths, the OLD "
  170. "behavior for this policy will add the linker search paths even "
  171. "though they are not needed for their own libraries. "
  172. "When this policy is set to OLD, CMake will produce a link line such as\n"
  173. " ... -L/path/to /path/to/libA.so -lB ...\n"
  174. "which will allow library B to be found as it was previously. "
  175. "When this policy is set to NEW, CMake will produce a link line such as\n"
  176. " ... /path/to/libA.so -lB ...\n"
  177. "which more accurately matches what the project specified."
  178. "\n"
  179. "The setting for this policy used when generating the link line is that "
  180. "in effect when the target is created by an add_executable or "
  181. "add_library command. For the example described above, the code\n"
  182. " cmake_policy(SET CMP0003 OLD) # or cmake_policy(VERSION 2.4)\n"
  183. " add_executable(myexe myexe.c)\n"
  184. " target_link_libraries(myexe /path/to/libA.so B)\n"
  185. "will work and suppress the warning for this policy. "
  186. "It may also be updated to work with the corrected linking approach:\n"
  187. " cmake_policy(SET CMP0003 NEW) # or cmake_policy(VERSION 2.6)\n"
  188. " link_directories(/path/to) # needed to find library B\n"
  189. " add_executable(myexe myexe.c)\n"
  190. " target_link_libraries(myexe /path/to/libA.so B)\n"
  191. "Even better, library B may be specified with a full path:\n"
  192. " add_executable(myexe myexe.c)\n"
  193. " target_link_libraries(myexe /path/to/libA.so /path/to/libB.so)\n"
  194. "When all items on the link line have known paths CMake does not check "
  195. "this policy so it has no effect.",
  196. 2,6,0, cmPolicies::WARN);
  197. this->DefinePolicy(
  198. CMP0004, "CMP0004",
  199. "Libraries linked may not have leading or trailing whitespace.",
  200. "CMake versions 2.4 and below silently removed leading and trailing "
  201. "whitespace from libraries linked with code like\n"
  202. " target_link_libraries(myexe \" A \")\n"
  203. "This could lead to subtle errors in user projects.\n"
  204. "The OLD behavior for this policy is to silently remove leading and "
  205. "trailing whitespace. "
  206. "The NEW behavior for this policy is to diagnose the existence of "
  207. "such whitespace as an error. "
  208. "The setting for this policy used when checking the library names is "
  209. "that in effect when the target is created by an add_executable or "
  210. "add_library command.",
  211. 2,6,0, cmPolicies::WARN);
  212. this->DefinePolicy(
  213. CMP0005, "CMP0005",
  214. "Preprocessor definition values are now escaped automatically.",
  215. "This policy determines whether or not CMake should generate escaped "
  216. "preprocessor definition values added via add_definitions. "
  217. "CMake versions 2.4 and below assumed that only trivial values would "
  218. "be given for macros in add_definitions calls. "
  219. "It did not attempt to escape non-trivial values such as string "
  220. "literals in generated build rules. "
  221. "CMake versions 2.6 and above support escaping of most values, but "
  222. "cannot assume the user has not added escapes already in an attempt to "
  223. "work around limitations in earlier versions.\n"
  224. "The OLD behavior for this policy is to place definition values given "
  225. "to add_definitions directly in the generated build rules without "
  226. "attempting to escape anything. "
  227. "The NEW behavior for this policy is to generate correct escapes "
  228. "for all native build tools automatically. "
  229. "See documentation of the COMPILE_DEFINITIONS target property for "
  230. "limitations of the escaping implementation.",
  231. 2,6,0, cmPolicies::WARN);
  232. }
  233. cmPolicies::~cmPolicies()
  234. {
  235. // free the policies
  236. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  237. = this->Policies.begin();
  238. for (;i != this->Policies.end(); ++i)
  239. {
  240. delete i->second;
  241. }
  242. }
  243. void cmPolicies::DefinePolicy(cmPolicies::PolicyID iD,
  244. const char *idString,
  245. const char *shortDescription,
  246. const char *longDescription,
  247. unsigned int majorVersionIntroduced,
  248. unsigned int minorVersionIntroduced,
  249. unsigned int patchVersionIntroduced,
  250. cmPolicies::PolicyStatus status)
  251. {
  252. // a policy must be unique and can only be defined once
  253. if (this->Policies.find(iD) != this->Policies.end())
  254. {
  255. cmSystemTools::Error("Attempt to redefine a CMake policy for policy "
  256. "ID ", this->GetPolicyIDString(iD).c_str());
  257. return;
  258. }
  259. this->Policies[iD] = new cmPolicy(iD, idString,
  260. shortDescription,
  261. longDescription,
  262. majorVersionIntroduced,
  263. minorVersionIntroduced,
  264. patchVersionIntroduced,
  265. status);
  266. this->PolicyStringMap[idString] = iD;
  267. }
  268. bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
  269. const char *version)
  270. {
  271. std::string ver = "2.4.0";
  272. if (version && strlen(version) > 0)
  273. {
  274. ver = version;
  275. }
  276. unsigned int majorVer = 2;
  277. unsigned int minorVer = 0;
  278. unsigned int patchVer = 0;
  279. // parse the string
  280. if(sscanf(ver.c_str(), "%u.%u.%u",
  281. &majorVer, &minorVer, &patchVer) < 2)
  282. {
  283. return false;
  284. }
  285. // it is an error if the policy version is less than 2.4
  286. if (majorVer < 2 || majorVer == 2 && minorVer < 4)
  287. {
  288. mf->IssueMessage(cmake::FATAL_ERROR,
  289. "An attempt was made to set the policy version of CMake to something "
  290. "earlier than \"2.4\". "
  291. "In CMake 2.4 and below backwards compatibility was handled with the "
  292. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  293. "In order to get compatibility features supporting versions earlier "
  294. "than 2.4 set policy CMP0001 to OLD to tell CMake to check the "
  295. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  296. "One way to so this is to set the policy version to 2.4 exactly."
  297. );
  298. }
  299. // now loop over all the policies and set them as appropriate
  300. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  301. = this->Policies.begin();
  302. for (;i != this->Policies.end(); ++i)
  303. {
  304. if (i->second->IsPolicyNewerThan(majorVer,minorVer,patchVer))
  305. {
  306. if (!mf->SetPolicy(i->second->ID, cmPolicies::WARN))
  307. {
  308. return false;
  309. }
  310. }
  311. else
  312. {
  313. if (!mf->SetPolicy(i->second->ID, cmPolicies::NEW))
  314. {
  315. return false;
  316. }
  317. }
  318. }
  319. return true;
  320. }
  321. // is this a valid status the listfile can set this policy to?
  322. bool cmPolicies::IsValidPolicyStatus(cmPolicies::PolicyID id,
  323. cmPolicies::PolicyStatus status)
  324. {
  325. // if they are setting a feature to anything other than OLD or WARN and the
  326. // feature is not known about then that is an error
  327. if (this->Policies.find(id) == this->Policies.end())
  328. {
  329. if (status == cmPolicies::WARN ||
  330. status == cmPolicies::OLD)
  331. {
  332. return true;
  333. }
  334. cmOStringStream error;
  335. error <<
  336. "Error: an attempt was made to enable the new behavior for " <<
  337. "a new feature that is in a later version of CMake than "
  338. "what you are runing, please upgrade to a newer version "
  339. "of CMake.";
  340. cmSystemTools::Error(error.str().c_str());
  341. return false;
  342. }
  343. // now we know the feature is defined, so the only issue is if someone is
  344. // setting it to WARN or OLD when the feature is REQUIRED_ALWAYS
  345. if ((status == cmPolicies::WARN ||
  346. status == cmPolicies::OLD) &&
  347. this->Policies[id]->Status == cmPolicies::REQUIRED_ALWAYS)
  348. {
  349. cmOStringStream error;
  350. error <<
  351. "Error: an attempt was made to enable the old behavior for " <<
  352. "a feature that is no longer supported. The feature in " <<
  353. "question is feature " <<
  354. id <<
  355. " which had new behavior introduced in CMake version " <<
  356. this->Policies[id]->GetVersionString() <<
  357. " please either update your CMakeLists files to conform to " <<
  358. "the new behavior " <<
  359. "or use an older version of CMake that still supports " <<
  360. "the old behavior. Run cmake --help-policies " <<
  361. id << " for more information.";
  362. cmSystemTools::Error(error.str().c_str());
  363. return false;
  364. }
  365. return true;
  366. }
  367. // is this a valid status the listfile can set this policy to?
  368. bool cmPolicies::IsValidUsedPolicyStatus(cmPolicies::PolicyID id,
  369. cmPolicies::PolicyStatus status)
  370. {
  371. // if they are setting a feature to anything other than OLD or WARN and the
  372. // feature is not known about then that is an error
  373. if (this->Policies.find(id) == this->Policies.end())
  374. {
  375. if (status == cmPolicies::WARN ||
  376. status == cmPolicies::OLD)
  377. {
  378. return true;
  379. }
  380. cmOStringStream error;
  381. error <<
  382. "Error: an attempt was made to enable the new behavior for " <<
  383. "a new feature that is in a later version of CMake than "
  384. "what you are runing, please upgrade to a newer version "
  385. "of CMake.";
  386. cmSystemTools::Error(error.str().c_str());
  387. return false;
  388. }
  389. // now we know the feature is defined, so the only issue is if someone is
  390. // setting it to WARN or OLD when the feature is REQUIRED_ALWAYS
  391. if ((status == cmPolicies::WARN ||
  392. status == cmPolicies::OLD) &&
  393. (this->Policies[id]->Status == cmPolicies::REQUIRED_ALWAYS ||
  394. this->Policies[id]->Status == cmPolicies::REQUIRED_IF_USED))
  395. {
  396. cmOStringStream error;
  397. error <<
  398. "Error: an attempt was made to enable the old behavior for " <<
  399. "a feature that is no longer supported. The feature in " <<
  400. "question is feature " <<
  401. id <<
  402. " which had new behavior introduced in CMake version " <<
  403. this->Policies[id]->GetVersionString() <<
  404. " please either update your CMakeLists files to conform to " <<
  405. "the new behavior " <<
  406. "or use an older version of CMake that still supports " <<
  407. "the old behavior. Run cmake --help-policies " <<
  408. id << " for more information.";
  409. cmSystemTools::Error(error.str().c_str());
  410. return false;
  411. }
  412. return true;
  413. }
  414. bool cmPolicies::GetPolicyID(const char *id, cmPolicies::PolicyID &pid)
  415. {
  416. if (!id || strlen(id) < 1)
  417. {
  418. return false;
  419. }
  420. std::map<std::string,cmPolicies::PolicyID>::iterator pos =
  421. this->PolicyStringMap.find(id);
  422. if (pos == this->PolicyStringMap.end())
  423. {
  424. return false;
  425. }
  426. pid = pos->second;
  427. return true;
  428. }
  429. std::string cmPolicies::GetPolicyIDString(cmPolicies::PolicyID pid)
  430. {
  431. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  432. this->Policies.find(pid);
  433. if (pos == this->Policies.end())
  434. {
  435. return "";
  436. }
  437. return pos->second->IDString;
  438. }
  439. ///! return a warning string for a given policy
  440. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  441. {
  442. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  443. this->Policies.find(id);
  444. if (pos == this->Policies.end())
  445. {
  446. cmSystemTools::Error(
  447. "Request for warning text for undefined policy!");
  448. return "Request for warning text for undefined policy!";
  449. }
  450. cmOStringStream msg;
  451. msg <<
  452. "Policy " << pos->second->IDString << " is not set: "
  453. "" << pos->second->ShortDescription << " "
  454. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  455. "policy details. "
  456. "Use the cmake_policy command to set the policy "
  457. "and suppress this warning.";
  458. return msg.str();
  459. }
  460. ///! return an error string for when a required policy is unspecified
  461. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  462. {
  463. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  464. this->Policies.find(id);
  465. if (pos == this->Policies.end())
  466. {
  467. cmSystemTools::Error(
  468. "Request for error text for undefined policy!");
  469. return "Request for warning text for undefined policy!";
  470. }
  471. cmOStringStream error;
  472. error <<
  473. "Policy " << pos->second->IDString << " is not set to NEW: "
  474. "" << pos->second->ShortDescription << " "
  475. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  476. "policy details. "
  477. "CMake now requires this policy to be set to NEW by the project. "
  478. "The policy may be set explicitly using the code\n"
  479. " cmake_policy(SET " << pos->second->IDString << " NEW)\n"
  480. "or by upgrading all policies with the code\n"
  481. " cmake_policy(VERSION " << pos->second->GetVersionString() <<
  482. ") # or later\n"
  483. "Run \"cmake --help-command cmake_policy\" for more information.";
  484. return error.str();
  485. }
  486. ///! Get the default status for a policy
  487. cmPolicies::PolicyStatus
  488. cmPolicies::GetPolicyStatus(cmPolicies::PolicyID id)
  489. {
  490. // if the policy is not know then what?
  491. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  492. this->Policies.find(id);
  493. if (pos == this->Policies.end())
  494. {
  495. // TODO is this right?
  496. return cmPolicies::WARN;
  497. }
  498. return pos->second->Status;
  499. }
  500. void cmPolicies::GetDocumentation(std::vector<cmDocumentationEntry>& v)
  501. {
  502. // now loop over all the policies and set them as appropriate
  503. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  504. = this->Policies.begin();
  505. for (;i != this->Policies.end(); ++i)
  506. {
  507. cmOStringStream full;
  508. full << i->second->LongDescription;
  509. full << "\nThis policy was introduced in CMake version ";
  510. full << i->second->GetVersionString() << ".";
  511. if(i->first != cmPolicies::CMP0000)
  512. {
  513. full << " "
  514. << "CMake version " << cmVersion::GetMajorVersion()
  515. << "." << cmVersion::GetMinorVersion() << " ";
  516. // add in some more text here based on status
  517. switch (i->second->Status)
  518. {
  519. case cmPolicies::WARN:
  520. full << "warns when the policy is not set and uses OLD behavior. "
  521. << "Use the cmake_policy command to set it to OLD or NEW "
  522. << "explicitly.";
  523. break;
  524. case cmPolicies::OLD:
  525. full << "defaults to the OLD behavior for this policy.";
  526. break;
  527. case cmPolicies::NEW:
  528. full << "defaults to the NEW behavior for this policy.";
  529. break;
  530. case cmPolicies::REQUIRED_IF_USED:
  531. full << "requires the policy to be set to NEW if you use it. "
  532. << "Use the cmake_policy command to set it to NEW.";
  533. break;
  534. case cmPolicies::REQUIRED_ALWAYS:
  535. full << "requires the policy to be set to NEW. "
  536. << "Use the cmake_policy command to set it to NEW.";
  537. break;
  538. }
  539. }
  540. cmDocumentationEntry e(i->second->IDString.c_str(),
  541. i->second->ShortDescription.c_str(),
  542. full.str().c_str());
  543. v.push_back(e);
  544. }
  545. }