From 35a7ed125bf7f051635b78e8e2babe441112cbb1 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Fri, 31 Jan 2025 14:00:32 -0500 Subject: find_package: Fix reporting of rejected CPS files' version The logic to extract the version of a CPS file into the location used to record files that were considered but rejected was happening too late, resulting in rejected files unnecessarily reporting their version as "unknown". Fix this by filling the variable sooner. --- Source/cmFindPackageCommand.cxx | 9 ++++++--- Tests/RunCMake/find_package-CPS/MissingComponent-stderr.txt | 2 +- .../find_package-CPS/MissingTransitiveComponent-stderr.txt | 2 +- Tests/RunCMake/find_package-CPS/cps/componenttest.cps | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index cd1938e..a47e9c3 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -2718,7 +2718,10 @@ bool cmFindPackageCommand::CheckVersion(std::string const& config_file) cmPackageInfoReader::Read(config_file); if (reader && reader->GetName() == this->Name) { cm::optional cpsVersion = reader->GetVersion(); - if (cpsVersion) { + bool const hasVersion = cpsVersion.has_value(); + + if (hasVersion) { + version = std::move(*cpsVersion); // TODO: Implement version check for CPS result = true; } else { @@ -2752,8 +2755,8 @@ bool cmFindPackageCommand::CheckVersion(std::string const& config_file) result = false; } - if (result && cpsVersion) { - this->VersionFound = (version = std::move(*cpsVersion)); + if (result && hasVersion) { + this->VersionFound = version; std::vector const& versionParts = reader->ParseVersion(); this->VersionFoundCount = static_cast(versionParts.size()); diff --git a/Tests/RunCMake/find_package-CPS/MissingComponent-stderr.txt b/Tests/RunCMake/find_package-CPS/MissingComponent-stderr.txt index d617cfd..14f1f0a 100644 --- a/Tests/RunCMake/find_package-CPS/MissingComponent-stderr.txt +++ b/Tests/RunCMake/find_package-CPS/MissingComponent-stderr.txt @@ -5,7 +5,7 @@ CMake Error at MissingComponent.cmake:[0-9]+ \(find_package\): The following configuration files were considered but not accepted: ( [^ -]*/Tests/RunCMake/find_package-CPS/cps/[Cc]omponent[Tt]est\.cps, version: unknown)+ +]*/Tests/RunCMake/find_package-CPS/cps/[Cc]omponent[Tt]est\.cps, version: 1\.0)+ Call Stack \(most recent call first\): CMakeLists\.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/find_package-CPS/MissingTransitiveComponent-stderr.txt b/Tests/RunCMake/find_package-CPS/MissingTransitiveComponent-stderr.txt index c102911..50c4afe 100644 --- a/Tests/RunCMake/find_package-CPS/MissingTransitiveComponent-stderr.txt +++ b/Tests/RunCMake/find_package-CPS/MissingTransitiveComponent-stderr.txt @@ -5,7 +5,7 @@ CMake Error in cps/[Tt]ransitive[Mm]issing\.cps: The following configuration files were considered but not accepted: ( [^ -]*/Tests/RunCMake/find_package-CPS/cps/[Cc]omponent[Tt]est\.cps, version: unknown)+ +]*/Tests/RunCMake/find_package-CPS/cps/[Cc]omponent[Tt]est\.cps, version: 1\.0)+ Call Stack \(most recent call first\): MissingTransitiveComponent\.cmake:[0-9]+ \(find_package\) diff --git a/Tests/RunCMake/find_package-CPS/cps/componenttest.cps b/Tests/RunCMake/find_package-CPS/cps/componenttest.cps index ef49af4..dbbe095 100644 --- a/Tests/RunCMake/find_package-CPS/cps/componenttest.cps +++ b/Tests/RunCMake/find_package-CPS/cps/componenttest.cps @@ -1,6 +1,7 @@ { "cps_version": "0.13", "name": "ComponentTest", + "version": "1.0", "cps_path": "@prefix@/cps", "components": {} } -- cgit v0.12 From 7a0e6983842c5d6f540e59ed2eaab0a509876393 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Fri, 31 Jan 2025 15:41:23 -0500 Subject: find_package: Fix CPS version parsing Update cmPackageInfoReader's version parsing to more fully conform to the specification and to reject non-conforming version strings. Start adding framework to support version schemas other than "simple". Fix how cmFindPackageCommand extracts version parts to not fail if more than four parts are present. --- Source/cmFindPackageCommand.cxx | 43 +++++++------ Source/cmPackageInfoReader.cxx | 84 +++++++++++++++++++------- Source/cmPackageInfoReader.h | 27 ++++++++- Tests/FindPackageCpsTest/CMakeLists.txt | 55 +++++++++++++---- Tests/FindPackageCpsTest/cps/badversion1.cps | 8 +++ Tests/FindPackageCpsTest/cps/badversion2.cps | 8 +++ Tests/FindPackageCpsTest/cps/badversion3.cps | 8 +++ Tests/FindPackageCpsTest/cps/badversion4.cps | 8 +++ Tests/FindPackageCpsTest/cps/customversion.cps | 8 +++ Tests/FindPackageCpsTest/cps/emptymarker.cps | 8 +++ Tests/FindPackageCpsTest/cps/longversion.cps | 8 +++ 11 files changed, 209 insertions(+), 56 deletions(-) create mode 100644 Tests/FindPackageCpsTest/cps/badversion1.cps create mode 100644 Tests/FindPackageCpsTest/cps/badversion2.cps create mode 100644 Tests/FindPackageCpsTest/cps/badversion3.cps create mode 100644 Tests/FindPackageCpsTest/cps/badversion4.cps create mode 100644 Tests/FindPackageCpsTest/cps/customversion.cps create mode 100644 Tests/FindPackageCpsTest/cps/emptymarker.cps create mode 100644 Tests/FindPackageCpsTest/cps/longversion.cps diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index a47e9c3..993fa93 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -2758,23 +2758,32 @@ bool cmFindPackageCommand::CheckVersion(std::string const& config_file) if (result && hasVersion) { this->VersionFound = version; - std::vector const& versionParts = reader->ParseVersion(); - this->VersionFoundCount = static_cast(versionParts.size()); - switch (this->VersionFoundCount) { - case 4: - this->VersionFoundTweak = versionParts[3]; - CM_FALLTHROUGH; - case 3: - this->VersionFoundPatch = versionParts[2]; - CM_FALLTHROUGH; - case 2: - this->VersionFoundMinor = versionParts[1]; - CM_FALLTHROUGH; - case 1: - this->VersionFoundMajor = versionParts[0]; - CM_FALLTHROUGH; - default: - break; + cm::optional const& + parsedVersion = reader->ParseVersion(); + if (parsedVersion) { + std::vector const& versionParts = + parsedVersion->ReleaseComponents; + + this->VersionFoundCount = + static_cast(versionParts.size()); + switch (std::min(this->VersionFoundCount, 4u)) { + case 4: + this->VersionFoundTweak = versionParts[3]; + CM_FALLTHROUGH; + case 3: + this->VersionFoundPatch = versionParts[2]; + CM_FALLTHROUGH; + case 2: + this->VersionFoundMinor = versionParts[1]; + CM_FALLTHROUGH; + case 1: + this->VersionFoundMajor = versionParts[0]; + CM_FALLTHROUGH; + default: + break; + } + } else { + this->VersionFoundCount = 0; } } this->CpsReader = std::move(reader); diff --git a/Source/cmPackageInfoReader.cxx b/Source/cmPackageInfoReader.cxx index a5349ca..dd03943 100644 --- a/Source/cmPackageInfoReader.cxx +++ b/Source/cmPackageInfoReader.cxx @@ -366,6 +366,62 @@ void AddDefinitions(cmMakefile* makefile, cmTarget* target, } } +cm::optional ParseSimpleVersion( + std::string const& version) +{ + if (version.empty()) { + return cm::nullopt; + } + + cmPackageInfoReader::Pep440Version result; + result.Simple = true; + + cm::string_view remnant{ version }; + for (;;) { + // Find the next part separator. + std::string::size_type const n = remnant.find_first_of(".+-"_s); + if (n == 0) { + // The part is an empty string. + return cm::nullopt; + } + + // Extract the part as a number. + cm::string_view const part = remnant.substr(0, n); + std::string::size_type const l = part.size(); + std::string::size_type p; + unsigned long const value = std::stoul(std::string{ part }, &p); + if (p != l || value > std::numeric_limits::max()) { + // The part was not a valid number or is too big. + return cm::nullopt; + } + result.ReleaseComponents.push_back(static_cast(value)); + + // Have we consumed the entire input? + if (n == std::string::npos) { + return { std::move(result) }; + } + + // Lop off the current part. + char const sep = remnant[n]; + remnant = remnant.substr(n + 1); + if (sep == '+' || sep == '-') { + // If we hit the local label, we're done. + result.LocalLabel = remnant; + return { std::move(result) }; + } + + // We just consumed a '.'; check that there's more. + if (remnant.empty()) { + // A trailing part separator is not allowed. + return cm::nullopt; + } + + // Continue with the remaining input. + } + + // Unreachable. +} + } // namespace std::unique_ptr cmPackageInfoReader::Read( @@ -428,40 +484,22 @@ cm::optional cmPackageInfoReader::GetVersion() const return cm::nullopt; } -std::vector cmPackageInfoReader::ParseVersion() const +cm::optional +cmPackageInfoReader::ParseVersion() const { // Check that we have a version. cm::optional const& version = this->GetVersion(); if (!version) { - return {}; + return cm::nullopt; } - std::vector result; - cm::string_view remnant{ *version }; - // Check if we know how to parse the version. Json::Value const& schema = this->Data["version_schema"]; if (schema.isNull() || cmStrCaseEq(schema.asString(), "simple"_s)) { - // Keep going until we run out of parts. - while (!remnant.empty()) { - std::string::size_type n = remnant.find('.'); - cm::string_view part = remnant.substr(0, n); - if (n == std::string::npos) { - remnant = {}; - } else { - remnant = remnant.substr(n + 1); - } - - unsigned long const value = std::stoul(std::string{ part }, &n); - if (n == 0 || value > std::numeric_limits::max()) { - // The part was not a valid number or is too big. - return {}; - } - result.push_back(static_cast(value)); - } + return ParseSimpleVersion(*version); } - return result; + return cm::nullopt; } std::vector cmPackageInfoReader::GetRequirements() const diff --git a/Source/cmPackageInfoReader.h b/Source/cmPackageInfoReader.h index d31cee9..509a72e 100644 --- a/Source/cmPackageInfoReader.h +++ b/Source/cmPackageInfoReader.h @@ -44,10 +44,31 @@ public: std::string GetName() const; cm::optional GetVersion() const; + // NOTE: The eventual intent is for CPS to support multiple version schemas, + // and in particular, we expect to want to support "simple", "custom", "rpm", + // "dpkg" and "pep440". Additionally, we desire to be able to parse each of + // these to the maximum extent possible; in particular, we want to be able + // to decompose "simple" and "pep440" versions into components represented + // as numeric types rather than strings, which is not possible with the "rpm" + // and "dpkg" schemas. Therefore, we require different data structures to + // represent different version schemas. + + struct Pep440Version + { + // NOTE: This structure is currently incomplete as we only support the + // "simple" schema at this time. + bool Simple; // "simple" can be represented as a subset of "pep440" + std::vector ReleaseComponents; + cm::optional LocalLabel; + }; + + // FIXME: Return a sum type (e.g. {cm,std}::variant) of possible versions + // when we support more than just the "simple" (and possibly "pep440") + // schema(s). /// If the package uses the 'simple' version scheme, obtain the version as - /// a numeric tuple. Returns an empty vector for other schemes or if no - /// version is specified. - std::vector ParseVersion() const; + /// a numeric tuple and optional trailing string. Returns a disengaged + /// optional for other schemes or if no version is specified. + cm::optional ParseVersion() const; std::vector GetRequirements() const; std::vector GetComponentNames() const; diff --git a/Tests/FindPackageCpsTest/CMakeLists.txt b/Tests/FindPackageCpsTest/CMakeLists.txt index 58de268..dfb59e8 100644 --- a/Tests/FindPackageCpsTest/CMakeLists.txt +++ b/Tests/FindPackageCpsTest/CMakeLists.txt @@ -22,23 +22,52 @@ set(CMAKE_FIND_FRAMEWORK FIRST) add_executable(FindPackageCpsTest FindPackageTest.cxx) ############################################################################### + +function(expect PACKAGE VAR OP VALUE WHAT) + if(NOT ${PACKAGE}_${VAR} ${OP} ${VALUE}) + message(SEND_ERROR "${PACKAGE} wrong ${WHAT} ${${PACKAGE}_${VAR}} !") + endif() +endfunction() + +function(test_version PACKAGE LITERAL COUNT MAJOR MINOR PATCH TWEAK) + if(NOT ${PACKAGE}_FOUND) + message(SEND_ERROR "${PACKAGE} not found !") + else() + expect(${PACKAGE} VERSION STREQUAL "${LITERAL}" "version") + expect(${PACKAGE} VERSION_COUNT EQUAL ${COUNT} "version count") + expect(${PACKAGE} VERSION_MAJOR EQUAL ${MAJOR} "major version") + expect(${PACKAGE} VERSION_MINOR EQUAL ${MINOR} "minor version") + expect(${PACKAGE} VERSION_PATCH EQUAL ${PATCH} "patch version") + expect(${PACKAGE} VERSION_TWEAK EQUAL ${TWEAK} "tweak version") + endif() +endfunction() + +function(test_unparsed_version PACKAGE VERSION) + find_package(${PACKAGE} CONFIG) + test_version(${PACKAGE} "${VERSION}" 0 0 0 0 0) +endfunction() + +############################################################################### # Test a basic package search. # It should NOT find the package's CMake package file. find_package(Sample CONFIG) -if(NOT Sample_FOUND) - message(SEND_ERROR "Sample not found !") -elseif(NOT Sample_VERSION STREQUAL "2.10.11") - message(SEND_ERROR "Sample wrong version ${Sample_VERSION} !") -elseif(NOT Sample_VERSION_MAJOR EQUAL 2) - message(SEND_ERROR "Sample wrong major version ${Sample_VERSION_MAJOR} !") -elseif(NOT Sample_VERSION_MINOR EQUAL 10) - message(SEND_ERROR "Sample wrong minor version ${Sample_VERSION_MINOR} !") -elseif(NOT Sample_VERSION_PATCH EQUAL 11) - message(SEND_ERROR "Sample wrong patch version ${Sample_VERSION_PATCH} !") -elseif(NOT Sample_VERSION_TWEAK EQUAL 0) - message(SEND_ERROR "Sample wrong tweak version ${Sample_VERSION_TWEAK} !") -endif() +test_version(Sample "2.10.11" 3 2 10 11 0) + +############################################################################### +# Test some more complicated version parsing. + +find_package(LongVersion CONFIG) +test_version(LongVersion "1.1.2.3.5.8+fibonacci" 6 1 1 2 3) + +find_package(EmptyMarker CONFIG) +test_version(EmptyMarker "1.1+" 2 1 1 0 0) + +test_unparsed_version(BadVersion1 "1..1") +test_unparsed_version(BadVersion2 "1.1a.0") +test_unparsed_version(BadVersion3 "1.1.") +test_unparsed_version(BadVersion4 "+42") +test_unparsed_version(CustomVersion "VII") ############################################################################### # Test glob sorting. diff --git a/Tests/FindPackageCpsTest/cps/badversion1.cps b/Tests/FindPackageCpsTest/cps/badversion1.cps new file mode 100644 index 0000000..05acfa1 --- /dev/null +++ b/Tests/FindPackageCpsTest/cps/badversion1.cps @@ -0,0 +1,8 @@ +{ + "cps_version": "0.13", + "name": "BadVersion1", + "version": "1..1", + "version_schema": "simple", + "cps_path": "@prefix@/cps", + "components": {} +} diff --git a/Tests/FindPackageCpsTest/cps/badversion2.cps b/Tests/FindPackageCpsTest/cps/badversion2.cps new file mode 100644 index 0000000..99e683d --- /dev/null +++ b/Tests/FindPackageCpsTest/cps/badversion2.cps @@ -0,0 +1,8 @@ +{ + "cps_version": "0.13", + "name": "BadVersion2", + "version": "1.1a.0", + "version_schema": "simple", + "cps_path": "@prefix@/cps", + "components": {} +} diff --git a/Tests/FindPackageCpsTest/cps/badversion3.cps b/Tests/FindPackageCpsTest/cps/badversion3.cps new file mode 100644 index 0000000..97e670d --- /dev/null +++ b/Tests/FindPackageCpsTest/cps/badversion3.cps @@ -0,0 +1,8 @@ +{ + "cps_version": "0.13", + "name": "BadVersion3", + "version": "1.1.", + "version_schema": "simple", + "cps_path": "@prefix@/cps", + "components": {} +} diff --git a/Tests/FindPackageCpsTest/cps/badversion4.cps b/Tests/FindPackageCpsTest/cps/badversion4.cps new file mode 100644 index 0000000..2574866 --- /dev/null +++ b/Tests/FindPackageCpsTest/cps/badversion4.cps @@ -0,0 +1,8 @@ +{ + "cps_version": "0.13", + "name": "BadVersion4", + "version": "+42", + "version_schema": "simple", + "cps_path": "@prefix@/cps", + "components": {} +} diff --git a/Tests/FindPackageCpsTest/cps/customversion.cps b/Tests/FindPackageCpsTest/cps/customversion.cps new file mode 100644 index 0000000..30aa29d --- /dev/null +++ b/Tests/FindPackageCpsTest/cps/customversion.cps @@ -0,0 +1,8 @@ +{ + "cps_version": "0.13", + "name": "CustomVersion", + "version": "VII", + "version_schema": "roman", + "cps_path": "@prefix@/cps", + "components": {} +} diff --git a/Tests/FindPackageCpsTest/cps/emptymarker.cps b/Tests/FindPackageCpsTest/cps/emptymarker.cps new file mode 100644 index 0000000..2099c92 --- /dev/null +++ b/Tests/FindPackageCpsTest/cps/emptymarker.cps @@ -0,0 +1,8 @@ +{ + "cps_version": "0.13", + "name": "EmptyMarker", + "version": "1.1+", + "version_schema": "simple", + "cps_path": "@prefix@/cps", + "components": {} +} diff --git a/Tests/FindPackageCpsTest/cps/longversion.cps b/Tests/FindPackageCpsTest/cps/longversion.cps new file mode 100644 index 0000000..300952d --- /dev/null +++ b/Tests/FindPackageCpsTest/cps/longversion.cps @@ -0,0 +1,8 @@ +{ + "cps_version": "0.13", + "name": "LongVersion", + "version": "1.1.2.3.5.8+fibonacci", + "version_schema": "simple", + "cps_path": "@prefix@/cps", + "components": {} +} -- cgit v0.12 From 3e6466eb16fe1214e367228b8ac937c7ca4bd2de Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Wed, 12 Feb 2025 11:33:43 -0500 Subject: find_package: Honor version requests when finding CPS packages Teach find_package to check a CPS package's version (when provided) against a version request given to the find_package invocation. --- Help/command/find_package.rst | 119 ++++++++++++++++----- Source/cmFindPackageCommand.cxx | 57 ++++++++-- Source/cmPackageInfoReader.cxx | 13 ++- Source/cmPackageInfoReader.h | 10 +- Tests/FindPackageCpsTest/CMakeLists.txt | 13 ++- .../share/cps/SortLib/SortLib.cps | 7 ++ .../RunCMake/find_package-CPS/CompatVersion.cmake | 13 +++ .../RunCMake/find_package-CPS/CustomVersion.cmake | 13 +++ Tests/RunCMake/find_package-CPS/ExactVersion.cmake | 13 +++ .../find_package-CPS/MissingVersion1-result.txt | 1 + .../find_package-CPS/MissingVersion1-stderr.txt | 13 +++ .../find_package-CPS/MissingVersion1.cmake | 10 ++ .../find_package-CPS/MissingVersion2-result.txt | 1 + .../find_package-CPS/MissingVersion2-stderr.txt | 13 +++ .../find_package-CPS/MissingVersion2.cmake | 10 ++ .../find_package-CPS/MultipleVersions.cmake | 13 +++ Tests/RunCMake/find_package-CPS/RunCMakeTest.cmake | 16 +++ .../find_package-CPS/TransitiveVersion.cmake | 13 +++ .../RunCMake/find_package-CPS/VersionLimit1.cmake | 14 +++ .../RunCMake/find_package-CPS/VersionLimit2.cmake | 14 +++ .../find_package-CPS/VersionLimit3-result.txt | 1 + .../find_package-CPS/VersionLimit3-stderr.txt | 13 +++ .../RunCMake/find_package-CPS/VersionLimit3.cmake | 11 ++ .../find_package-CPS/VersionLimit4-result.txt | 1 + .../find_package-CPS/VersionLimit4-stderr.txt | 13 +++ .../RunCMake/find_package-CPS/VersionLimit4.cmake | 11 ++ .../cps/CustomVersion/37/CustomVersion.cps | 8 ++ .../cps/CustomVersion/42/CustomVersion.cps | 8 ++ .../cps/CustomVersion/55/CustomVersion.cps | 8 ++ .../find_package-CPS/cps/sample/1.1.0/sample.cps | 8 ++ .../find_package-CPS/cps/sample/1.2.3/sample.cps | 8 ++ .../find_package-CPS/cps/sample/1.4.2/sample.cps | 8 ++ .../find_package-CPS/cps/sample/1.5.0/sample.cps | 7 ++ .../find_package-CPS/cps/transitiveversion.cps | 11 ++ 34 files changed, 449 insertions(+), 43 deletions(-) create mode 100644 Tests/FindPackageCpsTest/share/cps/SortLib/SortLib.cps create mode 100644 Tests/RunCMake/find_package-CPS/CompatVersion.cmake create mode 100644 Tests/RunCMake/find_package-CPS/CustomVersion.cmake create mode 100644 Tests/RunCMake/find_package-CPS/ExactVersion.cmake create mode 100644 Tests/RunCMake/find_package-CPS/MissingVersion1-result.txt create mode 100644 Tests/RunCMake/find_package-CPS/MissingVersion1-stderr.txt create mode 100644 Tests/RunCMake/find_package-CPS/MissingVersion1.cmake create mode 100644 Tests/RunCMake/find_package-CPS/MissingVersion2-result.txt create mode 100644 Tests/RunCMake/find_package-CPS/MissingVersion2-stderr.txt create mode 100644 Tests/RunCMake/find_package-CPS/MissingVersion2.cmake create mode 100644 Tests/RunCMake/find_package-CPS/MultipleVersions.cmake create mode 100644 Tests/RunCMake/find_package-CPS/TransitiveVersion.cmake create mode 100644 Tests/RunCMake/find_package-CPS/VersionLimit1.cmake create mode 100644 Tests/RunCMake/find_package-CPS/VersionLimit2.cmake create mode 100644 Tests/RunCMake/find_package-CPS/VersionLimit3-result.txt create mode 100644 Tests/RunCMake/find_package-CPS/VersionLimit3-stderr.txt create mode 100644 Tests/RunCMake/find_package-CPS/VersionLimit3.cmake create mode 100644 Tests/RunCMake/find_package-CPS/VersionLimit4-result.txt create mode 100644 Tests/RunCMake/find_package-CPS/VersionLimit4-stderr.txt create mode 100644 Tests/RunCMake/find_package-CPS/VersionLimit4.cmake create mode 100644 Tests/RunCMake/find_package-CPS/cps/CustomVersion/37/CustomVersion.cps create mode 100644 Tests/RunCMake/find_package-CPS/cps/CustomVersion/42/CustomVersion.cps create mode 100644 Tests/RunCMake/find_package-CPS/cps/CustomVersion/55/CustomVersion.cps create mode 100644 Tests/RunCMake/find_package-CPS/cps/sample/1.1.0/sample.cps create mode 100644 Tests/RunCMake/find_package-CPS/cps/sample/1.2.3/sample.cps create mode 100644 Tests/RunCMake/find_package-CPS/cps/sample/1.4.2/sample.cps create mode 100644 Tests/RunCMake/find_package-CPS/cps/sample/1.5.0/sample.cps create mode 100644 Tests/RunCMake/find_package-CPS/cps/transitiveversion.cps diff --git a/Help/command/find_package.rst b/Help/command/find_package.rst index 1b6a555..cae7150 100644 --- a/Help/command/find_package.rst +++ b/Help/command/find_package.rst @@ -79,18 +79,14 @@ The command has a few modes by which it searches for packages: version files are used). .. note:: - If the experimental ``CMAKE_EXPERIMENTAL_FIND_CPS_PACKAGES`` is enabled, files named ``.cps`` and ``.cps`` are also considered. These files provide package information according to the |CPS|_ (CPS), which is more portable than CMake script. Aside from any explicitly noted exceptions, any references to "config files", "config mode", "package configuration files", and so forth refer equally to both - CPS and CMake-script files. However, some features of ``find_package`` - are not supported at this time when a CPS file is found. In particular, - if a ``VERSION`` requirement is specified, only ``.cps`` files which do not - provide version information will be rejected. (We expect to implement - proper version validation in the near future.) + CPS and CMake-script files. This functionality is a work in progress, and + some features may be missing. Search is implemented in a manner that will tend to prefer |CPS| files over CMake-script config files in most cases. Specifying ``CONFIGS`` @@ -211,15 +207,20 @@ specified: * A single version with the format ``major[.minor[.patch[.tweak]]]``, where each component is a numeric value. * A version range with the format ``versionMin...[<]versionMax`` where - ``versionMin`` and ``versionMax`` have the same format and constraints - on components being integers as the single version. By default, both end - points are included. By specifying ``<``, the upper end point will be - excluded. Version ranges are only supported with CMake 3.19 or later. - Note that it is not possible to extend the compatibility range specified - by the package's version file. For example, if the package version file - specifies compatibility within a minor version, it is not possible to - extend the compatibility to several minor versions by specifying a - version range. + ``versionMin`` and ``versionMax`` have the same format and constraints on + components being integers as the single version. By default, both end points + are included. By specifying ``<``, the upper end point will be excluded. + Version ranges are only supported with CMake 3.19 or later. + +.. note:: + With the exception of CPS packages, version support is currently provided + only on a package-by-package basis. When a version range is specified but + the package is only designed to expect a single version, the package will + ignore the upper end point of the range and only take the single version at + the lower end of the range into account. Non-CPS packages that do support + version ranges do so in a manner that is determined by the individual + package. See the `Version Selection`_ section below for details and + important caveats. The ``EXACT`` option requests that the version be matched exactly. This option is incompatible with the specification of a version range. @@ -227,11 +228,7 @@ is incompatible with the specification of a version range. If no ``[version]`` and/or component list is given to a recursive invocation inside a find-module, the corresponding arguments are forwarded automatically from the outer call (including the ``EXACT`` flag for -``[version]``). Version support is currently provided only on a -package-by-package basis (see the `Version Selection`_ section below). -When a version range is specified but the package is only designed to expect -a single version, the package will ignore the upper end point of the range and -only take the single version at the lower end of the range into account. +``[version]``). See the :command:`cmake_policy` command documentation for discussion of the ``NO_POLICY_SCOPE`` option. @@ -749,7 +746,7 @@ sets these variables: These variables are checked by the ``find_package`` command to determine whether the configuration file provides an acceptable version. They are not available after the ``find_package`` call returns. If the version -is acceptable the following variables are set: +is acceptable, the following variables are set: ``_VERSION`` Full provided version string @@ -766,12 +763,80 @@ is acceptable the following variables are set: and the corresponding package configuration file is loaded. +.. note:: + While the exact behavior of version matching is determined by the individual + package, many packages use :command:`write_basic_package_version_file` to + supply this logic. The version check scripts this produces have some notable + caveats with respect to version ranges: + + * The upper end of a version range acts as a hard limit on what versions will + be accepted. Thus, while a request for version ``1.4.0`` might be + satisfied by a package whose version is ``1.6.0`` and which advertises + 'same major version' compatibility, the same package will be rejected if + the requested version range is ``1.4.0...1.5.0``. + + * Both ends of the version range must match the package's advertised + compatibility level. For example, if a package advertises 'same major and + minor version' compatibility, requesting the version range + ``1.4.0...<1.5.5`` or ``1.4.0...1.5.0`` will result in that package being + rejected, even if the package version is ``1.4.1``. + + As a result, it is not possible to use a version range to extend the range + of compatible package versions that will be accepted. + |CPS| """"" -For |CPS| package configuration files, no version checking is performed at -this time. However, packages using the ``simple`` version schema will set -the following variables: +For |CPS| package configuration files, package version numbers are checked by +CMake according to the set of recognized version schemas. At present, the +following schemas are recognized: + + ``simple`` + Version numbers are a tuple of integers followed by an optional trailing + segment which is ignored with respect to version comparisons. + + ``custom`` + The mechanism for interpreting version numbers is unspecified. The version + strings must match exactly for the package to be accepted. + +Refer to |cps-version_schema|_ for a more detailed explanation of each schema +and how comparisons for each are performed. Note that the specification may +include schemas that are not supported by CMake. + +In addition to the package's ``version``, CPS allows packages to optionally +specify a |cps-compat_version|_, which is the oldest version for which the +package provides compatibility. That is, the package warrants that a consumer +expecting the ``compat_version`` should be able to use the package, even if the +package's actual version is newer. If not specified, the ``compat_version`` +is implicitly equal to the package version, i.e. no backwards compatibility is +provided. + +When a package uses a recognized schema, CMake will determine the package's +acceptability according to the following rules: + +* If ``EXACT`` was specified, or if the package does not supply a + ``compat_version``, the package's ``version`` must equal the requested + version. + +* Otherwise: + + * The package's ``version`` must be greater than or equal to the requested + (minimum) version, and + + * the package's ``compat_version`` must be less than or equal to the + requested (minimum) version, and + + * if a requested maximum version was given, it must be greater than (or equal + to, depending on whether the maximum version is specified as inclusive or + exclusive) the package's ``version``. + +.. note:: + This implementation of range matching was chosen in order to most closely + match the behavior of :command:`write_basic_package_version_file`, albeit + without the case where an overly broad range matches nothing. + +For packages using the ``simple`` version schema, if the version is acceptable, +the following variables are set: ``_VERSION`` Full provided version string @@ -878,3 +943,9 @@ requirements are not satisfied. .. _CPS: https://cps-org.github.io/cps/ .. |CPS| replace:: Common Package Specification + +.. _cps-compat_version: https://cps-org.github.io/cps/schema.html#compat-version +.. |cps-compat_version| replace:: ``compat_version`` + +.. _cps-version_schema: https://cps-org.github.io/cps/schema.html#version-schema +.. |cps-version_schema| replace:: ``version_schema`` diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 993fa93..48d33eb 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -59,6 +59,7 @@ namespace { using pdt = cmFindPackageCommand::PackageDescriptionType; +using ParsedVersion = cmPackageInfoReader::Pep440Version; template