brew/Library/Homebrew/compilers.rb
Misty De Meo ef1d9c0cd0 Implement fails_with for non-Apple compilers
This adds support for non-Apple GCC compilers in the fails_with code.
A fails_with block for a non-Apple compiler looks like:

fails_with :gcc => '4.8.1' do
  cause 'Foo'
end

Non-Apple compilers don't have build numbers, so compiler failures are
based on version strings instead.

Internally non-Apple compilers can be distinguished because they are
passed around as strings instead of symbols.

In addition, this alters the priority list for compilers, with the
following changes:

* Apple GCC 4.2 and LLVM-GCC swap positions, with GCC now taking
  priority. (Maybe LLVM-GCC should just go away.)
* Non-Apple GCC compilers are ranked below GCC 4.2 but above LLVM-GCC
  and Apple GCC 4.0.
2013-09-01 13:19:13 -07:00

95 lines
2.0 KiB
Ruby

class Compiler < Struct.new(:name, :priority)
def build
MacOS.send("#{name}_build_version")
end
def version
MacOS.non_apple_gcc_version(name) if name.is_a? String
end
end
class CompilerFailure
attr_reader :compiler, :version
attr_rw :build, :cause
def initialize compiler, &block
# 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
_, @version = compiler.shift
@compiler = 'gcc-' + @version.match(/(\d\.\d)/)[0]
else
@compiler = compiler
end
instance_eval(&block) if block_given?
@build = (@build || 9999).to_i unless compiler.is_a? Hash
end
end
class CompilerQueue
def initialize
@array = []
end
def <<(o)
@array << o
self
end
def pop
@array.delete(@array.max { |a, b| a.priority <=> b.priority })
end
def empty?
@array.empty?
end
end
class CompilerSelector
def initialize(f)
@f = f
@compilers = CompilerQueue.new
%w{clang llvm gcc gcc_4_0}.map(&:to_sym).each do |cc|
unless MacOS.send("#{cc}_build_version").nil?
@compilers << Compiler.new(cc, priority_for(cc))
end
end
# 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
end
# Attempts to select an appropriate alternate compiler, but
# if none can be found raises CompilerError instead
def compiler
begin
cc = @compilers.pop
end while @f.fails_with?(cc)
if cc.nil?
raise CompilerSelectionError
else
cc.name
end
end
private
def priority_for(cc)
case cc
when :clang then MacOS.clang_build_version >= 318 ? 3 : 0.5
when :gcc then 2
when :llvm then 1
when :gcc_4_0 then 0.25
# non-Apple gcc compilers
else 1.5
end
end
end