CPS: Fix importing from the prefix root

Tweak resolution of CPS prefix path to support the case of the .cps file
being in the prefix root.
This commit is contained in:
Matthew Woehlke 2025-02-20 14:49:53 -05:00
parent 4b5b172a65
commit da0d2a996d
3 changed files with 28 additions and 2 deletions

View File

@ -146,16 +146,24 @@ std::string DeterminePrefix(std::string const& filepath,
}
// Get and validate prefix-relative path.
std::string const& absPath = cmSystemTools::GetFilenamePath(filepath);
std::string relPath = data["cps_path"].asString();
cmSystemTools::ConvertToUnixSlashes(relPath);
if (relPath.empty() || !cmHasLiteralPrefix(relPath, "@prefix@/")) {
if (relPath.empty() || !cmHasLiteralPrefix(relPath, "@prefix@")) {
// The relative prefix is not valid.
return {};
}
if (relPath.size() == 8) {
// The relative path is exactly "@prefix@".
return absPath;
}
if (relPath[8] != '/') {
// The relative prefix is not valid.
return {};
}
relPath = relPath.substr(8);
// Get directory portion of the absolute path.
std::string const& absPath = cmSystemTools::GetFilenamePath(filepath);
if (ComparePathSuffix(absPath, relPath)) {
return absPath.substr(0, absPath.size() - relPath.size());
}

View File

@ -69,6 +69,14 @@ test_unparsed_version(BadVersion3 "1.1.")
test_unparsed_version(BadVersion4 "+42")
test_unparsed_version(CustomVersion "VII")
###############################################################################
# Test finding a package whose CPS file is in the package prefix root.
set(RootTest_DIR "${CMAKE_CURRENT_SOURCE_DIR}/RootTest")
find_package(RootTest)
if(NOT RootTest_FOUND)
message(SEND_ERROR "RootTest not found !")
endif()
###############################################################################
# Test glob sorting.

View File

@ -0,0 +1,10 @@
{
"cps_version": "0.13",
"name": "RootTest",
"cps_path": "@prefix@",
"components": {
"Sample": {
"type": "interface"
}
}
}