From 82a6d2f0fcb6b0b2f153324fb4f14a52f6873a4f Mon Sep 17 00:00:00 2001
From: "FeRD (Frank Dana)" <ferdnyc@gmail.com>
Date: Wed, 21 Apr 2021 13:21:12 -0400
Subject: [PATCH 1/8] mingw-w64-cmake

---
 CMakeLists.txt                  | 32 +++++++++-----------------------
 cmake/OpenCVDetectPython.cmake  |  2 +-
 cmake/OpenCVFindOpenEXR.cmake   |  4 +++-
 cmake/OpenCVGenPkgconfig.cmake  |  2 +-
 cmake/OpenCVInstallLayout.cmake |  2 +-
 cmake/OpenCVModule.cmake        |  4 +++-
 6 files changed, 19 insertions(+), 28 deletions(-)

 diff --git a/CMakeLists.txt b/CMakeLists.txt
 index 5a0c62e..6c15139 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -197,14 +197,14 @@ OCV_OPTION(OPENCV_ENABLE_NONFREE "Enable non-free algorithms" OFF)
 
 # 3rd party libs
 OCV_OPTION(OPENCV_FORCE_3RDPARTY_BUILD   "Force using 3rdparty code from source" OFF)
-OCV_OPTION(BUILD_ZLIB               "Build zlib from source"             (WIN32 OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
-OCV_OPTION(BUILD_TIFF               "Build libtiff from source"          (WIN32 OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
-OCV_OPTION(BUILD_OPENJPEG           "Build OpenJPEG from source"         (WIN32 OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
-OCV_OPTION(BUILD_JASPER             "Build libjasper from source"        (WIN32 OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
-OCV_OPTION(BUILD_JPEG               "Build libjpeg from source"          (WIN32 OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
-OCV_OPTION(BUILD_PNG                "Build libpng from source"           (WIN32 OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
+OCV_OPTION(BUILD_ZLIB               "Build zlib from source"             (MSVC OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
+OCV_OPTION(BUILD_TIFF               "Build libtiff from source"          (MSVC OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
+OCV_OPTION(BUILD_OPENJPEG           "Build OpenJPEG from source"         (MSVC OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
+OCV_OPTION(BUILD_JASPER             "Build libjasper from source"        (MSVC OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
+OCV_OPTION(BUILD_JPEG               "Build libjpeg from source"          (MSVC OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
+OCV_OPTION(BUILD_PNG                "Build libpng from source"           (MSVC OR ANDROID OR APPLE OR OPENCV_FORCE_3RDPARTY_BUILD) )
 OCV_OPTION(BUILD_OPENEXR            "Build openexr from source"          (OPENCV_FORCE_3RDPARTY_BUILD) )
-OCV_OPTION(BUILD_WEBP               "Build WebP from source"             (((WIN32 OR ANDROID OR APPLE) AND NOT WINRT) OR OPENCV_FORCE_3RDPARTY_BUILD) )
+OCV_OPTION(BUILD_WEBP               "Build WebP from source"             (((MSVC OR ANDROID OR APPLE) AND NOT WINRT) OR OPENCV_FORCE_3RDPARTY_BUILD) )
 OCV_OPTION(BUILD_TBB                "Download and build TBB from source" (ANDROID OR OPENCV_FORCE_3RDPARTY_BUILD) )
 OCV_OPTION(BUILD_IPP_IW             "Build IPP IW from source"           (NOT MINGW OR OPENCV_FORCE_3RDPARTY_BUILD) IF (X86_64 OR X86) AND NOT WINRT )
 OCV_OPTION(BUILD_ITT                "Build Intel ITT from source"
@@ -1007,7 +1093,7 @@ if(NOT OPENCV_LICENSE_FILE)
 endif()
 
 # for UNIX it does not make sense as LICENSE and readme will be part of the package automatically
-if(ANDROID OR NOT UNIX)
+if((ANDROID OR NOT UNIX) AND NOT MINGW)
   install(FILES ${OPENCV_LICENSE_FILE}
         PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
         DESTINATION ./ COMPONENT libs)
index 4ff02a7..e3bd127 100644
--- a/cmake/OpenCVDetectPython.cmake
+++ b/cmake/OpenCVDetectPython.cmake
@@ -176,7 +176,7 @@ if(NOT ${found})
     endif()
 
     if(NOT ANDROID AND NOT IOS AND NOT XROS)
-      if(CMAKE_HOST_UNIX)
+      if(CMAKE_HOST_UNIX OR MINGW)
         execute_process(COMMAND ${_executable} -c "from sysconfig import *; print(get_path('purelib'))"
                         RESULT_VARIABLE _cvpy_process
                         OUTPUT_VARIABLE _std_packages_path
diff --git a/cmake/OpenCVFindOpenEXR.cmake b/cmake/OpenCVFindOpenEXR.cmake
index ef633e8..02faa39 100644
--- a/cmake/OpenCVFindOpenEXR.cmake
+++ b/cmake/OpenCVFindOpenEXR.cmake
@@ -29,7 +29,9 @@ SET(OPENEXR_LIBRARIES "")
 SET(OPENEXR_LIBSEARCH_SUFFIXES "")
 file(TO_CMAKE_PATH "$ENV{ProgramFiles}" ProgramFiles_ENV_PATH)
 
-if(WIN32)
+if(MINGW)
+    SET(OPENEXR_ROOT $ENV{OpenEXR_HOME} CACHE STRING "Path to the OpenEXR install folder")
+elseif(MSVC)
     SET(OPENEXR_ROOT "C:/Deploy" CACHE STRING "Path to the OpenEXR \"Deploy\" folder")
     if(X86_64)
         SET(OPENEXR_LIBSEARCH_SUFFIXES x64/Release x64 x64/Debug)
diff --git a/cmake/OpenCVInstallLayout.cmake b/cmake/OpenCVInstallLayout.cmake
index d5f3579..4722de0 100644
--- a/cmake/OpenCVInstallLayout.cmake
+++ b/cmake/OpenCVInstallLayout.cmake
@@ -20,7 +20,7 @@ if(ANDROID)
   ocv_update(OPENCV_JNI_INSTALL_PATH            "${OPENCV_LIB_INSTALL_PATH}")
   ocv_update(OPENCV_JNI_BIN_INSTALL_PATH        "${OPENCV_JNI_INSTALL_PATH}")
 
-elseif(WIN32 AND CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
+elseif(MSVC AND CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
 
   if(DEFINED OpenCV_RUNTIME AND DEFINED OpenCV_ARCH)
     ocv_update(OPENCV_INSTALL_BINARIES_PREFIX "${OpenCV_ARCH}/${OpenCV_RUNTIME}/")
-- 
2.31.1
