diff options
| author | Mike McQuaid | 2017-12-02 10:30:47 +0000 |
|---|---|---|
| committer | GitHub | 2017-12-02 10:30:47 +0000 |
| commit | dfe0a1eb898dcee72dc113737c79bff588383130 (patch) | |
| tree | ffee0e421487ff3530f5d19a59e11ee577ddd327 | |
| parent | 1b289226284d87c846825563624661ced315b11a (diff) | |
| parent | 0ce7a74c585eb01df60f1e9d353825d90bd3d969 (diff) | |
| download | brew-dfe0a1eb898dcee72dc113737c79bff588383130.tar.bz2 | |
Merge pull request #3507 from sjackman/cpu
Hardware::CPU: Implement OS-agnostic methods
| -rw-r--r-- | Library/Homebrew/extend/os/linux/hardware/cpu.rb | 28 | ||||
| -rw-r--r-- | Library/Homebrew/extend/os/mac/hardware/cpu.rb | 16 | ||||
| -rw-r--r-- | Library/Homebrew/hardware.rb | 47 |
3 files changed, 44 insertions, 47 deletions
diff --git a/Library/Homebrew/extend/os/linux/hardware/cpu.rb b/Library/Homebrew/extend/os/linux/hardware/cpu.rb index 8bf67bec8..0c1078fdf 100644 --- a/Library/Homebrew/extend/os/linux/hardware/cpu.rb +++ b/Library/Homebrew/extend/os/linux/hardware/cpu.rb @@ -1,26 +1,13 @@ module Hardware class CPU class << self - def universal_archs - [].extend ArchitectureListExtension - end - def cpuinfo @cpuinfo ||= File.read("/proc/cpuinfo") end - def type - @type ||= if cpuinfo =~ /Intel|AMD/ - :intel - elsif cpuinfo =~ /ARM|Marvell/ - :arm - else - :dunno - end - end - def family return :arm if arm? + return :ppc if ppc? return :dunno unless intel? # See https://software.intel.com/en-us/articles/intel-architecture-and-processor-identification-with-cpuid-model-and-family-numbers cpu_family = cpuinfo[/^cpu family\s*: ([0-9]+)/, 1].to_i @@ -70,12 +57,9 @@ module Hardware end end - def cores - cpuinfo.scan(/^processor/).size - end - def flags - @flags ||= cpuinfo[/^(flags|Features).*/, 0].split + @flags ||= cpuinfo[/^(flags|Features).*/, 0]&.split + @flags ||= [] end # Compatibility with Mac method, which returns lowercase symbols @@ -95,12 +79,6 @@ module Hardware def sse4? flags.include? "sse4_1" end - - alias is_64_bit? lm? - - def bits - is_64_bit? ? 64 : 32 - end end end end diff --git a/Library/Homebrew/extend/os/mac/hardware/cpu.rb b/Library/Homebrew/extend/os/mac/hardware/cpu.rb index a216db6ae..cc41ae911 100644 --- a/Library/Homebrew/extend/os/mac/hardware/cpu.rb +++ b/Library/Homebrew/extend/os/mac/hardware/cpu.rb @@ -75,22 +75,6 @@ module Hardware sysctl_int("machdep.cpu.extmodel") end - def cores - sysctl_int("hw.ncpu") - end - - def bits - sysctl_bool("hw.cpu64bit_capable") ? 64 : 32 - end - - def arch_32_bit - intel? ? :i386 : :ppc - end - - def arch_64_bit - intel? ? :x86_64 : :ppc64 - end - # Returns an array that's been extended with ArchitectureListExtension, # which provides helpers like #as_arch_flags and #as_cmake_arch_flags. def universal_archs diff --git a/Library/Homebrew/hardware.rb b/Library/Homebrew/hardware.rb index 997598def..0559ae694 100644 --- a/Library/Homebrew/hardware.rb +++ b/Library/Homebrew/hardware.rb @@ -19,16 +19,48 @@ module Hardware end def arch_32_bit - :i386 + if arm? + :arm + elsif intel? + :i386 + elsif ppc? + :ppc32 + else + :dunno + end end def arch_64_bit - :x86_64 + if arm? + :arm64 + elsif intel? + :x86_64 + elsif ppc? + :ppc64 + else + :dunno + end + end + + def arch + case bits + when 32 + arch_32_bit + when 64 + arch_64_bit + else + :dunno + end + end + + def universal_archs + [arch].extend ArchitectureListExtension end def type case RUBY_PLATFORM when /x86_64/, /i\d86/ then :intel + when /arm/ then :arm when /ppc\d+/ then :ppc else :dunno end @@ -39,13 +71,16 @@ module Hardware end def cores - 1 + return @cores if @cores + @cores = Utils.popen_read("getconf", "_NPROCESSORS_ONLN").chomp.to_i + @cores = 1 unless $CHILD_STATUS.success? + @cores end def bits - case RUBY_PLATFORM - when /x86_64/, /ppc64/ then 64 - when /i\d86/, /ppc/ then 32 + @bits ||= case RUBY_PLATFORM + when /x86_64/, /ppc64/, /aarch64|arm64/ then 64 + when /i\d86/, /ppc/, /arm/ then 32 end end |
