Merge topic 'find_package-cps-multiple-inclusion' into release-4.0

b498611902 find_package: Add minimal support for CPS multiple inclusion

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !10581
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx
index 88cbc40..68a0499 100644
--- a/Source/cmFindPackageCommand.cxx
+++ b/Source/cmFindPackageCommand.cxx
@@ -1522,6 +1522,7 @@
     }
   }
 
+  std::string const fileVar = cmStrCat(this->Name, "_CONFIG");
   std::string const foundVar = cmStrCat(this->Name, "_FOUND");
   std::string const notFoundMessageVar =
     cmStrCat(this->Name, "_NOT_FOUND_MESSAGE");
@@ -1552,7 +1553,15 @@
     if (this->CpsReader) {
       // The package has been found.
       found = true;
-      result = this->ReadPackage();
+
+      // Don't read a CPS file if we've already read it.
+      cmValue const& previousFileFound =
+        this->Makefile->GetDefinition(fileVar);
+      if (previousFileFound.Compare(this->FileFound) == 0) {
+        result = true;
+      } else {
+        result = this->ReadPackage();
+      }
     } else if (this->ReadListFile(this->FileFound, DoPolicyScope)) {
       // The package has been found.
       found = true;
@@ -1725,7 +1734,6 @@
   this->Makefile->AddDefinition(foundVar, found ? "1" : "0");
 
   // Set a variable naming the configuration file that was found.
-  std::string const fileVar = cmStrCat(this->Name, "_CONFIG");
   if (found) {
     this->Makefile->AddDefinition(fileVar, this->FileFound);
   } else {
diff --git a/Tests/FindPackageCpsTest/CMakeLists.txt b/Tests/FindPackageCpsTest/CMakeLists.txt
index 88015e6..d905104 100644
--- a/Tests/FindPackageCpsTest/CMakeLists.txt
+++ b/Tests/FindPackageCpsTest/CMakeLists.txt
@@ -55,6 +55,12 @@
 test_version(Sample "2.10.11" 3 2 10 11 0)
 
 ###############################################################################
+# Test finding a package more than once.
+
+find_package(Repeat REQUIRED)
+find_package(Repeat REQUIRED)
+
+###############################################################################
 # Test some more complicated version parsing.
 
 find_package(LongVersion CONFIG)
diff --git a/Tests/FindPackageCpsTest/cps/Repeat.cps b/Tests/FindPackageCpsTest/cps/Repeat.cps
new file mode 100644
index 0000000..55354fd
--- /dev/null
+++ b/Tests/FindPackageCpsTest/cps/Repeat.cps
@@ -0,0 +1,10 @@
+{
+  "cps_version": "0.13",
+  "name": "Repeat",
+  "cps_path": "@prefix@/cps",
+  "components": {
+    "Repeat": {
+      "type": "interface"
+    }
+  }
+}