--- a/dlib/external/pybind11/tools/FindPythonLibsNew.cmake
+++ b/dlib/external/pybind11/tools/FindPythonLibsNew.cmake
@@ -138,7 +138,7 @@
 string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR})
 string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES ${PYTHON_SITE_PACKAGES})
 
-if(CMAKE_HOST_WIN32)
+if(CMAKE_HOST_WIN32 AND NOT (MSYS OR MINGW))
     set(PYTHON_LIBRARY
         "${PYTHON_PREFIX}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib")
 
--- a/tools/python/CMakeLists.txt
+++ b/tools/python/CMakeLists.txt
@@ -1,7 +1,7 @@
 
 CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)
 
-if (WIN32 AND NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio") 
+if (MSVC AND NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio") 
    message(FATAL_ERROR "\n"
       "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
       "You must use Visual Studio to build a python extension on windows. If you "
--- a/setup.py
+++ b/setup.py
@@ -34,6 +34,7 @@
 import sys
 import shutil
 import platform
+import sysconfig
 import subprocess
 import multiprocessing
 from distutils import log
@@ -145,7 +146,7 @@
         cfg = 'Debug' if self.debug else 'Release'
         build_args = ['--config', cfg]
 
-        if platform.system() == "Windows":
+        if platform.system() == "Windows" and sysconfig.get_platform() != 'mingw':
             cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
             if sys.maxsize > 2**32:
                 cmake_args += ['-A', 'x64']
@@ -188,8 +189,8 @@
         # We are limited either by RAM or CPU cores.  So pick the limiting amount
         # and return that.
         return max(min(num_cores, mem_cores), 1)
-    except ValueError:
+    except:
-        return 2 # just assume 2 if we can't get the os to tell us the right answer.
+        return 3 # just assume 3 if we can't get the os to tell us the right answer.
 
 
 from setuptools.command.test import test as TestCommand
