find_package: Respect CPS license

Modify CPS import to set the `SPDX_LICENSE` on imported targets using
the specified package `license`, if present (and if none of the other
mechanisms for setting a license apply).
This commit is contained in:
Matthew Woehlke
2025-10-23 14:30:30 -04:00
parent b27be83f4a
commit 94b6e36b6b
3 changed files with 38 additions and 9 deletions

View File

@@ -488,6 +488,15 @@ std::unique_ptr<cmPackageInfoReader> cmPackageInfoReader::Read(
reader->DefaultLicense = defaultLicense.asString();
} else if (parent) {
reader->DefaultLicense = parent->DefaultLicense;
} else {
// If there is no 'default_license', check for 'license'. Note that we
// intentionally allow `default_license` on an appendix to override the
// parent, but we do not consider `license` on an appendix. This is
// consistent with not allowing LICENSE and APPENDIX to be used together.
Json::Value const& packageLicense = reader->Data["license"];
if (!packageLicense.isNull()) {
reader->DefaultLicense = packageLicense.asString();
}
}
return reader;

View File

@@ -8,20 +8,24 @@ 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(TARGET ${COMPONENT})
get_target_property(license ${COMPONENT} "SPDX_LICENSE")
if (NOT "${license}" STREQUAL "${EXPECTED}")
message(SEND_ERROR
"Target ${target} has wrong license '${license}'"
"Target ${COMPONENT} has wrong license '${license}'"
" (expected '${EXPECTED}') !")
endif()
else()
message(SEND_ERROR "Expected target ${target} was not found !")
message(SEND_ERROR "Expected target ${COMPONENT} 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")
expect_license(LicenseTest::SpecifiedOnTarget "Apache-2.0")
expect_license(LicenseTest::InheritFromRoot "BSD-3-Clause")
expect_license(LicenseTest::InheritFromAppendix "Apache-2.0")
expect_license(LicenseTest::DisableInheritance "license-NOTFOUND")
find_package(PackageLicenseTest REQUIRED)
expect_license(PackageLicenseTest::SpecifiedOnTarget "Apache-2.0")
expect_license(PackageLicenseTest::InheritFromRoot "BSD-3-Clause")

View File

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