brew/Library/Homebrew/hardware.rb

248 lines
5.5 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
2020-04-05 15:44:50 +01:00
require "utils/popen"
2020-08-09 03:23:53 +02:00
# Helper module for querying hardware information.
2016-04-25 18:00:01 +01:00
module Hardware
2020-08-09 03:23:53 +02:00
# Helper module for querying CPU information.
2016-04-25 18:00:01 +01:00
class CPU
INTEL_32BIT_ARCHS = [:i386].freeze
INTEL_64BIT_ARCHS = [:x86_64].freeze
2022-11-15 05:22:42 +00:00
INTEL_ARCHS = (INTEL_32BIT_ARCHS + INTEL_64BIT_ARCHS).freeze
2018-04-14 03:01:33 +02:00
PPC_32BIT_ARCHS = [:ppc, :ppc32, :ppc7400, :ppc7450, :ppc970].freeze
PPC_64BIT_ARCHS = [:ppc64, :ppc64le, :ppc970].freeze
2022-11-15 05:22:42 +00:00
PPC_ARCHS = (PPC_32BIT_ARCHS + PPC_64BIT_ARCHS).freeze
ARM_64BIT_ARCHS = [:arm64, :aarch64].freeze
2022-11-15 05:22:42 +00:00
ARM_ARCHS = ARM_64BIT_ARCHS
ALL_ARCHS = [
2022-11-15 05:22:42 +00:00
*INTEL_ARCHS,
*PPC_ARCHS,
*ARM_ARCHS,
].freeze
INTEL_64BIT_OLDEST_CPU = :core2
2016-04-25 18:00:01 +01:00
class << self
def optimization_flags
@optimization_flags ||= {
native: arch_flag("native"),
ivybridge: "-march=ivybridge",
sandybridge: "-march=sandybridge",
westmere: "-march=westmere",
nehalem: "-march=nehalem",
core2: "-march=core2",
core: "-march=prescott",
arm_vortex_tempest: "", # TODO: -mcpu=apple-m1 when we've patched all our GCCs to support it
armv6: "-march=armv6",
armv8: "-march=armv8-a",
ppc64: "-mcpu=powerpc64",
ppc64le: "-mcpu=powerpc64le",
}.freeze
end
alias generic_optimization_flags optimization_flags
2020-10-20 12:03:48 +02:00
sig { returns(Symbol) }
def arch_32_bit
if arm?
:arm
elsif intel?
:i386
elsif ppc32?
:ppc32
else
:dunno
end
end
2020-10-20 12:03:48 +02:00
sig { returns(Symbol) }
def arch_64_bit
if arm?
:arm64
elsif intel?
:x86_64
elsif ppc64le?
:ppc64le
elsif ppc64?
:ppc64
else
:dunno
end
end
def arch
case bits
when 32
arch_32_bit
when 64
arch_64_bit
else
:dunno
end
end
2020-10-20 12:03:48 +02:00
sig { returns(Symbol) }
2016-04-25 18:00:01 +01:00
def type
case RUBY_PLATFORM
when /x86_64/, /i\d86/ then :intel
2019-02-21 21:14:04 -08:00
when /arm/, /aarch64/ then :arm
when /ppc|powerpc/ then :ppc
else :dunno
end
2016-04-25 18:00:01 +01:00
end
2020-10-20 12:03:48 +02:00
sig { returns(Symbol) }
2016-04-25 18:00:01 +01:00
def family
:dunno
end
2016-04-25 18:00:01 +01:00
def cores
return @cores if @cores
2018-09-17 02:45:00 +02:00
@cores = Utils.popen_read("getconf", "_NPROCESSORS_ONLN").chomp.to_i
@cores = 1 unless $CHILD_STATUS.success?
@cores
2016-04-25 18:00:01 +01:00
end
2016-04-25 18:00:01 +01:00
def bits
@bits ||= case RUBY_PLATFORM
when /x86_64/, /ppc64|powerpc64/, /aarch64|arm64/ then 64
when /i\d86/, /ppc/, /arm/ then 32
end
end
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
def sse4?
RUBY_PLATFORM.to_s.include?("x86_64")
2016-04-25 18:00:01 +01:00
end
2016-04-25 18:00:01 +01:00
def is_32_bit?
bits == 32
end
2016-04-25 18:00:01 +01:00
def is_64_bit?
bits == 64
end
2016-04-25 18:00:01 +01:00
def intel?
type == :intel
end
2016-04-25 18:00:01 +01:00
def ppc?
type == :ppc
end
def ppc32?
ppc? && is_32_bit?
end
def ppc64le?
ppc? && is_64_bit? && little_endian?
end
def ppc64?
ppc? && is_64_bit? && big_endian?
end
2024-04-22 21:05:48 +02:00
# Check whether the CPU architecture is ARM.
#
# @api internal
sig { returns(T::Boolean) }
def arm?
type == :arm
end
def little_endian?
!big_endian?
end
def big_endian?
[1].pack("I") == [1].pack("N")
end
def virtualized?
false
end
2016-04-25 18:00:01 +01:00
def features
[]
end
2015-02-23 21:38:36 -05:00
2016-04-25 18:00:01 +01:00
def feature?(name)
features.include?(name)
end
def arch_flag(arch)
return "-mcpu=#{arch}" if ppc?
"-march=#{arch}"
end
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
def in_rosetta2?
false
end
2015-02-23 21:38:36 -05:00
end
end
class << self
def 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"
when 12 then "dodeca"
2013-06-06 16:02:27 -05:00
else
Hardware::CPU.cores
2013-06-06 16:02:27 -05:00
end
end
def oldest_cpu(_version = nil)
if Hardware::CPU.intel?
if Hardware::CPU.is_64_bit?
Hardware::CPU::INTEL_64BIT_OLDEST_CPU
else
:core
end
elsif Hardware::CPU.arm?
if Hardware::CPU.is_64_bit?
:armv8
else
:armv6
end
elsif Hardware::CPU.ppc? && Hardware::CPU.is_64_bit?
if Hardware::CPU.little_endian?
:ppc64le
else
:ppc64
end
else
Hardware::CPU.family
end
2013-06-06 16:02:27 -05:00
end
alias generic_oldest_cpu oldest_cpu
2023-06-13 19:22:12 +01:00
# Returns a Rust flag to set the target CPU if necessary.
# Defaults to nil.
2024-06-30 02:23:29 -04:00
sig { params(arch: Symbol).returns(T.nilable(String)) }
def rustflags_target_cpu(arch)
# Rust already defaults to the oldest supported cpu for each target-triplet
# 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.
2024-06-30 02:35:29 -04:00
@target_cpu ||= case arch
when :core
:prescott
when :native, :ivybridge, :sandybridge, :westmere, :nehalem, :core2
2024-06-30 02:23:29 -04:00
arch
end
return if @target_cpu.blank?
2023-06-20 23:48:10 +01:00
"--codegen target-cpu=#{@target_cpu}"
2023-06-13 19:22:12 +01:00
end
2013-06-06 16:02:27 -05:00
end
end
2016-04-25 18:00:01 +01:00
require "extend/os/hardware"