Merge "Push gabi++ runtime before running the tests."
diff --git a/build/tools/build-host-gdb.sh b/build/tools/build-host-gdb.sh
index 4554874..86325d8 100755
--- a/build/tools/build-host-gdb.sh
+++ b/build/tools/build-host-gdb.sh
@@ -130,12 +130,12 @@
 # directory. Relative to $NDK_DIR.
 gdb_ndk_install_dir ()
 {
-    echo "gdb-$(get_toolchain_name_for_arch $(bh_tag_to_arch $2))-$3/prebuilt/$1"
+    echo "toolchains/gdb-$(get_toolchain_name_for_arch $(bh_tag_to_arch $2))-$3/prebuilt/$(install_dir_from_host_tag $1)"
 }
 
 python_build_install_dir ()
 {
-    echo "$PYTHON_BUILD_DIR/install/prebuilt/$1"
+    echo "$PYTHON_BUILD_DIR/install/prebuilt/$(install_dir_from_host_tag $1)"
 }
 
 # $1: host system tag
@@ -242,12 +242,29 @@
 
     # build the gdb stub and replace gdb with it. This is done post-install
     # so files are in the correct place when determining the relative path.
+
     case "$1" in
         windows*)
             dump "$TEXT Building gdb-stub"
-            run $BUILDTOOLS/build-gdb-stub.sh --gdb-executable-path=${DSTDIR}/$(bh_tag_to_config_triplet $2)-gdb.exe \
-                                              --python-prefix-dir=${PYDIR} \
-                                              --mingw-w64-gcc-path=${BH_HOST_CONFIG}-gcc
+            bh_setup_host_env
+            GCC_FOR_STUB=${BH_HOST_CONFIG}-gcc
+            GCC_FOR_STUB_TARGET=`$GCC_FOR_STUB -dumpmachine`
+            if [ "$GCC_FOR_STUB_TARGET" = "i586-mingw32msvc" ]; then
+                GCC_FOR_STUB=i686-w64-mingw32-gcc
+                dump "Override compiler for gdb-stub: $GCC_FOR_STUB"
+            fi
+
+            # Uses $TOOLCHAIN_PATH/bin/$(bh_tag_to_config_triplet $2)-gdb.exe (1) instead of
+            # ${DSTDIR}/bin/$(bh_tag_to_config_triplet $2)-gdb.exe (2) because
+            # the final layout is to (1) which is a folder deeper than (2).
+            # Sample (1):
+            #  $NDK/gdb-arm-linux-androideabi-7.6/prebuilt/windows/bin/arm-linux-androideabi-gdb.exe
+            # Sample (2):
+            #  $NDK/toolchains/arm-linux-androideabi-4.7/prebuilt/windows/bin/arm-linux-androideabi-gdb.exe
+            run $NDK_BUILDTOOLS_PATH/build-gdb-stub.sh \
+                --gdb-executable-path=${DSTDIR}/bin/$(bh_tag_to_config_triplet $2)-gdb.exe \
+                --python-prefix-dir=${PYDIR} \
+                --mingw-w64-gcc=${GCC_FOR_STUB}
             ;;
         *)
             ;;
diff --git a/build/tools/build-host-python.sh b/build/tools/build-host-python.sh
index c925058..b2e8413 100755
--- a/build/tools/build-host-python.sh
+++ b/build/tools/build-host-python.sh
@@ -432,7 +432,7 @@
     local SRCDIR="$(python_ndk_install_dir $1 $2)"
     # This is similar to BLDDIR=${BLDDIR%%$SRCDIR} (and requires we use windows and not windows-x86)
     BLDDIR=$(echo "$BLDDIR" | sed "s/$(echo "$SRCDIR" | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')//g")
-    local PACKAGENAME=ndk-python-$(python_host_tag $1).tar.bz2
+    local PACKAGENAME=ndk-python-$(install_dir_from_host_tag $1).tar.bz2
     local PACKAGE="$PACKAGE_DIR/$PACKAGENAME"
 
     need_install_host_python $1 $2
