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