Windows: Update build-mingw64-toolchain.sh for modern versions.

I've ensured that MinGW-w64 toolchains can be built with the
 revisions 5166, 5445 and 5578 (at time of writing the latest).
 For recent versions, a patch is needed for widl.exe usage
 (which generates .idl files). This patch has been submitted
 to the mingw-w64-public mailing list so hopefully won't be
 needed in the future.

Change-Id: I03d5b06b4dc3055cde42efb62465440b93ff38e9
diff --git a/build/tools/build-mingw64-toolchain.sh b/build/tools/build-mingw64-toolchain.sh
index a05e37f..1e2e457 100755
--- a/build/tools/build-mingw64-toolchain.sh
+++ b/build/tools/build-mingw64-toolchain.sh
@@ -132,16 +132,21 @@
     echo "WARNING: WARNING: WARNING: THIS SCRIPT PROBABLY ONLY WORKS ON LINUX!!"
 fi
 
+# GMP moving home?
+# GMP_VERSION=5.1.0
+# GMP_URL=ftp://ftp.gmplib.org/pub/gmp-$GMP_VERSION/
+# ..but the old one is still there:
 GMP_VERSION=5.0.5
+GMP_URL=http://ftp.gnu.org/gnu/gmp/
+
 MPFR_VERSION=3.1.1
 MPC_VERSION=1.0.1
 BINUTILS_VERSION=2.22
 GCC_VERSION=4.7.2
+
 # Need at least revision 5166
-#  "stdio.h (asprintf, vasprintf): Disable definitions stubs"
-#  as otherwise gold can't be built.
-# Only work up to svn@5177.  Better use
-#  https://mingw-w64.svn.sourceforge.net/svnroot/mingw-w64/stable/v2.x
+# For reference, I've built a working NDK with 5445
+# (latest as of Sun Feb 3 2013 is 5578)
 MINGW_W64_VERSION=svn@5166
 
 JOBS=$(( $NUM_CORES * 2 ))
@@ -389,39 +394,50 @@
 echo "The MD5 hashes for the original sources packages are:" >> $INSTALL_DIR/README
 fail_panic "Could not copy script to installation directory."
 
-download_package http://ftp.gnu.org/gnu/gmp/gmp-$GMP_VERSION.tar.bz2
+download_package ${GMP_URL}gmp-${GMP_VERSION}.tar.bz2
 download_package http://ftp.gnu.org/gnu/mpfr/mpfr-$MPFR_VERSION.tar.bz2
 download_package http://www.multiprecision.org/mpc/download/mpc-$MPC_VERSION.tar.gz
 download_package http://ftp.gnu.org/gnu/binutils/binutils-$BINUTILS_VERSION.tar.bz2
 download_package http://ftp.gnu.org/gnu/gcc/gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.bz2
 
+PREFIX_FOR_TARGET=$INSTALL_DIR/$TARGET_TAG
+WITH_WIDL=$INSTALL_DIR/bin
+MINGW_W64_REVISION=
 MINGW_W64_VERSION_NO_REV=$(echo $MINGW_W64_VERSION | awk 'BEGIN { FS="@" }; { print $1 }')
 if [ "$MINGW_W64_VERSION_NO_REV" = "svn" ];  then
     MINGW_W64_REVISION=$(echo $MINGW_W64_VERSION | awk 'BEGIN { FS="@" }; { print $2 }')
     if [ ! -z "$MINGW_W64_REVISION" ] ; then
+        if [ $MINGW_W64_REVISION -lt 5186 ] ; then
+            PREFIX_FOR_TARGET=$INSTALL_DIR
+        fi
+        if [ $MINGW_W64_REVISION -lt 5252 ] ; then
+            WITH_WIDL=mingw-w64-widl
+        elif [ $MINGW_W64_REVISION -lt 5258 ] ; then
+            WITH_WIDL=$TARGET_TAG-widl
+        fi
         MINGW_W64_REVISION2=-r$MINGW_W64_REVISION
         MINGW_W64_REVISION=@${MINGW_W64_REVISION}
     fi
     MINGW_W64_SRC=$SRC_DIR/mingw-w64-svn$MINGW_W64_REVISION2
     MINGW_W64_VERSION=svn
-else
-    MINGW_W64_SRC=$SRC_DIR/mingw-w64-$MINGW_W64_VERSION
+fi
+
+if [ -z "$MINGW_W64_REVISION" ] ; then
+    # Released versions of MinGW-w64 don't provide easily accessible information
+    # about the svn revision which this script needs to know.
+    fail_panic "Building MinGW-w64 toolchain requires specifying an svn version"
 fi
 
 if [ ! -d $MINGW_W64_SRC ]; then