@@ -441,6 +441,11 @@
     run pack_archive "$PACKAGE" "$BLDDIR" "$SRCDIR"
 }
 
+need_package_host_python ()
+{
+    bh_stamps_do package-host-python-$1-$2 package_host_python $1 $2
+}
+
 PYTHON_VERSION=$(commas_to_spaces $PYTHON_VERSION)
 ARCHS=$(commas_to_spaces $ARCHS)
 
@@ -457,7 +462,7 @@
         bh_setup_build_for_host $SYSTEM
         if [ $AUTO_BUILD != "yes" -o $SYSTEM != $BH_BUILD_TAG ]; then
             for VERSION in $PYTHON_VERSION; do
-                package_host_python $SYSTEM $VERSION
+                need_package_host_python $SYSTEM $VERSION
             done
         fi
     done
diff --git a/build/tools/common-build-host-funcs.sh b/build/tools/common-build-host-funcs.sh
index dc5d1da..fa317e6 100644
--- a/build/tools/common-build-host-funcs.sh
+++ b/build/tools/common-build-host-funcs.sh
@@ -847,11 +847,10 @@
     fi
 }
 
-# Some common functions needed by both python and gdb build scripts.
-#
 # Return host tag with only translation that windows-x86 -> windows
+#
 # $1: host system tag
-python_host_tag ()
+install_dir_from_host_tag ()
 {
     case $1 in
         windows-x86)
@@ -864,22 +863,23 @@
 }
 
 # Return the build install directory of a given Python version
+#
 # $1: host system tag
 # $2: python version
 # The suffix of this has to match python_ndk_install_dir
 #  as I package them from the build folder, substituting
 #  the end part of python_build_install_dir matching
 #  python_ndk_install_dir with nothing.
-# Needs to match with:
-#  python_build_install_dir () in build-host-gdb.sh
 python_build_install_dir ()
 {
-    echo "$BH_BUILD_DIR/install/prebuilt/$(python_host_tag $1)"
+    echo "$BH_BUILD_DIR/install/prebuilt/$(install_dir_from_host_tag $1)"
 }
 
 # Same as python_build_install_dir, but for the final NDK installation
 # directory. Relative to $NDK_DIR.
+#
+# $1: host system tag
 python_ndk_install_dir ()
 {
-    echo "prebuilt/$(python_host_tag $1)"
+    echo "prebuilt/$(install_dir_from_host_tag $1)"
 }
diff --git a/sources/android/support/include/wchar.h b/sources/android/support/include/wchar.h
index 26a8be7..e3d84f5 100644
--- a/sources/android/support/include/wchar.h
+++ b/sources/android/support/include/wchar.h
@@ -78,7 +78,7 @@
 typedef int wctype_t;
 typedef struct locale_struct* locale_t;
 
-// This is tricky: <stdio.h> indirectly includes <stdint.h>, which will
+// See http://b.android.com/This is tricky: <stdio.h> indirectly includes <stdint.h>, which will
 // already have defined WCHAR_MIN / WCHAR_MAX in the following cases:
 // - When compiling C sources
 // - When compiling C++ sources AND having __STDC_LIMIT_MACROS defined.
@@ -89,8 +89,21 @@
 // The constants here ensure that they match the INT32_MIN / INT32_MAX
 // definitions.
 #ifndef WCHAR_MAX
-#define WCHAR_MAX (0x7fffffff)
-#define WCHAR_MIN (0x80000000)
+#  ifndef __WCHAR_MAX__
+#    error "__WCHAR_MAX__ undefined. Check your toolchain."
+#  endif
+// Clang doesn't define __WCHAR_MIN__, only __WCHAR_MAX__
+#  ifndef __WCHAR_MIN__
+#    if __WCHAR_MAX__ == 0xffffffff
+#      define __WCHAR_MIN__   0U
+#    elif __WCHAR_MAX__ == 0x7fffffff
+#      define __WCHAR_MIN__   0x80000000
+#    else
+#      error "Invalid __WCHAR_MAX__ value. Check your toolchain."
+#    endif
+#  endif  // !__WCHAR_MIN
+#define WCHAR_MAX __WCHAR_MAX__
+#define WCHAR_MIN __WCHAR_MIN__
 #endif
 
 #define WEOF ((wint_t)(-1))
