From f3411b19be44e17001bd7949e974707a1c6bb4fc Mon Sep 17 00:00:00 2001
From: Harry Mallon <harry@codexdigital.com>
Date: Wed, 25 May 2016 18:18:47 +0100
Subject: [PATCH] Fix a problem where using find_path to find a header in
 subfolders in a framework could give the immediate path not the higher up
 path

* On OSX Make a CMakeLists.txt as follows to reproduce

cmake_minimum_required(VERSION 3.0)
find_path(IOKIT_INCLUDE_DIR "IOKit/pci/IOPCIDevice.h")
message("Path returned: ${IOKIT_INCLUDE_DIR}")
---
 Source/cmFindPathCommand.cxx | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/Source/cmFindPathCommand.cxx b/Source/cmFindPathCommand.cxx
index d24be6a..86478a8 100644
--- a/Source/cmFindPathCommand.cxx
+++ b/Source/cmFindPathCommand.cxx
@@ -117,7 +117,28 @@ std::string cmFindPathCommand::FindHeaderInFramework(std::string const& file,
     if (this->IncludeFileInPath) {
       return fheader;
     }
-    fheader = cmSystemTools::GetFilenamePath(fheader);
+
+    std::vector<std::string> fullPathComps;
+    cmSystemTools::SplitPath(fheader, fullPathComps);
+
+    std::vector<std::string> relPathComps;
+    cmSystemTools::SplitPath(file, relPathComps);
+
+    std::vector<std::string>::const_reverse_iterator relPart = relPathComps.rbegin();
+    std::vector<std::string>::const_reverse_iterator fullPart = fullPathComps.rbegin();
+    while (*fullPart++ == *relPart++)
+    {
+      fullPathComps.pop_back();
+
+      if (fullPart == fullPathComps.rend() ||
+          relPart  == relPathComps.rend())
+      {
+        return "";
+      }
+    }
+
+    fheader=cmSystemTools::JoinPath(fullPathComps);
+
     return fheader;
   }
   return "";
-- 
2.8.3

