Improvements to build environment determination

Simplified hardware model testing.

Even smarter compiler option generation using sysctl and new GCC 4.2 features.

Get processor count from sysctl and thus remove our dependency on RubyCocoa.
This commit is contained in:
spicyj 2009-09-02 13:17:15 -06:00 committed by Max Howell
parent 91d9a707b6
commit ba8ba4c451
4 changed files with 159 additions and 135 deletions

View File

@ -21,11 +21,10 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
require 'osx/cocoa' # to get number of cores
require 'fileutils' require 'fileutils'
require 'formula' require 'formula'
require 'download_strategy' require 'download_strategy'
require 'hw.model' require 'hardware'
# TODO # TODO
# 1. Indeed, there should be an option to build 32 or 64 bit binaries # 1. Indeed, there should be an option to build 32 or 64 bit binaries
@ -34,7 +33,7 @@ require 'hw.model'
# build systems we support to do it. # build systems we support to do it.
`sw_vers -productVersion` =~ /(10\.\d+)(\.\d+)?/ `/usr/bin/sw_vers -productVersion` =~ /(10\.\d+)(\.\d+)?/
MACOS_VERSION=$1.to_f MACOS_VERSION=$1.to_f
ENV['MACOSX_DEPLOYMENT_TARGET']=MACOS_VERSION.to_s ENV['MACOSX_DEPLOYMENT_TARGET']=MACOS_VERSION.to_s
@ -43,33 +42,21 @@ ENV['LDFLAGS']='' # to be consistent, we ignore the existing environment
# this is first, so when you see it in output, you notice it # this is first, so when you see it in output, you notice it
cflags='-O3' cflags='-O3'
# optimise all the way to eleven, references: if MACOS_VERSION >= 10.6
# http://en.gentoo-wiki.com/wiki/Safe_Cflags/Intel if Hardware.is_64bit?
# http://forums.mozillazine.org/viewtopic.php?f=12&t=577299 # 64 bits baby!
# http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/i386-and-x86_002d64-Options.html cflags<<" -m64"
case hw_model ENV['LDFLAGS']="-arch x86_64"
when :core1 end
# Core DUO is a 32 bit chip else
# NOTE technically we can do -msse4 with gcc 4.2, but I can't test it, so # GCC 4.2.1 is smart and will figure out the right compile flags
# haven't tried it, if you have a core1 chip, then please test and commit --mxcl # http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/i386-and-x86_002d64-Options.html
cflags<<" -march=prescott -mfpmath=sse -msse3 -mmmx" cflags<<"-march=native"
when :core2 end
# Core 2 DUO is a 64 bit chip
if MACOS_VERSION >= 10.6
# 64 bits baby! -mfpmath=sse is automatically switched on by -m64
# GCC 4.3 has a -march=core2, but this is 4.2 and nocona is correct
cflags<<" -m64 -march=nocona -msse4 -mmmx"
ENV['LDFLAGS']="-arch x86_64"
else
# We don't build 64 bit before 10.6 as nothing else is 64 bit, so any
# libraries we build would be unusable by 32 bit software
cflags<<" -march=nocona -mfpmath=sse -msse3 -mmmx"
end
when :xeon
# TODO what optimisations for xeon?
when :ppc then abort "Sorry, Homebrew does not support PowerPC architectures" case Hardware.cpu_type
when :dunno then abort "Sorry, Homebrew cannot determine what kind of Mac this is!" 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 end
# -w: keep signal to noise high # -w: keep signal to noise high
@ -77,14 +64,13 @@ end
ENV['CFLAGS']="#{cflags} -w -pipe -fomit-frame-pointer -mmacosx-version-min=#{MACOS_VERSION}" ENV['CFLAGS']="#{cflags} -w -pipe -fomit-frame-pointer -mmacosx-version-min=#{MACOS_VERSION}"
ENV['CXXFLAGS']=ENV['CFLAGS'] ENV['CXXFLAGS']=ENV['CFLAGS']
# lets use gcc 4.2, it is newer and "better", at least I believe so, mail me # lets use gcc 4.2, Xcode does after all
# if I'm wrong
if MACOS_VERSION==10.5 if MACOS_VERSION==10.5
ENV['CC']='gcc-4.2' ENV['CC']='gcc-4.2'
ENV['CXX']='g++-4.2' ENV['CXX']='g++-4.2'
end end
# compile faster # compile faster
ENV['MAKEFLAGS']="-j#{OSX::NSProcessInfo.processInfo.processorCount}" ENV['MAKEFLAGS']="-j#{Hardware.processor_count}"
# /usr/local is always in the build system path # /usr/local is always in the build system path
@ -109,6 +95,16 @@ module HomebrewEnvExtension
self['CC']='gcc-4.0' self['CC']='gcc-4.0'
self['CXX']='g++-4.0' self['CXX']='g++-4.0'
end end
# argh, we have to figure out the compile options ourselves and get
# rid of -march=native, so we 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.2.1/gcc/i386-and-x86_002d64-Options.html
remove_from_cflags '-march=native'
append_to_cflags Hardware.gcc_march
append_to_cflags Hardware.gcc_msse
append_to_cflags Hardware.gcc_mmx
end end
def osx_10_4 def osx_10_4
self['MACOSX_DEPLOYMENT_TARGET']=nil self['MACOSX_DEPLOYMENT_TARGET']=nil
@ -118,10 +114,11 @@ module HomebrewEnvExtension
%w[-mfpmath=sse -msse3 -mmmx -march=\w+].each {|s| remove_from_cflags s} %w[-mfpmath=sse -msse3 -mmmx -march=\w+].each {|s| remove_from_cflags s}
end end
def libxml2 def libxml2
self['CXXFLAGS']=self['CFLAGS']+=' -I/usr/include/libxml2' append_to_cflags ' -I/usr/include/libxml2'
end end
# TODO rename or alias to x11 # TODO rename or alias to x11
def libpng def libpng
# CPPFLAGS are the C-PreProcessor flags, *not* C++!
append 'CPPFLAGS', '-I/usr/X11R6/include' append 'CPPFLAGS', '-I/usr/X11R6/include'
append 'LDFLAGS', '-L/usr/X11R6/lib' append 'LDFLAGS', '-L/usr/X11R6/lib'
end end
@ -139,14 +136,18 @@ private
self[key]=ref+' '+value self[key]=ref+' '+value
end end
end end
def remove key, rx def append_to_cflags f
append 'CFLAGS', f
append 'CXXFLAGS', f
end
def remove key, value
return if self[key].nil? return if self[key].nil?
# sub! doesn't work as "the string is frozen" self[key]=self[key].sub value, '' # can't use sub! on ENV
self[key]=self[key].sub rx, ''
self[key]=nil if self[key].empty? # keep things clean self[key]=nil if self[key].empty? # keep things clean
end end
def remove_from_cflags rx def remove_from_cflags f
%w[CFLAGS CXXFLAGS].each {|key| remove key, rx} remove 'CFLAGS', f
remove 'CXXFLAGS', f
end end
end end
@ -157,7 +158,7 @@ ENV.extend HomebrewEnvExtension
# http://github.com/mxcl/homebrew/issues/#issue/13 # http://github.com/mxcl/homebrew/issues/#issue/13
paths=ENV['PATH'].split(':').reject do |p| paths=ENV['PATH'].split(':').reject do |p|
p.squeeze! '/' p.squeeze! '/'
p=~%r[^/opt/local] or p=~%r[^/sw] p =~ %r[^/opt/local] or p =~ %r[^/sw]
end end
ENV['PATH']=paths*':' ENV['PATH']=paths*':'
@ -169,7 +170,6 @@ def inreplace(path, before, after)
after.gsub! "\\", "\\\\" after.gsub! "\\", "\\\\"
after.gsub! "/", "\\/" after.gsub! "/", "\\/"
# TODO this sucks # FIXME use proper Ruby for teh exceptions!
# either use 'ed', or allow regexp and use a proper ruby function
safe_system "perl", "-pi", "-e", "s/#{before}/#{after}/g", path safe_system "perl", "-pi", "-e", "s/#{before}/#{after}/g", path
end end

