scons: don't compile some files with -gstabs if using mingw32

Compiling some (large) files with i686-pc-mingw32-gcc 4.2.2 (at least)
and the -gstabs option triggers a compiler error.  Use this work-around
to simply compile the effected files without -gstabs.
diff --git a/scons/crossmingw.py b/scons/crossmingw.py
index cc04622..4a695a4 100644
--- a/scons/crossmingw.py
+++ b/scons/crossmingw.py
@@ -128,6 +128,42 @@
                                     source_scanner=SCons.Tool.SourceFileScanner)
 SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan)
 
+
+
+def compile_without_gstabs(env, sources, c_file):
+    '''This is a hack used to compile some source files without the
+    -gstabs option.
+
+    It seems that some versions of mingw32's gcc (4.4.2 at least) die
+    when compiling large files with the -gstabs option.  -gstabs is
+    related to debug symbols and can be omitted from the effected
+    files.
+
+    This function compiles the given c_file without -gstabs, removes
+    the c_file from the sources list, then appends the new .o file to
+    sources.  Then return the new sources list.
+    '''
+
+    # Modify CCFLAGS to not have -gstabs option:
+    env2 = env.Clone()
+    flags = str(env2['CCFLAGS'])
+    flags = flags.replace("-gstabs", "")
+    env2['CCFLAGS'] = SCons.Util.CLVar(flags)
+    
+    # Build the special-case files:
+    obj_file = env2.SharedObject(c_file)
+
+    # Replace ".cpp" or ".c" with ".o"
+    o_file = c_file.replace(".cpp", ".o")
+    o_file = o_file.replace(".c", ".o")
+
+    # Replace the .c files with the specially-compiled .o file
+    sources.remove(c_file)
+    sources.append(o_file)
+
+    return sources
+
+
 def generate(env):
     mingw_prefix = find(env)
 
@@ -197,5 +233,7 @@
     # Avoid depending on gcc runtime DLLs
     env.AppendUnique(LINKFLAGS = ['-static-libgcc'])
 
+    env.AddMethod(compile_without_gstabs, 'compile_without_gstabs')
+
 def exists(env):
     return find(env)