-    if [ "$MINGW_W64_VERSION" = "svn" ];  then
-        echo "Checking out https://mingw-w64.svn.sourceforge.net/svnroot/mingw-w64/trunk$MINGW_W64_REVISION $MINGW_W64_SRC"
-        run svn co https://mingw-w64.svn.sourceforge.net/svnroot/mingw-w64/trunk$MINGW_W64_REVISION $MINGW_W64_SRC
-    else
-        download_package http://downloads.sourceforge.net/project/mingw-w64/mingw-w64/mingw-w64-release/mingw-w64-$MINGW_W64_VERSION.tar.gz
-    fi
+    echo "Checking out https://mingw-w64.svn.sourceforge.net/svnroot/mingw-w64/trunk$MINGW_W64_REVISION $MINGW_W64_SRC"
+    run svn co https://mingw-w64.svn.sourceforge.net/svnroot/mingw-w64/trunk$MINGW_W64_REVISION $MINGW_W64_SRC
 fi
 
 PATCHES_DIR="$PROGDIR/toolchain-patches-host/mingw-w64"
 if [ -d "$PATCHES_DIR" ] ; then
     PATCHES=$(find "$PATCHES_DIR" -name "*.patch" | sort)
-    echo "Patching mingw-w64-$MINGW_W64_VERSION"
     for PATCH in $PATCHES; do
+        echo "Patching mingw-w64-$MINGW_W64_REVISION with $PATCH"
         (cd $MINGW_W64_SRC && run patch -p0 < $PATCH)
     done
 fi
@@ -511,18 +527,44 @@
 
 build_host_package binutils-$BINUTILS_VERSION $BINUTILS_CONFIGURE_OPTIONS
 
