--- a/examples/tptp/tptp5.cpp
+++ b/examples/tptp/tptp5.cpp
@@ -1058,7 +1058,7 @@
     }
 
     bool mk_env_filename(const char* rel_name, std::string& inc_name) {
-#ifdef _WINDOWS
+#if defined(_MSC_VER)
         char buffer[1024];
         size_t sz;
         errno_t err = getenv_s( 
--- a/scripts/mk_util.py
+++ b/scripts/mk_util.py
@@ -77,6 +77,7 @@
 IS_CYGWIN=False
 IS_CYGWIN_MINGW=False
 IS_MSYS2=False
+IS_MSYS2_MINGW=False
 VERBOSE=True
 DEBUG_MODE=False
 SHOW_CPPS = True
@@ -171,6 +172,9 @@
 def is_msys2():
     return IS_MSYS2
 
+def is_msys2_mingw():
+    return IS_MSYS2_MINGW
+
 def norm_path(p):
     return os.path.expanduser(os.path.normpath(p))
 
@@ -627,10 +631,13 @@
             IS_CYGWIN_MINGW=True
     elif os.uname()[0].startswith('MSYS_NT') or os.uname()[0].startswith('MINGW'):
         IS_MSYS2=True
-        if os.uname()[4] == 'x86_64':
-            LINUX_X64=True
+        if (CC != None and "mingw" in CC):
+            IS_MSYS2_MINGW=True
         else:
-            LINUX_X64=False
+            if os.uname()[4] == 'x86_64':
+                LINUX_X64=True
+            else:
+                LINUX_X64=False
 
 
 if os.name == 'posix' and os.uname()[4] == 'arm64':
@@ -2031,6 +2038,9 @@
                 # Some ocamlmklib's don't like -g; observed on cygwin, but may be others as well.
                 OCAMLMKLIB += ' -g'
 
+            if is_msys2() and not(is_msys2_mingw()):
+                LIBZ3 = 'libz3.dll'
+
             z3mls = os.path.join(self.sub_dir, 'z3ml')
 
             LIBZ3ML = ''
@@ -2102,7 +2112,7 @@
             out.write(' %s' % ((os.path.join(self.sub_dir, 'z3ml.cmxa'))))
             out.write(' %s' % ((os.path.join(self.sub_dir, 'z3ml.cmxs'))))
             out.write(' %s' % ((os.path.join(self.sub_dir, 'dllz3ml'))))
-            if is_windows() or is_cygwin_mingw() or is_msys2():
+            if is_windows() or is_cygwin_mingw() or is_msys2() or is_msys2_mingw():
                 out.write('.dll')
             else:
                 out.write('.so') # .so also on OSX!
@@ -2621,7 +2631,7 @@
             SO_EXT         = '.dll'
             SLIBFLAGS      = '-shared'
             EXE_EXT        = '.exe'
-            LIB_EXT        = '.lib'
+            LIB_EXT        = '.a'
         else:
             raise MKException('Unsupported platform: %s' % sysname)
         if is64():
@@ -2633,7 +2643,7 @@
             SLIBFLAGS    = '%s -m32' % SLIBFLAGS
         if TRACE or DEBUG_MODE:
             CPPFLAGS     = '%s -D_TRACE' % CPPFLAGS
-        if is_cygwin_mingw() or is_msys2():
+        if is_cygwin_mingw() or is_msys2() or is_msys2_mingw():
             # when cross-compiling with MinGW, we need to statically link its standard libraries
             # and to make it create an import library.
             SLIBEXTRAFLAGS = '%s -static-libgcc -static-libstdc++ -Wl,--out-implib,libz3.dll.a' % SLIBEXTRAFLAGS
@@ -2679,6 +2689,8 @@
             print('C Compiler  :   %s' % CC)
             if is_cygwin_mingw():
                 print('MinGW32 cross:  %s' % (is_cygwin_mingw()))
+            if is_msys2_mingw():
+                print('MinGW32 cross:  %s' % (is_msys2_mingw()))
             print('Archive Tool:   %s' % AR)
             print('Arithmetic:     %s' % ARITH)
             print('Prefix:         %s' % PREFIX)
--- a/scripts/update_api.py
+++ b/scripts/update_api.py
@@ -1751,7 +1751,7 @@
 
 _file_manager = contextlib.ExitStack()
 atexit.register(_file_manager.close)
-_ext = 'dll' if sys.platform in ('win32', 'cygwin') else 'dylib' if sys.platform == 'darwin' else 'so'
+_ext = 'dll' if sys.platform in ('win32', 'cygwin', 'msys') else 'dylib' if sys.platform == 'darwin' else 'so'
 _lib = None
 _z3_lib_resource = importlib_resources.files('z3').joinpath('lib')
 _z3_lib_resource_path = _file_manager.enter_context(
--- a/src/api/python/setup.py
+++ b/src/api/python/setup.py
@@ -53,7 +53,7 @@
 if BUILD_PLATFORM in ('darwin', 'osx'):
     LIBRARY_FILE = "libz3.dylib"
     EXECUTABLE_FILE = "z3"
-elif BUILD_PLATFORM in ('win32', 'cygwin', 'win'):
+elif BUILD_PLATFORM in ('win32', 'cygwin', 'win', 'msys'):
     LIBRARY_FILE = "libz3.dll"
     EXECUTABLE_FILE = "z3.exe"
 else:
--- a/src/muz/fp/datalog_parser.cpp
+++ b/src/muz/fp/datalog_parser.cpp
@@ -99,7 +99,7 @@
          m_data_size(0) {
         m_data.resize(2*s_expansion_step);
         resize_data(0);
-#if _WINDOWS
+#if defined(_MSC_VER)
         errno_t err = fopen_s(&m_file, fname, "rb");
         m_ok = (m_file != nullptr) && (err == 0);
 #else
--- a/src/shell/main.cpp
+++ b/src/shell/main.cpp
@@ -409,7 +409,7 @@
         }
         disable_timeout();
         memory::finalize();
-#ifdef _WINDOWS
+#if defined(_MSC_VER)
         _CrtDumpMemoryLeaks();
 #endif
         return return_value;
--- a/src/test/for_each_file.cpp
+++ b/src/test/for_each_file.cpp
@@ -17,7 +17,7 @@
 Revision History:
 
 --*/
-#ifdef _WINDOWS
+#if defined(_MSC_VER)
 #include <string>
 #include <windows.h>
 #include <strsafe.h>
--- a/src/test/quant_elim.cpp
+++ b/src/test/quant_elim.cpp
@@ -497,7 +497,7 @@
    
 
     memory::finalize();
-#ifdef _WINDOWS
+#if defined(_MSC_VER)
     _CrtDumpMemoryLeaks();
 #endif
     exit(0);
--- a/src/test/quant_solve.cpp
+++ b/src/test/quant_solve.cpp
@@ -256,7 +256,7 @@
 
 #if 0
     memory::finalize();
-#ifdef _WINDOWS
+#if defined(_MSC_VER)
     _CrtDumpMemoryLeaks();
 #endif
     exit(0);
--- a/src/util/mpn.cpp
+++ b/src/util/mpn.cpp
@@ -357,7 +357,7 @@
     TRACE("mpn_to_string", tout << "[mpn] to_string "; display_raw(tout, a, lng); tout << " == "; );
 
     if (lng == 1) {
-#ifdef _WINDOWS
+#if defined(_MSC_VER)
         sprintf_s(buf, lbuf, "%u", *a);
 #else
         snprintf(buf, lbuf, "%u", *a);
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -45,7 +45,7 @@
 #define UINT64_MAX 0xffffffffffffffffull
 #endif
 
-#ifdef _WINDOWS
+#if defined(_MSC_VER)
 #define SPRINTF_D(_buffer_, _i_) sprintf_s(_buffer_, Z3_ARRAYSIZE(_buffer_), "%d", _i_)
 #define SPRINTF_U(_buffer_, _u_) sprintf_s(_buffer_, Z3_ARRAYSIZE(_buffer_), "%u", _u_)
 #define _Exit exit
