aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorMax Howell2009-07-31 16:06:14 +0100
committerMax Howell2009-07-31 16:06:14 +0100
commitdb69633b841f83299895c26df155d03d70352a9e (patch)
treeef77641a338555c606c0e700078f318f9df6098e /Library
parentf6e5874ff76b66ed2e2af30ec2c28e910d72bf89 (diff)
downloadhomebrew-db69633b841f83299895c26df155d03d70352a9e.tar.bz2
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.
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/brewkit.rb33
-rw-r--r--Library/Homebrew/env.rb1
-rw-r--r--Library/Homebrew/hw.model.c12
-rw-r--r--Library/Homebrew/hw.model.rb82
4 files changed, 125 insertions, 3 deletions
diff --git a/Library/Homebrew/brewkit.rb b/Library/Homebrew/brewkit.rb
index 1ff6e0af2..bc208236a 100644
--- a/Library/Homebrew/brewkit.rb
+++ b/Library/Homebrew/brewkit.rb
@@ -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
diff --git a/Library/Homebrew/env.rb b/Library/Homebrew/env.rb
index 7ff57071e..a9cb6aa91 100644
--- a/Library/Homebrew/env.rb
+++ b/Library/Homebrew/env.rb
@@ -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
diff --git a/Library/Homebrew/hw.model.c b/Library/Homebrew/hw.model.c
new file mode 100644
index 000000000..12436057b
--- /dev/null
+++ b/Library/Homebrew/hw.model.c
@@ -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;
+}
diff --git a/Library/Homebrew/hw.model.rb b/Library/Homebrew/hw.model.rb
new file mode 100644
index 000000000..87e6a6b3c
--- /dev/null
+++ b/Library/Homebrew/hw.model.rb
@@ -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