cmPolicies.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. this->DefinePolicy(
  323. CMP0011, "CMP0011",
  324. "Included scripts do automatic cmake_policy PUSH and POP.",
  325. "In CMake 2.6.2 and below, CMake Policy settings in scripts loaded by "
  326. "the include() and find_package() commands would affect the includer. "
  327. "Explicit invocations of cmake_policy(PUSH) and cmake_policy(POP) were "
  328. "required to isolate policy changes and protect the includer. "
  329. "While some scripts intend to affect the policies of their includer, "
  330. "most do not. "
  331. "In CMake 2.6.3 and above, include() and find_package() by default PUSH "
  332. "and POP an entry on the policy stack around an included script, "
  333. "but provide a NO_POLICY_SCOPE option to disable it. "
  334. "This policy determines whether or not to imply NO_POLICY_SCOPE for "
  335. "compatibility. "
  336. "The OLD behavior for this policy is to imply NO_POLICY_SCOPE for "
  337. "include() and find_package() commands. "
  338. "The NEW behavior for this policy is to allow the commands to do their "
  339. "default cmake_policy PUSH and POP.",
  340. 2,6,3, cmPolicies::WARN);
  341. this->DefinePolicy(
  342. CMP0012, "CMP0012",
  343. "The if() command can recognize named boolean constants.",
  344. "In CMake versions prior to 2.6.5 the only boolean constants were 0 "
  345. "and 1. Other boolean constants such as true, false, yes, no, "
  346. "on, off, y, n, notfound, ignore (all case insensitive) were recognized "
  347. "in some cases but not all. In later versions of cmake these values are "
  348. "treated as boolean constants more consistently and should not be used "
  349. "as variable names. "
  350. "The OLD behavior for this policy is to allow variables to have names "
  351. "such as true and to dereference them. "
  352. "The NEW behavior for this policy is to treat strings like true as a "
  353. "boolean constant.",
  354. 2,6,5, cmPolicies::WARN);
  355. this->DefinePolicy(
  356. CMP0013, "CMP0013",
  357. "Duplicate binary directories are not allowed.",
  358. "CMake 2.6.3 and below silently permitted add_subdirectory() calls "
  359. "to create the same binary directory multiple times. "
  360. "During build system generation files would be written and then "
  361. "overwritten in the build tree and could lead to strange behavior. "
  362. "CMake 2.6.4 and above explicitly detect duplicate binary directories. "
  363. "CMake 2.6.4 always considers this case an error. "
  364. "In CMake 2.6.5 and above this policy determines whether or not "
  365. "the case is an error. "
  366. "The OLD behavior for this policy is to allow duplicate binary "
  367. "directories. "
  368. "The NEW behavior for this policy is to disallow duplicate binary "
  369. "directories with an error.",
  370. 2,6,5, cmPolicies::WARN);
  371. }
  372. cmPolicies::~cmPolicies()
  373. {
  374. // free the policies
  375. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  376. = this->Policies.begin();
  377. for (;i != this->Policies.end(); ++i)
  378. {
  379. delete i->second;
  380. }
  381. }
  382. void cmPolicies::DefinePolicy(cmPolicies::PolicyID iD,
  383. const char *idString,
  384. const char *shortDescription,
  385. const char *longDescription,
  386. unsigned int majorVersionIntroduced,
  387. unsigned int minorVersionIntroduced,
  388. unsigned int patchVersionIntroduced,
  389. cmPolicies::PolicyStatus status)
  390. {
  391. // a policy must be unique and can only be defined once
  392. if (this->Policies.find(iD) != this->Policies.end())
  393. {
  394. cmSystemTools::Error("Attempt to redefine a CMake policy for policy "
  395. "ID ", this->GetPolicyIDString(iD).c_str());
  396. return;
  397. }
  398. this->Policies[iD] = new cmPolicy(iD, idString,
  399. shortDescription,
  400. longDescription,
  401. majorVersionIntroduced,
  402. minorVersionIntroduced,
  403. patchVersionIntroduced,
  404. status);
  405. this->PolicyStringMap[idString] = iD;
  406. }
  407. //----------------------------------------------------------------------------
  408. bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
  409. const char *version)
  410. {
  411. std::string ver = "2.4.0";
  412. if (version && strlen(version) > 0)
  413. {
  414. ver = version;
  415. }
  416. unsigned int majorVer = 2;
  417. unsigned int minorVer = 0;
  418. unsigned int patchVer = 0;
  419. // parse the string
  420. if(sscanf(ver.c_str(), "%u.%u.%u",
  421. &majorVer, &minorVer, &patchVer) < 2)
  422. {
  423. cmOStringStream e;
  424. e << "Invalid policy version value \"" << ver << "\". "
  425. << "A numeric major.minor[.patch] must be given.";
  426. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  427. return false;
  428. }
  429. // it is an error if the policy version is less than 2.4
  430. if (majorVer < 2 || majorVer == 2 && minorVer < 4)
  431. {
  432. mf->IssueMessage(cmake::FATAL_ERROR,
  433. "An attempt was made to set the policy version of CMake to something "
  434. "earlier than \"2.4\". "
  435. "In CMake 2.4 and below backwards compatibility was handled with the "
  436. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  437. "In order to get compatibility features supporting versions earlier "
  438. "than 2.4 set policy CMP0001 to OLD to tell CMake to check the "
  439. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  440. "One way to do this is to set the policy version to 2.4 exactly."
  441. );
  442. return false;
  443. }
  444. // It is an error if the policy version is greater than the running
  445. // CMake.
  446. if (majorVer > cmVersion::GetMajorVersion() ||
  447. (majorVer == cmVersion::GetMajorVersion() &&
  448. minorVer > cmVersion::GetMinorVersion()) ||
  449. (majorVer == cmVersion::GetMajorVersion() &&
  450. minorVer == cmVersion::GetMinorVersion() &&
  451. patchVer > cmVersion::GetPatchVersion()))
  452. {
  453. cmOStringStream e;
  454. e << "An attempt was made to set the policy version of CMake to \""
  455. << version << "\" which is greater than this version of CMake. "
  456. << "This is not allowed because the greater version may have new "
  457. << "policies not known to this CMake. "
  458. << "You may need a newer CMake version to build this project.";
  459. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  460. return false;
  461. }
  462. // now loop over all the policies and set them as appropriate
  463. std::vector<cmPolicies::PolicyID> ancientPolicies;
  464. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  465. = this->Policies.begin();
  466. for (;i != this->Policies.end(); ++i)
  467. {
  468. if (i->second->IsPolicyNewerThan(majorVer,minorVer,patchVer))
  469. {
  470. if(i->second->Status == cmPolicies::REQUIRED_ALWAYS)
  471. {
  472. ancientPolicies.push_back(i->first);
  473. }
  474. else if (!mf->SetPolicy(i->second->ID, cmPolicies::WARN))
  475. {
  476. return false;
  477. }
  478. }
  479. else
  480. {
  481. if (!mf->SetPolicy(i->second->ID, cmPolicies::NEW))
  482. {
  483. return false;
  484. }
  485. }
  486. }
  487. // Make sure the project does not use any ancient policies.
  488. if(!ancientPolicies.empty())
  489. {
  490. this->DiagnoseAncientPolicies(ancientPolicies,
  491. majorVer, minorVer, patchVer, mf);
  492. cmSystemTools::SetFatalErrorOccured();
  493. return false;
  494. }
  495. return true;
  496. }
  497. bool cmPolicies::GetPolicyID(const char *id, cmPolicies::PolicyID &pid)
  498. {
  499. if (!id || strlen(id) < 1)
  500. {
  501. return false;
  502. }
  503. std::map<std::string,cmPolicies::PolicyID>::iterator pos =
  504. this->PolicyStringMap.find(id);
  505. if (pos == this->PolicyStringMap.end())
  506. {
  507. return false;
  508. }
  509. pid = pos->second;
  510. return true;
  511. }
  512. std::string cmPolicies::GetPolicyIDString(cmPolicies::PolicyID pid)
  513. {
  514. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  515. this->Policies.find(pid);
  516. if (pos == this->Policies.end())
  517. {
  518. return "";
  519. }
  520. return pos->second->IDString;
  521. }
  522. ///! return a warning string for a given policy
  523. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  524. {
  525. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  526. this->Policies.find(id);
  527. if (pos == this->Policies.end())
  528. {
  529. cmSystemTools::Error(
  530. "Request for warning text for undefined policy!");
  531. return "Request for warning text for undefined policy!";
  532. }
  533. cmOStringStream msg;
  534. msg <<
  535. "Policy " << pos->second->IDString << " is not set: "
  536. "" << pos->second->ShortDescription << " "
  537. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  538. "policy details. "
  539. "Use the cmake_policy command to set the policy "
  540. "and suppress this warning.";
  541. return msg.str();
  542. }
  543. ///! return an error string for when a required policy is unspecified
  544. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  545. {
  546. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  547. this->Policies.find(id);
  548. if (pos == this->Policies.end())
  549. {
  550. cmSystemTools::Error(
  551. "Request for error text for undefined policy!");
  552. return "Request for warning text for undefined policy!";
  553. }
  554. cmOStringStream error;
  555. error <<
  556. "Policy " << pos->second->IDString << " is not set to NEW: "
  557. "" << pos->second->ShortDescription << " "
  558. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  559. "policy details. "
  560. "CMake now requires this policy to be set to NEW by the project. "
  561. "The policy may be set explicitly using the code\n"
  562. " cmake_policy(SET " << pos->second->IDString << " NEW)\n"
  563. "or by upgrading all policies with the code\n"
  564. " cmake_policy(VERSION " << pos->second->GetVersionString() <<
  565. ") # or later\n"
  566. "Run \"cmake --help-command cmake_policy\" for more information.";
  567. return error.str();
  568. }
  569. ///! Get the default status for a policy
  570. cmPolicies::PolicyStatus
  571. cmPolicies::GetPolicyStatus(cmPolicies::PolicyID id)
  572. {
  573. // if the policy is not know then what?
  574. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  575. this->Policies.find(id);
  576. if (pos == this->Policies.end())
  577. {
  578. // TODO is this right?
  579. return cmPolicies::WARN;
  580. }
  581. return pos->second->Status;
  582. }
  583. void cmPolicies::GetDocumentation(std::vector<cmDocumentationEntry>& v)
  584. {
  585. // now loop over all the policies and set them as appropriate
  586. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  587. = this->Policies.begin();
  588. for (;i != this->Policies.end(); ++i)
  589. {
  590. cmOStringStream full;
  591. full << i->second->LongDescription;
  592. full << "\nThis policy was introduced in CMake version ";
  593. full << i->second->GetVersionString() << ".";
  594. if(i->first != cmPolicies::CMP0000)
  595. {
  596. full << " "
  597. << "CMake version " << cmVersion::GetMajorVersion()
  598. << "." << cmVersion::GetMinorVersion() << " ";
  599. // add in some more text here based on status
  600. switch (i->second->Status)
  601. {
  602. case cmPolicies::WARN:
  603. full << "warns when the policy is not set and uses OLD behavior. "
  604. << "Use the cmake_policy command to set it to OLD or NEW "
  605. << "explicitly.";
  606. break;
  607. case cmPolicies::OLD:
  608. full << "defaults to the OLD behavior for this policy.";
  609. break;
  610. case cmPolicies::NEW:
  611. full << "defaults to the NEW behavior for this policy.";
  612. break;
  613. case cmPolicies::REQUIRED_IF_USED:
  614. full << "requires the policy to be set to NEW if you use it. "
  615. << "Use the cmake_policy command to set it to NEW.";
  616. break;
  617. case cmPolicies::REQUIRED_ALWAYS:
  618. full << "requires the policy to be set to NEW. "
  619. << "Use the cmake_policy command to set it to NEW.";
  620. break;
  621. }
  622. }
  623. cmDocumentationEntry e(i->second->IDString.c_str(),
  624. i->second->ShortDescription.c_str(),
  625. full.str().c_str());
  626. v.push_back(e);
  627. }
  628. }
  629. //----------------------------------------------------------------------------
  630. std::string
  631. cmPolicies::GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id)
  632. {
  633. std::string pid = this->GetPolicyIDString(id);
  634. cmOStringStream e;
  635. e << "Policy " << pid << " may not be set to OLD behavior because this "
  636. << "version of CMake no longer supports it. "
  637. << "The policy was introduced in "
  638. << "CMake version " << this->Policies[id]->GetVersionString()
  639. << ", and use of NEW behavior is now required."
  640. << "\n"
  641. << "Please either update your CMakeLists.txt files to conform to "
  642. << "the new behavior or use an older version of CMake that still "
  643. << "supports the old behavior. "
  644. << "Run cmake --help-policy " << pid << " for more information.";
  645. return e.str();
  646. }
  647. //----------------------------------------------------------------------------
  648. void
  649. cmPolicies::DiagnoseAncientPolicies(std::vector<PolicyID> const& ancient,
  650. unsigned int majorVer,
  651. unsigned int minorVer,
  652. unsigned int patchVer,
  653. cmMakefile* mf)
  654. {
  655. cmOStringStream e;
  656. e << "The project requests behavior compatible with CMake version \""
  657. << majorVer << "." << minorVer << "." << patchVer
  658. << "\", which requires OLD the behavior for some policies:\n";
  659. for(std::vector<PolicyID>::const_iterator
  660. i = ancient.begin(); i != ancient.end(); ++i)
  661. {
  662. cmPolicy const* policy = this->Policies[*i];
  663. e << " " << policy->IDString << ": " << policy->ShortDescription << "\n";
  664. }
  665. e << "However, this version of CMake no longer supports the OLD "
  666. << "behavior for these policies. "
  667. << "Please either update your CMakeLists.txt files to conform to "
  668. << "the new behavior or use an older version of CMake that still "
  669. << "supports the old behavior.";
  670. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  671. }