Change STL for Windows to libc++.

- Link libgcc_eh, which needs pthread, which in turn depends on
kernel32.  Wrap the libraries with --start-group and --end-group and
remove duplicates.
- Use SjLj exceptions for 32-bit.  libgcc_eh implements SjLj exception
model for 32-bit.
- Disable visibility annotations for libcxx and libcxxabi since we are
only going to support these as static libraries.
- Use Win32 threads.  MinGW pthreads throws an error when building
libcxx since it's pthread_mutex_initializer is not constant (needs a
cast).

Test: Build and test Windows binaries under Wine.

Change-Id: I594fa7673282ff5c1b7b80d49f48b7618960a148
diff --git a/core/cxx_stl_setup.mk b/core/cxx_stl_setup.mk
index 5171b8a..3590079 100644
--- a/core/cxx_stl_setup.mk
+++ b/core/cxx_stl_setup.mk
@@ -15,8 +15,7 @@
             endif
 
             ifeq ($($(my_prefix)OS),windows)
-                # libc++ is not supported on mingw.
-                my_cxx_stl := libstdc++
+                my_cxx_stl := libc++_static
             endif
         endif
     else
@@ -38,9 +37,9 @@
     endif
     ifdef LOCAL_IS_HOST_MODULE
         ifeq ($($(my_prefix)OS),windows)
-            ifneq ($(filter $(my_cxx_stl),libc++ libc++_static),)
-                # libc++ is not supported on mingw.
-                my_cxx_stl := libstdc++
+            ifneq ($(filter $(my_cxx_stl),libc++),)
+                # only libc++_static is supported on mingw.
+                my_cxx_stl := libc++_static
             endif
         endif
     endif
@@ -52,8 +51,9 @@
 darwin_dynamic_gcclibs := -lc -lSystem
 darwin_static_gcclibs := NO_STATIC_HOST_BINARIES_ON_DARWIN
 windows_dynamic_gcclibs := \
-    -lmsvcr110 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 \
-    -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt
+    -Wl,--start-group -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcr110 \
+    -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lpsapi \
+    -Wl,--end-group
 windows_static_gcclibs := NO_STATIC_HOST_BINARIES_ON_WINDOWS
 
 my_link_type := dynamic
@@ -100,6 +100,20 @@
         my_cppflags += -nostdinc++
         my_ldflags += -nodefaultlibs
         my_cxx_ldlibs += $($($(my_prefix)OS)_$(my_link_type)_gcclibs)
+
+        ifeq ($($(my_prefix)OS),windows)
+            # Use SjLj exceptions for 32-bit.  libgcc_eh implements SjLj
+            # exception model for 32-bit.
+            ifeq (x86,$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH))
+                my_cppflags += -fsjlj-exceptions
+            endif
+            # Disable visibility annotations since we're using libc++ static
+            # library.
+            my_cppflags += -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
+            my_cppflags += -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS
+            # Use Win32 threads in libc++.
+            my_cppflags += -D_LIBCPP_HAS_THREAD_API_WIN32
+        endif
     else
         ifeq (arm,$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH))
             my_static_libraries += libunwind_llvm
@@ -113,11 +127,7 @@
 else ifeq ($(my_cxx_stl),ndk)
     # Using an NDK STL. Handled in binary.mk.
 else ifeq ($(my_cxx_stl),libstdc++)
-    ifndef LOCAL_IS_HOST_MODULE
-        $(error $(LOCAL_PATH): $(LOCAL_MODULE): libstdc++ is not supported for device modules)
-    else ifneq ($($(my_prefix)OS),windows)
-        $(error $(LOCAL_PATH): $(LOCAL_MODULE): libstdc++ is not supported on $($(my_prefix)OS))
-    endif
+    $(error $(LOCAL_PATH): $(LOCAL_MODULE): libstdc++ is not supported)
 else ifeq ($(my_cxx_stl),none)
     ifdef LOCAL_IS_HOST_MODULE
         my_cppflags += -nostdinc++