Determine best optimization flags for host

We call sysctl to determine which exact Mac model we are running on and
optimize as well as possible.
This commit is contained in:
Max Howell 2009-07-31 16:06:14 +01:00
parent 055a694cd4
commit 7cb9c31f3e
4 changed files with 125 additions and 3 deletions

View File

@ -17,13 +17,38 @@
require 'osx/cocoa' # to get number of cores
require 'formula'
require 'hw.model'
ENV['MACOSX_DEPLOYMENT_TARGET']='10.5'
ENV['CFLAGS']='-O3 -w -pipe -fomit-frame-pointer -mmacosx-version-min=10.5'
ENV['LDFLAGS']='' # to be consistent, we ignore the environment usually already
# optimise all the way to eleven, references:
# http://en.gentoo-wiki.com/wiki/Safe_Cflags/Intel
# http://forums.mozillazine.org/viewtopic.php?f=12&t=577299
# http://gcc.gnu.org/onlinedocs/gcc-4.0.1/gcc/i386-and-x86_002d64-Options.html
ENV['MACOSX_DEPLOYMENT_TARGET']='10.5'
ENV['CFLAGS']=ENV['CXXFLAGS']='-O3 -w -pipe -fomit-frame-pointer -march=prescott -mmacosx-version-min=10.5'
# http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/i386-and-x86_002d64-Options.html
case hw_model
when :core1
# Core DUO is a 32 bit chip
ENV['CFLAGS']="#{ENV['CFLAGS']} -march=prescott -mfpmath=sse -msse3 -mmmx"
when :core2
# Core 2 DUO is a 64 bit chip
# GCC 4.3 will have a -march=core2, but for now nocona is correct
ENV['CFLAGS']="#{ENV['CFLAGS']} -march=nocona -mfpmath=sse -msse3 -mmmx"
# OK so we're not doing 64 bit yet... but we will with Snow Leopard
# -mfpmath=sse defaults to on for the x64 compiler
#ENV['CFLAGS']="#{ENV['CFLAGS']} -march=nocona -msse3 -mmmx -m64"
#ENV['LDFLAGS']="-arch x86_64"
when :xeon, :macpro
# TODO what optimisations for xeon?
when :ppc then abort "Sorry, Homebrew does not support PowerPC architectures"
when :dunno then abort "Sorry, Homebrew cannot determine what kind of Mac this is!"
end
ENV['CXXFLAGS']=ENV['CFLAGS']
# lets use gcc 4.2, it is newer and "better", at least I believe so, mail me
# if I'm wrong
@ -31,11 +56,13 @@ ENV['CC']='gcc-4.2'
ENV['CXX']='g++-4.2'
ENV['MAKEFLAGS']="-j#{OSX::NSProcessInfo.processInfo.processorCount}"
unless HOMEBREW_PREFIX == '/usr/local'
ENV['CPPFLAGS']="-I#{HOMEBREW_PREFIX}/include"
ENV['LDFLAGS']="-L#{HOMEBREW_PREFIX}/lib"
end
def inreplace(path, before, after)
before=Regexp.escape before.to_s
after=Regexp.escape after.to_s

View File

@ -18,6 +18,7 @@
require 'pathname+yeast'
require 'utils'
# TODO if whoami == root then use /Library/Caches/Homebrew instead
HOMEBREW_VERSION='0.3'
HOMEBREW_CACHE=File.expand_path "~/Library/Caches/Homebrew"
HOMEBREW_PREFIX=Pathname.new(__FILE__).dirname.parent.parent.realpath

View File

@ -0,0 +1,12 @@
#include <sys/sysctl.h>
#include <stdio.h>
int main()
{
char buf[32];
size_t sz = sizeof(buf);
int r = sysctlbyname("hw.model", buf, &sz, NULL, 0);
if (r == 0)
printf("%.*s", sz, buf);
return r;
}

View File

@ -0,0 +1,82 @@
def hw_model_output
exe=Pathname.new(HOMEBREW_CACHE)+'hw.model'
Kernel.system "gcc -Os #{File.dirname __FILE__}/hw.model.c -o #{exe}" unless exe.file?
/(.*)(\d+),(\d+)/ =~ `#{exe}`
yield $1, $2.to_i, $3.to_i
end
# http://support.apple.com/kb/HT3696
# http://www.cocoadev.com/index.pl?MacintoshModels
def hw_model
hw_model_output do |model, major, minor|
case model
when "iMac"
if major <=4
:core1
elsif major <=8
:core2
else
$unknown_hw_model=true
:core2
end
when "MacBookAir"
if major <= 1
:core2
else
$unknown_hw_model=true
:core2
end
when "MacBook"
if major <= 1
:core1
elsif major <= 4
:core2
else
$unknown_hw_model=true
:core2
end
when "MacBookPro"
if major <= 1
:core1
elsif major <= 5
:core2
else
$unknown_hw_model=true
:core2
end
when "Macmini" # Mac mini (Core Duo/Solo)
if major <= 1
:core
else
$unknown_hw_model=true
:core
end
when "MacPro"
if major <= 3
:xeon
else
$unknown_hw_model=true
:xeon
end
when "PowerBook", "PowerMac", "RackMac" then :ppc
when "Xserve"
if major <=2
:xeon
else
$unknown_hw_model=true
:xeon
end
when "ADP" then :dunno # Developer Transition Kit
when "M43ADP" then :dunno # Development Mac Pro
else :dunno
end
end
end