cmPolicies.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. this->DefinePolicy(
  372. CMP0014, "CMP0014",
  373. "Input directories must have CMakeLists.txt.",
  374. "CMake versions before 2.8 silently ignored missing CMakeLists.txt "
  375. "files in directories referenced by add_subdirectory() or subdirs(), "
  376. "treating them as if present but empty. "
  377. "In CMake 2.8.0 and above this policy determines whether or not "
  378. "the case is an error. "
  379. "The OLD behavior for this policy is to silently ignore the problem. "
  380. "The NEW behavior for this policy is to report an error.",
  381. 2,8,0, cmPolicies::WARN);
  382. this->DefinePolicy(
  383. CMP0015, "CMP0015",
  384. "The set() CACHE mode and option() command make the cache value visible.",
  385. "In CMake 2.6 and below the CACHE mode of the set() command and the "
  386. "option() command did not expose the value from the named cache entry "
  387. "if it was already set both in the cache and as a local variable. "
  388. "This led to subtle differences between first and later configurations "
  389. "because a conflicting local variable would be overridden only when the "
  390. "cache value was first created. "
  391. "For example, the code\n"
  392. " set(x 1)\n"
  393. " set(before ${x})\n"
  394. " set(x 2 CACHE STRING \"X\")\n"
  395. " set(after ${x})\n"
  396. " message(STATUS \"${before},${after}\")\n"
  397. "would print \"1,2\" on the first run and \"1,1\" on future runs."
  398. "\n"
  399. "CMake 2.8.0 and above prefer to expose the cache value in all cases by "
  400. "removing the local variable definition, but this changes behavior in "
  401. "subtle cases when the local variable has a different value than that "
  402. "exposed from the cache. "
  403. "The example above will always print \"1,2\"."
  404. "\n"
  405. "This policy determines whether the commands should always expose the "
  406. "cache value. "
  407. "The OLD behavior for this policy is to leave conflicting local "
  408. "variable values untouched and hide the true cache value. "
  409. "The NEW behavior for this policy is to always expose the cache value.",
  410. 2,8,0, cmPolicies::WARN);
  411. }
  412. cmPolicies::~cmPolicies()
  413. {
  414. // free the policies
  415. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  416. = this->Policies.begin();
  417. for (;i != this->Policies.end(); ++i)
  418. {
  419. delete i->second;
  420. }
  421. }
  422. void cmPolicies::DefinePolicy(cmPolicies::PolicyID iD,
  423. const char *idString,
  424. const char *shortDescription,
  425. const char *longDescription,
  426. unsigned int majorVersionIntroduced,
  427. unsigned int minorVersionIntroduced,
  428. unsigned int patchVersionIntroduced,
  429. cmPolicies::PolicyStatus status)
  430. {
  431. // a policy must be unique and can only be defined once
  432. if (this->Policies.find(iD) != this->Policies.end())
  433. {
  434. cmSystemTools::Error("Attempt to redefine a CMake policy for policy "
  435. "ID ", this->GetPolicyIDString(iD).c_str());
  436. return;
  437. }
  438. this->Policies[iD] = new cmPolicy(iD, idString,
  439. shortDescription,
  440. longDescription,
  441. majorVersionIntroduced,
  442. minorVersionIntroduced,
  443. patchVersionIntroduced,
  444. status);
  445. this->PolicyStringMap[idString] = iD;
  446. }
  447. //----------------------------------------------------------------------------
  448. bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
  449. const char *version)
  450. {
  451. std::string ver = "2.4.0";
  452. if (version && strlen(version) > 0)
  453. {
  454. ver = version;
  455. }
  456. unsigned int majorVer = 2;
  457. unsigned int minorVer = 0;
  458. unsigned int patchVer = 0;
  459. // parse the string
  460. if(sscanf(ver.c_str(), "%u.%u.%u",
  461. &majorVer, &minorVer, &patchVer) < 2)
  462. {
  463. cmOStringStream e;
  464. e << "Invalid policy version value \"" << ver << "\". "
  465. << "A numeric major.minor[.patch] must be given.";
  466. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  467. return false;
  468. }
  469. // it is an error if the policy version is less than 2.4
  470. if (majorVer < 2 || (majorVer == 2 && minorVer < 4))
  471. {
  472. mf->IssueMessage(cmake::FATAL_ERROR,
  473. "An attempt was made to set the policy version of CMake to something "
  474. "earlier than \"2.4\". "
  475. "In CMake 2.4 and below backwards compatibility was handled with the "
  476. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  477. "In order to get compatibility features supporting versions earlier "
  478. "than 2.4 set policy CMP0001 to OLD to tell CMake to check the "
  479. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  480. "One way to do this is to set the policy version to 2.4 exactly."
  481. );
  482. return false;
  483. }
  484. // It is an error if the policy version is greater than the running
  485. // CMake.
  486. if (majorVer > cmVersion::GetMajorVersion() ||
  487. (majorVer == cmVersion::GetMajorVersion() &&
  488. minorVer > cmVersion::GetMinorVersion()) ||
  489. (majorVer == cmVersion::GetMajorVersion() &&
  490. minorVer == cmVersion::GetMinorVersion() &&
  491. patchVer > cmVersion::GetPatchVersion()))
  492. {
  493. cmOStringStream e;
  494. e << "An attempt was made to set the policy version of CMake to \""
  495. << version << "\" which is greater than this version of CMake. "
  496. << "This is not allowed because the greater version may have new "
  497. << "policies not known to this CMake. "
  498. << "You may need a newer CMake version to build this project.";
  499. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  500. return false;
  501. }
  502. // now loop over all the policies and set them as appropriate
  503. std::vector<cmPolicies::PolicyID> ancientPolicies;
  504. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  505. = this->Policies.begin();
  506. for (;i != this->Policies.end(); ++i)
  507. {
  508. if (i->second->IsPolicyNewerThan(majorVer,minorVer,patchVer))
  509. {
  510. if(i->second->Status == cmPolicies::REQUIRED_ALWAYS)
  511. {
  512. ancientPolicies.push_back(i->first);
  513. }
  514. else if (!mf->SetPolicy(i->second->ID, cmPolicies::WARN))
  515. {
  516. return false;
  517. }
  518. }
  519. else
  520. {
  521. if (!mf->SetPolicy(i->second->ID, cmPolicies::NEW))
  522. {
  523. return false;
  524. }
  525. }
  526. }
  527. // Make sure the project does not use any ancient policies.
  528. if(!ancientPolicies.empty())
  529. {
  530. this->DiagnoseAncientPolicies(ancientPolicies,
  531. majorVer, minorVer, patchVer, mf);
  532. cmSystemTools::SetFatalErrorOccured();
  533. return false;
  534. }
  535. return true;
  536. }
  537. bool cmPolicies::GetPolicyID(const char *id, cmPolicies::PolicyID &pid)
  538. {
  539. if (!id || strlen(id) < 1)
  540. {
  541. return false;
  542. }
  543. std::map<std::string,cmPolicies::PolicyID>::iterator pos =
  544. this->PolicyStringMap.find(id);
  545. if (pos == this->PolicyStringMap.end())
  546. {
  547. return false;
  548. }
  549. pid = pos->second;
  550. return true;
  551. }
  552. std::string cmPolicies::GetPolicyIDString(cmPolicies::PolicyID pid)
  553. {
  554. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  555. this->Policies.find(pid);
  556. if (pos == this->Policies.end())
  557. {
  558. return "";
  559. }
  560. return pos->second->IDString;
  561. }
  562. ///! return a warning string for a given policy
  563. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  564. {
  565. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  566. this->Policies.find(id);
  567. if (pos == this->Policies.end())
  568. {
  569. cmSystemTools::Error(
  570. "Request for warning text for undefined policy!");
  571. return "Request for warning text for undefined policy!";
  572. }
  573. cmOStringStream msg;
  574. msg <<
  575. "Policy " << pos->second->IDString << " is not set: "
  576. "" << pos->second->ShortDescription << " "
  577. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  578. "policy details. "
  579. "Use the cmake_policy command to set the policy "
  580. "and suppress this warning.";
  581. return msg.str();
  582. }
  583. ///! return an error string for when a required policy is unspecified
  584. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  585. {
  586. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  587. this->Policies.find(id);
  588. if (pos == this->Policies.end())
  589. {
  590. cmSystemTools::Error(
  591. "Request for error text for undefined policy!");
  592. return "Request for warning text for undefined policy!";
  593. }
  594. cmOStringStream error;
  595. error <<
  596. "Policy " << pos->second->IDString << " is not set to NEW: "
  597. "" << pos->second->ShortDescription << " "
  598. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  599. "policy details. "
  600. "CMake now requires this policy to be set to NEW by the project. "
  601. "The policy may be set explicitly using the code\n"
  602. " cmake_policy(SET " << pos->second->IDString << " NEW)\n"
  603. "or by upgrading all policies with the code\n"
  604. " cmake_policy(VERSION " << pos->second->GetVersionString() <<
  605. ") # or later\n"
  606. "Run \"cmake --help-command cmake_policy\" for more information.";
  607. return error.str();
  608. }
  609. ///! Get the default status for a policy
  610. cmPolicies::PolicyStatus
  611. cmPolicies::GetPolicyStatus(cmPolicies::PolicyID id)
  612. {
  613. // if the policy is not know then what?
  614. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  615. this->Policies.find(id);
  616. if (pos == this->Policies.end())
  617. {
  618. // TODO is this right?
  619. return cmPolicies::WARN;
  620. }
  621. return pos->second->Status;
  622. }
  623. void cmPolicies::GetDocumentation(std::vector<cmDocumentationEntry>& v)
  624. {
  625. // now loop over all the policies and set them as appropriate
  626. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  627. = this->Policies.begin();
  628. for (;i != this->Policies.end(); ++i)
  629. {
  630. cmOStringStream full;
  631. full << i->second->LongDescription;
  632. full << "\nThis policy was introduced in CMake version ";
  633. full << i->second->GetVersionString() << ".";
  634. if(i->first != cmPolicies::CMP0000)
  635. {
  636. full << " "
  637. << "CMake version " << cmVersion::GetCMakeVersion() << " ";
  638. // add in some more text here based on status
  639. switch (i->second->Status)
  640. {
  641. case cmPolicies::WARN:
  642. full << "warns when the policy is not set and uses OLD behavior. "
  643. << "Use the cmake_policy command to set it to OLD or NEW "
  644. << "explicitly.";
  645. break;
  646. case cmPolicies::OLD:
  647. full << "defaults to the OLD behavior for this policy.";
  648. break;
  649. case cmPolicies::NEW:
  650. full << "defaults to the NEW behavior for this policy.";
  651. break;
  652. case cmPolicies::REQUIRED_IF_USED:
  653. full << "requires the policy to be set to NEW if you use it. "
  654. << "Use the cmake_policy command to set it to NEW.";
  655. break;
  656. case cmPolicies::REQUIRED_ALWAYS:
  657. full << "requires the policy to be set to NEW. "
  658. << "Use the cmake_policy command to set it to NEW.";
  659. break;
  660. }
  661. }
  662. cmDocumentationEntry e(i->second->IDString.c_str(),
  663. i->second->ShortDescription.c_str(),
  664. full.str().c_str());
  665. v.push_back(e);
  666. }
  667. }
  668. //----------------------------------------------------------------------------
  669. std::string
  670. cmPolicies::GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id)
  671. {
  672. std::string pid = this->GetPolicyIDString(id);
  673. cmOStringStream e;
  674. e << "Policy " << pid << " may not be set to OLD behavior because this "
  675. << "version of CMake no longer supports it. "
  676. << "The policy was introduced in "
  677. << "CMake version " << this->Policies[id]->GetVersionString()
  678. << ", and use of NEW behavior is now required."
  679. << "\n"
  680. << "Please either update your CMakeLists.txt files to conform to "
  681. << "the new behavior or use an older version of CMake that still "
  682. << "supports the old behavior. "
  683. << "Run cmake --help-policy " << pid << " for more information.";
  684. return e.str();
  685. }
  686. //----------------------------------------------------------------------------
  687. void
  688. cmPolicies::DiagnoseAncientPolicies(std::vector<PolicyID> const& ancient,
  689. unsigned int majorVer,
  690. unsigned int minorVer,
  691. unsigned int patchVer,
  692. cmMakefile* mf)
  693. {
  694. cmOStringStream e;
  695. e << "The project requests behavior compatible with CMake version \""
  696. << majorVer << "." << minorVer << "." << patchVer
  697. << "\", which requires the OLD behavior for some policies:\n";
  698. for(std::vector<PolicyID>::const_iterator
  699. i = ancient.begin(); i != ancient.end(); ++i)
  700. {
  701. cmPolicy const* policy = this->Policies[*i];
  702. e << " " << policy->IDString << ": " << policy->ShortDescription << "\n";
  703. }
  704. e << "However, this version of CMake no longer supports the OLD "
  705. << "behavior for these policies. "
  706. << "Please either update your CMakeLists.txt files to conform to "
  707. << "the new behavior or use an older version of CMake that still "
  708. << "supports the old behavior.";
  709. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  710. }