Browse Source

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.
Matthew Woehlke 10 months ago
parent
commit
35a7ed125b

+ 6 - 3
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<std::string> 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<unsigned> const& versionParts = reader->ParseVersion();
           this->VersionFoundCount = static_cast<unsigned>(versionParts.size());

+ 1 - 1
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\)

+ 1 - 1
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\)

+ 1 - 0
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": {}
 }