winpthreads: new configure option `--with-winnt`

This option allows users who build the library specify the minimal
Windows version they want winpthreads to be able to run on.

This option has two effects:

1. Define `WINPTHREADS_WINNT` macro during compilation.
2. Pass linker option in order to set subsystem version in the created DLL.

In case if this option specifies Windows version not supported by
the host (--host), it will halt configuration with an error message.

Signed-off-by: Kirill Makurin <maiddaisuki@outlook.com>
Signed-off-by: LIU Hao <lh_mouse@126.com>
diff --git a/mingw-w64-libraries/winpthreads/README b/mingw-w64-libraries/winpthreads/README
index bdbb2cb..0243472 100644
--- a/mingw-w64-libraries/winpthreads/README
+++ b/mingw-w64-libraries/winpthreads/README
@@ -14,27 +14,31 @@
 
 ### Windows NT
 
-You can define `WINPTHREADS_WINNT` macro to specify the minimal Windows NT
-version you want `winpthreads` to be able to run on.
+You can use `--with-winnt=version` option to specify the minimal Windows NT
+version you want `winpthreads` to be able to run on. The supported values are:
 
-It can be defined to any `_WIN32_WINNT_*` macro from `sdkddkver.h` or their
-corresponding values. For Windows NT 3.1, NT 3.5 and NT 3.51, which do not
-have `_WIN32_WINNT_*` constans, you have to use appropriate values directly:
+| Value    | Meaning         |
+|----------|-----------------|
+| nt3.1    | Windows NT 3.1  |
+| nt3.5    | Windows NT 3.5  |
+| nt3.51   | Windows NT 3.51 |
+| nt4.0    | Windows NT 4.0  |
+| win2000  | Windows 2000    |
+| winxp    | Windows XP      |
+| winvista | Windows Vista   |
+| win7     | Windows 7       |
+| win8     | Windows 8       |
+| win8.1   | Windows 8.1     |
+| win10    | Windows 10      |
 
-| Windows Version | Hex Value |
-|-----------------|-----------|
-| Windows NT 3.1  | `0x030A`  |
-| Windows NT 3.5  | `0x0332`  |
-| Windows NT 3.51 | `0x0333`  |
+Default and allowed values for `--with-winnt` option are target-dependent:
 
-Default and allowed values for `WINPTHREADS_WINNT` are target-dependent:
-
-| Target            | Minimal Allowed Value | Default Value                                           |
-|-------------------|-----------------------|---------------------------------------------------------|
-| x86               | Windows NT 3.1        | Windows NT 3.1 with mingw-w64; Windows XP with MSVC     |
-| x64               | Windows XP x64        | Windows XP x64                                          |
-| arm32             | Windows 8             | Windows 8                                               |
-| arm64 and arm64ec | Windows 10            | Windows 10                                              |
+| Target            | Minimal Allowed Value | Default Value                             |
+|-------------------|-----------------------|-------------------------------------------|
+| x86               | `nt3.1`               | `nt3.1` with mingw-w64; `winxp` with MSVC |
+| x64               | `winxp`               | `winxp`                                   |
+| arm32             | `win8`                | `win8`                                    |
+| arm64 and arm64ec | `win10`               | `win10`                                   |
 
 ### Win9x
 
@@ -42,8 +46,8 @@
 `winpthreads` with Win9x support.  
 This macro is only meaningful for x86 and is ignored on other targets.
 
-This macro can be defined to non-zero value only when `WINPTHREADS_WINNT` macro
-is defined to value of `_WIN32_WINNT_WS03` or lower.
+This macro can be defined to non-zero value only when value passed to
+`--with-winnt` option is `winxp` or lower.
 
 By default, it is enabled with mingw-w64 (if the above condition is true) and
 disabled with MSVC.
diff --git a/mingw-w64-libraries/winpthreads/configure.ac b/mingw-w64-libraries/winpthreads/configure.ac
index 8edc900..e467b20 100644
--- a/mingw-w64-libraries/winpthreads/configure.ac
+++ b/mingw-w64-libraries/winpthreads/configure.ac
@@ -13,8 +13,30 @@
 
 AC_CANONICAL_HOST
 
-# Checks for programs.
+# Validate $host_cpu and simplify it for use in later checks
+winpthreads_host=$host_cpu
+
+AS_CASE(
+  [$winpthreads_host],
+    [i?86],
+      [winpthreads_host=x86],
+    [x86_64],
+      [winpthreads_host=x64],
+    [arm | armv7],
+      [winpthreads_host=arm32],
+    [aarch64],
+      [winpthreads_host=arm64],
+    [arm64ec],
+      [winpthreads_host=arm64ec],
+dnl default
+      [AC_MSG_ERROR([Unsupported host '$host'])]
+)
+
+dnl This invocation may set variable `ac_no_link` to "yes", which will make
+dnl `AC_LINK_IFELSE` abort configuration. Each use of `AC_LINK_IFELSE` must
+dnl be guarded with `test $ac_no_link = no` check.
 AC_NO_EXECUTABLES
