cmPolicies.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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.\n"
  99. "Note that the command invocation must appear in the CMakeLists.txt "
  100. "file itself; a call in an included file is not sufficient. "
  101. "However, the cmake_policy command may be called to set policy "
  102. "CMP0000 to OLD or NEW behavior explicitly. "
  103. "The OLD behavior is to silently ignore the missing invocation. "
  104. "The NEW behavior is to issue an error instead of a warning. "
  105. "An included file may set CMP0000 explicitly to affect how this "
  106. "policy is enforced for the main CMakeLists.txt file.",
  107. 2,6,0, cmPolicies::WARN
  108. );
  109. this->DefinePolicy(
  110. CMP0001, "CMP0001",
  111. "CMAKE_BACKWARDS_COMPATIBILITY should no longer be used.",
  112. "The OLD behavior is to check CMAKE_BACKWARDS_COMPATIBILITY and present "
  113. "it to the user. "
  114. "The NEW behavior is to ignore CMAKE_BACKWARDS_COMPATIBILITY "
  115. "completely.\n"
  116. "In CMake 2.4 and below the variable CMAKE_BACKWARDS_COMPATIBILITY was "
  117. "used to request compatibility with earlier versions of CMake. "
  118. "In CMake 2.6 and above all compatibility issues are handled by policies "
  119. "and the cmake_policy command. "
  120. "However, CMake must still check CMAKE_BACKWARDS_COMPATIBILITY for "
  121. "projects written for CMake 2.4 and below.",
  122. 2,6,0, cmPolicies::WARN
  123. );
  124. this->DefinePolicy(
  125. CMP0002, "CMP0002",
  126. "Logical target names must be globally unique.",
  127. "Targets names created with "
  128. "add_executable, add_library, or add_custom_target "
  129. "are logical build target names. "
  130. "Logical target names must be globally unique because:\n"
  131. " - Unique names may be referenced unambiguously both in CMake\n"
  132. " code and on make tool command lines.\n"
  133. " - Logical names are used by Xcode and VS IDE generators\n"
  134. " to produce meaningful project names for the targets.\n"
  135. "The logical name of executable and library targets does not "
  136. "have to correspond to the physical file names built. "
  137. "Consider using the OUTPUT_NAME target property to create two "
  138. "targets with the same physical name while keeping logical "
  139. "names distinct. "
  140. "Custom targets must simply have globally unique names (unless one "
  141. "uses the global property ALLOW_DUPLICATE_CUSTOM_TARGETS with a "
  142. "Makefiles generator).",
  143. 2,6,0, cmPolicies::WARN
  144. );
  145. this->DefinePolicy(
  146. CMP0003, "CMP0003",
  147. "Libraries linked via full path no longer produce linker search paths.",
  148. "This policy affects how libraries whose full paths are NOT known "
  149. "are found at link time, but was created due to a change in how CMake "
  150. "deals with libraries whose full paths are known. "
  151. "Consider the code\n"
  152. " target_link_libraries(myexe /path/to/libA.so)\n"
  153. "CMake 2.4 and below implemented linking to libraries whose full paths "
  154. "are known by splitting them on the link line into separate components "
  155. "consisting of the linker search path and the library name. "
  156. "The example code might have produced something like\n"
  157. " ... -L/path/to -lA ...\n"
  158. "in order to link to library A. "
  159. "An analysis was performed to order multiple link directories such that "
  160. "the linker would find library A in the desired location, but there "
  161. "are cases in which this does not work. "
  162. "CMake versions 2.6 and above use the more reliable approach of passing "
  163. "the full path to libraries directly to the linker in most cases. "
  164. "The example code now produces something like\n"
  165. " ... /path/to/libA.so ....\n"
  166. "Unfortunately this change can break code like\n"
  167. " target_link_libraries(myexe /path/to/libA.so B)\n"
  168. "where \"B\" is meant to find \"/path/to/libB.so\". "
  169. "This code is wrong because the user is asking the linker to find "
  170. "library B but has not provided a linker search path (which may be "
  171. "added with the link_directories command). "
  172. "However, with the old linking implementation the code would work "
  173. "accidentally because the linker search path added for library A "
  174. "allowed library B to be found."
  175. "\n"
  176. "In order to support projects depending on linker search paths "
  177. "added by linking to libraries with known full paths, the OLD "
  178. "behavior for this policy will add the linker search paths even "
  179. "though they are not needed for their own libraries. "
  180. "When this policy is set to OLD, CMake will produce a link line such as\n"
  181. " ... -L/path/to /path/to/libA.so -lB ...\n"
  182. "which will allow library B to be found as it was previously. "
  183. "When this policy is set to NEW, CMake will produce a link line such as\n"
  184. " ... /path/to/libA.so -lB ...\n"
  185. "which more accurately matches what the project specified."
  186. "\n"
  187. "The setting for this policy used when generating the link line is that "
  188. "in effect when the target is created by an add_executable or "
  189. "add_library command. For the example described above, the code\n"
  190. " cmake_policy(SET CMP0003 OLD) # or cmake_policy(VERSION 2.4)\n"
  191. " add_executable(myexe myexe.c)\n"
  192. " target_link_libraries(myexe /path/to/libA.so B)\n"
  193. "will work and suppress the warning for this policy. "
  194. "It may also be updated to work with the corrected linking approach:\n"
  195. " cmake_policy(SET CMP0003 NEW) # or cmake_policy(VERSION 2.6)\n"
  196. " link_directories(/path/to) # needed to find library B\n"
  197. " add_executable(myexe myexe.c)\n"
  198. " target_link_libraries(myexe /path/to/libA.so B)\n"
  199. "Even better, library B may be specified with a full path:\n"
  200. " add_executable(myexe myexe.c)\n"
  201. " target_link_libraries(myexe /path/to/libA.so /path/to/libB.so)\n"
  202. "When all items on the link line have known paths CMake does not check "
  203. "this policy so it has no effect.\n"
  204. "Note that the warning for this policy will be issued for at most "
  205. "one target. This avoids flooding users with messages for every "
  206. "target when setting the policy once will probably fix all targets.",
  207. 2,6,0, cmPolicies::WARN);
  208. this->DefinePolicy(
  209. CMP0004, "CMP0004",
  210. "Libraries linked may not have leading or trailing whitespace.",
  211. "CMake versions 2.4 and below silently removed leading and trailing "
  212. "whitespace from libraries linked with code like\n"
  213. " target_link_libraries(myexe \" A \")\n"
  214. "This could lead to subtle errors in user projects.\n"
  215. "The OLD behavior for this policy is to silently remove leading and "
  216. "trailing whitespace. "
  217. "The NEW behavior for this policy is to diagnose the existence of "
  218. "such whitespace as an error. "
  219. "The setting for this policy used when checking the library names is "
  220. "that in effect when the target is created by an add_executable or "
  221. "add_library command.",
  222. 2,6,0, cmPolicies::WARN);
  223. this->DefinePolicy(
  224. CMP0005, "CMP0005",
  225. "Preprocessor definition values are now escaped automatically.",
  226. "This policy determines whether or not CMake should generate escaped "
  227. "preprocessor definition values added via add_definitions. "
  228. "CMake versions 2.4 and below assumed that only trivial values would "
  229. "be given for macros in add_definitions calls. "
  230. "It did not attempt to escape non-trivial values such as string "
  231. "literals in generated build rules. "
  232. "CMake versions 2.6 and above support escaping of most values, but "
  233. "cannot assume the user has not added escapes already in an attempt to "
  234. "work around limitations in earlier versions.\n"
  235. "The OLD behavior for this policy is to place definition values given "
  236. "to add_definitions directly in the generated build rules without "
  237. "attempting to escape anything. "
  238. "The NEW behavior for this policy is to generate correct escapes "
  239. "for all native build tools automatically. "
  240. "See documentation of the COMPILE_DEFINITIONS target property for "
  241. "limitations of the escaping implementation.",
  242. 2,6,0, cmPolicies::WARN);
  243. this->DefinePolicy(
  244. CMP0006, "CMP0006",
  245. "Installing MACOSX_BUNDLE targets requires a BUNDLE DESTINATION.",
  246. "This policy determines whether the install(TARGETS) command must be "
  247. "given a BUNDLE DESTINATION when asked to install a target with the "
  248. "MACOSX_BUNDLE property set. "
  249. "CMake 2.4 and below did not distinguish application bundles from "
  250. "normal executables when installing targets. "
  251. "CMake 2.6 provides a BUNDLE option to the install(TARGETS) command "
  252. "that specifies rules specific to application bundles on the Mac. "
  253. "Projects should use this option when installing a target with the "
  254. "MACOSX_BUNDLE property set.\n"
  255. "The OLD behavior for this policy is to fall back to the RUNTIME "
  256. "DESTINATION if a BUNDLE DESTINATION is not given. "
  257. "The NEW behavior for this policy is to produce an error if a bundle "
  258. "target is installed without a BUNDLE DESTINATION.",
  259. 2,6,0, cmPolicies::WARN);
  260. this->DefinePolicy(
  261. CMP0007, "CMP0007",
  262. "list command no longer ignores empty elements.",
  263. "This policy determines whether the list command will "
  264. "ignore empty elements in the list. "
  265. "CMake 2.4 and below list commands ignored all empty elements"
  266. " in the list. For example, a;b;;c would have length 3 and not 4. "
  267. "The OLD behavior for this policy is to ignore empty list elements. "
  268. "The NEW behavior for this policy is to correctly count empty "
  269. "elements in a list. ",
  270. 2,6,0, cmPolicies::WARN);
  271. this->DefinePolicy(
  272. CMP0008, "CMP0008",
  273. "Libraries linked by full-path must have a valid library file name.",
  274. "In CMake 2.4 and below it is possible to write code like\n"
  275. " target_link_libraries(myexe /full/path/to/somelib)\n"
  276. "where \"somelib\" is supposed to be a valid library file name "
  277. "such as \"libsomelib.a\" or \"somelib.lib\". "
  278. "For Makefile generators this produces an error at build time "
  279. "because the dependency on the full path cannot be found. "
  280. "For VS IDE and Xcode generators this used to work by accident because "
  281. "CMake would always split off the library directory and ask the "
  282. "linker to search for the library by name (-lsomelib or somelib.lib). "
  283. "Despite the failure with Makefiles, some projects have code like this "
  284. "and build only with VS and/or Xcode. "
  285. "This version of CMake prefers to pass the full path directly to the "
  286. "native build tool, which will fail in this case because it does "
  287. "not name a valid library file."
  288. "\n"
  289. "This policy determines what to do with full paths that do not appear "
  290. "to name a valid library file. "
  291. "The OLD behavior for this policy is to split the library name from the "
  292. "path and ask the linker to search for it. "
  293. "The NEW behavior for this policy is to trust the given path and "
  294. "pass it directly to the native build tool unchanged.",
  295. 2,6,1, cmPolicies::WARN);
  296. this->DefinePolicy(
  297. CMP0009, "CMP0009",
  298. "FILE GLOB_RECURSE calls should not follow symlinks by default.",
  299. "In CMake 2.6.1 and below, FILE GLOB_RECURSE calls would follow "
  300. "through symlinks, sometimes coming up with unexpectedly large "
  301. "result sets because of symlinks to top level directories that "
  302. "contain hundreds of thousands of files."
  303. "\n"
  304. "This policy determines whether or not to follow symlinks "
  305. "encountered during a FILE GLOB_RECURSE call. "
  306. "The OLD behavior for this policy is to follow the symlinks. "
  307. "The NEW behavior for this policy is not to follow the symlinks "
  308. "by default, but only if FOLLOW_SYMLINKS is given as an additional "
  309. "argument to the FILE command.",
  310. 2,6,2, cmPolicies::WARN);
  311. this->DefinePolicy(
  312. CMP0010, "CMP0010",
  313. "Bad variable reference syntax is an error.",
  314. "In CMake 2.6.2 and below, incorrect variable reference syntax such as "
  315. "a missing close-brace (\"${FOO\") was reported but did not stop "
  316. "processing of CMake code. "
  317. "This policy determines whether a bad variable reference is an error. "
  318. "The OLD behavior for this policy is to warn about the error, leave "
  319. "the string untouched, and continue. "
  320. "The NEW behavior for this policy is to report an error.",
  321. 2,6,3, cmPolicies::WARN);
  322. }
  323. cmPolicies::~cmPolicies()
  324. {
  325. // free the policies
  326. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  327. = this->Policies.begin();
  328. for (;i != this->Policies.end(); ++i)
  329. {
  330. delete i->second;
  331. }
  332. }
  333. void cmPolicies::DefinePolicy(cmPolicies::PolicyID iD,
  334. const char *idString,
  335. const char *shortDescription,
  336. const char *longDescription,
  337. unsigned int majorVersionIntroduced,
  338. unsigned int minorVersionIntroduced,
  339. unsigned int patchVersionIntroduced,
  340. cmPolicies::PolicyStatus status)
  341. {
  342. // a policy must be unique and can only be defined once
  343. if (this->Policies.find(iD) != this->Policies.end())
  344. {
  345. cmSystemTools::Error("Attempt to redefine a CMake policy for policy "
  346. "ID ", this->GetPolicyIDString(iD).c_str());
  347. return;
  348. }
  349. this->Policies[iD] = new cmPolicy(iD, idString,
  350. shortDescription,
  351. longDescription,
  352. majorVersionIntroduced,
  353. minorVersionIntroduced,
  354. patchVersionIntroduced,
  355. status);
  356. this->PolicyStringMap[idString] = iD;
  357. }
  358. //----------------------------------------------------------------------------
  359. bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
  360. const char *version)
  361. {
  362. std::string ver = "2.4.0";
  363. if (version && strlen(version) > 0)
  364. {
  365. ver = version;
  366. }
  367. unsigned int majorVer = 2;
  368. unsigned int minorVer = 0;
  369. unsigned int patchVer = 0;
  370. // parse the string
  371. if(sscanf(ver.c_str(), "%u.%u.%u",
  372. &majorVer, &minorVer, &patchVer) < 2)
  373. {
  374. cmOStringStream e;
  375. e << "Invalid policy version value \"" << ver << "\". "
  376. << "A numeric major.minor[.patch] must be given.";
  377. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  378. return false;
  379. }
  380. // it is an error if the policy version is less than 2.4
  381. if (majorVer < 2 || majorVer == 2 && minorVer < 4)
  382. {
  383. mf->IssueMessage(cmake::FATAL_ERROR,
  384. "An attempt was made to set the policy version of CMake to something "
  385. "earlier than \"2.4\". "
  386. "In CMake 2.4 and below backwards compatibility was handled with the "
  387. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  388. "In order to get compatibility features supporting versions earlier "
  389. "than 2.4 set policy CMP0001 to OLD to tell CMake to check the "
  390. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  391. "One way to do this is to set the policy version to 2.4 exactly."
  392. );
  393. return false;
  394. }
  395. // It is an error if the policy version is greater than the running
  396. // CMake.
  397. if (majorVer > cmVersion::GetMajorVersion() ||
  398. (majorVer == cmVersion::GetMajorVersion() &&
  399. minorVer > cmVersion::GetMinorVersion()) ||
  400. (majorVer == cmVersion::GetMajorVersion() &&
  401. minorVer == cmVersion::GetMinorVersion() &&
  402. patchVer > cmVersion::GetPatchVersion()))
  403. {
  404. cmOStringStream e;
  405. e << "An attempt was made to set the policy version of CMake to \""
  406. << version << "\" which is greater than this version of CMake. "
  407. << "This is not allowed because the greater version may have new "
  408. << "policies not known to this CMake. "
  409. << "You may need a newer CMake version to build this project.";
  410. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  411. return false;
  412. }
  413. // now loop over all the policies and set them as appropriate
  414. std::vector<cmPolicies::PolicyID> ancientPolicies;
  415. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  416. = this->Policies.begin();
  417. for (;i != this->Policies.end(); ++i)
  418. {
  419. if (i->second->IsPolicyNewerThan(majorVer,minorVer,patchVer))
  420. {
  421. if(i->second->Status == cmPolicies::REQUIRED_ALWAYS)
  422. {
  423. ancientPolicies.push_back(i->first);
  424. }
  425. else if (!mf->SetPolicy(i->second->ID, cmPolicies::WARN))
  426. {
  427. return false;
  428. }
  429. }
  430. else
  431. {
  432. if (!mf->SetPolicy(i->second->ID, cmPolicies::NEW))
  433. {
  434. return false;
  435. }
  436. }
  437. }
  438. // Make sure the project does not use any ancient policies.
  439. if(!ancientPolicies.empty())
  440. {
  441. this->DiagnoseAncientPolicies(ancientPolicies,
  442. majorVer, minorVer, patchVer, mf);
  443. cmSystemTools::SetFatalErrorOccured();
  444. return false;
  445. }
  446. return true;
  447. }
  448. bool cmPolicies::GetPolicyID(const char *id, cmPolicies::PolicyID &pid)
  449. {
  450. if (!id || strlen(id) < 1)
  451. {
  452. return false;
  453. }
  454. std::map<std::string,cmPolicies::PolicyID>::iterator pos =
  455. this->PolicyStringMap.find(id);
  456. if (pos == this->PolicyStringMap.end())
  457. {
  458. return false;
  459. }
  460. pid = pos->second;
  461. return true;
  462. }
  463. std::string cmPolicies::GetPolicyIDString(cmPolicies::PolicyID pid)
  464. {
  465. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  466. this->Policies.find(pid);
  467. if (pos == this->Policies.end())
  468. {
  469. return "";
  470. }
  471. return pos->second->IDString;
  472. }
  473. ///! return a warning string for a given policy
  474. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  475. {
  476. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  477. this->Policies.find(id);
  478. if (pos == this->Policies.end())
  479. {
  480. cmSystemTools::Error(
  481. "Request for warning text for undefined policy!");
  482. return "Request for warning text for undefined policy!";
  483. }
  484. cmOStringStream msg;
  485. msg <<
  486. "Policy " << pos->second->IDString << " is not set: "
  487. "" << pos->second->ShortDescription << " "
  488. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  489. "policy details. "
  490. "Use the cmake_policy command to set the policy "
  491. "and suppress this warning.";
  492. return msg.str();
  493. }
  494. ///! return an error string for when a required policy is unspecified
  495. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  496. {
  497. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  498. this->Policies.find(id);
  499. if (pos == this->Policies.end())
  500. {
  501. cmSystemTools::Error(
  502. "Request for error text for undefined policy!");
  503. return "Request for warning text for undefined policy!";
  504. }
  505. cmOStringStream error;
  506. error <<
  507. "Policy " << pos->second->IDString << " is not set to NEW: "
  508. "" << pos->second->ShortDescription << " "
  509. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  510. "policy details. "
  511. "CMake now requires this policy to be set to NEW by the project. "
  512. "The policy may be set explicitly using the code\n"
  513. " cmake_policy(SET " << pos->second->IDString << " NEW)\n"
  514. "or by upgrading all policies with the code\n"
  515. " cmake_policy(VERSION " << pos->second->GetVersionString() <<
  516. ") # or later\n"
  517. "Run \"cmake --help-command cmake_policy\" for more information.";
  518. return error.str();
  519. }
  520. ///! Get the default status for a policy
  521. cmPolicies::PolicyStatus
  522. cmPolicies::GetPolicyStatus(cmPolicies::PolicyID id)
  523. {
  524. // if the policy is not know then what?
  525. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  526. this->Policies.find(id);
  527. if (pos == this->Policies.end())
  528. {
  529. // TODO is this right?
  530. return cmPolicies::WARN;
  531. }
  532. return pos->second->Status;
  533. }
  534. void cmPolicies::GetDocumentation(std::vector<cmDocumentationEntry>& v)
  535. {
  536. // now loop over all the policies and set them as appropriate
  537. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  538. = this->Policies.begin();
  539. for (;i != this->Policies.end(); ++i)
  540. {
  541. cmOStringStream full;
  542. full << i->second->LongDescription;
  543. full << "\nThis policy was introduced in CMake version ";
  544. full << i->second->GetVersionString() << ".";
  545. if(i->first != cmPolicies::CMP0000)
  546. {
  547. full << " "
  548. << "CMake version " << cmVersion::GetMajorVersion()
  549. << "." << cmVersion::GetMinorVersion() << " ";
  550. // add in some more text here based on status
  551. switch (i->second->Status)
  552. {
  553. case cmPolicies::WARN:
  554. full << "warns when the policy is not set and uses OLD behavior. "
  555. << "Use the cmake_policy command to set it to OLD or NEW "
  556. << "explicitly.";
  557. break;
  558. case cmPolicies::OLD:
  559. full << "defaults to the OLD behavior for this policy.";
  560. break;
  561. case cmPolicies::NEW:
  562. full << "defaults to the NEW behavior for this policy.";
  563. break;
  564. case cmPolicies::REQUIRED_IF_USED:
  565. full << "requires the policy to be set to NEW if you use it. "
  566. << "Use the cmake_policy command to set it to NEW.";
  567. break;
  568. case cmPolicies::REQUIRED_ALWAYS:
  569. full << "requires the policy to be set to NEW. "
  570. << "Use the cmake_policy command to set it to NEW.";
  571. break;
  572. }
  573. }
  574. cmDocumentationEntry e(i->second->IDString.c_str(),
  575. i->second->ShortDescription.c_str(),
  576. full.str().c_str());
  577. v.push_back(e);
  578. }
  579. }
  580. //----------------------------------------------------------------------------
  581. std::string
  582. cmPolicies::GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id)
  583. {
  584. std::string pid = this->GetPolicyIDString(id);
  585. cmOStringStream e;
  586. e << "Policy " << pid << " may not be set to OLD behavior because this "
  587. << "version of CMake no longer supports it. "
  588. << "The policy was introduced in "
  589. << "CMake version " << this->Policies[id]->GetVersionString()
  590. << ", and use of NEW behavior is now required."
  591. << "\n"
  592. << "Please either update your CMakeLists.txt files to conform to "
  593. << "the new behavior or use an older version of CMake that still "
  594. << "supports the old behavior. "
  595. << "Run cmake --help-policy " << pid << " for more information.";
  596. return e.str();
  597. }
  598. //----------------------------------------------------------------------------
  599. void
  600. cmPolicies::DiagnoseAncientPolicies(std::vector<PolicyID> const& ancient,
  601. unsigned int majorVer,
  602. unsigned int minorVer,
  603. unsigned int patchVer,
  604. cmMakefile* mf)
  605. {
  606. cmOStringStream e;
  607. e << "The project requests behavior compatible with CMake version \""
  608. << majorVer << "." << minorVer << "." << patchVer
  609. << "\", which requires OLD the behavior for some policies:\n";
  610. for(std::vector<PolicyID>::const_iterator
  611. i = ancient.begin(); i != ancient.end(); ++i)
  612. {
  613. cmPolicy const* policy = this->Policies[*i];
  614. e << " " << policy->IDString << ": " << policy->ShortDescription << "\n";
  615. }
  616. e << "However, this version of CMake no longer supports the OLD "
  617. << "behavior for these policies. "
  618. << "Please either update your CMakeLists.txt files to conform to "
  619. << "the new behavior or use an older version of CMake that still "
  620. << "supports the old behavior.";
  621. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  622. }