cmPolicies.cxx 12 KB

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