+dnl Checks for programs.
 AC_PROG_CC
 AM_PROG_AS
 AM_PROG_AR
@@ -46,6 +68,175 @@
 LT_INIT([win32-dll])
 LT_LANG([Windows Resource])
 
+AC_ARG_WITH(
+  [winnt],
+  [AS_HELP_STRING(
+    [--with-winnt=VERSION],
+    [Minimal Windows NT version to support],
+  )],
+dnl then
+  [AS_CASE(
+    [$withval],
+      [win10 | win8 | win8.1 | win7 | winvista | winxp | win2000 | nt4.0 | nt3.51 | nt3.5 | nt3.1],
+        [:],
+dnl   default
+        [AC_MSG_ERROR([Invalid value '$withval' passed to --with-winnt option])]
+  )],
+dnl else
+  [AS_CASE(
+    [$winpthreads_host],
+      [x86],
+        [AS_IF(
+          [test $msvc = yes],
+            [with_winnt=winxp],
+dnl       else
+            [with_winnt=nt3.1]
+        )],
+      [x64],
+        [with_winnt=winxp],
+      [arm32],
+        [with_winnt=win8],
+      [arm64 | arm64ec],
+        [with_winnt=win10]
+  )]
+)
+
+# Major and minor subsystem versions corresponding to --with-winnt option
+subsystem_major=
+subsystem_minor=
+
+# `WINPTHREADS_WINNT_*` value corresponding to --with-winnt option;
+# see misc.h for valid values
+winpthreads_winnt=
+
+AS_CASE(
+  [$with_winnt],
+    [win10],
+      [AS_IF(
+        # FIXME: mingw-w64 tools produce broken binaries when subsystem
+        # version is set to 6.3 or higher.
+        [test $msvc = yes],
+          [subsystem_major=10]
+          [subsystem_minor=0],
+dnl     else
+          [subsystem_major=6]
+          [subsystem_minor=2]
+      )]
+      [winpthreads_winnt=WINPTHREADS_WINNT_WIN10],
+    [win8.1],
+      [AS_CASE(
+        [$winpthreads_host],
+          [x86 | x64 | arm32],
+            # FIXME: mingw-w64 tools produce broken binaries when subsystem
+            # version is set to 6.3 or higher.
+            [AS_IF(
+              [test $msvc = yes],
+                [subsystem_major=6]
+                [subsystem_minor=3],
+dnl           else
+                [subsystem_major=6]
+                [subsystem_minor=2]
+            )]
+            [winpthreads_winnt=WINPTHREADS_WINNT_WIN8_1],
+dnl     default
+            [AC_MSG_ERROR([Windows 8.1 does not support running '$host_cpu' binaries])]
+      )],
+    [win8],
+      [AS_CASE(
+        [$winpthreads_host],
+          [x86 | x64 | arm32],
+            [subsystem_major=6]
+            [subsystem_minor=2]
+            [winpthreads_winnt=WINPTHREADS_WINNT_WIN8],
+dnl     default
+            [AC_MSG_ERROR([Windows 8 does not support running '$host_cpu' binaries])]
+      )],
+    [win7],
+      [AS_CASE(
+        [$winpthreads_host],
+          [x86 | x64],
+            [subsystem_major=6]
+            [subsystem_minor=1]
+            [winpthreads_winnt=WINPTHREADS_WINNT_WIN7],
+dnl     default
+            [AC_MSG_ERROR([Windows 7 does not support running '$host_cpu' binaries])]
+      )],
+    [winvista],
+      [AS_CASE(
+        [$winpthreads_host],
+          [x86 | x64],
+            [subsystem_major=6]
+            [subsystem_minor=0]
+            [winpthreads_winnt=WINPTHREADS_WINNT_VISTA],
+dnl     default
+            [AC_MSG_ERROR([Windows Vista does not support running '$host_cpu' binaries])]
+      )],
+    [winxp],
+      [AS_CASE(
+        [$winpthreads_host],
+          [x86],
+            [subsystem_major=5]
+            [subsystem_minor=1]
+            [winpthreads_winnt=WINPTHREADS_WINNT_XP],
+          [x64],
+            [subsystem_major=5]
+            [subsystem_minor=2]
+            [winpthreads_winnt=WINPTHREADS_WINNT_2003],
+dnl       default
+            [AC_MSG_ERROR([Windows XP does not support running '$host_cpu' binaries])]
+      )],
+    [win2000],
+      [AS_IF(
+        [test $winpthreads_host = x86],
+          [subsystem_major=5]
+          [subsystem_minor=0]
+          [winpthreads_winnt=WINPTHREADS_WINNT_2000],
+dnl     else
+          [AC_MSG_ERROR([Windows 2000 does not support running '$host_cpu' binaries])]
+      )],
+    [nt4.0],
+      [AS_IF(
+        [test $winpthreads_host = x86],
+          [subsystem_major=4]
+          [subsystem_minor=0]
+          [winpthreads_winnt=WINPTHREADS_WINNT_NT_4],
+dnl     else
+          [AC_MSG_ERROR([Windows NT 4.0 does not support running '$host_cpu' binaries])]
+      )],
+    [nt3.51],
+      [AS_IF(
+        [test $winpthreads_host = x86],
+          [subsystem_major=3]
+          [subsystem_minor=51]
+          [winpthreads_winnt=WINPTHREADS_WINNT_NT_3_51],
+dnl     else
+          [AC_MSG_ERROR([Windows NT 3.51 does not support running '$host_cpu' binaries])]
+      )],
+    [nt3.5],
+      [AS_IF(
+        [test $winpthreads_host = x86],
+          [subsystem_major=3]
+          [subsystem_minor=50]
+          [winpthreads_winnt=WINPTHREADS_WINNT_NT_3_5],
+dnl     else
+          [AC_MSG_ERROR([Windows NT 3.5 does not support running '$host_cpu' binaries])]
+      )],
+    [nt3.1],
+      [AS_IF(
+        [test $winpthreads_host = x86],
+          [subsystem_major=3]
+          [subsystem_minor=10]
+          [winpthreads_winnt=WINPTHREADS_WINNT_NT_3_1],
+dnl     else
+          [AC_MSG_ERROR([Windows NT 3.1 does not support running '$host_cpu' binaries])]
+      )]
+)
+
+AC_DEFINE_UNQUOTED(
+  [WINPTHREADS_WINNT], [$winpthreads_winnt],
+  [Minimal Windows NT version to be supported by the library]
+)
+
 dnl Global compiler and linker flags. We handle compiler-specific flags in
 dnl configure.ac rather than in each Makefile.am. Note that it does not prevent
 dnl each individual Makefile.am from extending these variables.
