Coverage Report

Created: 2026-04-29 07:01

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