Bläddra i källkod

Merge topic 'fix-cps-cmake-deps' into release-4.0

c3d279841b find_package: CPS component requirements != CMake components

Acked-by: Kitware Robot <[email protected]>
Acked-by: buildbot <[email protected]>
Merge-request: !10427
Brad King 8 månader sedan
förälder
incheckning
2b0d0b1544

+ 35 - 9
Source/cmFindPackageCommand.cxx

@@ -1525,6 +1525,7 @@ bool cmFindPackageCommand::HandlePackageMode(
   bool result = true;
   bool found = false;
   bool configFileSetFOUNDFalse = false;
+  std::vector<std::string> missingTargets;
 
   if (fileFound) {
     if (this->Makefile->IsDefinitionSet(foundVar) &&
@@ -1559,6 +1560,17 @@ bool cmFindPackageCommand::HandlePackageMode(
         notFoundMessage =
           this->Makefile->GetSafeDefinition(notFoundMessageVar);
       }
+
+      // Check whether the required targets are defined.
+      if (found && !this->RequiredTargets.empty()) {
+        for (std::string const& t : this->RequiredTargets) {
+          std::string qualifiedTarget = cmStrCat(this->Name, "::"_s, t);
+          if (!this->Makefile->FindImportedTarget(qualifiedTarget)) {
+            missingTargets.emplace_back(std::move(qualifiedTarget));
+            found = false;
+          }
+        }
+      }
     } else {
       // The configuration file is invalid.
       result = false;
@@ -1593,10 +1605,18 @@ bool cmFindPackageCommand::HandlePackageMode(
         if (!notFoundMessage.empty()) {
           e << " Reason given by package: \n" << notFoundMessage << "\n";
         }
-      }
-      // If there are files in ConsideredConfigs, it means that FooConfig.cmake
-      // have been found, but they didn't have appropriate versions.
-      else if (!this->ConsideredConfigs.empty()) {
+      } else if (!missingTargets.empty()) {
+        e << "Found package configuration file:\n"
+             "  "
+          << this->FileFound
+          << "\n"
+             "but the following required targets were not found:\n"
+             "  "
+          << cmJoin(cmMakeRange(missingTargets), ", "_s);
+      } else if (!this->ConsideredConfigs.empty()) {
+        // If there are files in ConsideredConfigs, it means that
+        // FooConfig.cmake have been found, but they didn't have appropriate
+        // versions.
         auto duplicate_end = cmRemoveDuplicates(this->ConsideredConfigs);
         e << "Could not find a configuration file for package \"" << this->Name
           << "\" that "
@@ -1903,7 +1923,7 @@ bool cmFindPackageCommand::ReadPackage()
     return false;
   }
 
-  auto const hasComponentsRequested =
+  bool const hasComponentsRequested =
     !this->RequiredComponents.empty() || !this->OptionalComponents.empty();
 
   cmMakefile::CallRAII scope{ this->Makefile, this->FileFound, this->Status };
@@ -1994,8 +2014,9 @@ bool cmFindPackageCommand::FindPackageDependencies(
                    fp.VersionPatch, fp.VersionTweak);
 
     fp.Components = cmJoin(cmMakeRange(dep.Components), ";"_s);
-    fp.RequiredComponents =
+    fp.OptionalComponents =
       std::set<std::string>{ dep.Components.begin(), dep.Components.end() };
+    fp.RequiredTargets = fp.OptionalComponents;
 
     // TODO set hints
 
@@ -2793,10 +2814,14 @@ bool cmFindPackageCommand::CheckVersion(std::string const& config_file)
         }
 
         // Verify that all required components are available.
+        std::set<std::string> requiredComponents = this->RequiredComponents;
+        requiredComponents.insert(this->RequiredTargets.begin(),
+                                  this->RequiredTargets.end());
+
         std::vector<std::string> missingComponents;
-        std::set_difference(this->RequiredComponents.begin(),
-                            this->RequiredComponents.end(),
-                            allComponents.begin(), allComponents.end(),
+        std::set_difference(requiredComponents.begin(),
+                            requiredComponents.end(), allComponents.begin(),
+                            allComponents.end(),
                             std::back_inserter(missingComponents));
         if (!missingComponents.empty()) {
           result = false;
@@ -2833,6 +2858,7 @@ bool cmFindPackageCommand::CheckVersion(std::string const& config_file)
         }
         this->CpsReader = std::move(reader);
         this->CpsAppendices = std::move(appendices);
+        this->RequiredComponents = std::move(requiredComponents);
       }
     }
   } else {

+ 1 - 0
Source/cmFindPackageCommand.h

@@ -254,6 +254,7 @@ private:
   std::string Components;
   std::set<std::string> RequiredComponents;
   std::set<std::string> OptionalComponents;
+  std::set<std::string> RequiredTargets;
   std::string DebugBuffer;
 
   struct ConfigName

+ 9 - 0
Source/cmMakefile.cxx

@@ -1728,6 +1728,15 @@ std::string const& cmMakefile::GetCurrentBinaryDirectory() const
   return this->StateSnapshot.GetDirectory().GetCurrentBinary();
 }
 
+cmTarget* cmMakefile::FindImportedTarget(std::string const& name) const
+{
+  auto const i = this->ImportedTargets.find(name);
+  if (i != this->ImportedTargets.end()) {
+    return i->second;
+  }
+  return nullptr;
+}
+
 std::vector<cmTarget*> cmMakefile::GetImportedTargets() const
 {
   std::vector<cmTarget*> tgts;

+ 2 - 0
Source/cmMakefile.h

@@ -477,6 +477,8 @@ public:
   }
   std::vector<cmTarget*> GetImportedTargets() const;
 
