aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorMisty De Meo2017-04-17 18:01:33 +0800
committerGitHub2017-04-17 18:01:33 +0800
commit1deb884314e875872032b86e327dc9f79e2cf042 (patch)
tree564796012b89f2b47ead98cf3d38c676ccfe41ed /Library
parent46f6dc4c93b96abd528c3a0a808a5ca5a795c7f3 (diff)
parent944bff4de2ec3821480ee9097e06016cce2b2925 (diff)
downloadbrew-1deb884314e875872032b86e327dc9f79e2cf042.tar.bz2
Merge pull request #2496 from mistydemeo/hardware_cpu_can_run_arch_helper
Hardware cpu can run arch helper
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/extend/os/mac/hardware/cpu.rb43
-rw-r--r--Library/Homebrew/hardware.rb12
-rw-r--r--Library/Homebrew/test/hardware_spec.rb47
-rw-r--r--Library/Homebrew/test/os/mac/hardware_spec.rb56
4 files changed, 158 insertions, 0 deletions
diff --git a/Library/Homebrew/extend/os/mac/hardware/cpu.rb b/Library/Homebrew/extend/os/mac/hardware/cpu.rb
index f180995fb..22d118e1a 100644
--- a/Library/Homebrew/extend/os/mac/hardware/cpu.rb
+++ b/Library/Homebrew/extend/os/mac/hardware/cpu.rb
@@ -107,6 +107,20 @@ module Hardware
end
end
+ # Determines whether the current CPU and macOS combination
+ # can run an executable of the specified architecture.
+ # `arch` is a symbol in the same format returned by
+ # Hardware::CPU.family
+ def can_run?(arch)
+ if Hardware::CPU.intel?
+ intel_can_run? arch
+ elsif Hardware::CPU.ppc?
+ ppc_can_run? arch
+ else
+ false
+ end
+ end
+
def features
@features ||= sysctl_n(
"machdep.cpu.features",
@@ -162,6 +176,35 @@ module Hardware
@properties[keys] = Utils.popen_read("/usr/sbin/sysctl", "-n", *keys)
end
end
+
+ def intel_can_run?(arch)
+ case arch
+ when :ppc
+ # Rosetta is still available
+ MacOS.version < :lion
+ when :ppc64
+ # Rosetta never supported PPC64
+ false
+ when :x86_64
+ Hardware::CPU.is_64_bit?
+ when :i386
+ true
+ else # dunno
+ false
+ end
+ end
+
+ def ppc_can_run?(arch)
+ case arch
+ when :ppc
+ true
+ when :ppc64
+ Hardware::CPU.is_64_bit?
+ else
+ # Intel is never supported
+ false
+ end
+ end
end
end
end
diff --git a/Library/Homebrew/hardware.rb b/Library/Homebrew/hardware.rb
index fe07aee9d..997598def 100644
--- a/Library/Homebrew/hardware.rb
+++ b/Library/Homebrew/hardware.rb
@@ -80,6 +80,18 @@ module Hardware
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
diff --git a/Library/Homebrew/test/hardware_spec.rb b/Library/Homebrew/test/hardware_spec.rb
index c5f8daf4e..de8d77e68 100644
--- a/Library/Homebrew/test/hardware_spec.rb
+++ b/Library/Homebrew/test/hardware_spec.rb
@@ -36,5 +36,52 @@ module Hardware
).to include(described_class.family)
end
end
+
+ describe "::can_run?" do
+ it "reports that Intel machines can run Intel executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :intel
+ allow(Hardware::CPU).to receive(:bits).and_return 64
+ expect(Hardware::CPU.can_run?(:i386)).to be true
+ expect(Hardware::CPU.can_run?(:x86_64)).to be true
+ end
+
+ it "reports that PowerPC machines can run PowerPC executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :ppc
+ allow(Hardware::CPU).to receive(:bits).and_return 64
+ expect(Hardware::CPU.can_run?(:ppc)).to be true
+ expect(Hardware::CPU.can_run?(:ppc64)).to be true
+ end
+
+ it "reports that 32-bit Intel machines can't run x86_64 executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :intel
+ allow(Hardware::CPU).to receive(:bits).and_return 32
+ expect(Hardware::CPU.can_run?(:x86_64)).to be false
+ end
+
+ it "reports that 32-bit PowerPC machines can't run ppc64 executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :ppc
+ allow(Hardware::CPU).to receive(:bits).and_return 32
+ expect(Hardware::CPU.can_run?(:ppc64)).to be false
+ end
+
+ it "identifies that Intel and PowerPC machines can't run each others' executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :ppc
+ expect(Hardware::CPU.can_run?(:i386)).to be false
+ expect(Hardware::CPU.can_run?(:x86_64)).to be false
+
+ allow(Hardware::CPU).to receive(:type).and_return :intel
+ expect(Hardware::CPU.can_run?(:ppc)).to be false
+ expect(Hardware::CPU.can_run?(:ppc64)).to be false
+ end
+
+ it "returns false for unknown CPU types" do
+ allow(Hardware::CPU).to receive(:type).and_return :dunno
+ expect(Hardware::CPU.can_run?(:i386)).to be false
+ end
+
+ it "returns false for unknown arches" do
+ expect(Hardware::CPU.can_run?(:blah)).to be false
+ end
+ end
end
end
diff --git a/Library/Homebrew/test/os/mac/hardware_spec.rb b/Library/Homebrew/test/os/mac/hardware_spec.rb
new file mode 100644
index 000000000..fa577ba7d
--- /dev/null
+++ b/Library/Homebrew/test/os/mac/hardware_spec.rb
@@ -0,0 +1,56 @@
+require "hardware"
+require "extend/os/mac/hardware/cpu"
+
+describe Hardware::CPU do
+ describe "::can_run?" do
+ it "reports that Intel Macs can run Intel executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :intel
+ allow(Hardware::CPU).to receive(:bits).and_return 64
+ expect(Hardware::CPU.can_run?(:i386)).to be true
+ expect(Hardware::CPU.can_run?(:x86_64)).to be true
+ end
+
+ it "reports that PowerPC Macs can run PowerPC executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :ppc
+ allow(Hardware::CPU).to receive(:bits).and_return 64
+ expect(Hardware::CPU.can_run?(:ppc)).to be true
+ expect(Hardware::CPU.can_run?(:ppc64)).to be true
+ end
+
+ it "reports that 32-bit Intel Macs can't run x86_64 executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :intel
+ allow(Hardware::CPU).to receive(:bits).and_return 32
+ expect(Hardware::CPU.can_run?(:x86_64)).to be false
+ end
+
+ it "reports that 32-bit PowerPC Macs can't run ppc64 executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :ppc
+ allow(Hardware::CPU).to receive(:bits).and_return 32
+ expect(Hardware::CPU.can_run?(:ppc64)).to be false
+ end
+
+ it "reports that Intel Macs can only run 32-bit PowerPC executables on 10.6 and older" do
+ allow(Hardware::CPU).to receive(:type).and_return :intel
+ allow(OS::Mac).to receive(:version).and_return OS::Mac::Version.new "10.6"
+ expect(Hardware::CPU.can_run?(:ppc)).to be true
+
+ allow(OS::Mac).to receive(:version).and_return OS::Mac::Version.new "10.7"
+ expect(Hardware::CPU.can_run?(:ppc)).to be false
+ end
+
+ it "reports that PowerPC Macs can't run Intel executables" do
+ allow(Hardware::CPU).to receive(:type).and_return :ppc
+ expect(Hardware::CPU.can_run?(:i386)).to be false
+ expect(Hardware::CPU.can_run?(:x86_64)).to be false
+ end
+
+ it "returns false for unknown CPU types" do
+ allow(Hardware::CPU).to receive(:type).and_return :dunno
+ expect(Hardware::CPU.can_run?(:i386)).to be false
+ end
+
+ it "returns false for unknown arches" do
+ expect(Hardware::CPU.can_run?(:blah)).to be false
+ end
+ end
+end