1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 19:08:07 +08:00

find_package: Restore component requirements in nested calls

Fix logic to populate required and optional components from CMake
variables when `find_package` is called in a nested context.

This was broken in commit e2a6416622 (find_package: Refactor in support
of recursion, 2024-11-29, v4.0.0-rc1~356^2), which promoted the
component sets from locals (in cmFindPackageCommand::InitialPass) to
member variables. Previously, in a nested context, these sets were
simply not filled, and we relied on the variables indicating component
requirement to already be set. When logic was added to properly fill the
sets (which is needed for CPS), it blindly dumped all components into
the required set, without actually checking whether the context had
marked the components as required or optional.

Fixes: #26824
This commit is contained in:
Matthew Woehlke
2025-04-01 16:16:52 -04:00
committed by Brad King
parent faa225b63b
commit 37823b366f
6 changed files with 22 additions and 3 deletions

View File

@@ -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);
}
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -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)