Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmCMakeMinimumRequired.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmCMakeMinimumRequired.h"
4
5
#include <cstdio>
6
#include <sstream>
7
8
#include "cmExecutionStatus.h"
9
#include "cmMakefile.h"
10
#include "cmMessageType.h"
11
#include "cmSystemTools.h"
12
#include "cmVersion.h"
13
14
namespace {
15
bool EnforceUnknownArguments(std::string const& version_max,
16
                             std::vector<std::string> const& unknown_arguments,
17
                             cmExecutionStatus& status);
18
}
19
20
// cmCMakeMinimumRequired
21
bool cmCMakeMinimumRequired(std::vector<std::string> const& args,
22
                            cmExecutionStatus& status)
23
0
{
24
  // Process arguments.
25
0
  std::string version_string;
26
0
  bool doing_version = false;
27
0
  std::vector<std::string> unknown_arguments;
28
0
  for (std::string const& arg : args) {
29
0
    if (arg == "VERSION") {
30
0
      doing_version = true;
31
0
    } else if (arg == "FATAL_ERROR") {
32
0
      if (doing_version) {
33
0
        status.SetError("called with no value for VERSION.");
34
0
        return false;
35
0
      }
36
0
      doing_version = false;
37
0
    } else if (doing_version) {
38
0
      doing_version = false;
39
0
      version_string = arg;
40
0
    } else {
41
0
      unknown_arguments.push_back(arg);
42
0
    }
43
0
  }
44
0
  if (doing_version) {
45
0
    status.SetError("called with no value for VERSION.");
46
0
    return false;
47
0
  }
48
49
  // Make sure there was a version to check.
50
0
  if (version_string.empty()) {
51
0
    return EnforceUnknownArguments(std::string(), unknown_arguments, status);
52
0
  }
53
54
  // Separate the <min> version and any trailing ...<max> component.
55
0
  std::string::size_type const dd = version_string.find("...");
56
0
  std::string const version_min = version_string.substr(0, dd);
57
0
  std::string const version_max = dd != std::string::npos
58
0
    ? version_string.substr(dd + 3, std::string::npos)
59
0
    : std::string();
60
0
  if (dd != std::string::npos &&
61
0
      (version_min.empty() || version_max.empty())) {
62
0
    std::ostringstream e;
63
0
    e << "VERSION \"" << version_string
64
0
      << R"(" does not have a version on both sides of "...".)";
65
0
    status.SetError(e.str());
66
0
    return false;
67
0
  }
68
69
  // Save the required version string.
70
0
  status.GetMakefile().AddDefinition("CMAKE_MINIMUM_REQUIRED_VERSION",
71
0
                                     version_min);
72
73
  // Get the current version number.
74
0
  unsigned int current_major = cmVersion::GetMajorVersion();
75
0
  unsigned int current_minor = cmVersion::GetMinorVersion();
76
0
  unsigned int current_patch = cmVersion::GetPatchVersion();
77
0
  unsigned int current_tweak = cmVersion::GetTweakVersion();
78
79
  // Parse at least two components of the version number.
80
  // Use zero for those not specified.
81
0
  unsigned int required_major = 0;
82
0
  unsigned int required_minor = 0;
83
0
  unsigned int required_patch = 0;
84
0
  unsigned int required_tweak = 0;
85
0
  if (sscanf(version_min.c_str(), "%u.%u.%u.%u", &required_major,
86
0
             &required_minor, &required_patch, &required_tweak) < 2) {
87
0
    std::ostringstream e;
88
0
    e << "could not parse VERSION \"" << version_min << "\".";
89
0
    status.SetError(e.str());
90
0
    return false;
91
0
  }
92
93
  // Compare the version numbers.
94
0
  if ((current_major < required_major) ||
95
0
      (current_major == required_major && current_minor < required_minor) ||
96
0
      (current_major == required_major && current_minor == required_minor &&
97
0
       current_patch < required_patch) ||
98
0
      (current_major == required_major && current_minor == required_minor &&
99
0
       current_patch == required_patch && current_tweak < required_tweak)) {
100
    // The current version is too low.
101
0
    std::ostringstream e;
102
0
    e << "CMake " << version_min
103
0
      << " or higher is required.  You are running version "
104
0
      << cmVersion::GetCMakeVersion();
105
0
    status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR, e.str());
106
0
    cmSystemTools::SetFatalErrorOccurred();
107
0
    return true;
108
0
  }
109
110
  // The version is not from the future, so enforce unknown arguments.
111
0
  if (!EnforceUnknownArguments(version_max, unknown_arguments, status)) {
112
0
    return false;
113
0
  }
114
115
0
  if (required_major < 2 || (required_major == 2 && required_minor < 4)) {
116
0
    status.GetMakefile().IssueMessage(
117
0
      MessageType::AUTHOR_WARNING,
118
0
      "Compatibility with CMake < 2.4 is not supported by CMake >= 3.0.");
119
0
    status.GetMakefile().SetPolicyVersion("2.4", version_max);
120
0
  } else {
121
0
    status.GetMakefile().SetPolicyVersion(version_min, version_max);
122
0
  }
123
124
0
  return true;
125
0
}
126
127
namespace {
128
bool EnforceUnknownArguments(std::string const& version_max,
129
                             std::vector<std::string> const& unknown_arguments,
130
                             cmExecutionStatus& status)
131
0
{
132
0
  if (unknown_arguments.empty()) {
133
0
    return true;
134
0
  }
135
136
  // Consider the max version if at least two components were given.
137
0
  unsigned int max_major = 0;
138
0
  unsigned int max_minor = 0;
139
0
  unsigned int max_patch = 0;
140
0
  unsigned int max_tweak = 0;
141
0
  if (sscanf(version_max.c_str(), "%u.%u.%u.%u", &max_major, &max_minor,
142
0
             &max_patch, &max_tweak) >= 2) {
143
0
    unsigned int current_major = cmVersion::GetMajorVersion();
144
0
    unsigned int current_minor = cmVersion::GetMinorVersion();
145
0
    unsigned int current_patch = cmVersion::GetPatchVersion();
146
0
    unsigned int current_tweak = cmVersion::GetTweakVersion();
147
148
0
    if ((current_major < max_major) ||
149
0
        (current_major == max_major && current_minor < max_minor) ||
150
0
        (current_major == max_major && current_minor == max_minor &&
151
0
         current_patch < max_patch) ||
152
0
        (current_major == max_major && current_minor == max_minor &&
153
0
         current_patch == max_patch && current_tweak < max_tweak)) {
154
      // A ...<max> version was given that is larger than the current
155
      // version of CMake, so tolerate unknown arguments.
156
0
      return true;
157
0
    }
158
0
  }
159
160
0
  std::ostringstream e;
161
0
  e << "called with unknown argument \"" << unknown_arguments[0] << "\".";
162
0
  status.SetError(e.str());
163
0
  return false;
164
0
}
165
}