| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "mach" | 
					
						
							| 
									
										
										
										
											2013-08-01 20:24:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | module MacCPUs | 
					
						
							|  |  |  |   OPTIMIZATION_FLAGS = { | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     :penryn => "-march=core2 -msse4.1", | 
					
						
							|  |  |  |     :core2 => "-march=core2", | 
					
						
							|  |  |  |     :core => "-march=prescott", | 
					
						
							|  |  |  |     :g3 => "-mcpu=750", | 
					
						
							|  |  |  |     :g4 => "-mcpu=7400", | 
					
						
							|  |  |  |     :g4e => "-mcpu=7450", | 
					
						
							|  |  |  |     :g5 => "-mcpu=970" | 
					
						
							| 
									
										
										
										
											2013-06-13 11:57:13 -05:00
										 |  |  |   }.freeze | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def optimization_flags | 
					
						
							|  |  |  |     OPTIMIZATION_FLAGS | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |   # These methods use info spewed out by sysctl. | 
					
						
							|  |  |  |   # Look in <mach/machine.h> for decoding info. | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   def type | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  |     case sysctl_int("hw.cputype") | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |     when 7
 | 
					
						
							|  |  |  |       :intel | 
					
						
							|  |  |  |     when 18
 | 
					
						
							|  |  |  |       :ppc | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       :dunno | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   def family | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  |     if intel? | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  |       case sysctl_int("hw.cpufamily") | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |       when 0x73d67300 # Yonah: Core Solo/Duo | 
					
						
							|  |  |  |         :core | 
					
						
							|  |  |  |       when 0x426f69ef # Merom: Core 2 Duo | 
					
						
							|  |  |  |         :core2 | 
					
						
							|  |  |  |       when 0x78ea4fbc # Penryn | 
					
						
							|  |  |  |         :penryn | 
					
						
							|  |  |  |       when 0x6b5a4cd2 # Nehalem | 
					
						
							|  |  |  |         :nehalem | 
					
						
							|  |  |  |       when 0x573B5EEC # Arrandale | 
					
						
							|  |  |  |         :arrandale | 
					
						
							|  |  |  |       when 0x5490B78C # Sandy Bridge | 
					
						
							|  |  |  |         :sandybridge | 
					
						
							|  |  |  |       when 0x1F65E835 # Ivy Bridge | 
					
						
							|  |  |  |         :ivybridge | 
					
						
							| 
									
										
										
										
											2013-07-01 00:44:54 -05:00
										 |  |  |       when 0x10B282DC # Haswell | 
					
						
							|  |  |  |         :haswell | 
					
						
							| 
									
										
										
										
											2015-04-24 15:27:38 +09:00
										 |  |  |       when 0x582ed09c # Broadwell | 
					
						
							|  |  |  |         :broadwell | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |       else | 
					
						
							|  |  |  |         :dunno | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  |     elsif ppc? | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  |       case sysctl_int("hw.cpusubtype") | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |       when 9
 | 
					
						
							|  |  |  |         :g3  # PowerPC 750 | 
					
						
							|  |  |  |       when 10
 | 
					
						
							|  |  |  |         :g4  # PowerPC 7400 | 
					
						
							|  |  |  |       when 11
 | 
					
						
							|  |  |  |         :g4e # PowerPC 7450 | 
					
						
							|  |  |  |       when 100
 | 
					
						
							| 
									
										
										
										
											2013-10-03 20:05:32 -07:00
										 |  |  |         # This is the only 64-bit PPC CPU type, so it's useful | 
					
						
							| 
									
										
										
										
											2014-04-26 23:31:39 -07:00
										 |  |  |         # to distinguish in `brew config` output and in bottle tags | 
					
						
							| 
									
										
										
										
											2015-08-06 15:45:52 +08:00
										 |  |  |         MacOS.prefer_64_bit? ? :g5_64 : :g5 # PowerPC 970 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |       else | 
					
						
							|  |  |  |         :dunno | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 19:14:07 -07:00
										 |  |  |   def extmodel | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  |     sysctl_int("machdep.cpu.extmodel") | 
					
						
							| 
									
										
										
										
											2013-10-30 19:14:07 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   def cores | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  |     sysctl_int("hw.ncpu") | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def bits | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  |     sysctl_bool("hw.cpu64bit_capable") ? 64 : 32
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-01 20:19:00 -07:00
										 |  |  |   def arch_32_bit | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  |     intel? ? :i386 : :ppc | 
					
						
							| 
									
										
										
										
											2013-08-01 20:19:00 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def arch_64_bit | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  |     intel? ? :x86_64 : :ppc64 | 
					
						
							| 
									
										
										
										
											2013-08-01 20:19:00 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-01 20:24:18 -07:00
										 |  |  |   # Returns an array that's been extended with ArchitectureListExtension, | 
					
						
							|  |  |  |   # which provides helpers like #as_arch_flags and #as_cmake_arch_flags. | 
					
						
							|  |  |  |   def universal_archs | 
					
						
							|  |  |  |     # Building 64-bit is a no-go on Tiger, and pretty hit or miss on Leopard. | 
					
						
							|  |  |  |     # Don't even try unless Tigerbrew's experimental 64-bit Leopard support is enabled. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     if MacOS.version <= :leopard && !MacOS.prefer_64_bit? | 
					
						
							| 
									
										
										
										
											2013-08-01 20:24:18 -07:00
										 |  |  |       [arch_32_bit].extend ArchitectureListExtension | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       [arch_32_bit, arch_64_bit].extend ArchitectureListExtension | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 21:57:03 -07:00
										 |  |  |   def features | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:23 -05:00
										 |  |  |     @features ||= sysctl_n( | 
					
						
							|  |  |  |       "machdep.cpu.features", | 
					
						
							|  |  |  |       "machdep.cpu.extfeatures", | 
					
						
							|  |  |  |       "machdep.cpu.leaf7_features" | 
					
						
							|  |  |  |     ).split(" ").map { |s| s.downcase.to_sym } | 
					
						
							| 
									
										
										
										
											2014-06-21 21:57:03 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  |   def aes? | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     sysctl_bool("hw.optional.aes") | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   def altivec? | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     sysctl_bool("hw.optional.altivec") | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-07 12:47:40 -05:00
										 |  |  |   def avx? | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     sysctl_bool("hw.optional.avx1_0") | 
					
						
							| 
									
										
										
										
											2013-04-07 12:47:40 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  |   def avx2? | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     sysctl_bool("hw.optional.avx2_0") | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   def sse3? | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     sysctl_bool("hw.optional.sse3") | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 11:24:07 -07:00
										 |  |  |   def ssse3? | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     sysctl_bool("hw.optional.supplementalsse3") | 
					
						
							| 
									
										
										
										
											2013-10-30 11:24:07 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   def sse4? | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     sysctl_bool("hw.optional.sse4_1") | 
					
						
							| 
									
										
										
										
											2013-07-02 16:08:03 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def sse4_2? | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     sysctl_bool("hw.optional.sse4_2") | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:23 -05:00
										 |  |  |   private | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  |   def sysctl_bool(key) | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:23 -05:00
										 |  |  |     sysctl_int(key) == 1
 | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def sysctl_int(key) | 
					
						
							|  |  |  |     sysctl_n(key).to_i | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:23 -05:00
										 |  |  |   def sysctl_n(*keys) | 
					
						
							|  |  |  |     (@properties ||= {}).fetch(keys) do | 
					
						
							|  |  |  |       @properties[keys] = Utils.popen_read("/usr/sbin/sysctl", "-n", *keys) | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-02-26 23:22:22 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  | end |