hardware.rb: use typed: strict
Signed-off-by: Patrick Linnane <patrick@linnane.io>
This commit is contained in:
parent
f9b81af2ea
commit
d992a4f923
@ -1,4 +1,4 @@
|
|||||||
# typed: true # rubocop:todo Sorbet/StrictSigil
|
# typed: strict
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "utils/popen"
|
require "utils/popen"
|
||||||
@ -9,23 +9,24 @@ module Hardware
|
|||||||
class CPU
|
class CPU
|
||||||
INTEL_32BIT_ARCHS = [:i386].freeze
|
INTEL_32BIT_ARCHS = [:i386].freeze
|
||||||
INTEL_64BIT_ARCHS = [:x86_64].freeze
|
INTEL_64BIT_ARCHS = [:x86_64].freeze
|
||||||
INTEL_ARCHS = (INTEL_32BIT_ARCHS + INTEL_64BIT_ARCHS).freeze
|
INTEL_ARCHS = T.let((INTEL_32BIT_ARCHS + INTEL_64BIT_ARCHS).freeze, T::Array[Symbol])
|
||||||
PPC_32BIT_ARCHS = [:ppc, :ppc32, :ppc7400, :ppc7450, :ppc970].freeze
|
PPC_32BIT_ARCHS = [:ppc, :ppc32, :ppc7400, :ppc7450, :ppc970].freeze
|
||||||
PPC_64BIT_ARCHS = [:ppc64, :ppc64le, :ppc970].freeze
|
PPC_64BIT_ARCHS = [:ppc64, :ppc64le, :ppc970].freeze
|
||||||
PPC_ARCHS = (PPC_32BIT_ARCHS + PPC_64BIT_ARCHS).freeze
|
PPC_ARCHS = T.let((PPC_32BIT_ARCHS + PPC_64BIT_ARCHS).freeze, T::Array[Symbol])
|
||||||
ARM_64BIT_ARCHS = [:arm64, :aarch64].freeze
|
ARM_64BIT_ARCHS = [:arm64, :aarch64].freeze
|
||||||
ARM_ARCHS = ARM_64BIT_ARCHS
|
ARM_ARCHS = ARM_64BIT_ARCHS
|
||||||
ALL_ARCHS = [
|
ALL_ARCHS = T.let([
|
||||||
*INTEL_ARCHS,
|
*INTEL_ARCHS,
|
||||||
*PPC_ARCHS,
|
*PPC_ARCHS,
|
||||||
*ARM_ARCHS,
|
*ARM_ARCHS,
|
||||||
].freeze
|
].freeze, T::Array[Symbol])
|
||||||
|
|
||||||
INTEL_64BIT_OLDEST_CPU = :core2
|
INTEL_64BIT_OLDEST_CPU = :core2
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
sig { returns(T::Hash[Symbol, String]) }
|
||||||
def optimization_flags
|
def optimization_flags
|
||||||
@optimization_flags ||= {
|
@optimization_flags ||= T.let({
|
||||||
native: arch_flag("native"),
|
native: arch_flag("native"),
|
||||||
ivybridge: "-march=ivybridge",
|
ivybridge: "-march=ivybridge",
|
||||||
sandybridge: "-march=sandybridge",
|
sandybridge: "-march=sandybridge",
|
||||||
@ -38,7 +39,7 @@ module Hardware
|
|||||||
armv8: "-march=armv8-a",
|
armv8: "-march=armv8-a",
|
||||||
ppc64: "-mcpu=powerpc64",
|
ppc64: "-mcpu=powerpc64",
|
||||||
ppc64le: "-mcpu=powerpc64le",
|
ppc64le: "-mcpu=powerpc64le",
|
||||||
}.freeze
|
}.freeze, T.nilable(T::Hash[Symbol, String]))
|
||||||
end
|
end
|
||||||
alias generic_optimization_flags optimization_flags
|
alias generic_optimization_flags optimization_flags
|
||||||
|
|
||||||
@ -70,6 +71,7 @@ module Hardware
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(Symbol) }
|
||||||
def arch
|
def arch
|
||||||
case bits
|
case bits
|
||||||
when 32
|
when 32
|
||||||
@ -96,19 +98,21 @@ module Hardware
|
|||||||
:dunno
|
:dunno
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T.nilable(Integer)) }
|
||||||
def cores
|
def cores
|
||||||
return @cores if @cores
|
return @cores if @cores
|
||||||
|
|
||||||
@cores = Utils.popen_read("getconf", "_NPROCESSORS_ONLN").chomp.to_i
|
@cores = Utils.popen_read("getconf", "_NPROCESSORS_ONLN").chomp.to_i
|
||||||
@cores = 1 unless $CHILD_STATUS.success?
|
@cores = T.let(1, T.nilable(Integer)) unless $CHILD_STATUS.success?
|
||||||
@cores
|
@cores
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T.nilable(Integer)) }
|
||||||
def bits
|
def bits
|
||||||
@bits ||= case RUBY_PLATFORM
|
@bits ||= T.let(case RUBY_PLATFORM
|
||||||
when /x86_64/, /ppc64|powerpc64/, /aarch64|arm64/ then 64
|
when /x86_64/, /ppc64|powerpc64/, /aarch64|arm64/ then 64
|
||||||
when /i\d86/, /ppc/, /arm/ then 32
|
when /i\d86/, /ppc/, /arm/ then 32
|
||||||
end
|
end, T.nilable(Integer))
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { returns(T::Boolean) }
|
sig { returns(T::Boolean) }
|
||||||
@ -116,30 +120,37 @@ module Hardware
|
|||||||
RUBY_PLATFORM.to_s.include?("x86_64")
|
RUBY_PLATFORM.to_s.include?("x86_64")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def is_32_bit?
|
def is_32_bit?
|
||||||
bits == 32
|
bits == 32
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def is_64_bit?
|
def is_64_bit?
|
||||||
bits == 64
|
bits == 64
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def intel?
|
def intel?
|
||||||
type == :intel
|
type == :intel
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def ppc?
|
def ppc?
|
||||||
type == :ppc
|
type == :ppc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def ppc32?
|
def ppc32?
|
||||||
ppc? && is_32_bit?
|
ppc? && is_32_bit?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def ppc64le?
|
def ppc64le?
|
||||||
ppc? && is_64_bit? && little_endian?
|
ppc? && is_64_bit? && little_endian?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def ppc64?
|
def ppc64?
|
||||||
ppc? && is_64_bit? && big_endian?
|
ppc? && is_64_bit? && big_endian?
|
||||||
end
|
end
|
||||||
@ -152,26 +163,32 @@ module Hardware
|
|||||||
type == :arm
|
type == :arm
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def little_endian?
|
def little_endian?
|
||||||
!big_endian?
|
!big_endian?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def big_endian?
|
def big_endian?
|
||||||
[1].pack("I") == [1].pack("N")
|
[1].pack("I") == [1].pack("N")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(FalseClass) }
|
||||||
def virtualized?
|
def virtualized?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Array[String]) }
|
||||||
def features
|
def features
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(name: T::Array[String]).returns(T::Boolean) }
|
||||||
def feature?(name)
|
def feature?(name)
|
||||||
features.include?(name)
|
features.include?(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(arch: T.any(String, Symbol)).returns(String) }
|
||||||
def arch_flag(arch)
|
def arch_flag(arch)
|
||||||
return "-mcpu=#{arch}" if ppc?
|
return "-mcpu=#{arch}" if ppc?
|
||||||
|
|
||||||
@ -186,6 +203,7 @@ module Hardware
|
|||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
sig { returns(T.nilable(T.any(Integer, String))) }
|
||||||
def cores_as_words
|
def cores_as_words
|
||||||
case Hardware::CPU.cores
|
case Hardware::CPU.cores
|
||||||
when 1 then "single"
|
when 1 then "single"
|
||||||
@ -231,12 +249,12 @@ module Hardware
|
|||||||
# Rust already defaults to the oldest supported cpu for each target-triplet
|
# Rust already defaults to the oldest supported cpu for each target-triplet
|
||||||
# so it's safe to ignore generic archs such as :armv6 here.
|
# so it's safe to ignore generic archs such as :armv6 here.
|
||||||
# Rust defaults to apple-m1 since Rust 1.71 for aarch64-apple-darwin.
|
# Rust defaults to apple-m1 since Rust 1.71 for aarch64-apple-darwin.
|
||||||
@target_cpu ||= case arch
|
@target_cpu ||= T.let(case arch
|
||||||
when :core
|
when :core
|
||||||
:prescott
|
:prescott
|
||||||
when :native, :ivybridge, :sandybridge, :westmere, :nehalem, :core2
|
when :native, :ivybridge, :sandybridge, :westmere, :nehalem, :core2
|
||||||
arch
|
arch
|
||||||
end
|
end, T.nilable(Symbol))
|
||||||
return if @target_cpu.blank?
|
return if @target_cpu.blank?
|
||||||
|
|
||||||
"--codegen target-cpu=#{@target_cpu}"
|
"--codegen target-cpu=#{@target_cpu}"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user