brew/Library/Homebrew/cxxstdlib.rb

86 lines
2.4 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: true
# frozen_string_literal: true
require "compilers"
2020-08-14 03:39:31 +02:00
# Combination of C++ standard library and compiler.
class CxxStdlib
2020-10-20 12:03:48 +02:00
extend T::Sig
include CompilerConstants
2020-08-14 03:39:31 +02:00
# Error for when a formula's dependency was built with a different C++ standard library.
class CompatibilityError < StandardError
def initialize(formula, dep, stdlib)
2017-10-15 02:28:32 +02:00
super <<~EOS
2015-05-27 21:33:11 +08:00
#{formula.full_name} dependency #{dep.name} was built with a different C++ standard
library (#{stdlib.type_string} from #{stdlib.compiler}). This may cause problems at runtime.
2018-06-06 23:34:19 -04:00
EOS
end
end
def self.create(type, compiler)
2020-12-01 17:04:59 +00:00
raise ArgumentError, "Invalid C++ stdlib type: #{type}" if type && [:libstdcxx, :libcxx].exclude?(type)
2018-09-17 02:45:00 +02:00
2021-01-29 09:11:35 +00:00
apple_compiler = !compiler.to_s.match?(GNU_GCC_REGEXP)
2020-08-14 03:39:31 +02:00
CxxStdlib.new(type, compiler, apple_compiler)
end
def self.check_compatibility(formula, deps, keg, compiler)
return if formula.skip_cxxstdlib_check?
stdlib = create(keg.detect_cxx_stdlibs.first, compiler)
begin
stdlib.check_dependencies(formula, deps)
rescue CompatibilityError => e
opoo e.message
end
end
attr_reader :type, :compiler
2020-08-14 03:39:31 +02:00
def initialize(type, compiler, apple_compiler)
@type = type
@compiler = compiler.to_sym
2020-08-14 03:39:31 +02:00
@apple_compiler = apple_compiler
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, nor between GNU compiler versions.
def compatible_with?(other)
return true if type.nil? || other.type.nil?
return false unless type == other.type
2014-08-02 20:09:42 -05:00
apple_compiler? && other.apple_compiler? ||
!other.apple_compiler? && compiler.to_s[4..6] == other.compiler.to_s[4..6]
end
def check_dependencies(formula, deps)
deps.each do |dep|
# Software is unlikely to link against libraries from build-time deps, so
# it doesn't matter if they link against different C++ stdlibs.
next if dep.build?
dep_stdlib = Tab.for_formula(dep.to_formula).cxxstdlib
raise CompatibilityError.new(formula, dep, dep_stdlib) unless compatible_with? dep_stdlib
end
end
def type_string
type.to_s.gsub(/cxx$/, "c++")
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2014-08-03 15:28:26 -05:00
def inspect
"#<#{self.class.name}: #{compiler} #{type}>"
end
2020-08-14 03:39:31 +02:00
def apple_compiler?
@apple_compiler
end
end