[MinGW] Fix mingw building issues

Fix config, missing header and command flags issues
Add MinGW tutorial about how to build `gn` and "examples/simple_build"

Change-Id: I3761dde6af96a2b61dfa19f339fac97b55ceb186
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/17182
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Reviewed-by: David Turner <digit@google.com>
Commit-Queue: David Turner <digit@google.com>
diff --git a/README.md b/README.md
index 4bb1a9e..f96d895 100644
--- a/README.md
+++ b/README.md
@@ -107,6 +107,10 @@
 expected to be found in `PATH`. This can be overridden by setting the `CC`, `CXX`,
 and `AR` environment variables.
 
+On MSYS and MinGW, the default compiler is `g++`, a recent version is
+expected to be found in `PATH`. This can be overridden by setting the `CC`, `CXX`,
+`LD` and `AR` environment variables.
+
 On z/OS, building GN requires [ZOSLIB](https://github.com/ibmruntimes/zoslib) to be
 installed, as described at that URL. When building with `build/gen.py`, use the option
 `--zoslib-dir` to specify the path to [ZOSLIB](https://github.com/ibmruntimes/zoslib):
diff --git a/build/gen.py b/build/gen.py
index 9168035..6f3652e 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -245,7 +245,7 @@
   describe_output = subprocess.check_output(
       ['git', 'describe', 'HEAD', '--abbrev=12', '--match', ROOT_TAG],
       shell=host.is_windows(), cwd=REPO_ROOT)
-  mo = re.match(ROOT_TAG + '-(\d+)-g([0-9a-f]+)', describe_output.decode())
+  mo = re.match(ROOT_TAG + r'-(\d+)-g([0-9a-f]+)', describe_output.decode())
   if not mo:
     raise ValueError(
         'Unexpected output from git describe when generating version header')
@@ -317,7 +317,12 @@
 
   if platform.is_windows():
     executable_ext = '.exe'
-    library_ext = '.lib'
+
+    if platform.is_msvc():
+      library_ext = '.lib'
+    else:
+      library_ext = '.a'
+
     object_ext = '.obj'
   else:
     executable_ext = ''
@@ -505,21 +510,16 @@
       if not options.no_static_libstdcpp:
         ldflags.append('-static-libstdc++')
 
-      if platform.is_mingw() or platform.is_msys():
-        cflags.remove('-std=c++20')
-        cflags.extend([
-          '-Wno-deprecated-copy',
-          '-Wno-implicit-fallthrough',
-          '-Wno-redundant-move',
-          '-Wno-unused-variable',
-          '-Wno-format',             # Use of %llx, which is supported by _UCRT, false positive
-          '-Wno-strict-aliasing',    # Dereferencing punned pointer
-          '-Wno-cast-function-type', # Casting FARPROC to RegDeleteKeyExPtr
-          '-std=gnu++20',
-        ])
-      else:
-        # This is needed by libc++.
-        libs.append('-ldl')
+      cflags.extend([
+        '-Wno-deprecated-copy',
+        '-Wno-implicit-fallthrough',
+        '-Wno-redundant-move',
+        '-Wno-unused-variable',
+        '-Wno-format',             # Use of %llx, which is supported by _UCRT, false positive
+        '-Wno-strict-aliasing',    # Dereferencing punned pointer
+        '-Wno-cast-function-type', # Casting FARPROC to RegDeleteKeyExPtr
+      ])
+
     elif platform.is_darwin():
       min_mac_version_flag = '-mmacosx-version-min=10.9'
       cflags.append(min_mac_version_flag)
diff --git a/docs/mingw.md b/docs/mingw.md
new file mode 100644
index 0000000..a80005b
--- /dev/null
+++ b/docs/mingw.md
@@ -0,0 +1,34 @@
+# Building and using `gn` with MinGW on Windows
+
+At the time of writing: 2024-04-13
+Test environment: Windows 11, intel x86_64 CPU
+
+## Requirements
+
+1.  Install [MSYS2](https://www.msys2.org/)
+1.  Start a Terminal windows for MSYS2's MinGW development by opening one of `clang32.exe`, `clang64.exe`, `clangarm64.exe`,`mingw32.exe`, `mingw64.exe`, `ucrt64.exe`.
+1.  To download clang toolchain, Ninja and git programs, run command: `pacman -S mingw-w64-clang-x86_64-toolchain mingw-w64-clang-x86_64-ninja git`
+
+## Build `gn`
+
+1.  To clone `gn` source code, run command: `git clone https://gn.googlesource.com/gn`
+1.  Run command: `cd gn`
+1.  Run command: `build/gen.py --platform mingw`  
+    This configuration generates a static executable that does not depend on MSYS2, and can be run in any development environment.  
+    Use `--help` flag to check more configuration options.  
+    Use `--allow-warning` flag to build with warnings.
+1.  Run command: `ninja -C out`
+
+## Testing
+
+1.  Run command: `out/gn --version`
+1.  Run command: `out/gn_unittests`
+
+> Notes:
+>
+> For "mingw-w64-clang-x86_64-toolchain" in the clang64 environment, g++ is a copy of clang++.
+>
+> The toolchain that builds `gn` does not use vendor lock-in compiler command flags,
+> so `gn` can be built with these clang++, g++.
+>
+> However the build rules in [examples/simple_build](../examples/simple_build/) require GCC-specific compiler macros, and thus only work in the `ucrt64` MSYS2 development environment.