Index: libffi/ChangeLog
===================================================================
--- libffi.orig/ChangeLog
+++ libffi/ChangeLog
@@ -201,6 +201,13 @@
 	* testsuite/libffi.call/cls_longdouble.c: Likewise.
 	* testsuite/libffi.call/huge_struct.c: Likewise.
 
+2010-08-05  Dan Witte  <dwitte@mozilla.com>
+
+        * Makefile.am: Pass FFI_DEBUG define to msvcc.sh for linking to the
+        debug CRT when --enable-debug is given.
+        * configure.ac: Define it.
+        * msvcc.sh: Translate -g and -DFFI_DEBUG appropriately.
+
 2010-08-04  Dan Witte  <dwitte@mozilla.com>
 
 	* src/x86/ffitarget.h: Add X86_ANY define for all x86/x86_64
Index: libffi/Makefile.am
===================================================================
--- libffi.orig/Makefile.am
+++ libffi/Makefile.am
@@ -180,6 +180,14 @@ libffi_convenience_la_SOURCES = $(libffi
 nodist_libffi_convenience_la_SOURCES = $(nodist_libffi_la_SOURCES)
 
 AM_CFLAGS = -Wall -g -fexceptions
+if FFI_DEBUG
+# Build debug. Define FFI_DEBUG on the commandline so that, when building with
+# MSVC, it can link against the debug CRT.
+AM_CFLAGS += -DFFI_DEBUG
+else
+# Build opt.
+AM_CFLAGS += -O2
+endif
 
 libffi_la_LDFLAGS = -version-info `grep -v '^\#' $(srcdir)/libtool-version` $(LTLDFLAGS) $(AM_LTLDFLAGS)
 
Index: libffi/configure.ac
===================================================================
--- libffi.orig/configure.ac
+++ libffi/configure.ac
@@ -16,10 +16,14 @@ AM_INIT_AUTOMAKE
 # We must force CC to /not/ be precious variables; otherwise
 # the wrong, non-multilib-adjusted value will be used in multilibs.
 # As a side effect, we have to subst CFLAGS ourselves.
+# Also save and restore CFLAGS, since AC_PROG_CC will come up with
+# defaults of its own if none are provided.
 
 m4_rename([_AC_ARG_VAR_PRECIOUS],[real_PRECIOUS])
 m4_define([_AC_ARG_VAR_PRECIOUS],[])
+save_CFLAGS=$CFLAGS
 AC_PROG_CC
+CFLAGS=$save_CFLAGS
 m4_undefine([_AC_ARG_VAR_PRECIOUS])
 m4_rename([real_PRECIOUS],[_AC_ARG_VAR_PRECIOUS])
 
@@ -400,6 +404,7 @@ AC_ARG_ENABLE(debug,
   if test "$enable_debug" = "yes"; then
     AC_DEFINE(FFI_DEBUG, 1, [Define this if you want extra debugging.])
   fi)
+AM_CONDITIONAL(FFI_DEBUG, test "$enable_debug" = "yes")
 
 AC_ARG_ENABLE(structs,
 [  --disable-structs       omit code for struct support],
Index: libffi/Makefile.in
===================================================================
--- libffi.orig/Makefile.in
+++ libffi/Makefile.in
@@ -62,6 +62,11 @@ target_triplet = @target@
 @SH64_TRUE@am__append_24 = src/sh64/sysv.S src/sh64/ffi.c
 @PA_LINUX_TRUE@am__append_25 = src/pa/linux.S src/pa/ffi.c
 @PA_HPUX_TRUE@am__append_26 = src/pa/hpux32.S src/pa/ffi.c
+# Build debug. Define FFI_DEBUG on the commandline so that, when building with
+# MSVC, it can link against the debug CRT.
+@FFI_DEBUG_TRUE@am__append_27 = -DFFI_DEBUG
+# Build opt.
+@FFI_DEBUG_FALSE@am__append_28 = -O2
 subdir = .
 DIST_COMMON = README $(am__configure_deps) $(srcdir)/Makefile.am \
 	$(srcdir)/Makefile.in $(srcdir)/doc/stamp-vti \
@@ -489,7 +494,7 @@ nodist_libffi_la_SOURCES = $(am__append_
 	$(am__append_24) $(am__append_25) $(am__append_26)
 libffi_convenience_la_SOURCES = $(libffi_la_SOURCES)
 nodist_libffi_convenience_la_SOURCES = $(nodist_libffi_la_SOURCES)
-AM_CFLAGS = -Wall -g -fexceptions
+AM_CFLAGS = -Wall -g -fexceptions $(am__append_27) $(am__append_28)
 libffi_la_LDFLAGS = -version-info `grep -v '^\#' $(srcdir)/libtool-version` $(LTLDFLAGS) $(AM_LTLDFLAGS)
 AM_CPPFLAGS = -I. -I$(top_srcdir)/include -Iinclude -I$(top_srcdir)/src
 AM_CCASFLAGS = $(AM_CPPFLAGS)
Index: libffi/configure
===================================================================
--- libffi.orig/configure
+++ libffi/configure
@@ -617,6 +617,8 @@ LTLIBOBJS
 LIBOBJS
 toolexeclibdir
 toolexecdir
+FFI_DEBUG_FALSE
+FFI_DEBUG_TRUE
 TARGETDIR
 TARGET
 HAVE_LONG_DOUBLE
@@ -3123,9 +3125,12 @@ am__tar='${AMTAR} chof - "$$tardir"'; am
 # We must force CC to /not/ be precious variables; otherwise
 # the wrong, non-multilib-adjusted value will be used in multilibs.
 # As a side effect, we have to subst CFLAGS ourselves.
+# Also save and restore CFLAGS, since AC_PROG_CC will come up with
+# defaults of its own if none are provided.
 
 
 
+save_CFLAGS=$CFLAGS
 ac_ext=c
 ac_cpp='$CPP $CPPFLAGS'
 ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
@@ -4105,6 +4110,7 @@ else
 fi
 
 
+CFLAGS=$save_CFLAGS
 
 
 
@@ -13335,6 +13341,14 @@ $as_echo "#define FFI_DEBUG 1" >>confdef
   fi
 fi
 
+ if test "$enable_debug" = "yes"; then
+  FFI_DEBUG_TRUE=
+  FFI_DEBUG_FALSE='#'
+else
+  FFI_DEBUG_TRUE='#'
+  FFI_DEBUG_FALSE=
+fi
+
 
 # Check whether --enable-structs was given.
 if test "${enable_structs+set}" = set; then :
@@ -13649,6 +13663,10 @@ if test -z "${PA64_HPUX_TRUE}" && test -
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
 fi
 
+if test -z "${FFI_DEBUG_TRUE}" && test -z "${FFI_DEBUG_FALSE}"; then
+  as_fn_error $? "conditional \"FFI_DEBUG\" was never defined.
+Usually this means the macro was only invoked conditionally." "$LINENO" 5
+fi
 
 : "${CONFIG_STATUS=./config.status}"
 ac_write_fail=0