diff --git a/sources/android/support/tests/minitest/minitest.h b/sources/android/support/tests/minitest/minitest.h
index 6a1ca21..e375305 100644
--- a/sources/android/support/tests/minitest/minitest.h
+++ b/sources/android/support/tests/minitest/minitest.h
@@ -370,7 +370,7 @@
     if (!(expression)) { \
       printf("ASSERT_TRUE:%s:%d: expression '%s' return 'false', expected 'true'\n", \
              __FILE__, __LINE__, #expression); \
-      minitest_testcaseFatalFailure(); \
+      minitest_testcase->FatalFailure(); \
       return; \
     } \
   } while (0)
@@ -380,7 +380,7 @@
     if (!!(expression)) { \
       printf("ASSERT_FALSE:%s:%d: expression '%s' return 'true', expected 'false'\n", \
              __FILE__, __LINE__, #expression); \
-      minitest_testcaseFatalFailure(); \
+      minitest_testcase->FatalFailure(); \
       return; \
     } \
   } while (0)
diff --git a/sources/android/support/tests/wchar_unittest.cc b/sources/android/support/tests/wchar_unittest.cc
index 4bd6a69..95c027c 100644
--- a/sources/android/support/tests/wchar_unittest.cc
+++ b/sources/android/support/tests/wchar_unittest.cc
@@ -6,6 +6,15 @@
 TEST(wchar, wchar_limits) {
   ASSERT_EQ(4U, sizeof(wchar_t));
   ASSERT_EQ(sizeof(int), sizeof(wint_t));
+#ifdef __arm__
+  ASSERT_GT(wchar_t(0), wchar_t(-1));
+  ASSERT_EQ(wchar_t(0), WCHAR_MIN);
+  ASSERT_EQ(wchar_t(0xffffffff), WCHAR_MAX);
+#else
+  ASSERT_LT(wchar_t(0), wchar_t(-1));
+  ASSERT_EQ(wchar_t(0x80000000), WCHAR_MIN);
+  ASSERT_EQ(wchar_t(0x7fffffff), WCHAR_MAX);
+#endif
 }
 
 TEST(wchar, wcschr) {
diff --git a/tests/build/wchar_t-size/jni/Android.mk b/tests/build/wchar_t-size/jni/Android.mk
index 2aaf0b8..cc8ed41 100644
--- a/tests/build/wchar_t-size/jni/Android.mk
+++ b/tests/build/wchar_t-size/jni/Android.mk
@@ -6,6 +6,7 @@
   test_default.c \
   test_8bit.c \
   test_8bit_cplusplus.cpp \
-  test_8bit_cplusplus_stdc_limit_macros.cpp
+  test_8bit_cplusplus_stdc_limit_macros.cpp \
+  test_always_signed.c \
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/build/wchar_t-size/jni/Application.mk b/tests/build/wchar_t-size/jni/Application.mk
index b7b94e9..f51c740 100644
--- a/tests/build/wchar_t-size/jni/Application.mk
+++ b/tests/build/wchar_t-size/jni/Application.mk
@@ -1,3 +1,2 @@
-# The issue only exists for ARM.
-APP_ABI := armeabi armeabi-v7a
+APP_ABI := all
 APP_PLATFORM := android-3
diff --git a/tests/build/wchar_t-size/jni/test_8bit.c b/tests/build/wchar_t-size/jni/test_8bit.c
index 980d2f1..7089e1d 100644
--- a/tests/build/wchar_t-size/jni/test_8bit.c
+++ b/tests/build/wchar_t-size/jni/test_8bit.c
@@ -7,7 +7,7 @@
 #define _WCHAR_IS_8BIT
 #include <wchar.h>
 
-#if __ANDROID_API__ != 3
+#if defined(__arm__) && __ANDROID_API__ != 3
 #error "You should target API level 3 when compiling this file!"
 #endif
 
@@ -17,10 +17,15 @@
 #define STATIC_ASSERT(condition) \
   static char CONCAT(dummy_,__LINE__)[1 - 2*(!(condition))];
 
+#ifdef __arm__
 STATIC_ASSERT(sizeof(__WCHAR_TYPE__) == 1);
 STATIC_ASSERT(sizeof(wchar_t) == 1);
+#else
+STATIC_ASSERT(sizeof(__WCHAR_TYPE__) == 4);
+STATIC_ASSERT(sizeof(wchar_t) == 4);
+#endif
 
 // Since this is C code, the old behaviour was to always define
-// these constants as 32 bit values.
+// these constants as signed 32 bit values.
 STATIC_ASSERT(WCHAR_MIN == 0x80000000);
 STATIC_ASSERT(WCHAR_MAX == 0x7fffffff);
diff --git a/tests/build/wchar_t-size/jni/test_8bit_cplusplus.cpp b/tests/build/wchar_t-size/jni/test_8bit_cplusplus.cpp
index 29e847f..3396aab 100644
--- a/tests/build/wchar_t-size/jni/test_8bit_cplusplus.cpp
+++ b/tests/build/wchar_t-size/jni/test_8bit_cplusplus.cpp
@@ -7,7 +7,7 @@
 #define _WCHAR_IS_8BIT
 #include <wchar.h>
 
-#if __ANDROID_API__ != 3
+#if defined(__arm__) && __ANDROID_API__ != 3
 #error "You should target API level 3 when compiling this file!"
 #endif
 
@@ -17,13 +17,23 @@
 #define STATIC_ASSERT(condition) \
   static char CONCAT(dummy_,__LINE__)[1 - 2*(!(condition))];
 
+#ifdef __arm__
 STATIC_ASSERT(sizeof(__WCHAR_TYPE__) == 1);
+#else
+STATIC_ASSERT(sizeof(__WCHAR_TYPE__) == 4);
+#endif
 
 // wchar_t is never redefined by <stddef.h> because it's a C++ keyword,
 // unlike in C.
 STATIC_ASSERT(sizeof(wchar_t) == 4);
 
-// Since this is C++ code, the old behaviour was to define
-// these constants to 8-bit values.
+// Since this is C++ code, and __STC_LIMIT_MACROS was not defined, the
+// old behaviour on ARM was to define these constants to 8-bit values.
+// Otherwise, always signed 32-bit values.
+#ifdef __arm__
 STATIC_ASSERT(WCHAR_MIN == 0);
 STATIC_ASSERT(WCHAR_MAX == 255);
+#else
+STATIC_ASSERT(WCHAR_MIN == 0x80000000);
+STATIC_ASSERT(WCHAR_MAX == 0x7fffffff);
+#endif
diff --git a/tests/build/wchar_t-size/jni/test_8bit_cplusplus_stdc_limit_macros.cpp b/tests/build/wchar_t-size/jni/test_8bit_cplusplus_stdc_limit_macros.cpp
index d159299..06c1c33 100644
--- a/tests/build/wchar_t-size/jni/test_8bit_cplusplus_stdc_limit_macros.cpp
+++ b/tests/build/wchar_t-size/jni/test_8bit_cplusplus_stdc_limit_macros.cpp
@@ -9,7 +9,7 @@
 #define _WCHAR_IS_8BIT
 #include <wchar.h>
 
-#if __ANDROID_API__ != 3
+#if defined(__arm__) && __ANDROID_API__ != 3
 #error "You should target API level 3 when compiling this file!"
 #endif
 
@@ -19,12 +19,17 @@
 #define STATIC_ASSERT(condition) \
   static char CONCAT(dummy_,__LINE__)[1 - 2*(!(condition))];
 
+#ifdef __arm__
 STATIC_ASSERT(sizeof(__WCHAR_TYPE__) == 1);
+#else
+STATIC_ASSERT(sizeof(__WCHAR_TYPE__) == 4);
+#endif
 
 // wchar_t is never redefined by <stddef.h> because it's a C++ keyword,
 // unlike in C.
 STATIC_ASSERT(sizeof(wchar_t) == 4);
 
-// This is C++ code but __STDC_LIMIT_MACROS was defined.
+// This is C++ code but __STDC_LIMIT_MACROS was defined, and
+// _WCHAR_IS_8BIT is defined, so the values are always 32-bit signed.
 STATIC_ASSERT(WCHAR_MIN == 0x80000000);
 STATIC_ASSERT(WCHAR_MAX == 0x7fffffff);
diff --git a/tests/build/wchar_t-size/jni/test_always_signed.c b/tests/build/wchar_t-size/jni/test_always_signed.c
new file mode 100644
index 0000000..9e6a1c9
--- /dev/null
+++ b/tests/build/wchar_t-size/jni/test_always_signed.c
@@ -0,0 +1,16 @@
+// This checks that, by default, wchar_t is 32-bit and
+// WCHAR_MIN/WCHAR_MAX are 32-bit signed on all platforms when
+// _WCHAR_IS_ALWAYS_SIGNED is defined.
+#define _WCHAR_IS_ALWAYS_SIGNED 1
+#include <wchar.h>
+
+#define CONCAT(x,y) CONCAT_(x,y)
+#define CONCAT_(x,y) x ## y
+
+#define STATIC_ASSERT(condition) \
+  static char CONCAT(dummy_,__LINE__)[1 - 2*(!(condition))];
+
+STATIC_ASSERT(sizeof(wchar_t) == 4);
+
+STATIC_ASSERT(WCHAR_MIN == -1-2147483647);
+STATIC_ASSERT(WCHAR_MAX == 2147483647);
diff --git a/tests/build/wchar_t-size/jni/test_default.c b/tests/build/wchar_t-size/jni/test_default.c
index 08d8090..55ed5e7 100644
--- a/tests/build/wchar_t-size/jni/test_default.c
+++ b/tests/build/wchar_t-size/jni/test_default.c
@@ -1,9 +1,9 @@
 // This checks that, by default, wchar_t is 32-bit and
-// WCHAR_MIN/WCHAR_MAX are 32-bit signed.
+// WCHAR_MIN/WCHAR_MAX are 32-bit signed on all platforms except ARM.
 #include <android/api-level.h>
 #include <wchar.h>
 
-#if __ANDROID_API__ != 3
+#if defined(__arm__) && __ANDROID_API__ != 3
 #error "You should target API level 3 when compiling this file!"
 #endif
 
@@ -14,5 +14,11 @@
   static char CONCAT(dummy_,__LINE__)[1 - 2*(!(condition))];
 
 STATIC_ASSERT(sizeof(wchar_t) == 4);
+
+#ifdef __arm__
+STATIC_ASSERT(WCHAR_MIN == 0U);
+STATIC_ASSERT(WCHAR_MAX == 2*2147483647U + 1U);
+#else
 STATIC_ASSERT(WCHAR_MIN == -1-2147483647);
 STATIC_ASSERT(WCHAR_MAX == 2147483647);
+#endif
diff --git a/tests/device/issue42891-boost-1_52/jni/boost/boost/smart_ptr/detail/sp_has_sync.hpp b/tests/device/issue42891-boost-1_52/jni/boost/boost/smart_ptr/detail/sp_has_sync.hpp
index 12acea8..3f823d2 100644
--- a/tests/device/issue42891-boost-1_52/jni/boost/boost/smart_ptr/detail/sp_has_sync.hpp
+++ b/tests/device/issue42891-boost-1_52/jni/boost/boost/smart_ptr/detail/sp_has_sync.hpp
@@ -24,7 +24,7 @@
 
 #define BOOST_SP_HAS_SYNC
 
-#if defined( __arm__ )  || defined( __armel__ )
+#if defined( __arm__ )  || defined( __armel__ ) || defined( __le32__ )
 #undef BOOST_SP_HAS_SYNC
 #endif
 
diff --git a/tests/device/test-cpufeatures/jni/Android.mk b/tests/device/test-cpufeatures/jni/Android.mk
index 415e9a4..8b20bd0 100644
--- a/tests/device/test-cpufeatures/jni/Android.mk
+++ b/tests/device/test-cpufeatures/jni/Android.mk
@@ -26,6 +26,7 @@
 include $(BUILD_EXECUTABLE)
 endif
 
+ifeq ($(TARGET_ARCH),arm)
 ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
 include $(CLEAR_VARS)
 LOCAL_MODULE := test_arm_idiv_support
@@ -47,5 +48,6 @@
 LOCAL_STATIC_LIBRARIES := cpufeatures
 include $(BUILD_EXECUTABLE)
 endif
+endif
 
 $(call import-module,android/cpufeatures)
diff --git a/tests/standalone/builtin-macros/run.sh b/tests/standalone/builtin-macros/run.sh
index 27a11a7..b40a418 100644
--- a/tests/standalone/builtin-macros/run.sh
+++ b/tests/standalone/builtin-macros/run.sh
@@ -72,6 +72,31 @@
     COUNT=$(( $COUNT + 1 ))
 }
 
