find_package: Support GLOBAL for CPS imports

Teach CPS import to respect `find_package(GLOBAL)`.

Fixes: #27370
This commit is contained in:
Matthew Woehlke
2025-12-12 17:31:26 -05:00
parent 485d51f474
commit df94baa828
5 changed files with 49 additions and 17 deletions

View File

@@ -2238,7 +2238,7 @@ bool cmFindPackageCommand::ImportPackageTargets(cmPackageState& packageState,
}
// Import base file.
if (!reader.ImportTargets(this->Makefile, this->Status)) {
if (!reader.ImportTargets(this->Makefile, this->Status, this->GlobalScope)) {
return false;
}

View File

@@ -783,10 +783,10 @@ void cmPackageInfoReader::SetTargetProperties(
cmTarget* cmPackageInfoReader::AddLibraryComponent(
cmMakefile* makefile, cmStateEnums::TargetType type, std::string const& name,
Json::Value const& data, std::string const& package) const
Json::Value const& data, std::string const& package, bool global) const
{
// Create the imported target.
cmTarget* const target = makefile->AddImportedTarget(name, type, false);
cmTarget* const target = makefile->AddImportedTarget(name, type, global);
target->SetOrigin(cmTarget::Origin::Cps);
// Set target properties.
@@ -800,7 +800,7 @@ cmTarget* cmPackageInfoReader::AddLibraryComponent(
}
bool cmPackageInfoReader::ImportTargets(cmMakefile* makefile,
cmExecutionStatus& status)
cmExecutionStatus& status, bool global)
{
std::string const& package = this->GetName();
@@ -822,23 +822,23 @@ bool cmPackageInfoReader::ImportTargets(cmMakefile* makefile,
}
}
auto createTarget = [&](cmStateEnums::TargetType typeEnum) {
return this->AddLibraryComponent(makefile, typeEnum, fullName, *ci,
package, global);
};
cmTarget* target = nullptr;
if (type == "symbolic"_s) {
target = this->AddLibraryComponent(
makefile, cmStateEnums::INTERFACE_LIBRARY, fullName, *ci, package);
target = createTarget(cmStateEnums::INTERFACE_LIBRARY);
target->SetSymbolic(true);
} else if (type == "dylib"_s) {
target = this->AddLibraryComponent(
makefile, cmStateEnums::SHARED_LIBRARY, fullName, *ci, package);
target = createTarget(cmStateEnums::SHARED_LIBRARY);
} else if (type == "module"_s) {
target = this->AddLibraryComponent(
makefile, cmStateEnums::MODULE_LIBRARY, fullName, *ci, package);
target = createTarget(cmStateEnums::MODULE_LIBRARY);
} else if (type == "archive"_s) {
target = this->AddLibraryComponent(
makefile, cmStateEnums::STATIC_LIBRARY, fullName, *ci, package);
target = createTarget(cmStateEnums::STATIC_LIBRARY);
} else if (type == "interface"_s) {
target = this->AddLibraryComponent(
makefile, cmStateEnums::INTERFACE_LIBRARY, fullName, *ci, package);
target = createTarget(cmStateEnums::INTERFACE_LIBRARY);
} else {
makefile->IssueMessage(MessageType::WARNING,
cmStrCat(R"(component ")"_s, fullName,
@@ -862,7 +862,7 @@ bool cmPackageInfoReader::ImportTargets(cmMakefile* makefile,
}
cmTarget* const target = makefile->AddImportedTarget(
package, cmStateEnums::INTERFACE_LIBRARY, false);
package, cmStateEnums::INTERFACE_LIBRARY, global);
for (std::string const& name : defaultComponents) {
std::string const& fullName = cmStrCat(package, "::"_s, name);
AppendProperty(makefile, target, "LINK_LIBRARIES"_s, {}, fullName);

View File

@@ -77,7 +77,8 @@ public:
std::vector<std::string> GetComponentNames() const;
/// Create targets for components specified in the CPS file.
bool ImportTargets(cmMakefile* makefile, cmExecutionStatus& status);
bool ImportTargets(cmMakefile* makefile, cmExecutionStatus& status,
bool global);
/// Add configuration-specific properties for targets.
bool ImportTargetConfigurations(cmMakefile* makefile,
@@ -90,7 +91,7 @@ private:
cmStateEnums::TargetType type,
std::string const& name,
Json::Value const& data,
std::string const& package) const;
std::string const& package, bool global) const;
void AddTargetConfiguration(cmTarget* target,
cm::string_view configuration) const;

View File

@@ -47,6 +47,13 @@ function(test_unparsed_version PACKAGE VERSION)
test_version(${PACKAGE} "${VERSION}" 0 0 0 0 0)
endfunction()
function(test_global TARGET VALUE)
get_target_property(global "${TARGET}" IMPORTED_GLOBAL)
if((VALUE AND NOT global) OR (NOT VALUE AND global))
message(SEND_ERROR "${TARGET} has wrong IMPORTED_GLOBAL '${global}' !")
endif()
endfunction()
###############################################################################
# Test a basic package search.
# It should NOT find the package's CMake package file.
@@ -172,6 +179,20 @@ else()
message(SEND_ERROR "Foo::RelativeTest has wrong includes '${rt_includes}' !")
endif()
set(rt_includes)
test_global(Foo::Empty FALSE)
endif()
###############################################################################
# Test importing into global scope.
find_package(Global CONFIG GLOBAL)
if(NOT Global_FOUND)
message(SEND_ERROR "Global not found !")
elseif(NOT TARGET Global::Target)
message(SEND_ERROR "Global::Target missing !")
else()
test_global(Foo::Empty FALSE)
endif()
###############################################################################

View File

@@ -0,0 +1,10 @@
{
"cps_version": "0.13",
"name": "Global",
"cps_path": "@prefix@/cps",
"components": {
"Target": {
"type": "interface"
}
}
}