#!/bin/sh
# $NetBSD: cpuflags.NetBSD,v 1.34 2003/04/22 08:01:37 abs Exp $
# ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-current/pkgsrc/devel/cpuflags/files/cpuflags.NetBSD

if [ -x /sbin/sysctl ] ;then
    SYSCTL=/sbin/sysctl
else
    SYSCTL=/usr/sbin/sysctl	# NetBSD 1.3
fi

hw_machine=`$SYSCTL -n hw.machine`
hw_machine_arch=`$SYSCTL -n hw.machine_arch`
hw_model=`$SYSCTL -n hw.model`

# We're almost certainly crosscompiling
if [ -n "$MACHINE" -a $hw_machine != "$MACHINE" ]; then
    echo
    exit
fi

case $hw_machine_arch in

    alpha)
	# cpu0 at mainbus0: ID 0 (primary), 21164A-0 (unknown ...
	case "`egrep '^cpu0 ' /var/run/dmesg.boot`" in
	    *[\(\ ]2106[46][-A\ \)]*)	FLAGS="-mcpu=21064"	;;
	    *[\(\ ]21164[-\ \)]*)	FLAGS="-mcpu=21164"	;;
	    *[\(\ ]21164A[-\ \)]*)	FLAGS="-mcpu=21164a"	;;
	    *[\(\ ]21264B[-\ \)]*)	FLAGS="-mcpu=21264"	;;
	esac
	;;

    arm | arm32) case $hw_model in
	ARM610*)		FLAGS="-mcpu=arm610"		;; # risc pc
	ARM710*)		FLAGS="-mcpu=arm710"		;; # risc pc
	SA-110*)	
	    case $hw_machine in			 # arm32 split post 1.5
		cats|shark|hpcarm|netwinder)
		    FLAGS="-mcpu=strongarm110" ;;
		acorn32)
		    FLAGS="-march=armv3m -mtune=strongarm" ;;
		*)
		    # memorybus in strongarm risc pc machines cannot support
		    # certain strongarm instructions, but in 1.5 and earlier
		    # all strongarm machines are 'arm32', so uname and sysctl
		    # no use
		    if egrep -q 'ofbus0|footbridge0' /var/run/dmesg.boot \
							    2>/dev/null ; then
			FLAGS="-mcpu=strongarm110"		   # shark/cats
		    else
			FLAGS="-march=armv3m -mtune=strongarm"	   # risc pc
		    fi
	    esac ;;
	esac ;;

    i386) case $hw_model in
	*386-class*)		FLAGS='-march=i386'		;;
	*486-class*)		FLAGS='-march=i486'		;;
	*AMD\ K6*)		FLAGS='-march=k6'		;;
	*586-class*)		FLAGS='-march=pentium'		;;
	*686-class*)		FLAGS='-march=pentiumpro'	;;
    esac ;;

    m68k) case $hw_model in				   # Examples
	*\(68020*)		FLAGS='-m68020'		;; # Untested
	*\(68030*)		FLAGS='-m68030'		;; # Mac LC III
	*\(68040*)		FLAGS='-m68040'		;; # Untested
	*\(68060*)		FLAGS='-m68060'		;; # Upgr amiga 3000
    esac ;;

    mipseb|mipsel)
	# cpu0 at mainbus0: QED R4600 Orion CPU (0x2020) Rev. 2.0 with ...
	case "`egrep '^cpu0 ' /var/run/dmesg.boot`" in
	    *\ MIPS\ R2000\ *)	FLAGS="-mcpu=r2000"	   ;;
	    *\ MIPS\ R3000\ *)	FLAGS="-mcpu=r3000"	   ;;
	    *\ MIPS\ R3000A\ *)	FLAGS="-mcpu=r3000"	   ;;
	    *\ Toshiba\ TX3912\ *)    FLAGS="-mcpu=r3900"  ;;
	    *\ Toshiba\ TX392[27]\ *) FLAGS="-mcpu=r3900"  ;;
	    *\ MIPS\ R4000\ *)	FLAGS="-mcpu=r4000 -mips2" ;; # really mips3
	    *\ MIPS\ R4400\ *)	FLAGS="-mcpu=r4400 -mips2" ;; # really mips3
	    *\ NEC\ VR4100\ *)	FLAGS="-mcpu=r4100 -mips2" ;; # really mips3
	    *\ NEC\ VR4300\ *)	FLAGS="-mcpu=r4300 -mips2" ;; # really mips3
	    *\ QED\ R4600\ *)	FLAGS="-mcpu=r4600 -mips2" ;; # really mips3
	    *\ MIPS\ R5000\ *)	FLAGS="-mcpu=r5000 -mips2" ;; # really mips4
	    *\ QED\ RM5200\ *)	FLAGS="-mcpu=r5000 -mips2" ;; # really mips4
	    *\ MIPS\ R6000\ *)	FLAGS="-mcpu=r6000 -mips2" ;;
	    *\ MIPS\ R8000\ *)	FLAGS="-mcpu=r8000 -mips2" ;; # really mips4
	esac
	;;

    powerpc) case $hw_model in				   # Examples
	601\ *)		   	FLAGS='-mcpu=601'	;; # Untested
	602\ *)		   	FLAGS='-mcpu=602'	;; # Untested
	603\ *)		   	FLAGS='-mcpu=603'	;; # Untested
	603ev\ *)		FLAGS='-mcpu=603e'	;; # PM 4400
	604\ *)		   	FLAGS='-mcpu=604'	;; # Mac 8500
	604ev\ *)		FLAGS='-mcpu=604e'	;; # upgr B&W G3
	7450\ *)	   	FLAGS='-mcpu=740'	;; # tibook 550
	7400\ *)	   	FLAGS='-mcpu=740'	;; # AGP G4/400 Mac
	750\ *)		   	FLAGS='-mcpu=750'	;; # Untested
	740\ *)		   	FLAGS='-mcpu=740'	;; # Untested
	620\ *)		   	FLAGS='-mcpu=620'	;; # Untested
    esac ;;

    sparc | sparc64) case " $hw_model" in			   # Examples
	*[\ \(]MB86900/1A*)	FLAGS='-mcpu=cypress'		;; # ss1+
	*[\ \(]CY7C601*)	FLAGS='-mcpu=cypress'		;; # ss2
	*[\ \(]W8601/8701*)	FLAGS='-mcpu=cypress'		;; # elc
	*[\ \(]MB86904*)	FLAGS='-mcpu=supersparc'	;; # ss5 usparc
	*[\ \(]MB86907*)	FLAGS='-mcpu=supersparc'	;; # ss5 usparc
	*[\ \(]TMS390S10*)	FLAGS='-mcpu=supersparc'	;; # classic  "
	*[\ \(]TMS390Z50*)	FLAGS='-mcpu=supersparc'	;; # ss10/ss20
	*[\ \(]RT620/625*)	FLAGS='-mcpu=supersparc'	;; # ss20 ross
	*[\ \(]MB86930*)	FLAGS='-mcpu=sparclite'		;; # from gcc
	*[\ \(]MB86934*)	FLAGS='-mcpu=sparclite'		;; # from gcc
	# under 1.5.1 -mcpu=ultrasparc chokes egcs-2.91.66 compiling perl
	*[\ \(]SUNW,UltraSPARC*) FLAGS='-mcpu=v9'		;; # Ultra
    esac ;;

    vax) # No VAX specific gcc optimisations available
	NONE=1
	;;

