cmCMakeMinimumRequired.cxx 3.6 KB

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