aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cxxstdlib.rb
blob: 34ce92d99f49de526d0a9ebd574851ac4f26ada5 (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
require "compilers"

class CxxStdlib
  include CompilerConstants

  def self.create(type, compiler)
    if type && ![:libstdcxx, :libcxx].include?(type)
      raise ArgumentError, "Invalid C++ stdlib type: #{type}"
    end
    klass = GNU_GCC_REGEXP === compiler.to_s ? GnuStdlib : AppleStdlib
    klass.new(type, compiler)
  end

  attr_reader :type, :compiler

  def initialize(type, compiler)
    @type = type
    @compiler = compiler.to_sym
  end

  # If either package doesn't use C++, all is well
  # libstdc++ and libc++ aren't ever intercompatible
  # libstdc++ is compatible across Apple compilers, but
  # not between Apple and GNU compilers, or between GNU compiler versions
  def compatible_with?(other)
    return true if type.nil? || other.type.nil?

    return false unless type == other.type

    if apple_compiler?
      return false unless other.apple_compiler?
    else
      return false if other.apple_compiler?
      return false unless compiler.to_s[4..6] == other.compiler.to_s[4..6]
    end

    true
  end

  def check_dependencies(formula, deps)
    unless formula.skip_cxxstdlib_check?
      deps.each do |dep|
        # Software is unlikely to link against anything from its
        # buildtime deps, so it doesn't matter at all if they link
        # against different C++ stdlibs
        next if dep.build?

        dep_stdlib = Tab.for_formula(dep.to_formula).cxxstdlib
        if !compatible_with? dep_stdlib
          raise IncompatibleCxxStdlibs.new(formula, dep, dep_stdlib, self)
        end
      end
    end
  end

  def type_string
    type.to_s.gsub(/cxx$/, 'c++')
  end

  class AppleStdlib < CxxStdlib
    def apple_compiler?
      true
    end
  end

  class GnuStdlib < CxxStdlib
    def apple_compiler?
      false
    end
  end
end