diff --git a/Library/Homebrew/extend/os/mac/hardware/cpu.rb b/Library/Homebrew/extend/os/mac/hardware/cpu.rb index 03269d48fd..919c47bc84 100644 --- a/Library/Homebrew/extend/os/mac/hardware/cpu.rb +++ b/Library/Homebrew/extend/os/mac/hardware/cpu.rb @@ -63,6 +63,14 @@ module Hardware [arch_64_bit, arch_32_bit].extend ArchitectureListExtension end + # True when running under an Intel-based shell via Rosetta on an + # Apple Silicon Mac. This can be detected via seeing if there's a + # conflict between what `uname` report and the underlying `sysctl` flags, + # since the `sysctl` flags don't change behaviour under Rosetta. + def running_under_rosetta? + intel? && physical_cpu_arm64? + end + def features @features ||= sysctl_n( "machdep.cpu.features", @@ -109,6 +117,12 @@ module Hardware private + # Note: this is more reliable than checking uname. + # `sysctl` returns the right answer even when running in Rosetta. + def physical_cpu_arm64? + sysctl_bool("hw.optional.arm64") + end + def sysctl_bool(key) sysctl_int(key) == 1 end diff --git a/Library/Homebrew/hardware.rb b/Library/Homebrew/hardware.rb index 5d93e7df92..4d02fef9aa 100644 --- a/Library/Homebrew/hardware.rb +++ b/Library/Homebrew/hardware.rb @@ -154,6 +154,10 @@ module Hardware "-march=#{arch}" end + + def running_under_rosetta? + false + end end end