Browse Source

Merge topic 'fix-cps-in-prefix-root' into release-4.0

da0d2a996d CPS: Fix importing from the prefix root

Acked-by: Kitware Robot <[email protected]>
Acked-by: buildbot <[email protected]>
Merge-request: !10375
Brad King 10 months ago
parent
commit
0dd8cdc831

+ 10 - 2
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());
   }

+ 8 - 0
Tests/FindPackageCpsTest/CMakeLists.txt

@@ -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.
 

+ 10 - 0
Tests/FindPackageCpsTest/RootTest/RootTest.cps

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