From da0d2a996d83956aa1342d91364d435a9d0ae9d1 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Thu, 20 Feb 2025 14:49:53 -0500 Subject: 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. --- Source/cmPackageInfoReader.cxx | 12 ++++++++++-- Tests/FindPackageCpsTest/CMakeLists.txt | 8 ++++++++ Tests/FindPackageCpsTest/RootTest/RootTest.cps | 10 ++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 Tests/FindPackageCpsTest/RootTest/RootTest.cps diff --git a/Source/cmPackageInfoReader.cxx b/Source/cmPackageInfoReader.cxx index 40ed54e..503764f 100644 --- a/Source/cmPackageInfoReader.cxx +++ b/Source/cmPackageInfoReader.cxx @@ -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()); } diff --git a/Tests/FindPackageCpsTest/CMakeLists.txt b/Tests/FindPackageCpsTest/CMakeLists.txt index 73b76da..561d401 100644 --- a/Tests/FindPackageCpsTest/CMakeLists.txt +++ b/Tests/FindPackageCpsTest/CMakeLists.txt @@ -70,6 +70,14 @@ 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. set(SortLib_DIR "" CACHE FILEPATH "Wipe out find results for testing." FORCE) diff --git a/Tests/FindPackageCpsTest/RootTest/RootTest.cps b/Tests/FindPackageCpsTest/RootTest/RootTest.cps new file mode 100644 index 0000000..5176954 --- /dev/null +++ b/Tests/FindPackageCpsTest/RootTest/RootTest.cps @@ -0,0 +1,10 @@ +{ + "cps_version": "0.13", + "name": "RootTest", + "cps_path": "@prefix@", + "components": { + "Sample": { + "type": "interface" + } + } +} -- cgit v0.12