mirror of
https://github.com/Kitware/CMake.git
synced 2026-02-21 14:40:26 -06:00
cmCommand refactor: cmCMakeMinimumRequired
This commit is contained in:
@@ -5,26 +5,32 @@
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "cmExecutionStatus.h"
|
||||
#include "cmMakefile.h"
|
||||
#include "cmMessageType.h"
|
||||
#include "cmSystemTools.h"
|
||||
#include "cmVersion.h"
|
||||
|
||||
class cmExecutionStatus;
|
||||
namespace {
|
||||
bool EnforceUnknownArguments(std::string const& version_max,
|
||||
std::vector<std::string> const& unknown_arguments,
|
||||
cmExecutionStatus& status);
|
||||
}
|
||||
|
||||
// cmCMakeMinimumRequired
|
||||
bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args,
|
||||
cmExecutionStatus&)
|
||||
bool cmCMakeMinimumRequired(std::vector<std::string> const& args,
|
||||
cmExecutionStatus& status)
|
||||
{
|
||||
// Process arguments.
|
||||
std::string version_string;
|
||||
bool doing_version = false;
|
||||
std::vector<std::string> unknown_arguments;
|
||||
for (std::string const& arg : args) {
|
||||
if (arg == "VERSION") {
|
||||
doing_version = true;
|
||||
} else if (arg == "FATAL_ERROR") {
|
||||
if (doing_version) {
|
||||
this->SetError("called with no value for VERSION.");
|
||||
status.SetError("called with no value for VERSION.");
|
||||
return false;
|
||||
}
|
||||
doing_version = false;
|
||||
@@ -32,17 +38,17 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args,
|
||||
doing_version = false;
|
||||
version_string = arg;
|
||||
} else {
|
||||
this->UnknownArguments.push_back(arg);
|
||||
unknown_arguments.push_back(arg);
|
||||
}
|
||||
}
|
||||
if (doing_version) {
|
||||
this->SetError("called with no value for VERSION.");
|
||||
status.SetError("called with no value for VERSION.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make sure there was a version to check.
|
||||
if (version_string.empty()) {
|
||||
return this->EnforceUnknownArguments(std::string());
|
||||
return EnforceUnknownArguments(std::string(), unknown_arguments, status);
|
||||
}
|
||||
|
||||
// Separate the <min> version and any trailing ...<max> component.
|
||||
@@ -56,12 +62,13 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args,
|
||||
std::ostringstream e;
|
||||
e << "VERSION \"" << version_string
|
||||
<< R"(" does not have a version on both sides of "...".)";
|
||||
this->SetError(e.str());
|
||||
status.SetError(e.str());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Save the required version string.
|
||||
this->Makefile->AddDefinition("CMAKE_MINIMUM_REQUIRED_VERSION", version_min);
|
||||
status.GetMakefile().AddDefinition("CMAKE_MINIMUM_REQUIRED_VERSION",
|
||||
version_min);
|
||||
|
||||
// Get the current version number.
|
||||
unsigned int current_major = cmVersion::GetMajorVersion();
|
||||
@@ -79,7 +86,7 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args,
|
||||
&required_minor, &required_patch, &required_tweak) < 2) {
|
||||
std::ostringstream e;
|
||||
e << "could not parse VERSION \"" << version_min << "\".";
|
||||
this->SetError(e.str());
|
||||
status.SetError(e.str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -95,32 +102,34 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args,
|
||||
e << "CMake " << version_min
|
||||
<< " or higher is required. You are running version "
|
||||
<< cmVersion::GetCMakeVersion();
|
||||
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
|
||||
status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR, e.str());
|
||||
cmSystemTools::SetFatalErrorOccured();
|
||||
return true;
|
||||
}
|
||||
|
||||
// The version is not from the future, so enforce unknown arguments.
|
||||
if (!this->EnforceUnknownArguments(version_max)) {
|
||||
if (!EnforceUnknownArguments(version_max, unknown_arguments, status)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (required_major < 2 || (required_major == 2 && required_minor < 4)) {
|
||||
this->Makefile->IssueMessage(
|
||||
status.GetMakefile().IssueMessage(
|
||||
MessageType::AUTHOR_WARNING,
|
||||
"Compatibility with CMake < 2.4 is not supported by CMake >= 3.0.");
|
||||
this->Makefile->SetPolicyVersion("2.4", version_max);
|
||||
status.GetMakefile().SetPolicyVersion("2.4", version_max);
|
||||
} else {
|
||||
this->Makefile->SetPolicyVersion(version_min, version_max);
|
||||
status.GetMakefile().SetPolicyVersion(version_min, version_max);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool cmCMakeMinimumRequired::EnforceUnknownArguments(
|
||||
std::string const& version_max)
|
||||
namespace {
|
||||
bool EnforceUnknownArguments(std::string const& version_max,
|
||||
std::vector<std::string> const& unknown_arguments,
|
||||
cmExecutionStatus& status)
|
||||
{
|
||||
if (this->UnknownArguments.empty()) {
|
||||
if (unknown_arguments.empty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -149,7 +158,8 @@ bool cmCMakeMinimumRequired::EnforceUnknownArguments(
|
||||
}
|
||||
|
||||
std::ostringstream e;
|
||||
e << "called with unknown argument \"" << this->UnknownArguments[0] << "\".";
|
||||
this->SetError(e.str());
|
||||
e << "called with unknown argument \"" << unknown_arguments[0] << "\".";
|
||||
status.SetError(e.str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,38 +8,14 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cm_memory.hxx"
|
||||
|
||||
#include "cmCommand.h"
|
||||
|
||||
class cmExecutionStatus;
|
||||
|
||||
/** \class cmCMakeMinimumRequired
|
||||
/**
|
||||
* \brief cmake_minimum_required command
|
||||
*
|
||||
* cmCMakeMinimumRequired implements the cmake_minimum_required CMake command
|
||||
*/
|
||||
class cmCMakeMinimumRequired : public cmCommand
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* This is a virtual constructor for the command.
|
||||
*/
|
||||
std::unique_ptr<cmCommand> Clone() override
|
||||
{
|
||||
return cm::make_unique<cmCMakeMinimumRequired>();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is called when the command is first encountered in
|
||||
* the CMakeLists.txt file.
|
||||
*/
|
||||
bool InitialPass(std::vector<std::string> const& args,
|
||||
cmExecutionStatus& status) override;
|
||||
|
||||
private:
|
||||
std::vector<std::string> UnknownArguments;
|
||||
bool EnforceUnknownArguments(std::string const& version_max);
|
||||
};
|
||||
bool cmCMakeMinimumRequired(std::vector<std::string> const& args,
|
||||
cmExecutionStatus& status);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -117,8 +117,7 @@
|
||||
void GetScriptingCommands(cmState* state)
|
||||
{
|
||||
state->AddBuiltinCommand("break", cmBreakCommand);
|
||||
state->AddBuiltinCommand("cmake_minimum_required",
|
||||
cm::make_unique<cmCMakeMinimumRequired>());
|
||||
state->AddBuiltinCommand("cmake_minimum_required", cmCMakeMinimumRequired);
|
||||
state->AddBuiltinCommand("cmake_policy",
|
||||
cm::make_unique<cmCMakePolicyCommand>());
|
||||
state->AddBuiltinCommand("configure_file",
|
||||
|
||||
Reference in New Issue
Block a user