Pārlūkot izejas kodu

Merge topic 'find_package-fix-nested-calls' into release-4.0

37823b366f find_package: Restore component requirements in nested calls

Acked-by: Kitware Robot <[email protected]>
Merge-request: !10576
Brad King 10 mēneši atpakaļ
vecāks
revīzija
7167236ba3

+ 9 - 3
Source/cmFindPackageCommand.cxx

@@ -911,10 +911,16 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
         this->VersionExact = this->Makefile->IsOn(exact);
       }
       if (this->Components.empty()) {
-        std::string const components_var = this->Name + "_FIND_COMPONENTS";
-        this->Components = this->Makefile->GetSafeDefinition(components_var);
+        std::string const componentsVar = this->Name + "_FIND_COMPONENTS";
+        this->Components = this->Makefile->GetSafeDefinition(componentsVar);
         for (auto const& component : cmList{ this->Components }) {
-          this->RequiredComponents.insert(component);
+          std::string const crVar =
+            cmStrCat(this->Name, "_FIND_REQUIRED_"_s, component);
+          if (this->Makefile->GetDefinition(crVar).IsOn()) {
+            this->RequiredComponents.insert(component);
+          } else {
+            this->OptionalComponents.insert(component);
+          }
         }
       }
     }

+ 3 - 0
Tests/RunCMake/find_package/ComponentRecursion-stdout.txt

@@ -0,0 +1,3 @@
+-- ComponentTest_FIND_REQUIRED_A '1'
+-- ComponentTest_FIND_REQUIRED_B '0'
+-- ComponentTest_FIND_REQUIRED_C '0'

+ 2 - 0
Tests/RunCMake/find_package/ComponentRecursion.cmake

@@ -0,0 +1,2 @@
+set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
+find_package(ComponentTest MODULE REQUIRED COMPONENTS A OPTIONAL_COMPONENTS B C)

+ 5 - 0
Tests/RunCMake/find_package/ComponentTestConfig.cmake

@@ -0,0 +1,5 @@
+foreach(c IN LISTS ComponentTest_FIND_COMPONENTS)
+  message(STATUS
+    "ComponentTest_FIND_REQUIRED_${c} "
+    "'${ComponentTest_FIND_REQUIRED_${c}}'")
+endforeach()

+ 2 - 0
Tests/RunCMake/find_package/FindComponentTest.cmake

@@ -0,0 +1,2 @@
+find_package(ComponentTest CONFIG
+   NO_DEFAULT_PATH PATHS ${CMAKE_CURRENT_LIST_DIR})

+ 1 - 0
Tests/RunCMake/find_package/RunCMakeTest.cmake

@@ -2,6 +2,7 @@ include(RunCMake)
 
 run_cmake(CMP0074-WARN)
 run_cmake(CMP0074-OLD)
+run_cmake(ComponentRecursion)
 run_cmake(ComponentRequiredAndOptional)
 run_cmake(EmptyRoots)
 run_cmake(FromPATHEnv)