cmCMakeMinimumRequired.cxx 3.4 KB

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