From 50341d4003f20b53c6fdf091e4de11a2226a4d2a Mon Sep 17 00:00:00 2001
From: David Gobbi <david.gobbi@gmail.com>
Date: Mon, 14 Sep 2015 22:48:49 -0600
Subject: [PATCH] FindPythonLibs: Match include dir to library version

The first change in this commit is to ensure that FindPythonLibs has
found the library before it searches for the include dir.  In this way,
it can search for an include dir that matches the library version.
Also, now find_path() is called twice: once with NO_DEFAULT_PATH
(to search for includes in a carefully constructed set of locations),
and once without NO_DEFAULT_PATH (to search elsewhere).

The second change is a fix to the way the framework include dir is
found on OS X.  Previously, the base paths in FRAMEWORK_INCLUDE_DIRS
when combined with the PATH_SUFFIXES resulted in an invalid path.
Note that the code doesn't yet try to match the suffixes "m" and "u"
between the executable, library, and include directory.
---
 Modules/FindPythonLibs.cmake | 51 +++++++++++++++++++++++++++-----------------
 1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/Modules/FindPythonLibs.cmake b/Modules/FindPythonLibs.cmake
index 127662d..31ad1d8 100644
--- a/Modules/FindPythonLibs.cmake
+++ b/Modules/FindPythonLibs.cmake
@@ -150,26 +150,39 @@ foreach(_CURRENT_VERSION ${_Python_VERSIONS})
     PATH_SUFFIXES python${_CURRENT_VERSION}/config
   )
 
-  set(PYTHON_FRAMEWORK_INCLUDES)
-  if(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_DIR)
-    foreach(dir ${Python_FRAMEWORKS})
-      list(APPEND PYTHON_FRAMEWORK_INCLUDES
-           ${dir}/Versions/${_CURRENT_VERSION}/include/python${_CURRENT_VERSION})
-    endforeach()
-  endif()
+  # Only look for include directory if library was found, this ensures
+  # that version will be matched between include dir and library
+  if(PYTHON_LIBRARY)
+    set(PYTHON_FRAMEWORK_INCLUDES)
+    if(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_DIR)
+      set(PYTHON_INCLUDE_DIR)
+      foreach(dir ${Python_FRAMEWORKS})
+        set(PYTHON_FRAMEWORK_INCLUDES ${PYTHON_FRAMEWORK_INCLUDES}
+          ${dir}/Versions/${_CURRENT_VERSION}/include)
+      endforeach()
+    endif()
 
-  find_path(PYTHON_INCLUDE_DIR
-    NAMES Python.h
-    PATHS
-      ${PYTHON_FRAMEWORK_INCLUDES}
-      [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
-      [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
-    PATH_SUFFIXES
-      python${_CURRENT_VERSION}mu
-      python${_CURRENT_VERSION}m
-      python${_CURRENT_VERSION}u
-      python${_CURRENT_VERSION}
-  )
+    find_path(PYTHON_INCLUDE_DIR
+      NAMES Python.h
+      PATHS
+        ${PYTHON_FRAMEWORK_INCLUDES}
+        [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
+        [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
+      PATH_SUFFIXES
+        python${_CURRENT_VERSION}mu
+        python${_CURRENT_VERSION}m
+        python${_CURRENT_VERSION}u
+        python${_CURRENT_VERSION}
+      NO_DEFAULT_PATH
+    )
+
+    # Broaden to default paths as a second step (mainly for UNIX)
+    find_path(PYTHON_INCLUDE_DIR
+      NAMES Python.h
+      PATH_SUFFIXES
+        python${_CURRENT_VERSION}
+    )
+  endif()
 
   # For backward compatibility, set PYTHON_INCLUDE_PATH.
   set(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}")
-- 
1.9.5 (Apple Git-50.3)