esac

if [ -z "$FLAGS" -a -z "$NONE" ] ; then
    echo 'Unknown machine - please send details to abs@netbsd.org'	>&2
    echo "  hw.model        : '$hw_model'"				>&2
    echo "  hw.machine      : '$hw_machine'"				>&2
    echo "  hw.machine_arch : '$hw_machine_arch'"			>&2
    echo "  cpu details     :"						>&2
    egrep '^cpu' /var/run/dmesg.boot 					>&2
fi

# Fixup flags for old gcc
if [ -n "$FLAGS" ]; then
    gcc_ver=`gcc -v 2>&1 | awk '/gcc version/ {sub("egcs-","");print $3}'`
    FLAGS=`awk -v "flags=$FLAGS" -v "gcc_ver=$gcc_ver" '
	{if (gcc_ver < $1){map[$2] = ""$3}}
	END{if (flags in map) {print map[flags]}else {print flags}}
	' <<EOD
2.90	-mcpu=21164a	
2.90	-march=i386		-mno-486
2.90	-march=i486		-m486
2.90	-march=pentium		-m486
2.90	-march=pentiumpro	-m486
2.90	-mcpu=supersparc	-msupersparc
2.90	-mcpu=sparclite		-msparclite
2.90	-mcpu=cypress		-mcypress
2.90	-mcpu=v9		-mv8
2.90	-mcpu=arm610		-m6
2.90	-mcpu=strongarm110	-m6
2.90	-mcpu=arm710		-m6
2.95	-march=k6		-march=pentium
EOD
`
fi

echo $FLAGS

exit 0