+build_mingw_tools ()
+{
+    local PKGNAME=$1
+    echo "$STAMP_DIR/$PKGNAME"
+    if [ ! -f "$STAMP_DIR/$PKGNAME" ]; then
+        (
+            mkdir -p $BUILD_DIR/$PKGNAME &&
+            cd $BUILD_DIR/$PKGNAME &&
+            log "$PKGNAME: Configuring" &&
+            run $MINGW_W64_SRC/mingw-w64-tools/widl/configure --prefix=$INSTALL_DIR --target=$TARGET_TAG
+            fail_panic "Can't configure mingw-64-tools"
+            log "$PKGNAME: Installing" &&
+            run make install -j$JOBS
+        ) || exit 1
+        touch $STAMP_DIR/$PKGNAME
+    fi
+}
+
 # Install the right mingw64 headers into the sysroot
 build_mingw_headers ()
 {
     local PKGNAME=$1
     if [ ! -f "$STAMP_DIR/$PKGNAME" ]; then
         (
+            # If --with-widl only identifies the program name (svn version dependent)...
+            if [ $(basename "$WITH_WIDL") = "$WITH_WIDL" ] ; then
+                # ...then need to add the right path too.
+                export PATH=$PATH:$INSTALL_DIR/bin
+            fi
+            fail_panic "Can't find widl"
             mkdir -p $BUILD_DIR/$PKGNAME &&
             cd $BUILD_DIR/$PKGNAME &&
             log "$PKGNAME: Configuring" &&
-            run $MINGW_W64_SRC/mingw-w64-headers/configure --prefix=$INSTALL_DIR --host=$TARGET_TAG --build=$HOST_TAG
+            run $MINGW_W64_SRC/mingw-w64-headers/configure --prefix=$PREFIX_FOR_TARGET --host=$TARGET_TAG \
+                --build=$HOST_TAG --with-widl=$WITH_WIDL --enable-sdk=all
             fail_panic "Can't configure mingw-64-headers"
 
+            run make
             log "$PKGNAME: Installing" &&
             run make install -j$JOBS &&
             run cd $INSTALL_DIR && 
@@ -625,11 +667,13 @@
 var_append GCC_CONFIGURE_OPTIONS "--enable-languages=c,c++"
 var_append GCC_CONFIGURE_OPTIONS "--with-sysroot=$INSTALL_DIR"
 
+# A bug in MinGW-w64 forces us to build and use widl.
+build_mingw_tools mingw-w64-tools
 build_mingw_headers mingw-w64-headers
 
 build_core_gcc gcc-$GCC_VERSION $GCC_CONFIGURE_OPTIONS
 
-CRT_CONFIGURE_OPTIONS="--host=$TARGET_TAG --with-sysroot=$INSTALL_DIR --prefix=$INSTALL_DIR"
+CRT_CONFIGURE_OPTIONS="--host=$TARGET_TAG --with-sysroot=$INSTALL_DIR --prefix=$PREFIX_FOR_TARGET"
 if [ "$TARGET_MULTILIBS" ]; then
     var_append CRT_CONFIGURE_OPTIONS "--enable-lib32"
 fi
diff --git a/build/tools/toolchain-patches-host/mingw-w64/mingw-w64-widl.patch b/build/tools/toolchain-patches-host/mingw-w64/mingw-w64-widl.patch
new file mode 100644
index 0000000..4d9d15c
--- /dev/null
+++ b/build/tools/toolchain-patches-host/mingw-w64/mingw-w64-widl.patch
@@ -0,0 +1,91 @@
+Index: mingw-w64-headers/Makefile.am
+===================================================================
+--- mingw-w64-headers/Makefile.am	(revision 5578)
++++ mingw-w64-headers/Makefile.am	(working copy)
+@@ -118,10 +118,14 @@
+ BUILT_SOURCES = $(IDL_SRCS:.idl=.h)
+ 
+ .idl.h: crt/_mingw.h
+-	$(WIDL) -DBOOL=WINBOOL -I$(srcdir)/include -I$(srcdir)/direct-x/include -Icrt -I$(srcdir)/crt -h -o $(srcdir)/$@ $<
++	$(WIDL) -DBOOL=WINBOOL -I$(srcdir)/include -I$(srcdir)/direct-x/include -Icrt -I$(srcdir)/crt -h -o $@ $<
+ 
++if SRCDIR_NEQ_BUILDDIR
++$(warning "srcdir != builddir, debugging comments in idl files will be sub-optimal")
+ endif
+ 
++endif
++
+ _mingw_directx.h: $(srcdir)/crt/sdks/_mingw_directx.h.in
+ 	$(SED) s/MINGW_HAS_DX$$/@MINGW_HAS_DX@/ $< > $@
+ 
+Index: mingw-w64-headers/Makefile.in
+===================================================================
+--- mingw-w64-headers/Makefile.in	(revision 5578)
++++ mingw-w64-headers/Makefile.in	(working copy)
+@@ -1016,8 +1016,10 @@
+ 	find $(distdir) -name ".svn" -type d -delete
+ 
+ @HAVE_WIDL_TRUE@.idl.h: crt/_mingw.h
+-@HAVE_WIDL_TRUE@	$(WIDL) -DBOOL=WINBOOL -I$(srcdir)/include -I$(srcdir)/direct-x/include -Icrt -I$(srcdir)/crt -h -o $(srcdir)/$@ $<
++@HAVE_WIDL_TRUE@	$(WIDL) -DBOOL=WINBOOL -I$(srcdir)/include -I$(srcdir)/direct-x/include -Icrt -I$(srcdir)/crt -h -o $@ $<
+ 
++@HAVE_WIDL_TRUE@@SRCDIR_NEQ_BUILDDIR_TRUE@$(warning "srcdir != builddir, debugging comments in idl files will be sub-optimal")
++
+ _mingw_directx.h: $(srcdir)/crt/sdks/_mingw_directx.h.in
+ 	$(SED) s/MINGW_HAS_DX$$/@MINGW_HAS_DX@/ $< > $@
+ 
+Index: mingw-w64-headers/configure
+===================================================================
+--- mingw-w64-headers/configure	(revision 5578)
++++ mingw-w64-headers/configure	(working copy)
+@@ -617,6 +617,8 @@
+ build_vendor
+ build_cpu
+ build
++SRCDIR_NEQ_BUILDDIR_FALSE
++SRCDIR_NEQ_BUILDDIR_TRUE
+ MAINT
+ MAINTAINER_MODE_FALSE
+ MAINTAINER_MODE_TRUE
+@@ -2310,6 +2312,16 @@
+ 
+ 
+ 
++# Check so we can warn about this.
++ if test ! "$srcdir" = "$builddir"; then
++  SRCDIR_NEQ_BUILDDIR_TRUE=
++  SRCDIR_NEQ_BUILDDIR_FALSE='#'
++else
++  SRCDIR_NEQ_BUILDDIR_TRUE='#'
++  SRCDIR_NEQ_BUILDDIR_FALSE=
++fi
++
++
+ # Make sure we can run config.sub.
+ $SHELL "$ac_aux_dir/config.sub" sun4 >/dev/null 2>&1 ||
+   as_fn_error $? "cannot run $SHELL $ac_aux_dir/config.sub" "$LINENO" 5
+@@ -2831,6 +2843,10 @@
+   as_fn_error $? "conditional \"MAINTAINER_MODE\" was never defined.
+ Usually this means the macro was only invoked conditionally." "$LINENO" 5
+ fi
++if test -z "${SRCDIR_NEQ_BUILDDIR_TRUE}" && test -z "${SRCDIR_NEQ_BUILDDIR_FALSE}"; then
++  as_fn_error $? "conditional \"SRCDIR_NEQ_BUILDDIR\" was never defined.
++Usually this means the macro was only invoked conditionally." "$LINENO" 5
++fi
+ if test -z "${HAVE_WIDL_TRUE}" && test -z "${HAVE_WIDL_FALSE}"; then
+   as_fn_error $? "conditional \"HAVE_WIDL\" was never defined.
+ Usually this means the macro was only invoked conditionally." "$LINENO" 5
+Index: mingw-w64-headers/configure.ac
+===================================================================
+--- mingw-w64-headers/configure.ac	(revision 5578)
++++ mingw-w64-headers/configure.ac	(working copy)
+@@ -10,6 +10,9 @@
+ AM_INIT_AUTOMAKE([foreign])
+ AM_MAINTAINER_MODE
+ 
++# Check so we can warn about this.
++AM_CONDITIONAL([SRCDIR_NEQ_BUILDDIR],[test ! "$srcdir" = "$builddir"])
++
+ AC_CANONICAL_HOST
+ 
+ # Checks for programs.