View File

@ -0,0 +1,106 @@
# Copyright 2009 Max Howell and other contributors.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
class Hardware
# These methods use info spewed out by sysctl.
# Look in <mach/machine.h> for decoding info.
def self.cpu_type
@@cpu_type ||= `/usr/sbin/sysctl -n hw.cputype`.to_i
case @@cpu_type
when 7
:intel
when 18
:ppc
else
:dunno
end
end
def self.intel_family
@@intel_family ||= `/usr/sbin/sysctl -n hw.cpufamily`.to_i
case @@intel_family
when 0x73d67300 # Yonah: Core Solo/Duo
:core
when 0x426f69ef # Merom: Core 2 Duo
:core2
when 0x78ea4fbc # Penryn
:penryn
when 0x6b5a4cd2 # Nehalem
:nehalem
else
:dunno
end
end
def self.processor_count
@@processor_count ||= `/usr/sbin/sysctl -n hw.ncpu`.to_i
end
def self.gcc_march # what to pass to gcc
@@gcc_march ||= if self.cpu_type == :intel
case self.intel_family
when :core
" -march=prescott"
when :core2, :penryn, :nehalem
# GCC 4.3 has a -march=core2, but this isn't 4.3 and nocona is correct
" -march=nocona"
end
else
""
end
end
def self.gcc_msse # what to pass to gcc
# avoid sse4 for now in case it blows up
@@gcc_msse ||= if sysctl_bool("hw.optional.sse3")
" -msse3 -mfpmath=sse"
else
""
end
end
def self.gcc_mmmx # what to pass to gcc
@@gcc_mmmx ||= if sysctl_bool("hw.optional.mmx")
" -mmmx"
else
""
end
end
def self.is_64bit?
@@is_64bit ||= sysctl_bool("hw.cpu64bit_capable")
end
protected
def self.sysctl_bool(property)
result = nil
IO.popen("/usr/sbin/sysctl -n #{property} 2>/dev/null") do |f|
result = f.gets.to_i # should be 0 or 1
end
$?.success? && result == 1 # sysctl call succeded and printed 1
end
end

