From 49e893ccd5ce3ae00c8eb89de5bf73567a4729ee 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                              | 2 +-
 Tests/RunCMake/find_path/FrameworksWithSubdirs-stdout.txt | 1 +
 Tests/RunCMake/find_path/FrameworksWithSubdirs.cmake      | 3 +++
 Tests/RunCMake/find_path/RunCMakeTest.cmake               | 4 ++++
 4 files changed, 9 insertions(+), 1 deletion(-)
 create mode 100644 Tests/RunCMake/find_path/FrameworksWithSubdirs-stdout.txt
 create mode 100644 Tests/RunCMake/find_path/FrameworksWithSubdirs.cmake

diff --git a/Source/cmFindPathCommand.cxx b/Source/cmFindPathCommand.cxx
index d24be6a..d71fc1a 100644
--- a/Source/cmFindPathCommand.cxx
+++ b/Source/cmFindPathCommand.cxx
@@ -117,7 +117,7 @@ std::string cmFindPathCommand::FindHeaderInFramework(std::string const& file,
     if (this->IncludeFileInPath) {
       return fheader;
     }
-    fheader = cmSystemTools::GetFilenamePath(fheader);
+    fheader.resize(fheader.size() - file.size());
     return fheader;
   }
   return "";
diff --git a/Tests/RunCMake/find_path/FrameworksWithSubdirs-stdout.txt b/Tests/RunCMake/find_path/FrameworksWithSubdirs-stdout.txt
new file mode 100644
index 0000000..1f41e50
--- /dev/null
+++ b/Tests/RunCMake/find_path/FrameworksWithSubdirs-stdout.txt
@@ -0,0 +1 @@
+-- IOKIT_INCLUDE_DIR='/System/Library/Frameworks/Kernel.framework/Headers'
diff --git a/Tests/RunCMake/find_path/FrameworksWithSubdirs.cmake b/Tests/RunCMake/find_path/FrameworksWithSubdirs.cmake
new file mode 100644
index 0000000..5d5b7de
--- /dev/null
+++ b/Tests/RunCMake/find_path/FrameworksWithSubdirs.cmake
@@ -0,0 +1,3 @@
+set(CMAKE_FRAMEWORK_PATH "/System/Library/Frameworks")
+find_path(IOKIT_INCLUDE_DIR "IOKit/pci/IOPCIDevice.h")
+message(STATUS "IOKIT_INCLUDE_DIR='${IOKIT_INCLUDE_DIR}'")
diff --git a/Tests/RunCMake/find_path/RunCMakeTest.cmake b/Tests/RunCMake/find_path/RunCMakeTest.cmake
index 5ce96e0..e05b614 100644
--- a/Tests/RunCMake/find_path/RunCMakeTest.cmake
+++ b/Tests/RunCMake/find_path/RunCMakeTest.cmake
@@ -3,3 +3,7 @@ include(RunCMake)
 if(WIN32 OR CYGWIN)
   run_cmake(PrefixInPATH)
 endif()
+
+if(APPLE)
+  run_cmake(FrameworksWithSubdirs)
+endif()
\ No newline at end of file
-- 
2.8.3