+# Check the definition of a given macro against multiple values
+# $1: macro name
+# $2+: list of acceptable values.
+macro_multi_check () {
+    echo -n "Checking $1: "
+    local VAL=$(macro_val $1)
+    if [ -z "$VAL" ]; then
+      echo "Missing built-in macro definition: $1"
+      return 1
+    fi
+    local VAL2 FOUND
+    shift
+    for VAL2 in "$@"; do
+      if [ "$VAL2" = "$VAL" ]; then
+        FOUND=true
+        break
+      fi
+    done
+    if [ -z "$FOUND" ]; then
+      echo "Invalid built-in macro definition: '$VAL', expected one of: $@"
+      return 1
+    fi
+    return 0
+}
+
 # Check that a given macro is undefined
 macro_check_undef () {
     if [ -n "$2" ]; then
@@ -114,6 +139,9 @@
         macro_check __ARMEL__ 1            "ARM little-endian"
         macro_check __THUMB_INTERWORK__ 1  "ARM thumb-interwork"
         macro_check __PIC__ 1              "Position independent code (-fpic)"
+        macro_check __WCHAR_TYPE__         "unsigned"
+        macro_check __WCHAR_MAX__          "4294967295U"
+        # Clang doesn't define __WCHAR_MIN__ so don't check it"
 
         case $ABI in
             armeabi)
@@ -148,6 +176,10 @@
         macro_check __SSE3__ 1       "SSE3 instruction set"
         macro_check __SSE_MATH__ 1   "Use SSE for math operations"
         macro_check __SSE2_MATH__ 1  "Use SSE2 for math operations"
+        # GCC defines is as 'long', and Clang as 'int'
+        macro_multi_check __WCHAR_TYPE__   "long" "int"
+        # GCC defines it with an L suffix, Clang doesn't.
+        macro_multi_check __WCHAR_MAX__    "2147483647L" "2147483647"
         ;;
 
     mips)
@@ -155,6 +187,9 @@
         macro_check _MIPS_ARCH_MIPS32 1 "Mips 32-bit ABI"
         macro_check __MIPSEL__ 1        "Mips little-endian"
         macro_check __PIC__ 1           "Position independent code (-fpic)"
+        # GCC defines it as "signed int", and Clang as "int"
+        macro_multi_check __WCHAR_TYPE__   "signed int" "int"
+        macro_check __WCHAR_MAX__       "2147483647"
         ;;
     *)
         echo "Unknown ABI: $ABI"