#!/bin/sh 
# try guessing CPU type and flags suitable for compiler version
# so far usefull only for linux i386 and m68k

decho()
{
    echo "$*" 1>&2
}

[ -f conf.log ] && rm -f conf.log
touch conf.log

## gcc version
## FIXME: use 'gcc -dumpversion'


[ -n "$EXACT_HW" ] && decho "exact hw emulation requested"

if [ -z "$CC" ] ; then
    decho "CC not set, using gcc"
    CC=gcc
fi
case $CC in
    *gcc*|*egcs*) ;;
    *) decho "CC=$CC not supported, trying gcc"
       CC=gcc
esac

#$CC -v 2> .gcc_version

#GCC_VER=` sed -n 's/^.*gcc version //p' .gcc_version`
##echo "GCC_VER = $GCC_VER"  1>&2
#GCC_VER=`echo $GCC_VER| sed -n 's/^[a-zA-Z -]*\([0-9.]*\).*/\1/p' `
##echo "GCC_VER = $GCC_VER"  1>&2

#GCC_VER=`cat .gcc_version`

GCC_VER=`$CC -dumpversion`
GCC_VER=${GCC_VER##egcs-}

#echo "gcc version $GCC_VER" 1>&2
decho "gcc version $GCC_VER"

case $GCC_VER in
	2.6*) decho "gcc version $GCC_VER - are you sure you don't want to update it?"
	      OLDFLAGS="y" ;;
	2.7*) OLDFLAGS="y" 
	       echo "GCC_XFLAGS+= -DNO_REGP"
	    ;;
	2.8*) ;;
	2.91.*) ## echo "GCC_XFLAGS+= -DUSE_AREGP" 
	    ;; # even 2.91.66 seems broken :(
	
	2.95.*) ;;
	3.0*) 
	       echo "GCC_XFLAGS+= -DUSE_BUILTIN_EXPECT"
	    ;;
	3.1*|3.2*|3.3*) 
	       echo "GCC_XFLAGS+= -DUSE_BUILTIN_EXPECT"
	       echo "GCC_XFLAGS+= -DBROKEN_GREGS"
	       decho "gcc with broken global register variables,"
	       decho "disable GREGS, see README"
	    ;;
	3.4*) 
	       echo "GCC_XFLAGS+= -DUSE_BUILTIN_EXPECT"
	    ;;
	2.96*)
	    decho "gcc $GCC_VER is unusable. Select another gcc version"
	    CC="/bin/false 'gcc-2.96 wont work'"
	    ;;
	4.0.2*)
	    # regp works;)
	    echo "GCC_XFLAGS+= -DUSE_BUILTIN_EXPECT"

	    ;;
	*) decho "gcc version $GCC_VER not known - using default settings";;
esac

#decho "OLDFLAGS is $OLDFLAGS"

decho "CC = $CC"
echo "CC = $CC"

ARCH=`uname -m`
if [ -f /proc/cpuinfo ] ; then
    CPU=`cat /proc/cpuinfo| sed -n 's/cpu.*: //p'`
    [ -z "$CPU" ] && CPU=`cat /proc/cpuinfo| sed -n 's/CPU[[:blank:]]*:[^[:alnum:]]*//p'`
fi

#decho "CPU type from /proc/cpuinfo: $CPU"

if [ -n "$DEF_CPU" ] ; then
    echo "using DEF_CPU=$DEF_CPU" 1>&2 ;
elif [ "$OLDFLAGS" = "y" ] ; then
    case $ARCH in
	    *486) DEF_CPU=-m486 ;;
	    *586) DEF_CPU=-m486 ;;  # gcc<2.8 doesn't know anything else..
	    *686) DEF_CPU=-m486 ;;
	    m68k) DEF_CPU=-m${CPU} ;;
	    *) decho "cannot determine CPU submodel automagically - this is not a problem, but you can try setting DEF_CPU for better performance"  ;;
    esac
else
    case $ARCH in
	    *486) DEF_CPU="-march=i486" ;;
	    *586) DEF_CPU="-march=pentium" ;;
	    *686) DEF_CPU="-march=pentiumpro" ;;
	    m68k) DEF_CPU=-m${CPU}
		case "$GCC_VER" in
			3.1*)
			    decho "WARNING - this gcc version may have a problem, see release notes in README"  ;;
		esac;;
	    *) 
		case "$GCC_VER" in
			3.1*|3.2*)
			    decho "WARNING - this gcc version may have a problem, see release notes in README"  ;;
		esac
		decho "cannot determine CPU submodel automagically - this is not a problem, but you can try setting DEF_CPU for better performance" ;;
    esac    
fi

if [ `uname` = Linux ] ; then
    case $ARCH in
	    *486) testvm=y ;;
	    *586) testvm=y ;;
            *686) testvm=y ;;
	  *m68k*) testvm=y ;;
	       *) GCC_XFLAGS=" -DEVM_SCR";;
    esac
fi


gcc -o vmtest vmtest.c 1>&2 2>>conf.log

if [ -n "$testvm" -a  -z "$EXACT_HW" ] ; then
    if ./vmtest 2>>conf.log ; then
	GCC_XFLAGS=" -DUSE_VM"
    else
	decho "vmtest failed, falling back to EVM_SCR" 
	decho "for m68k kernels get newest patch from rz@linux-m68k.org" 
	GCC_XFLAGS=" -DEVM_SCR";
    fi
else
    GCC_XFLAGS=" -DEVM_SCR";
fi

if ./vmtest 2>>conf.log; then
   gcc -o zmtest zmtest.c 1>&2 2>>conf.log
   if ./zmtest 2>>conf.log; then
        GCC_XFLAGS="$GCC_XFLAGS -DZEROMAP"
   else
        decho "zero mapping apparently not supprted"
   fi
fi

#rm .gcc_version

if [ -n "$DEF_CPU" ] ; then
    echo "DEF_CPU=$DEF_CPU"
    decho "adding cpu flag $DEF_CPU"
fi

if [ -n "$GCC_XFLAGS" ] ; then
    echo "GCC_XFLAGS+=$GCC_XFLAGS"
    decho "adding $GCC_XFLAGS"
fi

echo "QM_CONFIG_DONE=yes"
