cmPolicies.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. #include "cmPolicies.h"
  2. #include "cmake.h"
  3. #include "cmMakefile.h"
  4. #include "cmSourceFile.h"
  5. #include "cmVersion.h"
  6. #include "cmVersionMacros.h"
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <assert.h>
  11. const char* cmPolicies::PolicyStatusNames[] = {
  12. "OLD", "WARN", "NEW", "REQUIRED_IF_USED", "REQUIRED_ALWAYS"
  13. };
  14. class cmPolicy
  15. {
  16. public:
  17. cmPolicy(cmPolicies::PolicyID iD,
  18. const char *idString,
  19. const char *shortDescription,
  20. const char *longDescription,
  21. unsigned int majorVersionIntroduced,
  22. unsigned int minorVersionIntroduced,
  23. unsigned int patchVersionIntroduced,
  24. unsigned int tweakVersionIntroduced,
  25. cmPolicies::PolicyStatus status)
  26. {
  27. if (!idString || !shortDescription || ! longDescription)
  28. {
  29. cmSystemTools::Error("Attempt to define a policy without "
  30. "all parameters being specified!");
  31. return;
  32. }
  33. this->ID = iD;
  34. this->IDString = idString;
  35. this->ShortDescription = shortDescription;
  36. this->LongDescription = longDescription;
  37. this->MajorVersionIntroduced = majorVersionIntroduced;
  38. this->MinorVersionIntroduced = minorVersionIntroduced;
  39. this->PatchVersionIntroduced = patchVersionIntroduced;
  40. this->TweakVersionIntroduced = tweakVersionIntroduced;
  41. this->Status = status;
  42. }
  43. std::string GetVersionString()
  44. {
  45. cmOStringStream v;
  46. v << this->MajorVersionIntroduced << "." << this->MinorVersionIntroduced;
  47. v << "." << this->PatchVersionIntroduced;
  48. if(this->TweakVersionIntroduced > 0)
  49. {
  50. v << "." << this->TweakVersionIntroduced;
  51. }
  52. return v.str();
  53. }
  54. bool IsPolicyNewerThan(unsigned int majorV,
  55. unsigned int minorV,
  56. unsigned int patchV,
  57. unsigned int tweakV)
  58. {
  59. if (majorV < this->MajorVersionIntroduced)
  60. {
  61. return true;
  62. }
  63. if (majorV > this->MajorVersionIntroduced)
  64. {
  65. return false;
  66. }
  67. if (minorV < this->MinorVersionIntroduced)
  68. {
  69. return true;
  70. }
  71. if (minorV > this->MinorVersionIntroduced)
  72. {
  73. return false;
  74. }
  75. if (patchV < this->PatchVersionIntroduced)
  76. {
  77. return true;
  78. }
  79. if (patchV > this->PatchVersionIntroduced)
  80. {
  81. return false;
  82. }
  83. return (tweakV < this->TweakVersionIntroduced);
  84. }
  85. cmPolicies::PolicyID ID;
  86. std::string IDString;
  87. std::string ShortDescription;
  88. std::string LongDescription;
  89. unsigned int MajorVersionIntroduced;
  90. unsigned int MinorVersionIntroduced;
  91. unsigned int PatchVersionIntroduced;
  92. unsigned int TweakVersionIntroduced;
  93. cmPolicies::PolicyStatus Status;
  94. };
  95. cmPolicies::cmPolicies()
  96. {
  97. // define all the policies
  98. this->DefinePolicy(
  99. CMP0000, "CMP0000",
  100. "A minimum required CMake version must be specified.",
  101. "CMake requires that projects specify the version of CMake to which "
  102. "they have been written. "
  103. "This policy has been put in place so users trying to build the project "
  104. "may be told when they need to update their CMake. "
  105. "Specifying a version also helps the project build with CMake versions "
  106. "newer than that specified. "
  107. "Use the cmake_minimum_required command at the top of your main "
  108. " CMakeLists.txt file:\n"
  109. " cmake_minimum_required(VERSION <major>.<minor>)\n"
  110. "where \"<major>.<minor>\" is the version of CMake you want to support "
  111. "(such as \"2.6\"). "
  112. "The command will ensure that at least the given version of CMake is "
  113. "running and help newer versions be compatible with the project. "
  114. "See documentation of cmake_minimum_required for details.\n"
  115. "Note that the command invocation must appear in the CMakeLists.txt "
  116. "file itself; a call in an included file is not sufficient. "
  117. "However, the cmake_policy command may be called to set policy "
  118. "CMP0000 to OLD or NEW behavior explicitly. "
  119. "The OLD behavior is to silently ignore the missing invocation. "
  120. "The NEW behavior is to issue an error instead of a warning. "
  121. "An included file may set CMP0000 explicitly to affect how this "
  122. "policy is enforced for the main CMakeLists.txt file.",
  123. 2,6,0,0, cmPolicies::WARN
  124. );
  125. this->DefinePolicy(
  126. CMP0001, "CMP0001",
  127. "CMAKE_BACKWARDS_COMPATIBILITY should no longer be used.",
  128. "The OLD behavior is to check CMAKE_BACKWARDS_COMPATIBILITY and present "
  129. "it to the user. "
  130. "The NEW behavior is to ignore CMAKE_BACKWARDS_COMPATIBILITY "
  131. "completely.\n"
  132. "In CMake 2.4 and below the variable CMAKE_BACKWARDS_COMPATIBILITY was "
  133. "used to request compatibility with earlier versions of CMake. "
  134. "In CMake 2.6 and above all compatibility issues are handled by policies "
  135. "and the cmake_policy command. "
  136. "However, CMake must still check CMAKE_BACKWARDS_COMPATIBILITY for "
  137. "projects written for CMake 2.4 and below.",
  138. 2,6,0,0, cmPolicies::WARN
  139. );
  140. this->DefinePolicy(
  141. CMP0002, "CMP0002",
  142. "Logical target names must be globally unique.",
  143. "Targets names created with "
  144. "add_executable, add_library, or add_custom_target "
  145. "are logical build target names. "
  146. "Logical target names must be globally unique because:\n"
  147. " - Unique names may be referenced unambiguously both in CMake\n"
  148. " code and on make tool command lines.\n"
  149. " - Logical names are used by Xcode and VS IDE generators\n"
  150. " to produce meaningful project names for the targets.\n"
  151. "The logical name of executable and library targets does not "
  152. "have to correspond to the physical file names built. "
  153. "Consider using the OUTPUT_NAME target property to create two "
  154. "targets with the same physical name while keeping logical "
  155. "names distinct. "
  156. "Custom targets must simply have globally unique names (unless one "
  157. "uses the global property ALLOW_DUPLICATE_CUSTOM_TARGETS with a "
  158. "Makefiles generator).",
  159. 2,6,0,0, cmPolicies::WARN
  160. );
  161. this->DefinePolicy(
  162. CMP0003, "CMP0003",
  163. "Libraries linked via full path no longer produce linker search paths.",
  164. "This policy affects how libraries whose full paths are NOT known "
  165. "are found at link time, but was created due to a change in how CMake "
  166. "deals with libraries whose full paths are known. "
  167. "Consider the code\n"
  168. " target_link_libraries(myexe /path/to/libA.so)\n"
  169. "CMake 2.4 and below implemented linking to libraries whose full paths "
  170. "are known by splitting them on the link line into separate components "
  171. "consisting of the linker search path and the library name. "
  172. "The example code might have produced something like\n"
  173. " ... -L/path/to -lA ...\n"
  174. "in order to link to library A. "
  175. "An analysis was performed to order multiple link directories such that "
  176. "the linker would find library A in the desired location, but there "
  177. "are cases in which this does not work. "
  178. "CMake versions 2.6 and above use the more reliable approach of passing "
  179. "the full path to libraries directly to the linker in most cases. "
  180. "The example code now produces something like\n"
  181. " ... /path/to/libA.so ....\n"
  182. "Unfortunately this change can break code like\n"
  183. " target_link_libraries(myexe /path/to/libA.so B)\n"
  184. "where \"B\" is meant to find \"/path/to/libB.so\". "
  185. "This code is wrong because the user is asking the linker to find "
  186. "library B but has not provided a linker search path (which may be "
  187. "added with the link_directories command). "
  188. "However, with the old linking implementation the code would work "
  189. "accidentally because the linker search path added for library A "
  190. "allowed library B to be found."
  191. "\n"
  192. "In order to support projects depending on linker search paths "
  193. "added by linking to libraries with known full paths, the OLD "
  194. "behavior for this policy will add the linker search paths even "
  195. "though they are not needed for their own libraries. "
  196. "When this policy is set to OLD, CMake will produce a link line such as\n"
  197. " ... -L/path/to /path/to/libA.so -lB ...\n"
  198. "which will allow library B to be found as it was previously. "
  199. "When this policy is set to NEW, CMake will produce a link line such as\n"
  200. " ... /path/to/libA.so -lB ...\n"
  201. "which more accurately matches what the project specified."
  202. "\n"
  203. "The setting for this policy used when generating the link line is that "
  204. "in effect when the target is created by an add_executable or "
  205. "add_library command. For the example described above, the code\n"
  206. " cmake_policy(SET CMP0003 OLD) # or cmake_policy(VERSION 2.4)\n"
  207. " add_executable(myexe myexe.c)\n"
  208. " target_link_libraries(myexe /path/to/libA.so B)\n"
  209. "will work and suppress the warning for this policy. "
  210. "It may also be updated to work with the corrected linking approach:\n"
  211. " cmake_policy(SET CMP0003 NEW) # or cmake_policy(VERSION 2.6)\n"
  212. " link_directories(/path/to) # needed to find library B\n"
  213. " add_executable(myexe myexe.c)\n"
  214. " target_link_libraries(myexe /path/to/libA.so B)\n"
  215. "Even better, library B may be specified with a full path:\n"
  216. " add_executable(myexe myexe.c)\n"
  217. " target_link_libraries(myexe /path/to/libA.so /path/to/libB.so)\n"
  218. "When all items on the link line have known paths CMake does not check "
  219. "this policy so it has no effect.\n"
  220. "Note that the warning for this policy will be issued for at most "
  221. "one target. This avoids flooding users with messages for every "
  222. "target when setting the policy once will probably fix all targets.",
  223. 2,6,0,0, cmPolicies::WARN);
  224. this->DefinePolicy(
  225. CMP0004, "CMP0004",
  226. "Libraries linked may not have leading or trailing whitespace.",
  227. "CMake versions 2.4 and below silently removed leading and trailing "
  228. "whitespace from libraries linked with code like\n"
  229. " target_link_libraries(myexe \" A \")\n"
  230. "This could lead to subtle errors in user projects.\n"
  231. "The OLD behavior for this policy is to silently remove leading and "
  232. "trailing whitespace. "
  233. "The NEW behavior for this policy is to diagnose the existence of "
  234. "such whitespace as an error. "
  235. "The setting for this policy used when checking the library names is "
  236. "that in effect when the target is created by an add_executable or "
  237. "add_library command.",
  238. 2,6,0,0, cmPolicies::WARN);
  239. this->DefinePolicy(
  240. CMP0005, "CMP0005",
  241. "Preprocessor definition values are now escaped automatically.",
  242. "This policy determines whether or not CMake should generate escaped "
  243. "preprocessor definition values added via add_definitions. "
  244. "CMake versions 2.4 and below assumed that only trivial values would "
  245. "be given for macros in add_definitions calls. "
  246. "It did not attempt to escape non-trivial values such as string "
  247. "literals in generated build rules. "
  248. "CMake versions 2.6 and above support escaping of most values, but "
  249. "cannot assume the user has not added escapes already in an attempt to "
  250. "work around limitations in earlier versions.\n"
  251. "The OLD behavior for this policy is to place definition values given "
  252. "to add_definitions directly in the generated build rules without "
  253. "attempting to escape anything. "
  254. "The NEW behavior for this policy is to generate correct escapes "
  255. "for all native build tools automatically. "
  256. "See documentation of the COMPILE_DEFINITIONS target property for "
  257. "limitations of the escaping implementation.",
  258. 2,6,0,0, cmPolicies::WARN);
  259. this->DefinePolicy(
  260. CMP0006, "CMP0006",
  261. "Installing MACOSX_BUNDLE targets requires a BUNDLE DESTINATION.",
  262. "This policy determines whether the install(TARGETS) command must be "
  263. "given a BUNDLE DESTINATION when asked to install a target with the "
  264. "MACOSX_BUNDLE property set. "
  265. "CMake 2.4 and below did not distinguish application bundles from "
  266. "normal executables when installing targets. "
  267. "CMake 2.6 provides a BUNDLE option to the install(TARGETS) command "
  268. "that specifies rules specific to application bundles on the Mac. "
  269. "Projects should use this option when installing a target with the "
  270. "MACOSX_BUNDLE property set.\n"
  271. "The OLD behavior for this policy is to fall back to the RUNTIME "
  272. "DESTINATION if a BUNDLE DESTINATION is not given. "
  273. "The NEW behavior for this policy is to produce an error if a bundle "
  274. "target is installed without a BUNDLE DESTINATION.",
  275. 2,6,0,0, cmPolicies::WARN);
  276. this->DefinePolicy(
  277. CMP0007, "CMP0007",
  278. "list command no longer ignores empty elements.",
  279. "This policy determines whether the list command will "
  280. "ignore empty elements in the list. "
  281. "CMake 2.4 and below list commands ignored all empty elements"
  282. " in the list. For example, a;b;;c would have length 3 and not 4. "
  283. "The OLD behavior for this policy is to ignore empty list elements. "
  284. "The NEW behavior for this policy is to correctly count empty "
  285. "elements in a list. ",
  286. 2,6,0,0, cmPolicies::WARN);
  287. this->DefinePolicy(
  288. CMP0008, "CMP0008",
  289. "Libraries linked by full-path must have a valid library file name.",
  290. "In CMake 2.4 and below it is possible to write code like\n"
  291. " target_link_libraries(myexe /full/path/to/somelib)\n"
  292. "where \"somelib\" is supposed to be a valid library file name "
  293. "such as \"libsomelib.a\" or \"somelib.lib\". "
  294. "For Makefile generators this produces an error at build time "
  295. "because the dependency on the full path cannot be found. "
  296. "For VS IDE and Xcode generators this used to work by accident because "
  297. "CMake would always split off the library directory and ask the "
  298. "linker to search for the library by name (-lsomelib or somelib.lib). "
  299. "Despite the failure with Makefiles, some projects have code like this "
  300. "and build only with VS and/or Xcode. "
  301. "This version of CMake prefers to pass the full path directly to the "
  302. "native build tool, which will fail in this case because it does "
  303. "not name a valid library file."
  304. "\n"
  305. "This policy determines what to do with full paths that do not appear "
  306. "to name a valid library file. "
  307. "The OLD behavior for this policy is to split the library name from the "
  308. "path and ask the linker to search for it. "
  309. "The NEW behavior for this policy is to trust the given path and "
  310. "pass it directly to the native build tool unchanged.",
  311. 2,6,1,0, cmPolicies::WARN);
  312. this->DefinePolicy(
  313. CMP0009, "CMP0009",
  314. "FILE GLOB_RECURSE calls should not follow symlinks by default.",
  315. "In CMake 2.6.1 and below, FILE GLOB_RECURSE calls would follow "
  316. "through symlinks, sometimes coming up with unexpectedly large "
  317. "result sets because of symlinks to top level directories that "
  318. "contain hundreds of thousands of files."
  319. "\n"
  320. "This policy determines whether or not to follow symlinks "
  321. "encountered during a FILE GLOB_RECURSE call. "
  322. "The OLD behavior for this policy is to follow the symlinks. "
  323. "The NEW behavior for this policy is not to follow the symlinks "
  324. "by default, but only if FOLLOW_SYMLINKS is given as an additional "
  325. "argument to the FILE command.",
  326. 2,6,2,0, cmPolicies::WARN);
  327. this->DefinePolicy(
  328. CMP0010, "CMP0010",
  329. "Bad variable reference syntax is an error.",
  330. "In CMake 2.6.2 and below, incorrect variable reference syntax such as "
  331. "a missing close-brace (\"${FOO\") was reported but did not stop "
  332. "processing of CMake code. "
  333. "This policy determines whether a bad variable reference is an error. "
  334. "The OLD behavior for this policy is to warn about the error, leave "
  335. "the string untouched, and continue. "
  336. "The NEW behavior for this policy is to report an error.",
  337. 2,6,3,0, cmPolicies::WARN);
  338. this->DefinePolicy(
  339. CMP0011, "CMP0011",
  340. "Included scripts do automatic cmake_policy PUSH and POP.",
  341. "In CMake 2.6.2 and below, CMake Policy settings in scripts loaded by "
  342. "the include() and find_package() commands would affect the includer. "
  343. "Explicit invocations of cmake_policy(PUSH) and cmake_policy(POP) were "
  344. "required to isolate policy changes and protect the includer. "
  345. "While some scripts intend to affect the policies of their includer, "
  346. "most do not. "
  347. "In CMake 2.6.3 and above, include() and find_package() by default PUSH "
  348. "and POP an entry on the policy stack around an included script, "
  349. "but provide a NO_POLICY_SCOPE option to disable it. "
  350. "This policy determines whether or not to imply NO_POLICY_SCOPE for "
  351. "compatibility. "
  352. "The OLD behavior for this policy is to imply NO_POLICY_SCOPE for "
  353. "include() and find_package() commands. "
  354. "The NEW behavior for this policy is to allow the commands to do their "
  355. "default cmake_policy PUSH and POP.",
  356. 2,6,3,0, cmPolicies::WARN);
  357. this->DefinePolicy(
  358. CMP0012, "CMP0012",
  359. "if() recognizes numbers and boolean constants.",
  360. "In CMake versions 2.6.4 and lower the if() command implicitly "
  361. "dereferenced arguments corresponding to variables, even those named "
  362. "like numbers or boolean constants, except for 0 and 1. "
  363. "Numbers and boolean constants such as true, false, yes, no, "
  364. "on, off, y, n, notfound, ignore (all case insensitive) were recognized "
  365. "in some cases but not all. "
  366. "For example, the code \"if(TRUE)\" might have evaluated as false. "
  367. "Numbers such as 2 were recognized only in "
  368. "boolean expressions like \"if(NOT 2)\" (leading to false) "
  369. "but not as a single-argument like \"if(2)\" (also leading to false). "
  370. "Later versions of CMake prefer to treat numbers and boolean constants "
  371. "literally, so they should not be used as variable names."
  372. "\n"
  373. "The OLD behavior for this policy is to implicitly dereference variables "
  374. "named like numbers and boolean constants. "
  375. "The NEW behavior for this policy is to recognize numbers and "
  376. "boolean constants without dereferencing variables with such names.",
  377. 2,8,0,0, cmPolicies::WARN);
  378. this->DefinePolicy(
  379. CMP0013, "CMP0013",
  380. "Duplicate binary directories are not allowed.",
  381. "CMake 2.6.3 and below silently permitted add_subdirectory() calls "
  382. "to create the same binary directory multiple times. "
  383. "During build system generation files would be written and then "
  384. "overwritten in the build tree and could lead to strange behavior. "
  385. "CMake 2.6.4 and above explicitly detect duplicate binary directories. "
  386. "CMake 2.6.4 always considers this case an error. "
  387. "In CMake 2.8.0 and above this policy determines whether or not "
  388. "the case is an error. "
  389. "The OLD behavior for this policy is to allow duplicate binary "
  390. "directories. "
  391. "The NEW behavior for this policy is to disallow duplicate binary "
  392. "directories with an error.",
  393. 2,8,0,0, cmPolicies::WARN);
  394. this->DefinePolicy(
  395. CMP0014, "CMP0014",
  396. "Input directories must have CMakeLists.txt.",
  397. "CMake versions before 2.8 silently ignored missing CMakeLists.txt "
  398. "files in directories referenced by add_subdirectory() or subdirs(), "
  399. "treating them as if present but empty. "
  400. "In CMake 2.8.0 and above this policy determines whether or not "
  401. "the case is an error. "
  402. "The OLD behavior for this policy is to silently ignore the problem. "
  403. "The NEW behavior for this policy is to report an error.",
  404. 2,8,0,0, cmPolicies::WARN);
  405. this->DefinePolicy(
  406. CMP0015, "CMP0015",
  407. "link_directories() treats paths relative to the source dir.",
  408. "In CMake 2.8.0 and lower the link_directories() command passed relative "
  409. "paths unchanged to the linker. "
  410. "In CMake 2.8.1 and above the link_directories() command prefers to "
  411. "interpret relative paths with respect to CMAKE_CURRENT_SOURCE_DIR, "
  412. "which is consistent with include_directories() and other commands. "
  413. "The OLD behavior for this policy is to use relative paths verbatim in "
  414. "the linker command. "
  415. "The NEW behavior for this policy is to convert relative paths to "
  416. "absolute paths by appending the relative path to "
  417. "CMAKE_CURRENT_SOURCE_DIR.",
  418. 2,8,1,0, cmPolicies::WARN);
  419. this->DefinePolicy(
  420. CMP0016, "CMP0016",
  421. "target_link_libraries() reports error if only argument is not a target.",
  422. "In CMake 2.8.2 and lower the target_link_libraries() command silently "
  423. "ignored if it was called with only one argument, and this argument "
  424. "wasn't a valid target. "
  425. "In CMake 2.8.3 and above it reports an error in this case.",
  426. 2,8,3,0, cmPolicies::WARN);
  427. }
  428. cmPolicies::~cmPolicies()
  429. {
  430. // free the policies
  431. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  432. = this->Policies.begin();
  433. for (;i != this->Policies.end(); ++i)
  434. {
  435. delete i->second;
  436. }
  437. }
  438. void cmPolicies::DefinePolicy(cmPolicies::PolicyID iD,
  439. const char *idString,
  440. const char *shortDescription,
  441. const char *longDescription,
  442. unsigned int majorVersionIntroduced,
  443. unsigned int minorVersionIntroduced,
  444. unsigned int patchVersionIntroduced,
  445. unsigned int tweakVersionIntroduced,
  446. cmPolicies::PolicyStatus status)
  447. {
  448. // a policy must be unique and can only be defined once
  449. if (this->Policies.find(iD) != this->Policies.end())
  450. {
  451. cmSystemTools::Error("Attempt to redefine a CMake policy for policy "
  452. "ID ", this->GetPolicyIDString(iD).c_str());
  453. return;
  454. }
  455. this->Policies[iD] = new cmPolicy(iD, idString,
  456. shortDescription,
  457. longDescription,
  458. majorVersionIntroduced,
  459. minorVersionIntroduced,
  460. patchVersionIntroduced,
  461. tweakVersionIntroduced,
  462. status);
  463. this->PolicyStringMap[idString] = iD;
  464. }
  465. //----------------------------------------------------------------------------
  466. bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
  467. const char *version)
  468. {
  469. std::string ver = "2.4.0";
  470. if (version && strlen(version) > 0)
  471. {
  472. ver = version;
  473. }
  474. unsigned int majorVer = 2;
  475. unsigned int minorVer = 0;
  476. unsigned int patchVer = 0;
  477. unsigned int tweakVer = 0;
  478. // parse the string
  479. if(sscanf(ver.c_str(), "%u.%u.%u.%u",
  480. &majorVer, &minorVer, &patchVer, &tweakVer) < 2)
  481. {
  482. cmOStringStream e;
  483. e << "Invalid policy version value \"" << ver << "\". "
  484. << "A numeric major.minor[.patch[.tweak]] must be given.";
  485. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  486. return false;
  487. }
  488. // it is an error if the policy version is less than 2.4
  489. if (majorVer < 2 || (majorVer == 2 && minorVer < 4))
  490. {
  491. mf->IssueMessage(cmake::FATAL_ERROR,
  492. "An attempt was made to set the policy version of CMake to something "
  493. "earlier than \"2.4\". "
  494. "In CMake 2.4 and below backwards compatibility was handled with the "
  495. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  496. "In order to get compatibility features supporting versions earlier "
  497. "than 2.4 set policy CMP0001 to OLD to tell CMake to check the "
  498. "CMAKE_BACKWARDS_COMPATIBILITY variable. "
  499. "One way to do this is to set the policy version to 2.4 exactly."
  500. );
  501. return false;
  502. }
  503. // It is an error if the policy version is greater than the running
  504. // CMake.
  505. if (majorVer > cmVersion::GetMajorVersion() ||
  506. (majorVer == cmVersion::GetMajorVersion() &&
  507. minorVer > cmVersion::GetMinorVersion()) ||
  508. (majorVer == cmVersion::GetMajorVersion() &&
  509. minorVer == cmVersion::GetMinorVersion() &&
  510. patchVer > cmVersion::GetPatchVersion()) ||
  511. (majorVer == cmVersion::GetMajorVersion() &&
  512. minorVer == cmVersion::GetMinorVersion() &&
  513. patchVer == cmVersion::GetPatchVersion() &&
  514. tweakVer > cmVersion::GetTweakVersion()))
  515. {
  516. cmOStringStream e;
  517. e << "An attempt was made to set the policy version of CMake to \""
  518. << version << "\" which is greater than this version of CMake. "
  519. << "This is not allowed because the greater version may have new "
  520. << "policies not known to this CMake. "
  521. << "You may need a newer CMake version to build this project.";
  522. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  523. return false;
  524. }
  525. // now loop over all the policies and set them as appropriate
  526. std::vector<cmPolicies::PolicyID> ancientPolicies;
  527. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  528. = this->Policies.begin();
  529. for (;i != this->Policies.end(); ++i)
  530. {
  531. if (i->second->IsPolicyNewerThan(majorVer,minorVer,patchVer,tweakVer))
  532. {
  533. if(i->second->Status == cmPolicies::REQUIRED_ALWAYS)
  534. {
  535. ancientPolicies.push_back(i->first);
  536. }
  537. else if (!mf->SetPolicy(i->second->ID, cmPolicies::WARN))
  538. {
  539. return false;
  540. }
  541. }
  542. else
  543. {
  544. if (!mf->SetPolicy(i->second->ID, cmPolicies::NEW))
  545. {
  546. return false;
  547. }
  548. }
  549. }
  550. // Make sure the project does not use any ancient policies.
  551. if(!ancientPolicies.empty())
  552. {
  553. this->DiagnoseAncientPolicies(ancientPolicies,
  554. majorVer, minorVer, patchVer, mf);
  555. cmSystemTools::SetFatalErrorOccured();
  556. return false;
  557. }
  558. return true;
  559. }
  560. bool cmPolicies::GetPolicyID(const char *id, cmPolicies::PolicyID &pid)
  561. {
  562. if (!id || strlen(id) < 1)
  563. {
  564. return false;
  565. }
  566. std::map<std::string,cmPolicies::PolicyID>::iterator pos =
  567. this->PolicyStringMap.find(id);
  568. if (pos == this->PolicyStringMap.end())
  569. {
  570. return false;
  571. }
  572. pid = pos->second;
  573. return true;
  574. }
  575. std::string cmPolicies::GetPolicyIDString(cmPolicies::PolicyID pid)
  576. {
  577. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  578. this->Policies.find(pid);
  579. if (pos == this->Policies.end())
  580. {
  581. return "";
  582. }
  583. return pos->second->IDString;
  584. }
  585. ///! return a warning string for a given policy
  586. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  587. {
  588. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  589. this->Policies.find(id);
  590. if (pos == this->Policies.end())
  591. {
  592. cmSystemTools::Error(
  593. "Request for warning text for undefined policy!");
  594. return "Request for warning text for undefined policy!";
  595. }
  596. cmOStringStream msg;
  597. msg <<
  598. "Policy " << pos->second->IDString << " is not set: "
  599. "" << pos->second->ShortDescription << " "
  600. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  601. "policy details. "
  602. "Use the cmake_policy command to set the policy "
  603. "and suppress this warning.";
  604. return msg.str();
  605. }
  606. ///! return an error string for when a required policy is unspecified
  607. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  608. {
  609. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  610. this->Policies.find(id);
  611. if (pos == this->Policies.end())
  612. {
  613. cmSystemTools::Error(
  614. "Request for error text for undefined policy!");
  615. return "Request for warning text for undefined policy!";
  616. }
  617. cmOStringStream error;
  618. error <<
  619. "Policy " << pos->second->IDString << " is not set to NEW: "
  620. "" << pos->second->ShortDescription << " "
  621. "Run \"cmake --help-policy " << pos->second->IDString << "\" for "
  622. "policy details. "
  623. "CMake now requires this policy to be set to NEW by the project. "
  624. "The policy may be set explicitly using the code\n"
  625. " cmake_policy(SET " << pos->second->IDString << " NEW)\n"
  626. "or by upgrading all policies with the code\n"
  627. " cmake_policy(VERSION " << pos->second->GetVersionString() <<
  628. ") # or later\n"
  629. "Run \"cmake --help-command cmake_policy\" for more information.";
  630. return error.str();
  631. }
  632. ///! Get the default status for a policy
  633. cmPolicies::PolicyStatus
  634. cmPolicies::GetPolicyStatus(cmPolicies::PolicyID id)
  635. {
  636. // if the policy is not know then what?
  637. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  638. this->Policies.find(id);
  639. if (pos == this->Policies.end())
  640. {
  641. // TODO is this right?
  642. return cmPolicies::WARN;
  643. }
  644. return pos->second->Status;
  645. }
  646. void cmPolicies::GetDocumentation(std::vector<cmDocumentationEntry>& v)
  647. {
  648. // now loop over all the policies and set them as appropriate
  649. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  650. = this->Policies.begin();
  651. for (;i != this->Policies.end(); ++i)
  652. {
  653. cmOStringStream full;
  654. full << i->second->LongDescription;
  655. full << "\nThis policy was introduced in CMake version ";
  656. full << i->second->GetVersionString() << ".";
  657. if(i->first != cmPolicies::CMP0000)
  658. {
  659. full << " "
  660. << "CMake version " << cmVersion::GetCMakeVersion() << " ";
  661. // add in some more text here based on status
  662. switch (i->second->Status)
  663. {
  664. case cmPolicies::WARN:
  665. full << "warns when the policy is not set and uses OLD behavior. "
  666. << "Use the cmake_policy command to set it to OLD or NEW "
  667. << "explicitly.";
  668. break;
  669. case cmPolicies::OLD:
  670. full << "defaults to the OLD behavior for this policy.";
  671. break;
  672. case cmPolicies::NEW:
  673. full << "defaults to the NEW behavior for this policy.";
  674. break;
  675. case cmPolicies::REQUIRED_IF_USED:
  676. full << "requires the policy to be set to NEW if you use it. "
  677. << "Use the cmake_policy command to set it to NEW.";
  678. break;
  679. case cmPolicies::REQUIRED_ALWAYS:
  680. full << "requires the policy to be set to NEW. "
  681. << "Use the cmake_policy command to set it to NEW.";
  682. break;
  683. }
  684. }
  685. cmDocumentationEntry e(i->second->IDString.c_str(),
  686. i->second->ShortDescription.c_str(),
  687. full.str().c_str());
  688. v.push_back(e);
  689. }
  690. }
  691. //----------------------------------------------------------------------------
  692. std::string
  693. cmPolicies::GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id)
  694. {
  695. std::string pid = this->GetPolicyIDString(id);
  696. cmOStringStream e;
  697. e << "Policy " << pid << " may not be set to OLD behavior because this "
  698. << "version of CMake no longer supports it. "
  699. << "The policy was introduced in "
  700. << "CMake version " << this->Policies[id]->GetVersionString()
  701. << ", and use of NEW behavior is now required."
  702. << "\n"
  703. << "Please either update your CMakeLists.txt files to conform to "
  704. << "the new behavior or use an older version of CMake that still "
  705. << "supports the old behavior. "
  706. << "Run cmake --help-policy " << pid << " for more information.";
  707. return e.str();
  708. }
  709. //----------------------------------------------------------------------------
  710. void
  711. cmPolicies::DiagnoseAncientPolicies(std::vector<PolicyID> const& ancient,
  712. unsigned int majorVer,
  713. unsigned int minorVer,
  714. unsigned int patchVer,
  715. cmMakefile* mf)
  716. {
  717. cmOStringStream e;
  718. e << "The project requests behavior compatible with CMake version \""
  719. << majorVer << "." << minorVer << "." << patchVer
  720. << "\", which requires the OLD behavior for some policies:\n";
  721. for(std::vector<PolicyID>::const_iterator
  722. i = ancient.begin(); i != ancient.end(); ++i)
  723. {
  724. cmPolicy const* policy = this->Policies[*i];
  725. e << " " << policy->IDString << ": " << policy->ShortDescription << "\n";
  726. }
  727. e << "However, this version of CMake no longer supports the OLD "
  728. << "behavior for these policies. "
  729. << "Please either update your CMakeLists.txt files to conform to "
  730. << "the new behavior or use an older version of CMake that still "
  731. << "supports the old behavior.";
  732. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  733. }