+  cmTarget* FindImportedTarget(std::string const& name) const;
+
   cmTarget* FindLocalNonAliasTarget(std::string const& name) const;
 
   /** Find a target to use in place of the given name.  The target

+ 6 - 2
Tests/FindPackageCpsTest/cps/bar.cps

@@ -3,8 +3,12 @@
   "name": "Bar",
   "cps_path": "@prefix@/cps",
   "requires": {
-    "Dep1": null,
-    "Dep2": null
+    "Dep1": {
+      "components": [ "Target" ]
+    },
+    "Dep2": {
+      "components": [ "Target" ]
+    }
   },
   "components": {
     "Target1": {

+ 0 - 0
Tests/RunCMake/find_package-CPS/MissingTransitiveComponent-result.txt → Tests/RunCMake/find_package-CPS/MissingTransitiveComponentCMake-result.txt


+ 11 - 0
Tests/RunCMake/find_package-CPS/MissingTransitiveComponentCMake-stderr.txt

@@ -0,0 +1,11 @@
+CMake Error in cps/[Tt]ransitive[Mm]issing[Cc][Mm]ake\.cps:
+  Found package configuration file:
+(
+    [^
+]*/Tests/RunCMake/find_package-CPS/cmake/cmaketestpackage-config.cmake)+
+
+  but the following required targets were not found:[
+ ]+CMakeTestPackage::DoesNotExist
+Call Stack \(most recent call first\):
+  MissingTransitiveComponentCMake\.cmake:[0-9]+ \(find_package\)
+  CMakeLists\.txt:[0-9]+ \(include\)

+ 7 - 0
Tests/RunCMake/find_package-CPS/MissingTransitiveComponentCMake.cmake

@@ -0,0 +1,7 @@
+cmake_minimum_required(VERSION 4.0)
+
+include(Setup.cmake)
+
+###############################################################################
+# Test depending on components of another package which are unavailable.
+find_package(TransitiveMissingCMake REQUIRED)

+ 1 - 0
Tests/RunCMake/find_package-CPS/MissingTransitiveComponentCPS-result.txt

@@ -0,0 +1 @@
+1

+ 5 - 4
Tests/RunCMake/find_package-CPS/MissingTransitiveComponent-stderr.txt → Tests/RunCMake/find_package-CPS/MissingTransitiveComponentCPS-stderr.txt

@@ -1,4 +1,4 @@
-CMake Error in cps/[Tt]ransitive[Mm]issing\.cps:
+CMake Error in cps/[Tt]ransitive[Mm]issing[Cc][Pp][Ss]\.cps:
   Could not find a configuration file for package "ComponentTest" that is
   compatible with requested version ""\.
 
@@ -8,10 +8,11 @@ CMake Error in cps/[Tt]ransitive[Mm]issing\.cps:
 ]*/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\)
+  MissingTransitiveComponentCPS\.cmake:[0-9]+ \(find_package\)
   CMakeLists\.txt:[0-9]+ \(include\)
 +
-CMake Error at MissingTransitiveComponent\.cmake:[0-9]+ \(find_package\):
-  find_package could not find ComponentTest, required by TransitiveMissing\.
+CMake Error at MissingTransitiveComponentCPS\.cmake:[0-9]+ \(find_package\):
+  find_package could not find ComponentTest, required by
+  TransitiveMissingCPS\.
 Call Stack \(most recent call first\):
   CMakeLists\.txt:[0-9]+ \(include\)

+ 1 - 1
Tests/RunCMake/find_package-CPS/MissingTransitiveComponent.cmake → Tests/RunCMake/find_package-CPS/MissingTransitiveComponentCPS.cmake

@@ -4,4 +4,4 @@ include(Setup.cmake)
 
 ###############################################################################
 # Test depending on components of another package which are unavailable.
-find_package(TransitiveMissing REQUIRED)
+find_package(TransitiveMissingCPS REQUIRED)

+ 2 - 1
Tests/RunCMake/find_package-CPS/RunCMakeTest.cmake

@@ -27,5 +27,6 @@ run_cmake(VersionLimit4)
 run_cmake(MissingTransitiveDependency)
 run_cmake(MissingComponent)
 run_cmake(MissingComponentDependency)
-run_cmake(MissingTransitiveComponent)
+run_cmake(MissingTransitiveComponentCPS)
+run_cmake(MissingTransitiveComponentCMake)
 run_cmake(MissingTransitiveComponentDependency)

+ 1 - 0
Tests/RunCMake/find_package-CPS/cmake/cmaketestpackage-config.cmake

@@ -0,0 +1 @@
+# Test config file.

+ 11 - 0
Tests/RunCMake/find_package-CPS/cps/transitivemissingcmake.cps

@@ -0,0 +1,11 @@
+{
+  "cps_version": "0.13",
+  "name": "TransitiveMissingCMake",
+  "cps_path": "@prefix@/cps",
+  "requires": {
+    "CMakeTestPackage": {
+      "components": [ "DoesNotExist" ]
+    }
+  },
+  "components": {}
+}

+ 1 - 1
Tests/RunCMake/find_package-CPS/cps/transitivemissing.cps → Tests/RunCMake/find_package-CPS/cps/transitivemissingcps.cps

@@ -1,6 +1,6 @@
 {
   "cps_version": "0.13",
-  "name": "TransitiveMissing",
+  "name": "TransitiveMissingCPS",
   "cps_path": "@prefix@/cps",
   "requires": {
     "ComponentTest": {