Skip to content

Commit f17ee43

Browse files
authored
Merge pull request #18082 from Homebrew/hardware-typed-strict
2 parents f9b81af + a3600d0 commit f17ee43

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

Library/Homebrew/hardware.rb

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# typed: true # rubocop:todo Sorbet/StrictSigil
1+
# typed: strict
22
# frozen_string_literal: true
33

44
require "utils/popen"
@@ -9,23 +9,24 @@ module Hardware
99
class CPU
1010
INTEL_32BIT_ARCHS = [:i386].freeze
1111
INTEL_64BIT_ARCHS = [:x86_64].freeze
12-
INTEL_ARCHS = (INTEL_32BIT_ARCHS + INTEL_64BIT_ARCHS).freeze
12+
INTEL_ARCHS = T.let((INTEL_32BIT_ARCHS + INTEL_64BIT_ARCHS).freeze, T::Array[Symbol])
1313
PPC_32BIT_ARCHS = [:ppc, :ppc32, :ppc7400, :ppc7450, :ppc970].freeze
1414
PPC_64BIT_ARCHS = [:ppc64, :ppc64le, :ppc970].freeze
15-
PPC_ARCHS = (PPC_32BIT_ARCHS + PPC_64BIT_ARCHS).freeze
15+
PPC_ARCHS = T.let((PPC_32BIT_ARCHS + PPC_64BIT_ARCHS).freeze, T::Array[Symbol])
1616
ARM_64BIT_ARCHS = [:arm64, :aarch64].freeze
1717
ARM_ARCHS = ARM_64BIT_ARCHS
18-
ALL_ARCHS = [
18+
ALL_ARCHS = T.let([
1919
*INTEL_ARCHS,
2020
*PPC_ARCHS,
2121
*ARM_ARCHS,
22-
].freeze
22+
].freeze, T::Array[Symbol])
2323

2424
INTEL_64BIT_OLDEST_CPU = :core2
2525

2626
class << self
27+
sig { returns(T::Hash[Symbol, String]) }
2728
def optimization_flags
28-
@optimization_flags ||= {
29+
@optimization_flags ||= T.let({
2930
native: arch_flag("native"),
3031
ivybridge: "-march=ivybridge",
3132
sandybridge: "-march=sandybridge",
@@ -38,7 +39,7 @@ def optimization_flags
3839
armv8: "-march=armv8-a",
3940
ppc64: "-mcpu=powerpc64",
4041
ppc64le: "-mcpu=powerpc64le",
41-
}.freeze
42+
}.freeze, T.nilable(T::Hash[Symbol, String]))
4243
end
4344
alias generic_optimization_flags optimization_flags
4445

@@ -70,6 +71,7 @@ def arch_64_bit
7071
end
7172
end
7273

74+
sig { returns(Symbol) }
7375
def arch
7476
case bits
7577
when 32
@@ -96,50 +98,59 @@ def family
9698
:dunno
9799
end
98100

101+
sig { returns(Integer) }
99102
def cores
100103
return @cores if @cores
101104

102105
@cores = Utils.popen_read("getconf", "_NPROCESSORS_ONLN").chomp.to_i
103-
@cores = 1 unless $CHILD_STATUS.success?
106+
@cores = T.let(1, T.nilable(Integer)) unless $CHILD_STATUS.success?
104107
@cores
105108
end
106109

110+
sig { returns(T.nilable(Integer)) }
107111
def bits
108-
@bits ||= case RUBY_PLATFORM
112+
@bits ||= T.let(case RUBY_PLATFORM
109113
when /x86_64/, /ppc64|powerpc64/, /aarch64|arm64/ then 64
110114
when /i\d86/, /ppc/, /arm/ then 32
111-
end
115+
end, T.nilable(Integer))
112116
end
113117

114118
sig { returns(T::Boolean) }
115119
def sse4?
116120
RUBY_PLATFORM.to_s.include?("x86_64")
117121
end
118122

123+
sig { returns(T::Boolean) }
119124
def is_32_bit?
120125
bits == 32
121126
end
122127

128+
sig { returns(T::Boolean) }
123129
def is_64_bit?
124130
bits == 64
125131
end
126132

133+
sig { returns(T::Boolean) }
127134
def intel?
128135
type == :intel
129136
end
130137

138+
sig { returns(T::Boolean) }
131139
def ppc?
132140
type == :ppc
133141
end
134142

143+
sig { returns(T::Boolean) }
135144
def ppc32?
136145
ppc? && is_32_bit?
137146
end
138147

148+
sig { returns(T::Boolean) }
139149
def ppc64le?
140150
ppc? && is_64_bit? && little_endian?
141151
end
142152

153+
sig { returns(T::Boolean) }
143154
def ppc64?
144155
ppc? && is_64_bit? && big_endian?
145156
end
@@ -152,26 +163,32 @@ def arm?
152163
type == :arm
153164
end
154165

166+
sig { returns(T::Boolean) }
155167
def little_endian?
156168
!big_endian?
157169
end
158170

171+
sig { returns(T::Boolean) }
159172
def big_endian?
160173
[1].pack("I") == [1].pack("N")
161174
end
162175

176+
sig { returns(FalseClass) }
163177
def virtualized?
164178
false
165179
end
166180

181+
sig { returns(T::Array[Symbol]) }
167182
def features
168183
[]
169184
end
170185

186+
sig { params(name: Symbol).returns(T::Boolean) }
171187
def feature?(name)
172188
features.include?(name)
173189
end
174190

191+
sig { params(arch: T.any(String, Symbol)).returns(String) }
175192
def arch_flag(arch)
176193
return "-mcpu=#{arch}" if ppc?
177194

@@ -186,6 +203,7 @@ def in_rosetta2?
186203
end
187204

188205
class << self
206+
sig { returns(String) }
189207
def cores_as_words
190208
case Hardware::CPU.cores
191209
when 1 then "single"
@@ -195,7 +213,7 @@ def cores_as_words
195213
when 8 then "octa"
196214
when 12 then "dodeca"
197215
else
198-
Hardware::CPU.cores
216+
Hardware::CPU.cores.to_s
199217
end
200218
end
201219

@@ -231,12 +249,12 @@ def rustflags_target_cpu(arch)
231249
# Rust already defaults to the oldest supported cpu for each target-triplet
232250
# so it's safe to ignore generic archs such as :armv6 here.
233251
# Rust defaults to apple-m1 since Rust 1.71 for aarch64-apple-darwin.
234-
@target_cpu ||= case arch
252+
@target_cpu ||= T.let(case arch
235253
when :core
236254
:prescott
237255
when :native, :ivybridge, :sandybridge, :westmere, :nehalem, :core2
238256
arch
239-
end
257+
end, T.nilable(Symbol))
240258
return if @target_cpu.blank?
241259

242260
"--codegen target-cpu=#{@target_cpu}"

0 commit comments

Comments
 (0)