cmPolicies.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #include "cmPolicies.h"
  2. #include "cmake.h"
  3. #include "cmMakefile.h"
  4. #include "cmSourceFile.h"
  5. #include <map>
  6. #include <set>
  7. #include <queue>
  8. #include <assert.h>
  9. const char* cmPolicies::PolicyStatusNames[] = {
  10. "OLD", "WARN", "NEW", "REQUIRED_IF_USED", "REQUIRED_ALWAYS"
  11. };
  12. class cmPolicy
  13. {
  14. public:
  15. cmPolicy(cmPolicies::PolicyID iD,
  16. const char *idString,
  17. const char *shortDescription,
  18. const char *longDescription,
  19. unsigned int majorVersionIntroduced,
  20. unsigned int minorVersionIntroduced,
  21. unsigned int patchVersionIntroduced,
  22. cmPolicies::PolicyStatus status)
  23. {
  24. if (!idString || !shortDescription || ! longDescription)
  25. {
  26. cmSystemTools::Error("Attempt to define a policy without "
  27. "all parameters being specified!");
  28. return;
  29. }
  30. this->ID = iD;
  31. this->IDString = idString;
  32. this->ShortDescription = shortDescription;
  33. this->LongDescription = longDescription;
  34. this->MajorVersionIntroduced = majorVersionIntroduced;
  35. this->MinorVersionIntroduced = minorVersionIntroduced;
  36. this->PatchVersionIntroduced = patchVersionIntroduced;
  37. this->Status = status;
  38. }
  39. std::string GetVersionString()
  40. {
  41. cmOStringStream error;
  42. error << this->MajorVersionIntroduced << "." <<
  43. this->MinorVersionIntroduced << "." <<
  44. this->PatchVersionIntroduced;
  45. return error.str();
  46. }
  47. bool IsPolicyNewerThan(unsigned int majorV,
  48. unsigned int minorV,
  49. unsigned int patchV)
  50. {
  51. if (majorV < this->MajorVersionIntroduced)
  52. {
  53. return true;
  54. }
  55. if (majorV > this->MajorVersionIntroduced)
  56. {
  57. return false;
  58. }
  59. if (minorV < this->MinorVersionIntroduced)
  60. {
  61. return true;
  62. }
  63. if (minorV > this->MinorVersionIntroduced)
  64. {
  65. return false;
  66. }
  67. return (patchV < this->PatchVersionIntroduced);
  68. }
  69. cmPolicies::PolicyID ID;
  70. std::string IDString;
  71. std::string ShortDescription;
  72. std::string LongDescription;
  73. unsigned int MajorVersionIntroduced;
  74. unsigned int MinorVersionIntroduced;
  75. unsigned int PatchVersionIntroduced;
  76. cmPolicies::PolicyStatus Status;
  77. };
  78. cmPolicies::cmPolicies()
  79. {
  80. // define all the policies
  81. this->DefinePolicy(CMP_0000, "CMP_0000",
  82. "Missing a CMake version specification. You must have a cmake_policy "
  83. "or cmake_minimum_required call.",
  84. "CMake requires that projects specify what version of CMake they have "
  85. "been written to. The easiest way to do this is by placing a call to "
  86. "cmake_policy such as the following cmake_policy(VERSION 2.6) Replace "
  87. "2.6 in that example with the verison of CMake you are writing to. "
  88. "This policy is being put in place because it aids us in detecting "
  89. "and maintaining backwards compatibility.",
  90. 2,6,0, cmPolicies::WARN);
  91. this->PolicyStringMap["CMP_POLICY_SPECIFICATION"] = CMP_0000;
  92. this->DefinePolicy(CMP_0001, "CMP_0001",
  93. "CMake does not allow target names to include slash characters.",
  94. "CMake requires that target names not include any / or \\ characters "
  95. "please change the name of any targets to not use such characters."
  96. ,
  97. 2,4,0, cmPolicies::REQUIRED_IF_USED);
  98. this->PolicyStringMap["CMP_TARGET_NAMES_WITH_SLASHES"] = CMP_0001;
  99. this->DefinePolicy(CMP_0002, "CMP_0002",
  100. "CMake requires that target names be globaly unique.",
  101. "CMake requires that target names not include any / or \\ characters "
  102. "please change the name of any targets to not use such characters."
  103. ,
  104. 2,6,0, cmPolicies::WARN);
  105. this->PolicyStringMap["CMP_REQUIRE_UNIQUE_TARGET_NAMES"] = CMP_0002;
  106. this->DefinePolicy(CMP_0003, "CMP_0003",
  107. "CMake configures file immediately after 2.0.",
  108. "In CMake 2.0 and earlier the configure_file command would not "
  109. "configure the file until after processing all CMakeLists files. "
  110. "In CMake 2.2 and later the default behavior is that it will "
  111. "configure the file right when the command is invoked."
  112. ,
  113. 2,2,0, cmPolicies::NEW);
  114. this->PolicyStringMap["CMP_CONFIGURE_FILE_IMMEDIATE"] = CMP_0003;
  115. }
  116. cmPolicies::~cmPolicies()
  117. {
  118. // free the policies
  119. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  120. = this->Policies.begin();
  121. for (;i != this->Policies.end(); ++i)
  122. {
  123. delete i->second;
  124. }
  125. }
  126. void cmPolicies::DefinePolicy(cmPolicies::PolicyID iD,
  127. const char *idString,
  128. const char *shortDescription,
  129. const char *longDescription,
  130. unsigned int majorVersionIntroduced,
  131. unsigned int minorVersionIntroduced,
  132. unsigned int patchVersionIntroduced,
  133. cmPolicies::PolicyStatus status)
  134. {
  135. // a policy must be unique and can only be defined once
  136. if (this->Policies.find(iD) != this->Policies.end())
  137. {
  138. cmSystemTools::Error("Attempt to redefine a CMake policy for policy "
  139. "ID ", this->GetPolicyIDString(iD).c_str());
  140. return;
  141. }
  142. this->Policies[iD] = new cmPolicy(iD, idString,
  143. shortDescription,
  144. longDescription,
  145. majorVersionIntroduced,
  146. minorVersionIntroduced,
  147. patchVersionIntroduced,
  148. status);
  149. this->PolicyStringMap[idString] = iD;
  150. }
  151. bool cmPolicies::ApplyPolicyVersion(cmMakefile *mf,
  152. const char *version)
  153. {
  154. std::string ver = "2.4.0";
  155. if (version && strlen(version) > 0)
  156. {
  157. ver = version;
  158. }
  159. unsigned int majorVer = 2;
  160. unsigned int minorVer = 0;
  161. unsigned int patchVer = 0;
  162. // parse the string
  163. std::string major = ver.substr(0,ver.find('.'));
  164. std::string patch = ver.substr(ver.find('.'));
  165. std::string minor = patch.substr(0,patch.find('.'));
  166. patch = patch.substr(patch.find('.'));
  167. if (major.size())
  168. {
  169. majorVer = atoi(major.c_str());
  170. }
  171. if (minor.size())
  172. {
  173. minorVer = atoi(minor.c_str());
  174. }
  175. if (patch.size())
  176. {
  177. patchVer = atoi(patch.c_str());
  178. }
  179. // add in the old CMAKE_BACKWARDS_COMPATIBILITY var for old CMake compatibility
  180. if ((majorVer == 2 && minorVer <= 4) || majorVer < 2)
  181. {
  182. if (!mf->GetCacheManager()->
  183. GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  184. {
  185. mf->AddCacheDefinition
  186. ("CMAKE_BACKWARDS_COMPATIBILITY",version,
  187. "For backwards compatibility, what version of CMake commands and "
  188. "syntax should this version of CMake try to support.",
  189. cmCacheManager::STRING);
  190. }
  191. }
  192. // now loop over all the policies and set them as appropriate
  193. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  194. = this->Policies.begin();
  195. for (;i != this->Policies.end(); ++i)
  196. {
  197. if (i->second->IsPolicyNewerThan(majorVer,minorVer,patchVer))
  198. {
  199. if (!mf->SetPolicy(i->second->ID, cmPolicies::WARN))
  200. {
  201. return false;
  202. }
  203. }
  204. else
  205. {
  206. if (!mf->SetPolicy(i->second->ID, cmPolicies::NEW))
  207. {
  208. return false;
  209. }
  210. }
  211. }
  212. return true;
  213. }
  214. // is this a valid status the listfile can set this policy to?
  215. bool cmPolicies::IsValidPolicyStatus(cmPolicies::PolicyID id,
  216. cmPolicies::PolicyStatus status)
  217. {
  218. // if they are setting a feature to anything other than OLD or WARN and the
  219. // feature is not known about then that is an error
  220. if (this->Policies.find(id) == this->Policies.end())
  221. {
  222. if (status == cmPolicies::WARN ||
  223. status == cmPolicies::OLD)
  224. {
  225. return true;
  226. }
  227. cmOStringStream error;
  228. error <<
  229. "Error: an attempt was made to enable the new behavior for " <<
  230. "a new feature that is in a later version of CMake than "
  231. "what you are runing, please upgrade to a newer version "
  232. "of CMake.";
  233. cmSystemTools::Error(error.str().c_str());
  234. return false;
  235. }
  236. // now we know the feature is defined, so the only issue is if someone is
  237. // setting it to WARN or OLD when the feature is REQUIRED_ALWAYS
  238. if ((status == cmPolicies::WARN ||
  239. status == cmPolicies::OLD) &&
  240. this->Policies[id]->Status == cmPolicies::REQUIRED_ALWAYS)
  241. {
  242. cmOStringStream error;
  243. error <<
  244. "Error: an attempt was made to enable the old behavior for " <<
  245. "a feature that is no longer supported. The feature in " <<
  246. "question is feature " <<
  247. id <<
  248. " which had new behavior introduced in CMake version " <<
  249. this->Policies[id]->GetVersionString() <<
  250. " please either update your CMakeLists files to conform to " <<
  251. "the new behavior " <<
  252. "or use an older version of CMake that still supports " <<
  253. "the old behavior. Run cmake --help-policies " <<
  254. id << " for more information.";
  255. cmSystemTools::Error(error.str().c_str());
  256. return false;
  257. }
  258. return true;
  259. }
  260. // is this a valid status the listfile can set this policy to?
  261. bool cmPolicies::IsValidUsedPolicyStatus(cmPolicies::PolicyID id,
  262. cmPolicies::PolicyStatus status)
  263. {
  264. // if they are setting a feature to anything other than OLD or WARN and the
  265. // feature is not known about then that is an error
  266. if (this->Policies.find(id) == this->Policies.end())
  267. {
  268. if (status == cmPolicies::WARN ||
  269. status == cmPolicies::OLD)
  270. {
  271. return true;
  272. }
  273. cmOStringStream error;
  274. error <<
  275. "Error: an attempt was made to enable the new behavior for " <<
  276. "a new feature that is in a later version of CMake than "
  277. "what you are runing, please upgrade to a newer version "
  278. "of CMake.";
  279. cmSystemTools::Error(error.str().c_str());
  280. return false;
  281. }
  282. // now we know the feature is defined, so the only issue is if someone is
  283. // setting it to WARN or OLD when the feature is REQUIRED_ALWAYS
  284. if ((status == cmPolicies::WARN ||
  285. status == cmPolicies::OLD) &&
  286. (this->Policies[id]->Status == cmPolicies::REQUIRED_ALWAYS ||
  287. this->Policies[id]->Status == cmPolicies::REQUIRED_IF_USED))
  288. {
  289. cmOStringStream error;
  290. error <<
  291. "Error: an attempt was made to enable the old behavior for " <<
  292. "a feature that is no longer supported. The feature in " <<
  293. "question is feature " <<
  294. id <<
  295. " which had new behavior introduced in CMake version " <<
  296. this->Policies[id]->GetVersionString() <<
  297. " please either update your CMakeLists files to conform to " <<
  298. "the new behavior " <<
  299. "or use an older version of CMake that still supports " <<
  300. "the old behavior. Run cmake --help-policies " <<
  301. id << " for more information.";
  302. cmSystemTools::Error(error.str().c_str());
  303. return false;
  304. }
  305. return true;
  306. }
  307. bool cmPolicies::GetPolicyID(const char *id, cmPolicies::PolicyID &pid)
  308. {
  309. if (!id || strlen(id) < 1)
  310. {
  311. return false;
  312. }
  313. std::map<std::string,cmPolicies::PolicyID>::iterator pos =
  314. this->PolicyStringMap.find(id);
  315. if (pos == this->PolicyStringMap.end())
  316. {
  317. return false;
  318. }
  319. pid = pos->second;
  320. return true;
  321. }
  322. std::string cmPolicies::GetPolicyIDString(cmPolicies::PolicyID pid)
  323. {
  324. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  325. this->Policies.find(pid);
  326. if (pos == this->Policies.end())
  327. {
  328. return "";
  329. }
  330. return pos->second->IDString;
  331. }
  332. ///! return a warning string for a given policy
  333. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  334. {
  335. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  336. this->Policies.find(id);
  337. if (pos == this->Policies.end())
  338. {
  339. cmSystemTools::Error(
  340. "Request for warning text for undefined policy!");
  341. return "Request for warning text for undefined policy!";
  342. }
  343. cmOStringStream error;
  344. error <<
  345. "Warning " <<
  346. pos->second->IDString << ": " <<
  347. pos->second->ShortDescription <<
  348. " You can suppress this warning by adding either\n" <<
  349. "cmake_policy (OLD " <<
  350. pos->second->IDString << ") for the old behavior or " <<
  351. "cmake_policy(NEW " <<
  352. pos->second->IDString << ") for the new behavior. " <<
  353. "Run cmake --help-policy " <<
  354. pos->second->IDString << " for more information.";
  355. return error.str();
  356. }
  357. ///! return an error string for when a required policy is unspecified
  358. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  359. {
  360. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  361. this->Policies.find(id);
  362. if (pos == this->Policies.end())
  363. {
  364. cmSystemTools::Error(
  365. "Request for error text for undefined policy!");
  366. return "Request for warning text for undefined policy!";
  367. }
  368. cmOStringStream error;
  369. error <<
  370. "Error " <<
  371. pos->second->IDString << ": " <<
  372. pos->second->ShortDescription <<
  373. " This behavior is required now. You can suppress this message by "
  374. "specifying that your listfile is written to handle this new "
  375. "behavior by adding either\n" <<
  376. "cmake_policy (NEW " <<
  377. pos->second->IDString << ")\n or \n. " <<
  378. "cmake_policy (VERSION " <<
  379. pos->second->GetVersionString() << " ) or later."
  380. "Run cmake --help-policy " <<
  381. pos->second->IDString << " for more information.";
  382. return error.str();
  383. }
  384. ///! Get the default status for a policy
  385. cmPolicies::PolicyStatus
  386. cmPolicies::GetPolicyStatus(cmPolicies::PolicyID id)
  387. {
  388. // if the policy is not know then what?
  389. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator pos =
  390. this->Policies.find(id);
  391. if (pos == this->Policies.end())
  392. {
  393. // TODO is this right?
  394. return cmPolicies::WARN;
  395. }
  396. return pos->second->Status;
  397. }
  398. void cmPolicies::GetDocumentation(std::vector<cmDocumentationEntry>& v)
  399. {
  400. // now loop over all the policies and set them as appropriate
  401. std::map<cmPolicies::PolicyID,cmPolicy *>::iterator i
  402. = this->Policies.begin();
  403. for (;i != this->Policies.end(); ++i)
  404. {
  405. std::string full;
  406. full += i->second->LongDescription;
  407. // add in some more text here based on status
  408. // switch (i->second->Status)
  409. // {
  410. // case cmPolicies::WARN:
  411. cmDocumentationEntry e(i->second->IDString.c_str(),
  412. i->second->ShortDescription.c_str(),
  413. full.c_str());
  414. v.push_back(e);
  415. }
  416. }