[PATCH] Work around compiler bug

After upgrading my MinGW installation, TyrQuake started to crash in a very
strange way. Eventually, tracked it down to gcc's unit-at-a-time
optimisation. This patch disables unit-at-a-time for gcc earlier than
4.0. (Current MinGW is 3.4.5). Version checking script shamelessly stolen from
Linux.

diff -urN a/Makefile head/Makefile
--- a/Makefile	2006-02-19 07:54:33.000000000 +1030
+++ head/Makefile	2006-02-19 07:59:18.000000000 +1030
@@ -41,12 +41,6 @@
 	qwsv-linux-objs
 
 # ============================================================================
-# Helper functions
-# ============================================================================
-
-check_gcc = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
-
-# ============================================================================
 
 # FIXME - how to detect build env reliably...?
 ifeq ($(OSTYPE),msys)
@@ -55,6 +49,18 @@
 TOPDIR := $(shell pwd)
 endif
 
+# ============================================================================
+# Helper functions
+# ============================================================================
+
+cc-version = $(shell sh $(TOPDIR)/scripts/gcc-version \
+              $(if $(1), $(1), $(CC)))
+
+cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
+             > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
+
+GCC_VERSION := $(call cc-version)
+
 # ----------------------------
 # The two project directories
 # ----------------------------
@@ -237,12 +243,14 @@
 CFLAGS += -g
 STRIP_CMD = @echo "** Debug build - not stripping"
 else
-# Note that "-fomit-frame-pointer" seems to screw some things up
-# (at least on MinGW)
 CFLAGS += -O2
-CFLAGS += $(call check_gcc,-fweb,)
-CFLAGS += $(call check_gcc,-frename-registers,)
-CFLAGS += $(call check_gcc,-mtune=i686,-mcpu=i686)
+# -funit-at-a-time is buggy for MinGW GCC > 3.2
+# I'm assuming it's fixed for MinGW GCC >= 4.0 when that comes about
+CFLAGS += $(shell if [ $(GCC_VERSION) -lt 0400 ] ;\
+		then echo $(call cc-option,-fno-unit-at-a-time); fi ;)
+CFLAGS += $(call cc-option,-fweb,)
+CFLAGS += $(call cc-option,-frename-registers,)
+CFLAGS += $(call cc-option,-mtune=i686,-mcpu=i686)
 STRIP_CMD = strip
 endif
 
diff -urN a/scripts/gcc-version head/scripts/gcc-version
--- a/scripts/gcc-version	1970-01-01 09:30:00.000000000 +0930
+++ head/scripts/gcc-version	2006-02-18 20:36:46.000000000 +1030
@@ -0,0 +1,13 @@
+#!/bin/sh
+#
+# gcc-version gcc-command
+#
+# Prints the gcc version of `gcc-command' in a canonical 4-digit form
+# such as `0295' for gcc-2.95, `0303' for gcc-3.3, etc.
+#
+
+compiler="$*"
+
+MAJOR=$(echo __GNUC__ | $compiler -E -xc - | tail -n 1)
+MINOR=$(echo __GNUC_MINOR__ | $compiler -E -xc - | tail -n 1)
+printf "%02d%02d\\n" $MAJOR $MINOR
