Use separate WINE to test 32- and 64-bit windows toolchain

The new run-tests.sh uses wine12 and wine15 to test 32- and 64-bit
windows toolchains, respectively.  We'd like to have one WINE to run
it all, but WINE is tricky, and here is the compatibility report

 mingw compilers:
  i586-mingw32msvc-gcc 4.4.2 can only generate 32-bit host binaries (amd64-mingw32msvc is broken)
  x86_64-w64-mingw32 4.7.3 20121231 (prerelase) can generate both 32-bit and 64-bit (default)
 WINE:
  wine12 = wine-1.2.3  doesn't support 64-bit
  wine13 = wine-1.3.37
  wine14 = wine-1.4.1
  wine15 = wine-1.5.21

  NDK release/test 32-bit windows toolchain with i586-mingw32msvc-gcc/wine12
  Need better compiler/WINE for 64-bit windows toolchain

 Result:
  32-bit make.exe -B by i586-mingw32msvc-gcc fails in wine > 1.2.3
  32-bit make.exe -j2 by i586-mingw32msvc-gcc fails in wine > 1.2.3
  32-bit ld.bfd by i586-mingw32msvc-gcc fails in wine13 and wine15
  32-bit ld.gold by i586-mingw32msvc-gcc fails in wine13 and wine15

  32-bit awk.exe by x86_64-w64-mingw32 fails in wine12
  32-bit make.exe -B by x86_64-w64-mingw32 fails in wine > 1.2.3
  32-bit make.exe -j2 by x86_64-w64-mingw32 fails in wine > 1.2.3
  32-bit ld.gold by x86_64-w64-mingw32 fails in wine > 1.2.3

  >> Recommend to continue build 32-bit host binaries in i586-mingw32msvc-gcc and test in wine12

  64-bit make.exe -B fails in all wine
  64-bit make.exe -j2 fails in all wine
  64-bit ld.gold fails in all wine
  results of 64-bit binraries generated by earlier x86_64-w64-mingw32 4.5.4 are no different

  >> Recommend to build 64-bit host binaries in x86_64-w64-mingw32 4.7.3 and test in wine15
     w/o -B, use -j1, and with "APP_LDFLAGS=-fuse-ld=bfd".  Test the rest in real windows machine

It's possible to have multiple WINE, via PlayOnLinux for example.
Jst set WINEPREFIX to different directories and LD_LIBRARY_PATH to their own
shared library dependencies.

Change-Id: I8354c2b82a6f67e2c68b71947814909c4c036d75
diff --git a/tests/run-tests-all.sh b/tests/run-tests-all.sh
index 03e3a21..79b959b 100755
--- a/tests/run-tests-all.sh
+++ b/tests/run-tests-all.sh
@@ -54,6 +54,11 @@
             TEST_HOST_32BIT=yes
         fi
     ;;
+    windows*)
+        if [ "$ProgramW6432"!="" -a \
+             -d "$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64" ] ; then
+            TEST_HOST_32BIT=yes
+        fi
 esac
 
 #
@@ -76,33 +81,38 @@
 NDK_TOOLCHAIN_VERSION=clang3.1 ./run-tests.sh --continue-on-build-fail --full
 
 if [ "$TEST_HOST_32BIT" = "yes" ] ; then
-    dump "### Running $SYSTEM gcc 4.7 full tests (32-bit host)"
+    dump "### Running $SYSTEM gcc 4.7 tests (32-bit host)"
     NDK_HOST_32BIT=1 NDK_TOOLCHAIN_VERSION=4.7 ./run-tests.sh --continue-on-build-fail
-    dump "### Running $SYSTEM gcc 4.6 full tests (32-bit host)"
+    dump "### Running $SYSTEM gcc 4.6 tests (32-bit host)"
     NDK_HOST_32BIT=1 NDK_TOOLCHAIN_VERSION=4.6 ./run-tests.sh --continue-on-build-fail
-    dump "### Running $SYSTEM gcc 4.4.3 full tests (32-bit host)"
+    dump "### Running $SYSTEM gcc 4.4.3 tests (32-bit host)"
     NDK_HOST_32BIT=1 NDK_TOOLCHAIN_VERSION=4.4.3 ./run-tests.sh --continue-on-build-fail
-    dump "### Running $SYSTEM clang 3.1 full tests (32-bit host)"
+    dump "### Running $SYSTEM clang 3.1 tests (32-bit host)"
     NDK_HOST_32BIT=1 NDK_TOOLCHAIN_VERSION=clang3.1 ./run-tests.sh --continue-on-build-fail
 fi
 
