cmake/Source/cmCMakeMinimumRequired.cxx

166 lines
5.8 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCMakeMinimumRequired.h"
2020-02-01 23:06:01 +01:00
#include <cstdio>
2017-04-14 19:02:05 +02:00
#include <sstream>
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
#include "cmVersion.h"
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
namespace {
bool EnforceUnknownArguments(std::string const& version_max,
std::vector<std::string> const& unknown_arguments,
cmExecutionStatus& status);
}
// cmCMakeMinimumRequired
2020-02-01 23:06:01 +01:00
bool cmCMakeMinimumRequired(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
// Process arguments.
std::string version_string;
bool doing_version = false;
2020-02-01 23:06:01 +01:00
std::vector<std::string> unknown_arguments;
2018-01-26 17:06:56 +01:00
for (std::string const& arg : args) {
if (arg == "VERSION") {
doing_version = true;
2018-01-26 17:06:56 +01:00
} else if (arg == "FATAL_ERROR") {
2016-07-09 11:21:54 +02:00
if (doing_version) {
2020-02-01 23:06:01 +01:00
status.SetError("called with no value for VERSION.");
return false;
}
2016-07-09 11:21:54 +02:00
doing_version = false;
} else if (doing_version) {
doing_version = false;
2018-01-26 17:06:56 +01:00
version_string = arg;
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
unknown_arguments.push_back(arg);
}
2016-07-09 11:21:54 +02:00
}
if (doing_version) {
2020-02-01 23:06:01 +01:00
status.SetError("called with no value for VERSION.");
return false;
2016-07-09 11:21:54 +02:00
}
// Make sure there was a version to check.
2016-07-09 11:21:54 +02:00
if (version_string.empty()) {
2020-02-01 23:06:01 +01:00
return EnforceUnknownArguments(std::string(), unknown_arguments, status);
2018-08-09 18:06:22 +02:00
}
// Separate the <min> version and any trailing ...<max> component.
std::string::size_type const dd = version_string.find("...");
std::string const version_min = version_string.substr(0, dd);
std::string const version_max = dd != std::string::npos
? version_string.substr(dd + 3, std::string::npos)
: std::string();
if (dd != std::string::npos &&
(version_min.empty() || version_max.empty())) {
std::ostringstream e;
e << "VERSION \"" << version_string
2019-11-11 23:01:05 +01:00
<< R"(" does not have a version on both sides of "...".)";
2020-02-01 23:06:01 +01:00
status.SetError(e.str());
2018-08-09 18:06:22 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
// Save the required version string.
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition("CMAKE_MINIMUM_REQUIRED_VERSION",
version_min);
// Get the current version number.
2015-04-27 22:25:09 +02:00
unsigned int current_major = cmVersion::GetMajorVersion();
unsigned int current_minor = cmVersion::GetMinorVersion();
unsigned int current_patch = cmVersion::GetPatchVersion();
unsigned int current_tweak = cmVersion::GetTweakVersion();
2010-06-23 01:18:35 +03:00
// Parse at least two components of the version number.
// Use zero for those not specified.
2015-04-27 22:25:09 +02:00
unsigned int required_major = 0;
unsigned int required_minor = 0;
unsigned int required_patch = 0;
unsigned int required_tweak = 0;
2018-08-09 18:06:22 +02:00
if (sscanf(version_min.c_str(), "%u.%u.%u.%u", &required_major,
2016-07-09 11:21:54 +02:00
&required_minor, &required_patch, &required_tweak) < 2) {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2018-08-09 18:06:22 +02:00
e << "could not parse VERSION \"" << version_min << "\".";
2020-02-01 23:06:01 +01:00
status.SetError(e.str());
return false;
2016-07-09 11:21:54 +02:00
}
// Compare the version numbers.
2016-07-09 11:21:54 +02:00
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)) {
// The current version is too low.
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2018-08-09 18:06:22 +02:00
e << "CMake " << version_min
<< " or higher is required. You are running version "
2010-06-23 01:18:35 +03:00
<< cmVersion::GetCMakeVersion();
2020-02-01 23:06:01 +01:00
status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR, e.str());
cmSystemTools::SetFatalErrorOccured();
return true;
2016-07-09 11:21:54 +02:00
}
// The version is not from the future, so enforce unknown arguments.
2020-02-01 23:06:01 +01:00
if (!EnforceUnknownArguments(version_max, unknown_arguments, status)) {
return false;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (required_major < 2 || (required_major == 2 && required_minor < 4)) {
2020-02-01 23:06:01 +01:00
status.GetMakefile().IssueMessage(
2019-11-11 23:01:05 +01:00
MessageType::AUTHOR_WARNING,
2014-08-03 19:52:23 +02:00
"Compatibility with CMake < 2.4 is not supported by CMake >= 3.0.");
2020-02-01 23:06:01 +01:00
status.GetMakefile().SetPolicyVersion("2.4", version_max);
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
status.GetMakefile().SetPolicyVersion(version_min, version_max);
}
2013-03-16 19:13:01 +02:00
return true;
}
2020-02-01 23:06:01 +01:00
namespace {
bool EnforceUnknownArguments(std::string const& version_max,
std::vector<std::string> const& unknown_arguments,
cmExecutionStatus& status)
{
2020-02-01 23:06:01 +01:00
if (unknown_arguments.empty()) {
2018-08-09 18:06:22 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2018-08-09 18:06:22 +02:00
// Consider the max version if at least two components were given.
unsigned int max_major = 0;
unsigned int max_minor = 0;
unsigned int max_patch = 0;
unsigned int max_tweak = 0;
if (sscanf(version_max.c_str(), "%u.%u.%u.%u", &max_major, &max_minor,
&max_patch, &max_tweak) >= 2) {
unsigned int current_major = cmVersion::GetMajorVersion();
unsigned int current_minor = cmVersion::GetMinorVersion();
unsigned int current_patch = cmVersion::GetPatchVersion();
unsigned int current_tweak = cmVersion::GetTweakVersion();
if ((current_major < max_major) ||
(current_major == max_major && current_minor < max_minor) ||
(current_major == max_major && current_minor == max_minor &&
current_patch < max_patch) ||
(current_major == max_major && current_minor == max_minor &&
current_patch == max_patch && current_tweak < max_tweak)) {
// A ...<max> version was given that is larger than the current
// version of CMake, so tolerate unknown arguments.
return true;
}
}
std::ostringstream e;
2020-02-01 23:06:01 +01:00
e << "called with unknown argument \"" << unknown_arguments[0] << "\".";
status.SetError(e.str());
2018-08-09 18:06:22 +02:00
return false;
}
2020-02-01 23:06:01 +01:00
}