mirror of
https://github.com/Kitware/CMake.git
synced 2026-02-08 16:19:36 -06:00
find_package: Support GLOBAL for CPS imports
Teach CPS import to respect `find_package(GLOBAL)`. Fixes: #27370
This commit is contained in:
@@ -2238,7 +2238,7 @@ bool cmFindPackageCommand::ImportPackageTargets(cmPackageState& packageState,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Import base file.
|
// Import base file.
|
||||||
if (!reader.ImportTargets(this->Makefile, this->Status)) {
|
if (!reader.ImportTargets(this->Makefile, this->Status, this->GlobalScope)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -783,10 +783,10 @@ void cmPackageInfoReader::SetTargetProperties(
|
|||||||
|
|
||||||
cmTarget* cmPackageInfoReader::AddLibraryComponent(
|
cmTarget* cmPackageInfoReader::AddLibraryComponent(
|
||||||
cmMakefile* makefile, cmStateEnums::TargetType type, std::string const& name,
|
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.
|
// 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);
|
target->SetOrigin(cmTarget::Origin::Cps);
|
||||||
|
|
||||||
// Set target properties.
|
// Set target properties.
|
||||||
@@ -800,7 +800,7 @@ cmTarget* cmPackageInfoReader::AddLibraryComponent(
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool cmPackageInfoReader::ImportTargets(cmMakefile* makefile,
|
bool cmPackageInfoReader::ImportTargets(cmMakefile* makefile,
|
||||||
cmExecutionStatus& status)
|
cmExecutionStatus& status, bool global)
|
||||||
{
|
{
|
||||||
std::string const& package = this->GetName();
|
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;
|
cmTarget* target = nullptr;
|
||||||
if (type == "symbolic"_s) {
|
if (type == "symbolic"_s) {
|
||||||
target = this->AddLibraryComponent(
|
target = createTarget(cmStateEnums::INTERFACE_LIBRARY);
|
||||||
makefile, cmStateEnums::INTERFACE_LIBRARY, fullName, *ci, package);
|
|
||||||
target->SetSymbolic(true);
|
target->SetSymbolic(true);
|
||||||
} else if (type == "dylib"_s) {
|
} else if (type == "dylib"_s) {
|
||||||
target = this->AddLibraryComponent(
|
target = createTarget(cmStateEnums::SHARED_LIBRARY);
|
||||||
makefile, cmStateEnums::SHARED_LIBRARY, fullName, *ci, package);
|
|
||||||
} else if (type == "module"_s) {
|
} else if (type == "module"_s) {
|
||||||
target = this->AddLibraryComponent(
|
target = createTarget(cmStateEnums::MODULE_LIBRARY);
|
||||||
makefile, cmStateEnums::MODULE_LIBRARY, fullName, *ci, package);
|
|
||||||
} else if (type == "archive"_s) {
|
} else if (type == "archive"_s) {
|
||||||
target = this->AddLibraryComponent(
|
target = createTarget(cmStateEnums::STATIC_LIBRARY);
|
||||||
makefile, cmStateEnums::STATIC_LIBRARY, fullName, *ci, package);
|
|
||||||
} else if (type == "interface"_s) {
|
} else if (type == "interface"_s) {
|
||||||
target = this->AddLibraryComponent(
|
target = createTarget(cmStateEnums::INTERFACE_LIBRARY);
|
||||||
makefile, cmStateEnums::INTERFACE_LIBRARY, fullName, *ci, package);
|
|
||||||
} else {
|
} else {
|
||||||
makefile->IssueMessage(MessageType::WARNING,
|
makefile->IssueMessage(MessageType::WARNING,
|
||||||
cmStrCat(R"(component ")"_s, fullName,
|
cmStrCat(R"(component ")"_s, fullName,
|
||||||
@@ -862,7 +862,7 @@ bool cmPackageInfoReader::ImportTargets(cmMakefile* makefile,
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmTarget* const target = makefile->AddImportedTarget(
|
cmTarget* const target = makefile->AddImportedTarget(
|
||||||
package, cmStateEnums::INTERFACE_LIBRARY, false);
|
package, cmStateEnums::INTERFACE_LIBRARY, global);
|
||||||
for (std::string const& name : defaultComponents) {
|
for (std::string const& name : defaultComponents) {
|
||||||
std::string const& fullName = cmStrCat(package, "::"_s, name);
|
std::string const& fullName = cmStrCat(package, "::"_s, name);
|
||||||
AppendProperty(makefile, target, "LINK_LIBRARIES"_s, {}, fullName);
|
AppendProperty(makefile, target, "LINK_LIBRARIES"_s, {}, fullName);
|
||||||
|
|||||||
@@ -77,7 +77,8 @@ public:
|
|||||||
std::vector<std::string> GetComponentNames() const;
|
std::vector<std::string> GetComponentNames() const;
|
||||||
|
|
||||||
/// Create targets for components specified in the CPS file.
|
/// 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.
|
/// Add configuration-specific properties for targets.
|
||||||
bool ImportTargetConfigurations(cmMakefile* makefile,
|
bool ImportTargetConfigurations(cmMakefile* makefile,
|
||||||
@@ -90,7 +91,7 @@ private:
|
|||||||
cmStateEnums::TargetType type,
|
cmStateEnums::TargetType type,
|
||||||
std::string const& name,
|
std::string const& name,
|
||||||
Json::Value const& data,
|
Json::Value const& data,
|
||||||
std::string const& package) const;
|
std::string const& package, bool global) const;
|
||||||
|
|
||||||
void AddTargetConfiguration(cmTarget* target,
|
void AddTargetConfiguration(cmTarget* target,
|
||||||
cm::string_view configuration) const;
|
cm::string_view configuration) const;
|
||||||
|
|||||||
@@ -47,6 +47,13 @@ function(test_unparsed_version PACKAGE VERSION)
|
|||||||
test_version(${PACKAGE} "${VERSION}" 0 0 0 0 0)
|
test_version(${PACKAGE} "${VERSION}" 0 0 0 0 0)
|
||||||
endfunction()
|
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.
|
# Test a basic package search.
|
||||||
# It should NOT find the package's CMake package file.
|
# 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}' !")
|
message(SEND_ERROR "Foo::RelativeTest has wrong includes '${rt_includes}' !")
|
||||||
endif()
|
endif()
|
||||||
set(rt_includes)
|
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()
|
endif()
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|||||||
10
Tests/FindPackageCpsTest/cps/global.cps
Normal file
10
Tests/FindPackageCpsTest/cps/global.cps
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"cps_version": "0.13",
|
||||||
|
"name": "Global",
|
||||||
|
"cps_path": "@prefix@/cps",
|
||||||
|
"components": {
|
||||||
|
"Target": {
|
||||||
|
"type": "interface"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user