cmPolicies.cxx 20 KB

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