diff --git a/build/gen.py b/build/gen.py
index d1c649cb..8139b6f5 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -40,7 +40,10 @@ class Platform(object):
     elif self._platform.startswith('msys'):
       self._platform = 'msys'
     elif self._platform.startswith('win'):
-      self._platform = 'msvc'
+      if 'MSC' in sys.version:
+        self._platform = 'msvc'
+      else:
+        self._platform = 'mingw'
     elif self._platform.startswith('aix'):
       self._platform = 'aix'
     elif self._platform.startswith('fuchsia'):
@@ -399,10 +402,18 @@ def WriteGNNinja(path, platform, host, options, args_list):
     cxx = os.environ.get('CXX', 'g++')
     ld = os.environ.get('LD', 'g++')
     ar = os.environ.get('AR', 'ar -X64')
-  elif platform.is_msys() or platform.is_mingw():
+  elif platform.is_msys():
     cxx = os.environ.get('CXX', 'g++')
     ld = os.environ.get('LD', 'g++')
     ar = os.environ.get('AR', 'ar')
+  elif platform.is_mingw():
+    if 'Clang' in sys.version:
+      cxx = os.environ.get('CXX', 'clang++')
+      ld = os.environ.get('LD', 'clang++')
+    else:
+      cxx = os.environ.get('CXX', 'g++')
+      ld = os.environ.get('LD', 'g++')
+    ar = os.environ.get('AR', 'ar')
   else:
     cxx = os.environ.get('CXX', 'clang++')
     ld = cxx
@@ -541,6 +552,7 @@ def WriteGNNinja(path, platform, host, options, args_list):
       ldflags.append('-pthread')
 
     if platform.is_mingw() or platform.is_msys():
+      cflags.remove('-pthread')
       cflags.extend(['-DUNICODE',
                      '-DNOMINMAX',
                      '-DWIN32_LEAN_AND_MEAN',