+if [ "$SYSTEM" = "linux-x86" -a -d "$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64" ] ; then
+    # using 64-bit windows toolchain
+    dump "### Running windows-x86_64 4.7 tests"
+    NDK_TOOLCHAIN_VERSION=4.7 ./run-tests.sh --continue-on-build-fail --wine # --full
+    dump "### Running windows-x86_64 4.6 tests"
+    NDK_TOOLCHAIN_VERSION=4.6 ./run-tests.sh --continue-on-build-fail --wine # --full
+    dump "### Running windows-x86_64 4.4.3 tests"
+    NDK_TOOLCHAIN_VERSION=4.4.3 ./run-tests.sh --continue-on-build-fail --wine # --full
+    dump "### Running windows-x86_64 clang 3.1 tests"
+    NDK_TOOLCHAIN_VERSION=clang3.1 ./run-tests.sh --continue-on-build-fail --wine # --full
+fi
+
 if [ "$SYSTEM" = "linux-x86" -a -d "$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/windows" ] ; then
-    find_program WINE wine
-    if [ -n "$WINE" ]; then
-        WINE_VERSION=`$WINE --version`
-        if [ -n "$WINE_VERSION" -a "$WINE_VERSION" != "wine-1.4" ]; then
-            # enumerate all cases using windows toolchain
-            dump "### Running windows 4.7 tests"
-            NDK_TOOLCHAIN_VERSION=4.7 ./run-tests.sh --continue-on-build-fail --wine # --full
-            dump "### Running windows 4.6 tests"
-            NDK_TOOLCHAIN_VERSION=4.6 ./run-tests.sh --continue-on-build-fail --wine # --full
-            dump "### Running windows 4.4.3 tests"
-            NDK_TOOLCHAIN_VERSION=4.4.3 ./run-tests.sh --continue-on-build-fail --wine # --full
-            dump "### Running windows clang 3.1 tests"
-            NDK_TOOLCHAIN_VERSION=clang3.1 ./run-tests.sh --continue-on-build-fail --wine # --full
-        fi
-        # No need to check TEST_HOST_32BIT because Windows doesn't have 64-bit toolchain to begin with
-    fi
+    # using 32-bit windows toolchain
+    dump "### Running windows 4.7 tests"
+    NDK_HOST_32BIT=1 NDK_TOOLCHAIN_VERSION=4.7 ./run-tests.sh --continue-on-build-fail --wine # --full
+    dump "### Running windows 4.6 tests"
+    NDK_HOST_32BIT=1 NDK_TOOLCHAIN_VERSION=4.6 ./run-tests.sh --continue-on-build-fail --wine # --full
+    dump "### Running windows 4.4.3 tests"
+    NDK_HOST_32BIT=1 NDK_TOOLCHAIN_VERSION=4.4.3 ./run-tests.sh --continue-on-build-fail --wine # --full
+    dump "### Running windows clang 3.1 tests"
+    NDK_HOST_32BIT=1 NDK_TOOLCHAIN_VERSION=clang3.1 ./run-tests.sh --continue-on-build-fail --wine # --full
 fi
 
 # add more if you want ...
diff --git a/tests/run-tests.sh b/tests/run-tests.sh
index 281af71..77859c6 100755
--- a/tests/run-tests.sh
+++ b/tests/run-tests.sh
@@ -376,6 +376,23 @@
 ###
 
 NDK_BUILD_FLAGS="-B"
+if [ "$WINE" ]; then
+    case "$NDK_HOST_32BIT" in
+        1|true)
+            WINE=wine12
+            ;;
+        *)
+            WINE=wine15
+            NDK_BUILD_FLAGS=""  # make.exe -B hangs in wine > 1.2.x
+            if [ "$NDK_TOOLCHAIN_VERSION" != "4.4.3" ] ; then
+                APP_LDFLAGS=-fuse-ld=bfd # 64-bit ld.gold can't run in any wine!
+            fi
+            ;;
+    esac
+    find_program WINE_PROG $WINE
+    fail_panic "Can't locate $WINE"
+fi
+
 case $ABI in
     default)  # Let the APP_ABI in jni/Application.mk decide what to build
         ;;
@@ -400,10 +417,22 @@
 
 run_ndk_build ()
 {
+    EXTRA_FLAGS=
+    if [ -n "$APP_LDFLAGS" ] ; then
+        # APP_LDFLAGS in env. var. doesn't work
+        EXTRA_FLAGS="APP_LDFLAGS=$APP_LDFLAGS"
+    fi
     if [ "$WINE" ]; then
-        run wine cmd /c Z:$NDK/ndk-build.cmd -j$JOBS "$@"
+        if [ "$WINE" = "wine12" ]; then
+            run $WINE cmd /c Z:$NDK/ndk-build.cmd -j$JOBS "$@" $EXTRA_FLAGS
+        else
+            # do "clean" instead of -B
+            run $WINE cmd /c Z:$NDK/ndk-build.cmd clean
+            # make.exe can't do parallel build in wine > 1.2.x
+            run $WINE cmd /c Z:$NDK/ndk-build.cmd "$@" -j1 $EXTRA_FLAGS
+        fi
     else
-        run $NDK/ndk-build -j$JOBS "$@"
+        run $NDK/ndk-build -j$JOBS "$@" $EXTRA_FLAGS
     fi
 }