From 6aec5ed01c4d4c41d1b94ae3f275d788b734719e Mon Sep 17 00:00:00 2001 From: Steven Noonan Date: Sun, 18 Oct 2009 03:29:19 -0700 Subject: AUTO_OPT: add a bit of flexibility for future porting, Mac OS X support Signed-off-by: Steven Noonan --- crawl-ref/source/makefile | 39 +++++++------ crawl-ref/source/util/auto-opt.sh | 50 ----------------- crawl-ref/source/util/gcc-opt-x86.pl | 104 +++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 70 deletions(-) delete mode 100755 crawl-ref/source/util/auto-opt.sh create mode 100755 crawl-ref/source/util/gcc-opt-x86.pl diff --git a/crawl-ref/source/makefile b/crawl-ref/source/makefile index 03557f4a4f..461e9bd05c 100644 --- a/crawl-ref/source/makefile +++ b/crawl-ref/source/makefile @@ -28,25 +28,7 @@ GAME = crawl # These are really good options for the Intel C++ compiler: #CFOPTIMIZE := -O2 -parallel -# Define this to automatically generate code optimized for your machine -# (GCC only as of now). -# -# NOTE: Don't use this with a release build, since the generated code -# won't work for all machines. -ifdef AUTO_OPT_GCC - -ifdef CROSSHOST -error Can not do AUTO_OPT_GCC with CROSSHOST -endif - -ifdef HURRY -error Can not do AUTO_OPT_GCC with HURRY -endif - -AUTO_OPT_FLAGS := $(shell util/auto-opt.sh) -endif - -CFOPTIMIZE := -O2 $(AUTO_OPT_FLAGS) +CFOPTIMIZE := -O2 CFOTHERS := -fno-strict-aliasing -pipe CFOTHERS_L := -fsigned-char CFWARN := -Wall @@ -93,6 +75,7 @@ uname_V := $(shell sh -c 'uname -v 2>/dev/null || echo not') ifdef CROSSHOST NO_PKGCONFIG = YesPlease NO_SDLCONFIG = YesPlease + NO_AUTO_OPT = YesPlease NEED_STATIC = YesPlease CONFIGURE_FLAGS += --host=$(CROSSHOST) SDLCONFIG := $(shell pwd)/contrib/install/bin/sdl-config @@ -214,8 +197,24 @@ GXX := $(CROSSHOST)-g++ AR := $(CROSSHOST)-ar RANLIB := $(CROSSHOST)-ranlib endif -GCC_GTE_4_3_0 := $(shell util/gcc-gte.pl $(GCC) 4.3.0) GCC_GTE_4_0_0 := $(shell util/gcc-gte.pl $(GCC) 4.0.0) +GCC_GTE_4_2_0 := $(shell util/gcc-gte.pl $(GCC) 4.2.0) +GCC_GTE_4_3_0 := $(shell util/gcc-gte.pl $(GCC) 4.3.0) + +# Define this to automatically generate code optimized for your machine +# (GCC only as of now). +# +# NOTE: Don't use this with a release build, since the generated code +# won't work for all machines. +ifdef HURRY +NO_AUTO_OPT = YesPlease +endif + +ifdef AUTO_OPT +ifndef NO_AUTO_OPT +CFOPTIMIZE += $(shell util/gcc-opt.pl $(GCC)) +endif +endif RLTILES = rltiles diff --git a/crawl-ref/source/util/auto-opt.sh b/crawl-ref/source/util/auto-opt.sh deleted file mode 100755 index 8f4aa185cb..0000000000 --- a/crawl-ref/source/util/auto-opt.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash --noprofile - -######################################################################### -# Called by the makefile if AUTO_OPT_GCC is true, outputs the list of CPU -# specific optimization flags that should be used. -######################################################################### - -#TODO: Detect extra compiler options for: -# -# * Non-gcc compilers. -# * CYGWIN. -# * Mac OS X. -# * Non-x86 Linux systems. - -######################################################################### - -# This line makes GCC automatically detect the CPU architecture to use. Also -# might cause the use of some of the "-m" options we use down below, -# but GCC doesn't complain about an "-m" option being used repeatedly. -OUT="-mtune=native -march=native" - -if [ -f /proc/cpuinfo ]; then - # On Linux system, the "flags: " line of /proc/cpuinfo indicates which - # extended instruction sets are available (among other things), so we - # can use that to detect what to use. - - # Only get info for the first CPU; assumes that all CPUs in the system - # are the same - INFO=`cat /proc/cpuinfo | grep '^flags.*:' | head -n 1` - - # These are currently all x86 specific. - for flag in cx16 mmx sse sse2 sse3 sse4.1 sse4.2 sse4 sse4a 3dnow abm; do - # Put a space on either side of the flag name, so that it won't - # match in the middle of a flag. - if [[ $INFO == *\ $flag\ * ]] ; then - OUT="$OUT -m$flag" - fi - done - - if [[ $INFO == *\ lahf_lm\ * ]]; then - OUT="$OUT -msahf" - fi - - # Any available SSE lets us use -mfpmath=sse - if [[ $INFO == *\ sse* ]]; then - OUT="$OUT -mfpmath=sse" - fi -fi - -echo "$OUT" diff --git a/crawl-ref/source/util/gcc-opt-x86.pl b/crawl-ref/source/util/gcc-opt-x86.pl new file mode 100755 index 0000000000..808eecac2d --- /dev/null +++ b/crawl-ref/source/util/gcc-opt-x86.pl @@ -0,0 +1,104 @@ +#!/usr/bin/perl +# +# GCC optimization flag generator +# + +use strict; +use warnings; + +my $gcc = $ARGV[0]; +my $cpuinfo = $ARGV[1]; + +if ( ! $gcc ) { + die "Can't generate optimization flags (no compiler specified)\n"; +} + +if ( ! `which $gcc 2> /dev/null` ) { + die "Can't generate optimization flags ($gcc is missing?)\n"; +} + +# +# We allow people to provide their own cpuinfo-compatible +# descriptor. This may be useful later if we want to have +# specific architecture-specific optimizations for release +# builds or something. +# +if ( ! $cpuinfo ) { + $cpuinfo = "/proc/cpuinfo"; +} + +my %features; +my $family; +my $uname_S = `uname -s`; + +# +# Collect CPU feature list +# + +# The Linux way +if ( -e $cpuinfo ) { + open(FH, "< $cpuinfo"); + my @cpuinfo = ; + close FH; + + my @familyline = grep(/^cpu family/, @cpuinfo); + $familyline[0] =~ s/^cpu[ ]family[ \t]*[:][ ]*//; + $family = $familyline[0]; + + my @flags = grep(/^flags/, @cpuinfo); + $flags[0] =~ s/^flags[ \t]*[:][ \t]*//; + @flags = split(' ',$flags[0]); + foreach (@flags) { + $features{$_} = 1; + } +} + +# The Mac OS X way +if ( $uname_S =~ /^Darwin/ ) { + # All released Intel macs have CMOV, and + # sysctl doesn't provide this one. + $features{"cmov"} = 1; + + $features{"mmx"} = `sysctl -n hw.optional.mmx`; + $features{"sse"} = `sysctl -n hw.optional.sse`; + $features{"sse2"} = `sysctl -n hw.optional.sse2`; + $features{"pni"} = `sysctl -n hw.optional.sse3`; + $features{"ssse3"} = `sysctl -n hw.optional.supplementalsse3`; +} + +# +# Check the minimum march/mtune value +# +my $march = "i386"; +my $fpmath; + +if ( $features{"cmov"} && $features{"mmx"} ) { + $march = "pentium2"; +} +if ( $features{"sse"} ) { + $march = "pentium3"; + $fpmath = "sse" +} +if ( $features{"sse2"} ) { + $march = "pentium-m"; +} +if ( $features{"pni"} ) { + $march = "prescott"; +} + +# It's important to specify 'march=pentium4' for the +# Pentium 4, because it has vastly different optimization +# rules than other x86 processors. What's optimal for the +# Pentium 3 is extremely sub-optimal for the Pentium 4. +if ( $family == 15 ) { + $march = "pentium4"; +} + +print "-march=$march -mtune=$march"; +if ( $fpmath ) { + print " -mfpmath=$fpmath"; +} + +print "\n"; + +exit 0 -- cgit v1.2.3-54-g00ecf