diff options
author | Brad King <brad.king@kitware.com> | 2018-03-26 11:12:50 -0400 |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2018-03-26 11:31:08 -0400 |
commit | 8dc97acb03db6d3d2f9effd84fd81432a68aa23c (patch) | |
tree | c9f3414a235dc540c6c7121465fa17d7145a3e74 | |
parent | 6792c7787aedc0fb2a32b6ca563917f7447f2e80 (diff) | |
download | cmake-8dc97acb03db6d3d2f9effd84fd81432a68aa23c.tar.gz |
cmake_minimum_required: Tolerate unknown future arguments
When a `...<max>` version is given that is larger than the running
version of CMake, assume that the project is aware of a newer version of
CMake and that any unknown arguments are future arguments. This will
allow future versions of CMake to add arguments to the command that
projects can use without introducing errors in older versions of CMake
(back to 3.12).
-rw-r--r-- | Source/cmCMakeMinimumRequired.cxx | 45 | ||||
-rw-r--r-- | Source/cmCMakeMinimumRequired.h | 2 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_minimum_required/Future.cmake | 1 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_minimum_required/RunCMakeTest.cmake | 2 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_minimum_required/Unknown-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_minimum_required/Unknown-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_minimum_required/Unknown.cmake | 1 |
7 files changed, 45 insertions, 11 deletions
diff --git a/Source/cmCMakeMinimumRequired.cxx b/Source/cmCMakeMinimumRequired.cxx index c4834a875b..2b51976b93 100644 --- a/Source/cmCMakeMinimumRequired.cxx +++ b/Source/cmCMakeMinimumRequired.cxx @@ -42,7 +42,7 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args, // Make sure there was a version to check. if (version_string.empty()) { - return this->EnforceUnknownArguments(); + return this->EnforceUnknownArguments(std::string()); } // Separate the <min> version and any trailing ...<max> component. @@ -102,7 +102,7 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args, } // The version is not from the future, so enforce unknown arguments. - if (!this->EnforceUnknownArguments()) { + if (!this->EnforceUnknownArguments(version_max)) { return false; } @@ -118,14 +118,39 @@ bool cmCMakeMinimumRequired::InitialPass(std::vector<std::string> const& args, return true; } -bool cmCMakeMinimumRequired::EnforceUnknownArguments() +bool cmCMakeMinimumRequired::EnforceUnknownArguments( + std::string const& version_max) { - if (!this->UnknownArguments.empty()) { - std::ostringstream e; - e << "called with unknown argument \"" << this->UnknownArguments[0] - << "\"."; - this->SetError(e.str()); - return false; + if (this->UnknownArguments.empty()) { + return true; } - return true; + + // 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; + e << "called with unknown argument \"" << this->UnknownArguments[0] << "\"."; + this->SetError(e.str()); + return false; } diff --git a/Source/cmCMakeMinimumRequired.h b/Source/cmCMakeMinimumRequired.h index 18d9460ed6..f9b61e142c 100644 --- a/Source/cmCMakeMinimumRequired.h +++ b/Source/cmCMakeMinimumRequired.h @@ -34,7 +34,7 @@ public: private: std::vector<std::string> UnknownArguments; - bool EnforceUnknownArguments(); + bool EnforceUnknownArguments(std::string const& version_max); }; #endif diff --git a/Tests/RunCMake/cmake_minimum_required/Future.cmake b/Tests/RunCMake/cmake_minimum_required/Future.cmake new file mode 100644 index 0000000000..2b5c44517b --- /dev/null +++ b/Tests/RunCMake/cmake_minimum_required/Future.cmake @@ -0,0 +1 @@ +cmake_minimum_required(VERSION 3.11...99.0 SOME_FUTURE_OPTION) diff --git a/Tests/RunCMake/cmake_minimum_required/RunCMakeTest.cmake b/Tests/RunCMake/cmake_minimum_required/RunCMakeTest.cmake index 22aa5b76b3..1030211e72 100644 --- a/Tests/RunCMake/cmake_minimum_required/RunCMakeTest.cmake +++ b/Tests/RunCMake/cmake_minimum_required/RunCMakeTest.cmake @@ -2,6 +2,8 @@ include(RunCMake) run_cmake(Before24) run_cmake(CompatBefore24) +run_cmake(Future) run_cmake(PolicyBefore24) run_cmake(Range) run_cmake(RangeBad) +run_cmake(Unknown) diff --git a/Tests/RunCMake/cmake_minimum_required/Unknown-result.txt b/Tests/RunCMake/cmake_minimum_required/Unknown-result.txt new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/Tests/RunCMake/cmake_minimum_required/Unknown-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/cmake_minimum_required/Unknown-stderr.txt b/Tests/RunCMake/cmake_minimum_required/Unknown-stderr.txt new file mode 100644 index 0000000000..d698642b4f --- /dev/null +++ b/Tests/RunCMake/cmake_minimum_required/Unknown-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at Unknown.cmake:1 \(cmake_minimum_required\): + cmake_minimum_required called with unknown argument "SOME_UNKNOWN_OPTION". +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\)$ diff --git a/Tests/RunCMake/cmake_minimum_required/Unknown.cmake b/Tests/RunCMake/cmake_minimum_required/Unknown.cmake new file mode 100644 index 0000000000..6c70f917c0 --- /dev/null +++ b/Tests/RunCMake/cmake_minimum_required/Unknown.cmake @@ -0,0 +1 @@ +cmake_minimum_required(VERSION 3.11 SOME_UNKNOWN_OPTION) |