From ee8d292d557200a9578ccde051fd8b3ad6530dd2 Mon Sep 17 00:00:00 2001
From: Greg Jung <gvjung@gmail.com>
Date: Thu, 6 Aug 2015 23:07:05 -0700
Subject: [PATCH] FindPythonLibs avoids registry if MINGWE or MSYS; using
 NAMES_PER_DIR instead of NO_SYSTEM_ENVIRONMENT_PATH

  Instead of throwing all of the possible locations into a single
find_library(), for WIN32+MINGW builds this may latch into a windows' python.
This is not an issue when CMake is run from /MINGW.
CMake-3.3.0 (and above) can use PATH to discover targets of find_library, allowing
a user with several MINGW installations to use a single cmake (and cmake-gui) instead of
installing several copies for each mingw to be built with.  Unless the find_path or
find_library uses "NO_SYSTEM_ENVIRONMENT_PATH" which kills the new feature.
  Even though /mingw32/bin is frontmost in the path, windows/system32/python27.dll
was latched onto because the name matches earlier in the NAMES list than python2.7.
to avoid this all of the names are tested as the direcftories are presented, this via the
NAMES_PER_DIR qualifier.

   This makes is possible for me to run builds using Cmake-gui without installing cmake-gui
in /mingw32/bin (and qt5 along with it!).
---
 Modules/FindPythonLibs.cmake | 45 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 42 insertions(+), 3 deletions(-)

diff --git a/Modules/FindPythonLibs.cmake b/Modules/FindPythonLibs.cmake
index b80d3ce..f4e7ab2 100644
--- a/Modules/FindPythonLibs.cmake
+++ b/Modules/FindPythonLibs.cmake
@@ -109,7 +109,7 @@ unset(_PYTHON3_VERSIONS)
 
 foreach(_CURRENT_VERSION ${_Python_VERSIONS})
   string(REPLACE "." "" _CURRENT_VERSION_NO_DOTS ${_CURRENT_VERSION})
-  if(WIN32)
+  if(WIN32 AND NOT (MINGW OR MSYS))
     find_library(PYTHON_DEBUG_LIBRARY
       NAMES python${_CURRENT_VERSION_NO_DOTS}_d python
       PATHS
@@ -119,9 +119,32 @@ foreach(_CURRENT_VERSION ${_Python_VERSIONS})
       [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
       )
   endif()
+#
+# Go to the registry only if we are not MINGW, which should have its own Python installation.
+# also Unix-style goes here
+if((NOT WIN32) OR (MINGW OR MSYS))
+  find_library(PYTHON_LIBRARY
+    NAMES
+    python${_CURRENT_VERSION_NO_DOTS}
+    python${_CURRENT_VERSION}mu
+    python${_CURRENT_VERSION}m
+    python${_CURRENT_VERSION}u
+    python${_CURRENT_VERSION}
+# 
+    NAMES_PER_DIR
+    # Avoid finding the .dll in the PATH.  We want the .lib.
+# The NAMES_PER_DIR above should allow the library to be found where it was desired
+# in the prioritized location of PATH - cmake V3.3 behavior; 
+#    NO_SYSTEM_ENVIRONMENT_PATH works counter to cmake-3.3 improvement where CMake will look into path.
+# 
+  )
+endif()
+# if we still haven't found it, look in the registry also.
 
   set(PYTHON_FRAMEWORK_LIBRARIES)
-  if(Python_FRAMEWORKS AND NOT PYTHON_LIBRARY)
+# Accomodating Mac and all of the other lost souls ...
+if(NOT PYTHON_LIBRARY)
+  if(Python_FRAMEWORKS)
     foreach(dir ${Python_FRAMEWORKS})
       list(APPEND PYTHON_FRAMEWORK_LIBRARIES
            ${dir}/Versions/${_CURRENT_VERSION}/lib)
@@ -141,11 +164,13 @@ foreach(_CURRENT_VERSION ${_Python_VERSIONS})
     # Avoid finding the .dll in the PATH.  We want the .lib.
     NO_SYSTEM_ENVIRONMENT_PATH
   )
+endif()
   # Look for the static library in the Python config directory
   find_library(PYTHON_LIBRARY
     NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
+    NAMES_PER_DIR
     # Avoid finding the .dll in the PATH.  We want the .lib.
-    NO_SYSTEM_ENVIRONMENT_PATH
+#    NO_SYSTEM_ENVIRONMENT_PATH works counter to cmake-3.3
     # This is where the static library is usually located
     PATH_SUFFIXES python${_CURRENT_VERSION}/config
   )
@@ -165,6 +190,18 @@ foreach(_CURRENT_VERSION ${_Python_VERSIONS})
     endforeach()
   endif()
 
+if((NOT WIN32) OR (MINGW OR MSYS))
+  find_path(PYTHON_INCLUDE_DIR
+    NAMES Python.h
+    PATH_SUFFIXES
+      python${_CURRENT_VERSION}mu
+      python${_CURRENT_VERSION}m
+      python${_CURRENT_VERSION}u
+      python${_CURRENT_VERSION}
+  )
+endif()
+
+if(WIN32 AND NOT PYTHON_INCLUDE_DIR)
   find_path(PYTHON_INCLUDE_DIR
     NAMES Python.h
     PATHS
@@ -178,6 +215,8 @@ foreach(_CURRENT_VERSION ${_Python_VERSIONS})
       python${_CURRENT_VERSION}
   )
 
+endif()
+
   # For backward compatibility, set PYTHON_INCLUDE_PATH.
   set(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}")
 
-- 
2.4.6

