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
 @@ -189,20 +189,6 @@ if(UNIX AND NOT ANDROID)
    endif()
  endif()
  
 -# Add these standard paths to the search paths for FIND_PATH
 -# to find include files from these locations first
 -if(MINGW)
 -  if(EXISTS /mingw)
 -      list(APPEND CMAKE_INCLUDE_PATH /mingw)
 -  endif()
 -  if(EXISTS /mingw32)
 -      list(APPEND CMAKE_INCLUDE_PATH /mingw32)
 -  endif()
 -  if(EXISTS /mingw64)
 -      list(APPEND CMAKE_INCLUDE_PATH /mingw64)
 -  endif()
 -endif()
 -
  # ----------------------------------------------------------------------------
  # OpenCV cmake options
  # ----------------------------------------------------------------------------
 @@ -211,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_OPENEXR            "Build openexr from source"          (((WIN32 OR ANDROID OR APPLE) AND NOT WINRT) OR 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_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"          (((WIN32 OR ANDROID OR APPLE) AND NOT WINRT AND NOT MINGW) OR OPENCV_FORCE_3RDPARTY_BUILD) )
 +OCV_OPTION(BUILD_WEBP               "Build WebP from source"             (((WIN32 OR ANDROID OR APPLE) AND NOT WINRT AND NOT MINGW) 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"
 @@ -981,7 +967,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)
-      if(CMAKE_HOST_UNIX)
+      if(CMAKE_HOST_UNIX OR MINGW)
         execute_process(COMMAND ${_executable} -c "from distutils.sysconfig import *; print(get_python_lib())"
                         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
@@ -13,7 +13,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/OpenCVGenPkgconfig.cmake b/cmake/OpenCVGenPkgconfig.cmake
index 43d6a87..8d36b74 100644
--- a/cmake/OpenCVGenPkgconfig.cmake
+++ b/cmake/OpenCVGenPkgconfig.cmake
@@ -103,7 +103,7 @@ add_custom_target(gen-pkgconfig ALL SOURCES "${CMAKE_BINARY_DIR}/unix-install/${
 add_dependencies(developer_scripts gen-pkgconfig)
 
 
-if(UNIX AND NOT ANDROID)
+if((UNIX AND NOT ANDROID) OR MINGW)
   install(FILES ${CMAKE_BINARY_DIR}/unix-install/${OPENCV_PC_FILE_NAME} DESTINATION ${OPENCV_LIB_INSTALL_PATH}/pkgconfig COMPONENT dev)
 endif()
 
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}/")
diff --git a/cmake/OpenCVModule.cmake b/cmake/OpenCVModule.cmake
index 224953a..33b5d79 100644
--- a/cmake/OpenCVModule.cmake
+++ b/cmake/OpenCVModule.cmake
@@ -963,7 +963,9 @@ macro(_ocv_create_module)
   endif()
 
   set_target_properties(${the_module} PROPERTIES
-    OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
+    OUTPUT_NAME "${the_module}"
+    RUNTIME_OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
+    ARCHIVE_OUTPUT_NAME "${the_module}"
     DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
     COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
     COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
-- 
2.31.1
