find_package: Respect CPS default_license

Modify CPS import to set the `SPDX_LICENSE` on imported targets using
the specified `default_license`, if present (and if no per-component
`license` is specified).
This commit is contained in:
Matthew Woehlke
2025-06-19 13:50:04 -04:00
parent ff0bbe0913
commit 1d4fb4afa9
9 changed files with 84 additions and 31 deletions

View File

@@ -479,6 +479,14 @@ std::unique_ptr<cmPackageInfoReader> cmPackageInfoReader::Read(
}
}
// Check for a default license.
Json::Value const& defaultLicense = reader->Data["default_license"];
if (!defaultLicense.isNull()) {
reader->DefaultLicense = defaultLicense.asString();
} else if (parent) {
reader->DefaultLicense = parent->DefaultLicense;
}
return reader;
}
@@ -582,12 +590,14 @@ void cmPackageInfoReader::SetImportProperty(cmTarget* target,
}
}
void cmPackageInfoReader::SetMetaProperty(cmTarget* target,
cm::string_view property,
Json::Value const& value) const
void cmPackageInfoReader::SetMetaProperty(
cmTarget* target, cm::string_view property, Json::Value const& value,
std::string const& defaultValue) const
{
if (!value.isNull()) {
target->SetProperty(property.data(), value.asString());
} else if (!defaultValue.empty()) {
target->SetProperty(property.data(), defaultValue);
}
}
@@ -671,7 +681,8 @@ void cmPackageInfoReader::SetTargetProperties(
// Add other information.
if (configuration.empty()) {
this->SetMetaProperty(target, "SPDX_LICENSE"_s, data["license"]);
this->SetMetaProperty(target, "SPDX_LICENSE"_s, data["license"],
this->DefaultLicense);
}
}

View File

@@ -98,7 +98,8 @@ private:
cm::string_view configuration,
Json::Value const& value) const;
void SetMetaProperty(cmTarget* target, cm::string_view property,
Json::Value const& value) const;
Json::Value const& value,
std::string const& defaultValue = {}) const;
std::string ResolvePath(std::string path) const;
@@ -108,4 +109,5 @@ private:
std::map<std::string, cmTarget*> ComponentTargets;
std::vector<std::string> DefaultConfigurations;
std::string DefaultLicense;
};

View File

@@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 4.0)
include(Setup.cmake)
set(CMAKE_FIND_PACKAGE_SORT_ORDER NAME)
set(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
find_package(LicenseTest REQUIRED)
function(expect_license COMPONENT EXPECTED)
set(target LicenseTest::${COMPONENT})
if(TARGET ${target})
get_target_property(license ${target} "SPDX_LICENSE")
if (NOT "${license}" STREQUAL "${EXPECTED}")
message(SEND_ERROR
"Target ${target} has wrong license '${license}'"
" (expected '${EXPECTED}') !")
endif()
else()
message(SEND_ERROR "Expected target ${target} was not found !")
endif()
endfunction()
expect_license(SpecifiedOnTarget "Apache-2.0")
expect_license(InheritFromRoot "BSD-3-Clause")
expect_license(InheritFromAppendix "Apache-2.0")
expect_license(DisableInheritance "license-NOTFOUND")

View File

@@ -18,7 +18,7 @@ run_cmake(TransitiveVersion)
run_cmake(CustomVersion)
# Metadata Tests
run_cmake(SupplementalAttributes)
run_cmake(License)
# Version-matching failure tests
run_cmake(MissingVersion1)

View File

@@ -1,13 +0,0 @@
cmake_minimum_required(VERSION 4.0)
include(Setup.cmake)
set(CMAKE_FIND_PACKAGE_SORT_ORDER NAME)
set(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
find_package(SupplementalAttributesTest REQUIRED COMPONENTS Sample)
get_target_property(license SupplementalAttributesTest::Sample "SPDX_LICENSE")
if (NOT "${license}" STREQUAL "BSD-3-Clause")
message(SEND_ERROR "SupplementalAttributesTest wrong license ${license} !")
endif()

View File

@@ -0,0 +1,11 @@
{
"cps_version": "0.13",
"name": "LicenseTest",
"default_license": "",
"cps_path": "@prefix@/cps",
"components": {
"DisableInheritance": {
"type": "interface"
}
}
}

View File

@@ -0,0 +1,11 @@
{
"cps_version": "0.13",
"name": "LicenseTest",
"default_license": "Apache-2.0",
"cps_path": "@prefix@/cps",
"components": {
"InheritFromAppendix": {
"type": "interface"
}
}
}

View File

@@ -0,0 +1,16 @@
{
"cps_version": "0.13",
"name": "LicenseTest",
"version": "1.0",
"default_license": "BSD-3-Clause",
"cps_path": "@prefix@/cps",
"components": {
"SpecifiedOnTarget": {
"type": "interface",
"license": "Apache-2.0"
},
"InheritFromRoot": {
"type": "interface"
}
}
}

View File

@@ -1,12 +0,0 @@
{
"cps_version": "0.13",
"name": "SupplementalAttributesTest",
"version": "1.0",
"cps_path": "@prefix@/cps",
"components": {
"Sample": {
"type": "interface",
"license": "BSD-3-Clause"
}
}
}