cmCMakeMinimumRequired.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCMakeMinimumRequired.h"
  4. #include "cmVersion.h"
  5. // cmCMakeMinimumRequired
  6. bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args,
  7. cmExecutionStatus&)
  8. {
  9. // Process arguments.
  10. std::string version_string;
  11. bool doing_version = false;
  12. for (unsigned int i = 0; i < args.size(); ++i) {
  13. if (args[i] == "VERSION") {
  14. doing_version = true;
  15. } else if (args[i] == "FATAL_ERROR") {
  16. if (doing_version) {
  17. this->SetError("called with no value for VERSION.");
  18. return false;
  19. }
  20. doing_version = false;
  21. } else if (doing_version) {
  22. doing_version = false;
  23. version_string = args[i];
  24. } else {
  25. this->UnknownArguments.push_back(args[i]);
  26. }
  27. }
  28. if (doing_version) {
  29. this->SetError("called with no value for VERSION.");
  30. return false;
  31. }
  32. // Make sure there was a version to check.
  33. if (version_string.empty()) {
  34. return this->EnforceUnknownArguments();
  35. }
  36. // Save the required version string.
  37. this->Makefile->AddDefinition("CMAKE_MINIMUM_REQUIRED_VERSION",
  38. version_string.c_str());
  39. // Get the current version number.
  40. unsigned int current_major = cmVersion::GetMajorVersion();
  41. unsigned int current_minor = cmVersion::GetMinorVersion();
  42. unsigned int current_patch = cmVersion::GetPatchVersion();
  43. unsigned int current_tweak = cmVersion::GetTweakVersion();
  44. // Parse at least two components of the version number.
  45. // Use zero for those not specified.
  46. unsigned int required_major = 0;
  47. unsigned int required_minor = 0;
  48. unsigned int required_patch = 0;
  49. unsigned int required_tweak = 0;
  50. if (sscanf(version_string.c_str(), "%u.%u.%u.%u", &required_major,
  51. &required_minor, &required_patch, &required_tweak) < 2) {
  52. std::ostringstream e;
  53. e << "could not parse VERSION \"" << version_string << "\".";
  54. this->SetError(e.str());
  55. return false;
  56. }
  57. // Compare the version numbers.
  58. if ((current_major < required_major) ||
  59. (current_major == required_major && current_minor < required_minor) ||
  60. (current_major == required_major && current_minor == required_minor &&
  61. current_patch < required_patch) ||
  62. (current_major == required_major && current_minor == required_minor &&
  63. current_patch == required_patch && current_tweak < required_tweak)) {
  64. // The current version is too low.
  65. std::ostringstream e;
  66. e << "CMake " << version_string
  67. << " or higher is required. You are running version "
  68. << cmVersion::GetCMakeVersion();
  69. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  70. cmSystemTools::SetFatalErrorOccured();
  71. return true;
  72. }
  73. // The version is not from the future, so enforce unknown arguments.
  74. if (!this->EnforceUnknownArguments()) {
  75. return false;
  76. }
  77. if (required_major < 2 || (required_major == 2 && required_minor < 4)) {
  78. this->Makefile->IssueMessage(
  79. cmake::AUTHOR_WARNING,
  80. "Compatibility with CMake < 2.4 is not supported by CMake >= 3.0.");
  81. this->Makefile->SetPolicyVersion("2.4");
  82. } else {
  83. this->Makefile->SetPolicyVersion(version_string.c_str());
  84. }
  85. return true;
  86. }
  87. bool cmCMakeMinimumRequired::EnforceUnknownArguments()
  88. {
  89. if (!this->UnknownArguments.empty()) {
  90. std::ostringstream e;
  91. e << "called with unknown argument \"" << this->UnknownArguments[0]
  92. << "\".";
  93. this->SetError(e.str());
  94. return false;
  95. }
  96. return true;
  97. }