Coverage Report

Created: 2026-10-28 06:38

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