aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/compilers.rb
blob: e73f39f0188037f44263acae766273fba56f6ed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
module CompilerConstants
  GNU_GCC_VERSIONS = 3..9
  GNU_GCC_REGEXP = /^gcc-(4\.[3-9])$/
end

class CompilerFailure
  attr_reader :name
  attr_rw :version

  # Allows Apple compiler `fails_with` statements to keep using `build`
  # even though `build` and `version` are the same internally
  alias_method :build, :version

  # The cause is no longer used so we need not hold a reference to the string
  def cause(_); end

  def self.for_standard standard
    COLLECTIONS.fetch(standard) do
      raise ArgumentError, "\"#{standard}\" is not a recognized standard"
    end
  end

  def self.create(spec, &block)
    # Non-Apple compilers are in the format fails_with compiler => version
    if spec.is_a?(Hash)
      _, major_version = spec.each { |e| break e }
      name = "gcc-#{major_version}"
      # so fails_with :gcc => '4.8' simply marks all 4.8 releases incompatible
      version = "#{major_version}.999"
    else
      name = spec
      version = 9999
    end
    new(name, version, &block)
  end

  def initialize(name, version, &block)
    @name = name
    @version = version
    instance_eval(&block) if block_given?
  end

  def ===(compiler)
    name == compiler.name && version >= compiler.version
  end

  def inspect
    "#<#{self.class.name}: #{name} #{version}>"
  end

  COLLECTIONS = {
    :cxx11 => [
      create(:gcc_4_0),
      create(:gcc),
      create(:llvm),
      create(:clang) { build 425 },
      create(:gcc => "4.3"),
      create(:gcc => "4.4"),
      create(:gcc => "4.5"),
      create(:gcc => "4.6"),
    ],
    :openmp => [
      create(:clang),
      create(:llvm),
    ],
  }
end

class CompilerSelector
  include CompilerConstants

  Compiler = Struct.new(:name, :version)

  COMPILER_PRIORITY = {
    :clang   => [:clang, :gcc, :llvm, :gnu, :gcc_4_0],
    :gcc     => [:gcc, :llvm, :gnu, :clang, :gcc_4_0],
    :llvm    => [:llvm, :gcc, :gnu, :clang, :gcc_4_0],
    :gcc_4_0 => [:gcc_4_0, :gcc, :llvm, :gnu, :clang],
  }

  def self.select_for(formula, compilers=self.compilers)
    new(formula, MacOS, compilers).compiler
  end

  def self.compilers
    COMPILER_PRIORITY.fetch(MacOS.default_compiler)
  end

  attr_reader :formula, :failures, :versions, :compilers

  def initialize(formula, versions, compilers)
    @formula = formula
    @failures = formula.compiler_failures
    @versions = versions
    @compilers = compilers
  end

  def compiler
    find_compiler { |c| return c.name unless fails_with?(c) }
    raise CompilerSelectionError.new(formula)
  end

  private

  def find_compiler
    compilers.each do |compiler|
      case compiler
      when :gnu
        GNU_GCC_VERSIONS.reverse_each do |v|
          name = "gcc-4.#{v}"
          version = compiler_version(name)
          yield Compiler.new(name, version) if version
        end
      else
        version = compiler_version(compiler)
        yield Compiler.new(compiler, version) if version
      end
    end
  end

  def fails_with?(compiler)
    failures.any? { |failure| failure === compiler }
  end

  def compiler_version(name)
    case name
    when GNU_GCC_REGEXP
      versions.non_apple_gcc_version(name)
    else
      versions.send("#{name}_build_version")
    end
  end
end