1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmCMakeMinimumRequired.h"
8 #include "cmExecutionStatus.h"
9 #include "cmMakefile.h"
10 #include "cmMessageType.h"
11 #include "cmSystemTools.h"
12 #include "cmVersion.h"
15 bool EnforceUnknownArguments(std::string
const& version_max
,
16 std::vector
<std::string
> const& unknown_arguments
,
17 cmExecutionStatus
& status
);
20 // cmCMakeMinimumRequired
21 bool cmCMakeMinimumRequired(std::vector
<std::string
> const& args
,
22 cmExecutionStatus
& status
)
25 std::string version_string
;
26 bool doing_version
= false;
27 std::vector
<std::string
> unknown_arguments
;
28 for (std::string
const& arg
: args
) {
29 if (arg
== "VERSION") {
31 } else if (arg
== "FATAL_ERROR") {
33 status
.SetError("called with no value for VERSION.");
36 doing_version
= false;
37 } else if (doing_version
) {
38 doing_version
= false;
41 unknown_arguments
.push_back(arg
);
45 status
.SetError("called with no value for VERSION.");
49 // Make sure there was a version to check.
50 if (version_string
.empty()) {
51 return EnforceUnknownArguments(std::string(), unknown_arguments
, status
);
54 // Separate the <min> version and any trailing ...<max> component.
55 std::string::size_type
const dd
= version_string
.find("...");
56 std::string
const version_min
= version_string
.substr(0, dd
);
57 std::string
const version_max
= dd
!= std::string::npos
58 ? version_string
.substr(dd
+ 3, std::string::npos
)
60 if (dd
!= std::string::npos
&&
61 (version_min
.empty() || version_max
.empty())) {
63 e
<< "VERSION \"" << version_string
64 << R
"(" does
not have a version on both sides of
"...".)";
65 status.SetError(e.str());
69 // Save the required version string.
70 status.GetMakefile().AddDefinition("CMAKE_MINIMUM_REQUIRED_VERSION
",
73 // Get the current version number.
74 unsigned int current_major = cmVersion::GetMajorVersion();
75 unsigned int current_minor = cmVersion::GetMinorVersion();
76 unsigned int current_patch = cmVersion::GetPatchVersion();
77 unsigned int current_tweak = cmVersion::GetTweakVersion();
79 // Parse at least two components of the version number.
80 // Use zero for those not specified.
81 unsigned int required_major = 0;
82 unsigned int required_minor = 0;
83 unsigned int required_patch = 0;
84 unsigned int required_tweak = 0;
85 if (sscanf(version_min.c_str(), "%u
.%u
.%u
.%u
", &required_major,
86 &required_minor, &required_patch, &required_tweak) < 2) {
88 e << "could
not parse VERSION
\"" << version_min << "\".";
89 status.SetError(e.str());
93 // Compare the version numbers.
94 if ((current_major < required_major) ||
95 (current_major == required_major && current_minor < required_minor) ||
96 (current_major == required_major && current_minor == required_minor &&
97 current_patch < required_patch) ||
98 (current_major == required_major && current_minor == required_minor &&
99 current_patch == required_patch && current_tweak < required_tweak)) {
100 // The current version is too low.
101 std::ostringstream e;
102 e << "CMake
" << version_min
103 << " or higher is required
. You are running version
"
104 << cmVersion::GetCMakeVersion();
105 status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR, e.str());
106 cmSystemTools::SetFatalErrorOccurred();
110 // The version is not from the future, so enforce unknown arguments.
111 if (!EnforceUnknownArguments(version_max, unknown_arguments, status)) {
115 if (required_major < 2 || (required_major == 2 && required_minor < 4)) {
116 status.GetMakefile().IssueMessage(
117 MessageType::AUTHOR_WARNING,
118 "Compatibility with CMake
< 2.4 is
not supported by CMake
>= 3.0.");
119 status.GetMakefile().SetPolicyVersion("2.4", version_max);
121 status.GetMakefile().SetPolicyVersion(version_min, version_max);
128 bool EnforceUnknownArguments(std::string const& version_max,
129 std::vector<std::string> const& unknown_arguments,
130 cmExecutionStatus& status)
132 if (unknown_arguments.empty()) {
136 // Consider the max version if at least two components were given.
137 unsigned int max_major = 0;
138 unsigned int max_minor = 0;
139 unsigned int max_patch = 0;
140 unsigned int max_tweak = 0;
141 if (sscanf(version_max.c_str(), "%u
.%u
.%u
.%u
", &max_major, &max_minor,
142 &max_patch, &max_tweak) >= 2) {
143 unsigned int current_major = cmVersion::GetMajorVersion();
144 unsigned int current_minor = cmVersion::GetMinorVersion();
145 unsigned int current_patch = cmVersion::GetPatchVersion();
146 unsigned int current_tweak = cmVersion::GetTweakVersion();
148 if ((current_major < max_major) ||
149 (current_major == max_major && current_minor < max_minor) ||
150 (current_major == max_major && current_minor == max_minor &&
151 current_patch < max_patch) ||
152 (current_major == max_major && current_minor == max_minor &&
153 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.
160 std::ostringstream e;
161 e << "called with unknown argument
\"" << unknown_arguments[0] << "\".";
162 status.SetError(e.str());