| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  | class Compiler < Struct.new(:name, :priority) | 
					
						
							| 
									
										
										
										
											2013-09-28 12:21:16 -07:00
										 |  |  |   # The full version of the compiler for comparison purposes. | 
					
						
							|  |  |  |   def version | 
					
						
							|  |  |  |     if name.is_a? String | 
					
						
							|  |  |  |       MacOS.non_apple_gcc_version(name) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       MacOS.send("#{name}_build_version") | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-06-28 01:38:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-28 12:21:16 -07:00
										 |  |  |   # This is exposed under the `build` name for compatibility, since | 
					
						
							|  |  |  |   # `fails_with` continues to use `build` in the public API. | 
					
						
							|  |  |  |   # `build` indicates the build number of an Apple compiler. | 
					
						
							|  |  |  |   # This is preferred over version numbers since there are often | 
					
						
							|  |  |  |   # significant differences within the same version, | 
					
						
							|  |  |  |   # e.g. GCC 4.2 build 5553 vs 5666. | 
					
						
							|  |  |  |   # Non-Apple compilers don't have build numbers. | 
					
						
							|  |  |  |   alias_method :build, :version | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # The major version for non-Apple compilers. Used to indicate a compiler | 
					
						
							|  |  |  |   # series; for instance, if the version is 4.8.2, it would return "4.8". | 
					
						
							|  |  |  |   def major_version | 
					
						
							|  |  |  |     version.match(/(\d\.\d)/)[0] if name.is_a? String | 
					
						
							| 
									
										
										
										
											2013-06-28 01:38:09 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CompilerFailure | 
					
						
							| 
									
										
										
										
											2013-09-28 12:21:16 -07:00
										 |  |  |   attr_reader :compiler, :major_version | 
					
						
							|  |  |  |   attr_rw :cause, :version | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def initialize compiler, &block | 
					
						
							| 
									
										
										
										
											2013-06-28 01:38:09 -05:00
										 |  |  |     # Non-Apple compilers are in the format fails_with compiler => version | 
					
						
							|  |  |  |     if compiler.is_a? Hash | 
					
						
							|  |  |  |       # currently the only compiler for this case is GCC | 
					
						
							| 
									
										
										
										
											2013-09-28 12:21:16 -07:00
										 |  |  |       _, @major_version = compiler.shift | 
					
						
							|  |  |  |       @compiler = 'gcc-' + @major_version | 
					
						
							| 
									
										
										
										
											2013-06-28 01:38:09 -05:00
										 |  |  |     else | 
					
						
							|  |  |  |       @compiler = compiler | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  |     instance_eval(&block) if block_given? | 
					
						
							| 
									
										
										
										
											2013-12-12 14:53:53 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if compiler.is_a? Hash | 
					
						
							| 
									
										
										
										
											2013-09-28 12:21:16 -07:00
										 |  |  |       # so fails_with :gcc => '4.8' simply marks all 4.8 releases incompatible | 
					
						
							| 
									
										
										
										
											2013-12-12 14:53:53 -06:00
										 |  |  |       @version ||= @major_version + '.999' | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       @version = (@version || 9999).to_i | 
					
						
							| 
									
										
										
										
											2013-09-28 12:21:16 -07:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-09-28 12:21:16 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Allows Apple compiler `fails_with` statements to keep using `build` | 
					
						
							|  |  |  |   # even though `build` and `value` are the same internally | 
					
						
							|  |  |  |   alias_method :build, :version | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  | class CompilerQueue | 
					
						
							|  |  |  |   def initialize | 
					
						
							|  |  |  |     @array = [] | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  |   def <<(o) | 
					
						
							|  |  |  |     @array << o | 
					
						
							|  |  |  |     self | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def pop | 
					
						
							|  |  |  |     @array.delete(@array.max { |a, b| a.priority <=> b.priority }) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  |   def empty? | 
					
						
							|  |  |  |     @array.empty? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CompilerSelector | 
					
						
							| 
									
										
										
										
											2013-06-12 17:25:00 -05:00
										 |  |  |   def initialize(f) | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  |     @f = f | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  |     @compilers = CompilerQueue.new | 
					
						
							| 
									
										
										
										
											2013-05-20 19:35:48 -05:00
										 |  |  |     %w{clang llvm gcc gcc_4_0}.map(&:to_sym).each do |cc| | 
					
						
							| 
									
										
										
										
											2013-04-01 13:23:09 -05:00
										 |  |  |       unless MacOS.send("#{cc}_build_version").nil? | 
					
						
							|  |  |  |         @compilers << Compiler.new(cc, priority_for(cc)) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-06-28 01:38:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # non-Apple GCC 4.x | 
					
						
							|  |  |  |     SharedEnvExtension::GNU_GCC_VERSIONS.each do |v| | 
					
						
							|  |  |  |       unless MacOS.non_apple_gcc_version("gcc-4.#{v}").nil? | 
					
						
							|  |  |  |         # priority is based on version, with newest preferred first | 
					
						
							|  |  |  |         @compilers << Compiler.new("gcc-4.#{v}", 1.0 + v/10.0) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 19:35:07 -05:00
										 |  |  |   # Attempts to select an appropriate alternate compiler, but | 
					
						
							|  |  |  |   # if none can be found raises CompilerError instead | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  |   def compiler | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  |     begin | 
					
						
							|  |  |  |       cc = @compilers.pop | 
					
						
							|  |  |  |     end while @f.fails_with?(cc) | 
					
						
							| 
									
										
										
										
											2013-05-20 19:35:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if cc.nil? | 
					
						
							| 
									
										
										
										
											2013-12-03 22:16:37 -06:00
										 |  |  |       raise CompilerSelectionError.new(@f) | 
					
						
							| 
									
										
										
										
											2013-05-20 19:35:07 -05:00
										 |  |  |     else | 
					
						
							|  |  |  |       cc.name | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  |   private | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-13 02:07:01 -05:00
										 |  |  |   def priority_for(cc) | 
					
						
							|  |  |  |     case cc | 
					
						
							| 
									
										
										
										
											2013-04-01 12:33:42 -05:00
										 |  |  |     when :clang then MacOS.clang_build_version >= 318 ? 3 : 0.5
 | 
					
						
							| 
									
										
										
										
											2013-09-10 08:17:28 -07:00
										 |  |  |     when :gcc   then 2.5
 | 
					
						
							|  |  |  |     when :llvm  then 2
 | 
					
						
							| 
									
										
										
										
											2013-04-13 01:07:46 -05:00
										 |  |  |     when :gcc_4_0 then 0.25
 | 
					
						
							| 
									
										
										
										
											2013-06-28 01:38:09 -05:00
										 |  |  |     # non-Apple gcc compilers | 
					
						
							|  |  |  |     else 1.5
 | 
					
						
							| 
									
										
										
										
											2012-03-18 13:58:13 -05:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |