Permalink
Browse files

Hardware: separate out CPU values into CPU module

* CPU functions now exist in Hardware::CPU
* Added compatibility functions in compat/hardware_compat.rb
* Names are less specific to Mac hardware, e.g. CPU.family instead of
  Hardware.intel_family
* Hardware::CPU.family works for both Intel and PowerPC
* New helper methods on CPU, like .sse4? and .altivec?

Signed-off-by: Misty De Meo <mistydemeo@gmail.com>
  • Loading branch information...
1 parent 17a92ea commit e7f66519dc6881f49e594ddb2fae857580c2c033 @mistydemeo mistydemeo committed Mar 17, 2013
View
2 Library/Homebrew/cmd/--config.rb
@@ -85,7 +85,7 @@ def describe_ruby
end
def hardware
- "CPU: #{Hardware.cores_as_words}-core #{Hardware.bits}-bit #{Hardware.intel_family}"
+ "CPU: #{Hardware.cores_as_words}-core #{Hardware::CPU.bits}-bit #{Hardware::CPU.family}"
end
def kernel
View
29 Library/Homebrew/compat/hardware_compat.rb
@@ -0,0 +1,29 @@
+class Hardware
+ class << self
+ def is_32_bit?
+ not CPU.is_64_bit?
+ end
+
+ def is_64_bit?
+ CPU.is_64_bit?
+ end
+
+ def bits
+ Hardware::CPU.bits
+ end
+
+ def cpu_type
+ Hardware::CPU.type
+ end
+
+ def cpu_family
+ Hardware::CPU.family
+ end
+ alias_method :intel_family, :cpu_family
+ alias_method :ppc_family, :cpu_family
+
+ def processor_count
+ Hardware::CPU.cores
+ end
+ end
+end
View
19 Library/Homebrew/extend/ENV.rb
@@ -121,7 +121,8 @@ def gcc_4_0_1
self['CC'] = self['OBJC'] = "#{MacOS.dev_tools_path}/gcc-4.0"
self['CXX'] = self['OBJCXX'] = "#{MacOS.dev_tools_path}/g++-4.0"
replace_in_cflags '-O4', '-O3'
- set_cpu_cflags 'nocona -mssse3', :core => 'prescott', :bottle => 'generic'
+ set_cpu_cflags '-march=nocona -mssse3',
+ Hardware::CPU.optimization_flags
@compiler = :gcc
end
alias_method :gcc_4_0, :gcc_4_0_1
@@ -155,15 +156,15 @@ def gcc
end
replace_in_cflags '-O4', '-O3'
- set_cpu_cflags 'core2 -msse4', :penryn => 'core2 -msse4.1', :core2 => 'core2', :core => 'prescott', :bottle => 'generic'
+ set_cpu_cflags '-march=core2 -msse4', Hardware::CPU.optimization_flags
@compiler = :gcc
end
alias_method :gcc_4_2, :gcc
def llvm
self['CC'] = self['OBJC'] = MacOS.locate("llvm-gcc")
self['CXX'] = self['OBJCXX'] = MacOS.locate("llvm-g++")
- set_cpu_cflags 'core2 -msse4', :penryn => 'core2 -msse4.1', :core2 => 'core2', :core => 'prescott'
+ set_cpu_cflags '-march=core2 -msse4', Hardware::CPU.optimization_flags
@compiler = :llvm
end
@@ -172,7 +173,7 @@ def clang
self['CXX'] = self['OBJCXX'] = MacOS.locate("clang++")
replace_in_cflags(/-Xarch_i386 (-march=\S*)/, '\1')
# Clang mistakenly enables AES-NI on plain Nehalem
- set_cpu_cflags 'native', :nehalem => 'native -Xclang -target-feature -Xclang -aes'
+ set_cpu_cflags '-march=native', :nehalem => '-march=native -Xclang -target-feature -Xclang -aes'
append_to_cflags '-Qunused-arguments'
@compiler = :clang
end
@@ -325,10 +326,10 @@ def set_cpu_flags flags, default, map = {}
append flags, xarch unless xarch.empty?
if ARGV.build_bottle?
- append flags, '-mtune=' + map.fetch(:bottle) if map.has_key? :bottle
+ append flags, '-mtune=generic'
else
# Don't set -msse3 and older flags because -march does that for us
- append flags, '-march=' + map.fetch(Hardware.intel_family, default)
+ append flags, map.fetch(Hardware::CPU.family, default)
end
# not really a 'CPU' cflag, but is only used with clang
@@ -467,7 +468,8 @@ def fortran
flags_to_set.each {|key| self[key] = cflags}
# Ensure we use architecture optimizations for GCC 4.2.x
- set_cpu_flags flags_to_set, 'core2 -msse4', :penryn => 'core2 -msse4.1', :core2 => 'core2', :core => 'prescott', :bottle => 'generic'
+ set_cpu_flags flags_to_set, 'core2 -msse4',
+ Hardware::CPU.optimization_flags
elsif not self['FCFLAGS'] or self['FFLAGS']
opoo <<-EOS.undent
No Fortran optimization information was provided. You may want to consider
@@ -487,7 +489,8 @@ def fortran
fc_flag_vars.each {|key| self[key] = cflags}
# Ensure we use architecture optimizations for GCC 4.2.x
- set_cpu_flags fc_flag_vars, 'core2 -msse4', :penryn => 'core2 -msse4.1', :core2 => 'core2', :core => 'prescott', :bottle => 'generic'
+ set_cpu_flags fc_flag_vars, 'core2 -msse4',
+ Hardware::CPU.optimization_flags
else
onoe <<-EOS
View
40 Library/Homebrew/hardware.rb
@@ -1,11 +1,39 @@
+require 'hardware_compat'
+
class Hardware
+ module CPU extend self
+ def type
+ @type || :dunno
+ end
+
+ def family
+ @family || :dunno
+ end
+
+ def cores
+ @cores || 1
+ end
+
+ def bits
+ @bits || 64
+ end
+
+ def is_32_bit?
+ bits == 32
+ end
+
+ def is_64_bit?
+ bits == 64
+ end
+ end
+
case RUBY_PLATFORM.downcase
when /darwin/
require 'os/mac/hardware'
- extend MacOSHardware
+ CPU.extend MacCPUs
when /linux/
require 'os/linux/hardware'
- extend LinuxHardware
+ CPU.extend LinuxCPUs
else
raise "The system `#{`uname`.chomp}' is not supported."
end
@@ -19,12 +47,4 @@ def self.cores_as_words
Hardware.processor_count
end
end
-
- def self.is_32_bit?
- not self.is_64_bit?
- end
-
- def self.bits
- Hardware.is_64_bit? ? 64 : 32
- end
end
View
3 Library/Homebrew/macos.rb
@@ -9,6 +9,9 @@ def version
end
def cat
+ # PowerPC builds per processor, not per OS
+ return Hardware::CPU.family if Hardware::CPU.type == :ppc
+
if version == :mountain_lion then :mountain_lion
elsif version == :lion then :lion
elsif version == :snow_leopard
View
22 Library/Homebrew/os/linux/hardware.rb
@@ -1,6 +1,9 @@
-module LinuxHardware
- def cpu_type
- @@cpu_type ||= case `uname -m`
+module LinuxCPUs
+ OPTIMIZATION_FLAGS = {}
+ def optimization_flags; OPTIMIZATION_FLAGS.dup; end
+
+ def type
+ @cpu_type ||= case `uname -m`
when /x86_64/
:intel
when /i386/
@@ -10,16 +13,21 @@ def cpu_type
end
end
- def intel_family
+ def family
:dunno
end
+ alias_method :intel_family, :cpu_family
- def processor_count
+ def cores
`grep -c ^processor /proc/cpuinfo`.to_i
end
+ def bits
+ is_64_bit? ? 64 : 32
+ end
+
def is_64_bit?
- return @@is_64_bit if defined? @@is_64_bit
- @@is_64_bit = /64/ === `uname -m`
+ return @is_64_bit if defined? @is_64_bit
+ @is_64_bit = /64/ === `uname -m`
end
end
View
102 Library/Homebrew/os/mac/hardware.rb
@@ -1,10 +1,20 @@
-module MacOSHardware
+module MacCPUs
+ OPTIMIZATION_FLAGS = {
+ :penryn => '-march=core2 -msse4.1',
+ :core2 => '-march=core2',
+ :core => '-march=prescott',
+ :g3 => '-mcpu=750',
+ :g4 => '-mcpu=7400',
+ :g4e => '-mcpu=7450',
+ :g5 => '-mcpu=970'
+ }
+ def optimization_flags; OPTIMIZATION_FLAGS.dup; end
+
# These methods use info spewed out by sysctl.
# Look in <mach/machine.h> for decoding info.
- def cpu_type
- @@cpu_type ||= `/usr/sbin/sysctl -n hw.cputype`.to_i
-
- case @@cpu_type
+ def type
+ @type ||= `/usr/sbin/sysctl -n hw.cputype`.to_i
+ case @type
when 7
:intel
when 18
@@ -14,39 +24,69 @@ def cpu_type
end
end
- def 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
- when 0x573B5EEC # Arrandale
- :arrandale
- when 0x5490B78C # Sandy Bridge
- :sandybridge
- when 0x1F65E835 # Ivy Bridge
- :ivybridge
- else
- :dunno
+ def family
+ if type == :intel
+ @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
+ when 0x573B5EEC # Arrandale
+ :arrandale
+ when 0x5490B78C # Sandy Bridge
+ :sandybridge
+ when 0x1F65E835 # Ivy Bridge
+ :ivybridge
+ else
+ :dunno
+ end
+ elsif type == :ppc
+ @ppc_family ||= `/usr/sbin/sysctl -n hw.cpusubtype`.to_i
+ case @ppc_family
+ when 9
+ :g3 # PowerPC 750
+ when 10
+ :g4 # PowerPC 7400
+ when 11
+ :g4e # PowerPC 7450
+ when 100
+ :g5 # PowerPC 970
+ else
+ :dunno
+ end
end
end
- def processor_count
- @@processor_count ||= `/usr/sbin/sysctl -n hw.ncpu`.to_i
+ def cores
+ @cores ||= `/usr/sbin/sysctl -n hw.ncpu`.to_i
+ end
+
+ def bits
+ return @bits if defined? @bits
+
+ is_64_bit = sysctl_bool("hw.cpu64bit_capable")
+ @bits ||= is_64_bit ? 64 : 32
+ end
+
+ def altivec?
+ type == :ppc && family != :g3
end
- def is_64_bit?
- return @@is_64_bit if defined? @@is_64_bit
- @@is_64_bit = sysctl_bool("hw.cpu64bit_capable")
+ def sse3?
+ type == :intel
end
-protected
+ def sse4?
+ type == :intel && (family != :core && family != :core2)
+ end
+
+ protected
+
def sysctl_bool(property)
result = nil
IO.popen("/usr/sbin/sysctl -n #{property} 2>/dev/null") do |f|
View
4 Library/Homebrew/test/test_hardware.rb
@@ -5,13 +5,13 @@ class HardwareTests < Test::Unit::TestCase
# these will raise if we don't recognise your mac, but that prolly
# indicates something went wrong rather than we don't know
def test_hardware_cpu_type
- assert [:intel, :ppc].include?(Hardware.cpu_type)
+ assert [:intel, :ppc].include?(Hardware::CPU.type)
end
def test_hardware_intel_family
if Hardware.cpu_type == :intel
assert [:core, :core2, :penryn, :nehalem,
- :arrandale, :sandybridge, :ivybridge].include?(Hardware.intel_family)
+ :arrandale, :sandybridge, :ivybridge].include?(Hardware::CPU.family)
end
end
end

0 comments on commit e7f6651

Please sign in to comment.