cmPolicies.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "cmPolicies.h"
  2. #include "cmAlgorithms.h"
  3. #include "cmMakefile.h"
  4. #include "cmState.h"
  5. #include "cmSystemTools.h"
  6. #include "cmVersion.h"
  7. #include "cmake.h"
  8. #include <assert.h>
  9. #include <cmConfigure.h>
  10. #include <ctype.h>
  11. #include <sstream>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <vector>
  15. static bool stringToId(const char* input, cmPolicies::PolicyID& pid)
  16. {
  17. assert(input);
  18. if (strlen(input) != 7) {
  19. return false;
  20. }
  21. if (!cmHasLiteralPrefix(input, "CMP")) {
  22. return false;
  23. }
  24. if (cmHasLiteralSuffix(input, "0000")) {
  25. pid = cmPolicies::CMP0000;
  26. return true;
  27. }
  28. for (int i = 3; i < 7; ++i) {
  29. if (!isdigit(*(input + i))) {
  30. return false;
  31. }
  32. }
  33. long id;
  34. if (!cmSystemTools::StringToLong(input + 3, &id)) {
  35. return false;
  36. }
  37. if (id >= cmPolicies::CMPCOUNT) {
  38. return false;
  39. }
  40. pid = cmPolicies::PolicyID(id);
  41. return true;
  42. }
  43. #define CM_SELECT_ID_VERSION(F, A1, A2, A3, A4, A5, A6) F(A1, A3, A4, A5)
  44. #define CM_FOR_EACH_POLICY_ID_VERSION(POLICY) \
  45. CM_FOR_EACH_POLICY_TABLE(POLICY, CM_SELECT_ID_VERSION)
  46. #define CM_SELECT_ID_DOC(F, A1, A2, A3, A4, A5, A6) F(A1, A2)
  47. #define CM_FOR_EACH_POLICY_ID_DOC(POLICY) \
  48. CM_FOR_EACH_POLICY_TABLE(POLICY, CM_SELECT_ID_DOC)
  49. static const char* idToString(cmPolicies::PolicyID id)
  50. {
  51. switch (id) {
  52. #define POLICY_CASE(ID) \
  53. case cmPolicies::ID: \
  54. return #ID;
  55. CM_FOR_EACH_POLICY_ID(POLICY_CASE)
  56. #undef POLICY_CASE
  57. case cmPolicies::CMPCOUNT:
  58. return CM_NULLPTR;
  59. }
  60. return CM_NULLPTR;
  61. }
  62. static const char* idToVersion(cmPolicies::PolicyID id)
  63. {
  64. switch (id) {
  65. #define POLICY_CASE(ID, V_MAJOR, V_MINOR, V_PATCH) \
  66. case cmPolicies::ID: \
  67. return #V_MAJOR "." #V_MINOR "." #V_PATCH;
  68. CM_FOR_EACH_POLICY_ID_VERSION(POLICY_CASE)
  69. #undef POLICY_CASE
  70. case cmPolicies::CMPCOUNT:
  71. return CM_NULLPTR;
  72. }
  73. return CM_NULLPTR;
  74. }
  75. static bool isPolicyNewerThan(cmPolicies::PolicyID id, unsigned int majorV,
  76. unsigned int minorV, unsigned int patchV)
  77. {
  78. switch (id) {
  79. #define POLICY_CASE(ID, V_MAJOR, V_MINOR, V_PATCH) \
  80. case cmPolicies::ID: \
  81. return (majorV < (V_MAJOR) || \
  82. (majorV == (V_MAJOR) && minorV + 1 < (V_MINOR) + 1) || \
  83. (majorV == (V_MAJOR) && minorV == (V_MINOR) && \
  84. patchV + 1 < (V_PATCH) + 1));
  85. CM_FOR_EACH_POLICY_ID_VERSION(POLICY_CASE)
  86. #undef POLICY_CASE
  87. case cmPolicies::CMPCOUNT:
  88. return false;
  89. }
  90. return false;
  91. }
  92. const char* idToShortDescription(cmPolicies::PolicyID id)
  93. {
  94. switch (id) {
  95. #define POLICY_CASE(ID, SHORT_DESCRIPTION) \
  96. case cmPolicies::ID: \
  97. return SHORT_DESCRIPTION;
  98. CM_FOR_EACH_POLICY_ID_DOC(POLICY_CASE)
  99. #undef POLICY_CASE
  100. case cmPolicies::CMPCOUNT:
  101. return CM_NULLPTR;
  102. }
  103. return CM_NULLPTR;
  104. }
  105. static void DiagnoseAncientPolicies(
  106. std::vector<cmPolicies::PolicyID> const& ancient, unsigned int majorVer,
  107. unsigned int minorVer, unsigned int patchVer, cmMakefile* mf)
  108. {
  109. std::ostringstream e;
  110. e << "The project requests behavior compatible with CMake version \""
  111. << majorVer << "." << minorVer << "." << patchVer
  112. << "\", which requires the OLD behavior for some policies:\n";
  113. for (std::vector<cmPolicies::PolicyID>::const_iterator i = ancient.begin();
  114. i != ancient.end(); ++i) {
  115. e << " " << idToString(*i) << ": " << idToShortDescription(*i) << "\n";
  116. }
  117. e << "However, this version of CMake no longer supports the OLD "
  118. << "behavior for these policies. "
  119. << "Please either update your CMakeLists.txt files to conform to "
  120. << "the new behavior or use an older version of CMake that still "
  121. << "supports the old behavior.";
  122. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  123. }
  124. static bool GetPolicyDefault(cmMakefile* mf, std::string const& policy,
  125. cmPolicies::PolicyStatus* defaultSetting)
  126. {
  127. std::string defaultVar = "CMAKE_POLICY_DEFAULT_" + policy;
  128. std::string defaultValue = mf->GetSafeDefinition(defaultVar);
  129. if (defaultValue == "NEW") {
  130. *defaultSetting = cmPolicies::NEW;
  131. } else if (defaultValue == "OLD") {
  132. *defaultSetting = cmPolicies::OLD;
  133. } else if (defaultValue == "") {
  134. *defaultSetting = cmPolicies::WARN;
  135. } else {
  136. std::ostringstream e;
  137. e << defaultVar << " has value \"" << defaultValue
  138. << "\" but must be \"OLD\", \"NEW\", or \"\" (empty).";
  139. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  140. return false;
  141. }
  142. return true;
  143. }
  144. bool cmPolicies::ApplyPolicyVersion(cmMakefile* mf, const char* version)
  145. {
  146. std::string ver = "2.4.0";
  147. if (version && strlen(version) > 0) {
  148. ver = version;
  149. }
  150. unsigned int majorVer = 2;
  151. unsigned int minorVer = 0;
  152. unsigned int patchVer = 0;
  153. unsigned int tweakVer = 0;
  154. // parse the string
  155. if (sscanf(ver.c_str(), "%u.%u.%u.%u", &majorVer, &minorVer, &patchVer,
  156. &tweakVer) < 2) {
  157. std::ostringstream e;
  158. e << "Invalid policy version value \"" << ver << "\". "
  159. << "A numeric major.minor[.patch[.tweak]] must be given.";
  160. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  161. return false;
  162. }
  163. // it is an error if the policy version is less than 2.4
  164. if (majorVer < 2 || (majorVer == 2 && minorVer < 4)) {
  165. mf->IssueMessage(
  166. cmake::FATAL_ERROR,
  167. "Compatibility with CMake < 2.4 is not supported by CMake >= 3.0. "
  168. "For compatibility with older versions please use any CMake 2.8.x "
  169. "release or lower.");
  170. return false;
  171. }
  172. // It is an error if the policy version is greater than the running
  173. // CMake.
  174. if (majorVer > cmVersion::GetMajorVersion() ||
  175. (majorVer == cmVersion::GetMajorVersion() &&
  176. minorVer > cmVersion::GetMinorVersion()) ||
  177. (majorVer == cmVersion::GetMajorVersion() &&
  178. minorVer == cmVersion::GetMinorVersion() &&
  179. patchVer > cmVersion::GetPatchVersion()) ||
  180. (majorVer == cmVersion::GetMajorVersion() &&
  181. minorVer == cmVersion::GetMinorVersion() &&
  182. patchVer == cmVersion::GetPatchVersion() &&
  183. tweakVer > cmVersion::GetTweakVersion())) {
  184. std::ostringstream e;
  185. e << "An attempt was made to set the policy version of CMake to \""
  186. << version << "\" which is greater than this version of CMake. "
  187. << "This is not allowed because the greater version may have new "
  188. << "policies not known to this CMake. "
  189. << "You may need a newer CMake version to build this project.";
  190. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  191. return false;
  192. }
  193. // now loop over all the policies and set them as appropriate
  194. std::vector<cmPolicies::PolicyID> ancientPolicies;
  195. for (PolicyID pid = cmPolicies::CMP0000; pid != cmPolicies::CMPCOUNT;
  196. pid = PolicyID(pid + 1)) {
  197. if (isPolicyNewerThan(pid, majorVer, minorVer, patchVer)) {
  198. if (cmPolicies::GetPolicyStatus(pid) == cmPolicies::REQUIRED_ALWAYS) {
  199. ancientPolicies.push_back(pid);
  200. } else {
  201. cmPolicies::PolicyStatus status = cmPolicies::WARN;
  202. if (!GetPolicyDefault(mf, idToString(pid), &status) ||
  203. !mf->SetPolicy(pid, status)) {
  204. return false;
  205. }
  206. if (pid == cmPolicies::CMP0001 &&
  207. (status == cmPolicies::WARN || status == cmPolicies::OLD)) {
  208. if (!(mf->GetState()->GetInitializedCacheValue(
  209. "CMAKE_BACKWARDS_COMPATIBILITY"))) {
  210. // Set it to 2.4 because that is the last version where the
  211. // variable had meaning.
  212. mf->AddCacheDefinition(
  213. "CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
  214. "For backwards compatibility, what version of CMake "
  215. "commands and "
  216. "syntax should this version of CMake try to support.",
  217. cmStateEnums::STRING);
  218. }
  219. }
  220. }
  221. } else {
  222. if (!mf->SetPolicy(pid, cmPolicies::NEW)) {
  223. return false;
  224. }
  225. }
  226. }
  227. // Make sure the project does not use any ancient policies.
  228. if (!ancientPolicies.empty()) {
  229. DiagnoseAncientPolicies(ancientPolicies, majorVer, minorVer, patchVer, mf);
  230. cmSystemTools::SetFatalErrorOccured();
  231. return false;
  232. }
  233. return true;
  234. }
  235. bool cmPolicies::GetPolicyID(const char* id, cmPolicies::PolicyID& pid)
  236. {
  237. return stringToId(id, pid);
  238. }
  239. ///! return a warning string for a given policy
  240. std::string cmPolicies::GetPolicyWarning(cmPolicies::PolicyID id)
  241. {
  242. std::ostringstream msg;
  243. msg << "Policy " << idToString(id) << " is not set: "
  244. ""
  245. << idToShortDescription(id) << " "
  246. "Run \"cmake --help-policy "
  247. << idToString(id) << "\" for "
  248. "policy details. "
  249. "Use the cmake_policy command to set the policy "
  250. "and suppress this warning.";
  251. return msg.str();
  252. }
  253. ///! return an error string for when a required policy is unspecified
  254. std::string cmPolicies::GetRequiredPolicyError(cmPolicies::PolicyID id)
  255. {
  256. std::ostringstream error;
  257. error << "Policy " << idToString(id) << " is not set to NEW: "
  258. ""
  259. << idToShortDescription(id) << " "
  260. "Run \"cmake --help-policy "
  261. << idToString(id)
  262. << "\" for "
  263. "policy details. "
  264. "CMake now requires this policy to be set to NEW by the project. "
  265. "The policy may be set explicitly using the code\n"
  266. " cmake_policy(SET "
  267. << idToString(id) << " NEW)\n"
  268. "or by upgrading all policies with the code\n"
  269. " cmake_policy(VERSION "
  270. << idToVersion(id)
  271. << ") # or later\n"
  272. "Run \"cmake --help-command cmake_policy\" for more information.";
  273. return error.str();
  274. }
  275. ///! Get the default status for a policy
  276. cmPolicies::PolicyStatus cmPolicies::GetPolicyStatus(
  277. cmPolicies::PolicyID /*unused*/)
  278. {
  279. return cmPolicies::WARN;
  280. }
  281. std::string cmPolicies::GetRequiredAlwaysPolicyError(cmPolicies::PolicyID id)
  282. {
  283. std::string pid = idToString(id);
  284. std::ostringstream e;
  285. e << "Policy " << pid << " may not be set to OLD behavior because this "
  286. << "version of CMake no longer supports it. "
  287. << "The policy was introduced in "
  288. << "CMake version " << idToVersion(id)
  289. << ", and use of NEW behavior is now required."
  290. << "\n"
  291. << "Please either update your CMakeLists.txt files to conform to "
  292. << "the new behavior or use an older version of CMake that still "
  293. << "supports the old behavior. "
  294. << "Run cmake --help-policy " << pid << " for more information.";
  295. return e.str();
  296. }
  297. cmPolicies::PolicyStatus cmPolicies::PolicyMap::Get(
  298. cmPolicies::PolicyID id) const
  299. {
  300. PolicyStatus status = cmPolicies::WARN;
  301. if (this->Status[(POLICY_STATUS_COUNT * id) + OLD]) {
  302. status = cmPolicies::OLD;
  303. } else if (this->Status[(POLICY_STATUS_COUNT * id) + NEW]) {
  304. status = cmPolicies::NEW;
  305. }
  306. return status;
  307. }
  308. void cmPolicies::PolicyMap::Set(cmPolicies::PolicyID id,
  309. cmPolicies::PolicyStatus status)
  310. {
  311. this->Status[(POLICY_STATUS_COUNT * id) + OLD] = (status == OLD);
  312. this->Status[(POLICY_STATUS_COUNT * id) + WARN] = (status == WARN);
  313. this->Status[(POLICY_STATUS_COUNT * id) + NEW] = (status == NEW);
  314. }
  315. bool cmPolicies::PolicyMap::IsDefined(cmPolicies::PolicyID id) const
  316. {
  317. return this->Status[(POLICY_STATUS_COUNT * id) + OLD] ||
  318. this->Status[(POLICY_STATUS_COUNT * id) + WARN] ||
  319. this->Status[(POLICY_STATUS_COUNT * id) + NEW];
  320. }
  321. bool cmPolicies::PolicyMap::IsEmpty() const
  322. {
  323. return this->Status.none();
  324. }