@@ -79,6 +270,56 @@
     [DEFAULT_WARNINGS='-Wall']
 )
 
+# Figure out how to tell linker to set subsystem version
+winpthreads_subsystem=
+
+AS_IF(
+  [test $msvc = yes],
+    # The MSVC option has syntax
+    #
+    #   "/SUBSYSTEM:CONSOLE,major.minor"
+    #
+    # and must be passed as a single argument. We cannot pass it as
+    #
+    #   "-Wl,-SUBSYSTEM:CONSOLE,major.minor"
+    #
+    # because it will separate "major,minor" part, resulting in an error.
+    # A solution is to use -Xlinker option for the check.
+    #
+    # The /WX switch makes the linker treat warnings as errors; passing invalid
+    # subsystem version to /SUBSYSTEM switch results in a warning and linker
+    # simply uses the default subsystem version, which is not what we want.
+    [winpthreads_subsystem="-Xlinker -WX -Xlinker -SUBSYSTEM:CONSOLE,$subsystem_major.$subsystem_minor"],
+dnl else
+    [winpthreads_subsystem="-Wl,--subsystem,console:$subsystem_major.$subsystem_minor"]
+)
+
+AS_IF(
+  [test $ac_no_link = no],
+    [save_LDFLAGS=$LDFLAGS]
+    [LDFLAGS="$LDFLAGS $winpthreads_subsystem"]
+    [AC_LINK_IFELSE(
+      [AC_LANG_SOURCE(
+        [int main (void) {return 0;}]
+      )],
+dnl then
+      [LDFLAGS=$save_LDFLAGS],
+dnl else
+      [AC_MSG_ERROR([Linker cannot create binaries for '$with_winnt'])]
+    )]
+)
+
+# We cannot use "-Xlinker -SUBSYSTEM:CONSOLE,major.minor" when libtool is
+# involved because it converts it to -Wl switch. Use a hack to make it pass
+# "-Xlinker" and "-SUBSYSTEM:CONSOLE,major.minor" as two separate options to
+# the `compile` wrapper, which will then pass the option to the linker.
+AS_IF(
+  [test $msvc = yes],
+    [winpthreads_subsystem="-Xcompiler -Xlinker -Xcompiler -SUBSYSTEM:CONSOLE,$subsystem_major.$subsystem_minor"]
+)
+
+LINKER_SUBSYSTEM="$LINKER_SUBSYSTEM $winpthreads_subsystem"
+
 # config.h
 AH_BOTTOM(
 [#ifdef DLL_EXPORT