brew/Library/Homebrew/compilers.rb

161 lines
3.8 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: true
# frozen_string_literal: true
# @private
module CompilerConstants
2020-05-01 00:59:08 +10:00
GNU_GCC_VERSIONS = %w[4.9 5 6 7 8 9 10].freeze
GNU_GCC_REGEXP = /^gcc-(4\.9|[5-9]|10)$/.freeze
COMPILER_SYMBOL_MAP = {
2018-09-27 18:26:03 -07:00
"gcc" => :gcc,
"clang" => :clang,
"llvm_clang" => :llvm_clang,
}.freeze
2019-04-19 21:46:20 +09:00
COMPILERS = (COMPILER_SYMBOL_MAP.values +
GNU_GCC_VERSIONS.map { |n| "gcc-#{n}" }).freeze
end
# Class for checking compiler compatibility for a formula.
#
# @api private
class CompilerFailure
attr_reader :name
def version(val = nil)
2019-03-25 15:10:35 +00:00
@version = Version.parse(val.to_s) if val
@version
end
2014-06-16 16:08:41 -05:00
# Allows Apple compiler `fails_with` statements to keep using `build`
# even though `build` and `version` are the same internally.
2016-09-23 18:13:48 +02:00
alias build version
2014-06-16 16:08:41 -05:00
# 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.first
2014-08-03 10:47:47 -05:00
name = "gcc-#{major_version}"
# so fails_with :gcc => '7' simply marks all 7 releases incompatible
version = "#{major_version}.999"
else
2014-08-03 10:47:47 -05:00
name = spec
version = 9999
end
new(name, version, &block)
end
def initialize(name, version, &block)
2014-08-03 10:47:47 -05:00
@name = name
@version = Version.parse(version.to_s)
2020-11-16 22:18:56 +01:00
instance_eval(&block) if block
end
2016-09-24 18:59:44 +02:00
def fails_with?(compiler)
2014-08-03 13:12:59 -05:00
name == compiler.name && version >= compiler.version
end
2014-08-03 15:28:26 -05:00
def inspect
"#<#{self.class.name}: #{name} #{version}>"
end
COLLECTIONS = {
openmp: [
create(:clang),
2015-12-30 21:14:01 +00:00
],
}.freeze
end
# Class for selecting a compiler for a formula.
#
# @api private
class CompilerSelector
extend T::Sig
include CompilerConstants
Compiler = Struct.new(:name, :version)
COMPILER_PRIORITY = {
clang: [:clang, :gnu, :llvm_clang],
gcc: [:gnu, :gcc, :llvm_clang, :clang],
}.freeze
def self.select_for(formula, compilers = self.compilers)
new(formula, DevelopmentTools, compilers).compiler
end
def self.compilers
COMPILER_PRIORITY.fetch(DevelopmentTools.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
2013-03-13 02:07:01 -05:00
def compiler
find_compiler { |c| return c.name unless fails_with?(c) }
raise CompilerSelectionError, formula
end
private
sig { returns(String) }
def preferred_gcc
"gcc"
end
def gnu_gcc_versions
# prioritize gcc version provided by gcc formula.
v = Formulary.factory(preferred_gcc).version.to_s.slice(/\d+/)
GNU_GCC_VERSIONS - [v] + [v] # move the version to the end of the list
rescue FormulaUnavailableError
GNU_GCC_VERSIONS
end
def find_compiler
compilers.each do |compiler|
case compiler
when :gnu
gnu_gcc_versions.reverse_each do |v|
name = "gcc-#{v}"
version = compiler_version(name)
yield Compiler.new(name, version) unless version.null?
end
when :llvm
2016-11-13 23:37:51 +01:00
next # no-op. DSL supported, compiler is not.
else
version = compiler_version(compiler)
yield Compiler.new(compiler, version) unless version.null?
end
end
end
def fails_with?(compiler)
2016-09-24 18:59:44 +02:00
failures.any? { |failure| failure.fails_with?(compiler) }
end
def compiler_version(name)
2018-09-27 18:26:03 -07:00
case name.to_s
when "gcc", GNU_GCC_REGEXP
versions.non_apple_gcc_version(name.to_s)
else
versions.send("#{name}_build_version")
end
end
end
require "extend/os/compilers"