aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/hardware.rb
blob: 0559ae694c7e3defa9f4f456a10b94f712b6c137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require "os"

module Hardware
  class CPU
    INTEL_32BIT_ARCHS = [:i386].freeze
    INTEL_64BIT_ARCHS = [:x86_64].freeze
    PPC_32BIT_ARCHS   = [:ppc, :ppc7400, :ppc7450, :ppc970].freeze
    PPC_64BIT_ARCHS   = [:ppc64].freeze

    class << self
      OPTIMIZATION_FLAGS = {
        core2: "-march=core2",
        core: "-march=prescott",
        dunno: "-march=native",
      }.freeze

      def optimization_flags
        OPTIMIZATION_FLAGS
      end

      def arch_32_bit
        if arm?
          :arm
        elsif intel?
          :i386
        elsif ppc?
          :ppc32
        else
          :dunno
        end
      end

      def arch_64_bit
        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
      end

      def family
        :dunno
      end

      def cores
        return @cores if @cores
        @cores = Utils.popen_read("getconf", "_NPROCESSORS_ONLN").chomp.to_i
        @cores = 1 unless $CHILD_STATUS.success?
        @cores
      end

      def bits
        @bits ||= case RUBY_PLATFORM
        when /x86_64/, /ppc64/, /aarch64|arm64/ then 64
        when /i\d86/, /ppc/, /arm/ then 32
        end
      end

      def sse4?
        RUBY_PLATFORM.to_s.include?("x86_64")
      end

      def is_32_bit?
        bits == 32
      end

      def is_64_bit?
        bits == 64
      end

      def intel?
        type == :intel
      end

      def ppc?
        type == :ppc
      end

      def arm?
        type == :arm
      end

      def features
        []
      end

      def feature?(name)
        features.include?(name)
      end

      def can_run?(arch)
        if is_32_bit?
          arch_32_bit == arch
        elsif intel?
          [:i386, :x86_64].include? arch
        elsif ppc?
          [:ppc, :ppc64].include? arch
        else
          false
        end
      end
    end
  end

  def self.cores_as_words
    case Hardware::CPU.cores
    when 1 then "single"
    when 2 then "dual"
    when 4 then "quad"
    when 6 then "hexa"
    when 8 then "octa"
    else
      Hardware::CPU.cores
    end
  end

  def self.oldest_cpu
    if Hardware::CPU.intel?
      if Hardware::CPU.is_64_bit?
        :core2
      else
        :core
      end
    else
      Hardware::CPU.family
    end
  end
end

require "extend/os/hardware"