cmCMakeMinimumRequired.cxx 3.69 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
4
#include "cmCMakeMinimumRequired.h"

5
6
7
#include <sstream>
#include <stdio.h>

8
#include "cmMakefile.h"
9
#include "cmSystemTools.h"
10
#include "cmVersion.h"
11
12
13
#include "cmake.h"

class cmExecutionStatus;
14

15
// cmCMakeMinimumRequired
16
17
bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args,
                                         cmExecutionStatus&)
18
{
19
20
21
  // Process arguments.
  std::string version_string;
  bool doing_version = false;
22
23
  for (unsigned int i = 0; i < args.size(); ++i) {
    if (args[i] == "VERSION") {
24
      doing_version = true;
25
26
    } else if (args[i] == "FATAL_ERROR") {
      if (doing_version) {
27
28
29
        this->SetError("called with no value for VERSION.");
        return false;
      }
30
31
      doing_version = false;
    } else if (doing_version) {
32
33
      doing_version = false;
      version_string = args[i];
34
    } else {
35
      this->UnknownArguments.push_back(args[i]);
36
    }
37
38
  }
  if (doing_version) {
39
    this->SetError("called with no value for VERSION.");
40
    return false;
41
  }
42
43

  // Make sure there was a version to check.
44
  if (version_string.empty()) {
45
    return this->EnforceUnknownArguments();
46
  }
47
48

  // Save the required version string.
Ken Martin's avatar
Ken Martin committed
49
50
51
  this->Makefile->AddDefinition("CMAKE_MINIMUM_REQUIRED_VERSION",
                                version_string.c_str());

52
  // Get the current version number.
53
54
55
56
  unsigned int current_major = cmVersion::GetMajorVersion();
  unsigned int current_minor = cmVersion::GetMinorVersion();
  unsigned int current_patch = cmVersion::GetPatchVersion();
  unsigned int current_tweak = cmVersion::GetTweakVersion();
57

58
59
  // Parse at least two components of the version number.
  // Use zero for those not specified.
60
61
62
63
  unsigned int required_major = 0;
  unsigned int required_minor = 0;
  unsigned int required_patch = 0;
  unsigned int required_tweak = 0;
64
65
  if (sscanf(version_string.c_str(), "%u.%u.%u.%u", &required_major,
             &required_minor, &required_patch, &required_tweak) < 2) {
66
    std::ostringstream e;
67
    e << "could not parse VERSION \"" << version_string << "\".";
Stephen Kelly's avatar
Stephen Kelly committed
68
    this->SetError(e.str());
69
    return false;
70
  }
71
72

  // Compare the version numbers.
73
74
75
76
77
78
  if ((current_major < required_major) ||
      (current_major == required_major && current_minor < required_minor) ||
      (current_major == required_major && current_minor == required_minor &&
       current_patch < required_patch) ||
      (current_major == required_major && current_minor == required_minor &&
       current_patch == required_patch && current_tweak < required_tweak)) {
79
    // The current version is too low.
80
    std::ostringstream e;
81
    e << "CMake " << version_string
82
      << " or higher is required.  You are running version "
83
      << cmVersion::GetCMakeVersion();
84
    this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
85
    cmSystemTools::SetFatalErrorOccured();
86
    return true;
87
  }
88

89
  // The version is not from the future, so enforce unknown arguments.
90
  if (!this->EnforceUnknownArguments()) {
91
    return false;
92
  }
93

94
  if (required_major < 2 || (required_major == 2 && required_minor < 4)) {
95
96
97
    this->Makefile->IssueMessage(
      cmake::AUTHOR_WARNING,
      "Compatibility with CMake < 2.4 is not supported by CMake >= 3.0.");
98
    this->Makefile->SetPolicyVersion("2.4");
99
  } else {
100
101
    this->Makefile->SetPolicyVersion(version_string.c_str());
  }
102

103
104
105
  return true;
}

106
107
bool cmCMakeMinimumRequired::EnforceUnknownArguments()
{
108
  if (!this->UnknownArguments.empty()) {
109
    std::ostringstream e;
110
111
    e << "called with unknown argument \"" << this->UnknownArguments[0]
      << "\".";
Stephen Kelly's avatar
Stephen Kelly committed
112
    this->SetError(e.str());
113
    return false;
114
  }
115
116
  return true;
}