cmCMakeMinimumRequired.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCMakeMinimumRequired.h"
  11. #include "cmVersion.h"
  12. // cmCMakeMinimumRequired
  13. bool cmCMakeMinimumRequired
  14. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  15. {
  16. // Process arguments.
  17. std::string version_string;
  18. bool doing_version = false;
  19. for(unsigned int i=0; i < args.size(); ++i)
  20. {
  21. if(args[i] == "VERSION")
  22. {
  23. doing_version = true;
  24. }
  25. else if(args[i] == "FATAL_ERROR")
  26. {
  27. if(doing_version)
  28. {
  29. this->SetError("called with no value for VERSION.");
  30. return false;
  31. }
  32. doing_version = false;
  33. }
  34. else if(doing_version)
  35. {
  36. doing_version = false;
  37. version_string = args[i];
  38. }
  39. else
  40. {
  41. this->UnknownArguments.push_back(args[i]);
  42. }
  43. }
  44. if(doing_version)
  45. {
  46. this->SetError("called with no value for VERSION.");
  47. return false;
  48. }
  49. // Make sure there was a version to check.
  50. if(version_string.empty())
  51. {
  52. return this->EnforceUnknownArguments();
  53. }
  54. // Save the required version string.
  55. this->Makefile->AddDefinition("CMAKE_MINIMUM_REQUIRED_VERSION",
  56. version_string.c_str());
  57. // Get the current version number.
  58. int current_major = cmVersion::GetMajorVersion();
  59. int current_minor = cmVersion::GetMinorVersion();
  60. int current_patch = cmVersion::GetPatchVersion();
  61. // Parse the required version number. If no patch-level is given
  62. // use zero.
  63. int required_major = 0;
  64. int required_minor = 0;
  65. int required_patch = 0;
  66. if(sscanf(version_string.c_str(), "%d.%d.%d",
  67. &required_major, &required_minor, &required_patch) < 2)
  68. {
  69. cmOStringStream e;
  70. e << "could not parse VERSION \"" << version_string.c_str() << "\".";
  71. this->SetError(e.str().c_str());
  72. return false;
  73. }
  74. // Compare the version numbers.
  75. if((current_major < required_major) ||
  76. (current_major == required_major &&
  77. current_minor < required_minor) ||
  78. (current_major == required_major &&
  79. current_minor == required_minor &&
  80. current_patch < required_patch))
  81. {
  82. // The current version is too low.
  83. cmOStringStream e;
  84. e << "CMake " << version_string.c_str()
  85. << " or higher is required. You are running version "
  86. << current_major << "." << current_minor << "." << current_patch;
  87. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  88. cmSystemTools::SetFatalErrorOccured();
  89. return true;
  90. }
  91. // The version is not from the future, so enforce unknown arguments.
  92. if(!this->EnforceUnknownArguments())
  93. {
  94. return false;
  95. }
  96. if (required_major < 2 || (required_major == 2 && required_minor < 4))
  97. {
  98. this->Makefile->SetPolicyVersion("2.4");
  99. }
  100. else
  101. {
  102. this->Makefile->SetPolicyVersion(version_string.c_str());
  103. }
  104. return true;
  105. }
  106. //----------------------------------------------------------------------------
  107. bool cmCMakeMinimumRequired::EnforceUnknownArguments()
  108. {
  109. if(!this->UnknownArguments.empty())
  110. {
  111. cmOStringStream e;
  112. e << "called with unknown argument \""
  113. << this->UnknownArguments[0] << "\".";
  114. this->SetError(e.str().c_str());
  115. return false;
  116. }
  117. return true;
  118. }