View File

@ -1,88 +0,0 @@
# Copyright 2009 Max Howell and other contributors.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The output of the command is in the form of: `MacBook2,1'
# This yields: "MacBook", 2, 1
def hw_model_output
model=`/usr/sbin/sysctl hw.model`.match /hw.model: (\w+)(\d+),(\d+)/
yield model[1], model[2].to_i, model[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
else
$unknown_hw_model=true if major > 8
:core2
end
when "MacBookAir"
$unknown_hw_model=true if major > 1
:core2
when "MacBook"
if major <= 1
:core1
else
$unknown_hw_model=true if major > 4
:core2
end
when "MacBookPro"
if major <= 1
:core1
else
$unknown_hw_model=true if major > 5
:core2
end
when "Macmini" # Mac mini (Core Duo/Solo)
$unknown_hw_model=true if major > 1
:core
when "MacPro"
$unknown_hw_model=true if major > 3
# 'Xeon' is a marketing term, not a specific CPU:
# http://en.wikipedia.org/wiki/Xeon
# adamv says: I have a Mac Pro at work (MacPro4,1) and will try
# some compiler options out.
:xeon
when "PowerBook", "PowerMac", "RackMac" then :ppc
when "Xserve"
$unknown_hw_model=true if major > 2
:xeon
when "ADP" then :dunno # Developer Transition Kit
when "M43ADP" then :dunno # Development Mac Pro
else :dunno
end
end
end

View File

@ -12,6 +12,7 @@ require 'download_strategy'
require 'keg' require 'keg'
require 'utils' require 'utils'
require 'brew.h' require 'brew.h'
require 'hardware.rb'
# these are defined in bin/brew, but we don't want to break our actual # these are defined in bin/brew, but we don't want to break our actual
# homebrew tree, and we do want to test everything :) # homebrew tree, and we do want to test everything :)
@ -332,11 +333,16 @@ class BeerTasting <Test::Unit::TestCase
assert_equal '1.9.1-p243', f.version assert_equal '1.9.1-p243', f.version
end end
def test_hw_model # these will raise if we don't recognise your mac, but that prolly
require 'hw.model.rb' # indicates something went wrong rather than we don't know
# this will raise if we don't recognise your mac, but that prolly def test_hardware_cpu_type
# indicates something went wrong rather than we don't know assert [:intel, :ppc].include?(Hardware.cpu_type)
assert %w[core1 core2 xeon ppc].include?(hw_model.to_s) end
def test_hardware_intel_family
if Hardware.cpu_type == :intel
assert [:core, :core2, :penryn, :nehalem].include?(Hardware.intel_family)
end
end end
def test_brew_h def test_brew_h