-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Expand file tree
/
Copy pathcompilers.rb
More file actions
216 lines (185 loc) · 6.35 KB
/
compilers.rb
File metadata and controls
216 lines (185 loc) · 6.35 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# typed: strict
# frozen_string_literal: true
module CompilerConstants
# GCC 7 - Ubuntu 18.04 (ESM ends 2028-04-01)
# GCC 8 - RHEL 8 (ELS ends 2032-05-31)
GNU_GCC_VERSIONS = %w[7 8 9 10 11 12 13 14 15].freeze
GNU_GCC_REGEXP = /^gcc-(#{GNU_GCC_VERSIONS.join("|")})$/
COMPILER_SYMBOL_MAP = T.let({
"gcc" => :gcc,
"clang" => :clang,
"llvm_clang" => :llvm_clang,
}.freeze, T::Hash[String, Symbol])
COMPILERS = T.let((COMPILER_SYMBOL_MAP.values +
GNU_GCC_VERSIONS.map { |n| "gcc-#{n}" }).freeze, T::Array[T.any(String, Symbol)])
end
# Class for checking compiler compatibility for a formula.
class CompilerFailure # rubocop:todo Style/OneClassPerFile
sig { returns(Symbol) }
attr_reader :type
sig { params(val: T.any(Integer, String)).returns(Version) }
def version(val = T.unsafe(nil))
@version = Version.parse(val.to_s) if val
@version
end
# Allows Apple compiler `fails_with` statements to keep using `build`
# even though `build` and `version` are the same internally.
alias build version
# The cause is no longer used so we need not hold a reference to the string.
sig { params(_: String).void }
def cause(_); end
sig {
params(spec: T.any(Symbol, T::Hash[Symbol, String]), block: T.nilable(T.proc.void)).returns(T.attached_class)
}
def self.create(spec, &block)
# Non-Apple compilers are in the format fails_with compiler => version
if spec.is_a?(Hash)
compiler, major_version = spec.first
raise ArgumentError, "The `fails_with` hash syntax only supports GCC" if compiler != :gcc
type = compiler
# so `fails_with gcc: "7"` simply marks all 7 releases incompatible
version = "#{major_version}.999"
exact_major_match = true
else
type = spec
version = 9999
exact_major_match = false
end
new(type, version, exact_major_match:, &block)
end
sig { params(compiler: CompilerSelector::Compiler).returns(T::Boolean) }
def fails_with?(compiler)
version_matched = if type != :gcc
version >= compiler.version
elsif @exact_major_match
gcc_major(version) == gcc_major(compiler.version) && version >= compiler.version
else
gcc_major(version) >= gcc_major(compiler.version)
end
type == compiler.type && version_matched
end
sig { returns(String) }
def inspect
"#<#{self.class.name}: #{type} #{version}>"
end
private
sig {
params(
type: Symbol,
version: T.any(Integer, String),
exact_major_match: T::Boolean,
block: T.nilable(T.proc.void),
).void
}
def initialize(type, version, exact_major_match:, &block)
@type = type
@version = T.let(Version.parse(version.to_s), Version)
@exact_major_match = exact_major_match
instance_eval(&block) if block
end
sig { params(version: Version).returns(Version) }
def gcc_major(version)
Version.new(version.major.to_s)
end
end
# Class for selecting a compiler for a formula.
class CompilerSelector # rubocop:todo Style/OneClassPerFile
include CompilerConstants
class Compiler < T::Struct
const :type, Symbol
const :name, T.any(String, Symbol)
const :version, Version
end
COMPILER_PRIORITY = T.let({
clang: [:clang, :llvm_clang, :gnu],
gcc: [:gnu, :gcc, :llvm_clang, :clang],
}.freeze, T::Hash[Symbol, T::Array[Symbol]])
sig {
params(formula: T.any(Formula, SoftwareSpec), compilers: T.nilable(T::Array[Symbol]), testing_formula: T::Boolean)
.returns(T.any(String, Symbol))
}
def self.select_for(formula, compilers = nil, testing_formula: false)
if compilers.nil? && DevelopmentTools.default_compiler == :clang
deps = formula.deps.filter_map do |dep|
dep.name if dep.required? || (testing_formula && dep.test?) || (!testing_formula && dep.build?)
end
compilers = [:clang, :gnu, :llvm_clang] if deps.none?("llvm") && deps.any?(/^gcc(@\d+)?$/)
end
new(formula, DevelopmentTools, compilers || self.compilers).compiler
end
sig { returns(T::Array[Symbol]) }
def self.compilers
COMPILER_PRIORITY.fetch(DevelopmentTools.default_compiler)
end
sig { returns(T.any(Formula, SoftwareSpec)) }
attr_reader :formula
sig { returns(T::Array[CompilerFailure]) }
attr_reader :failures
sig { returns(T.class_of(DevelopmentTools)) }
attr_reader :versions
sig { returns(T::Array[Symbol]) }
attr_reader :compilers
sig {
params(
formula: T.any(Formula, SoftwareSpec),
versions: T.class_of(DevelopmentTools),
compilers: T::Array[Symbol],
).void
}
def initialize(formula, versions, compilers)
@formula = formula
@failures = T.let(formula.compiler_failures, T::Array[CompilerFailure])
@versions = versions
@compilers = compilers
end
sig { returns(T.any(String, Symbol)) }
def compiler
find_compiler { |c| return c.name unless fails_with?(c) }
raise CompilerSelectionError, formula
end
sig { returns(String) }
def self.preferred_gcc
"gcc"
end
private
sig { returns(T::Array[String]) }
def gnu_gcc_versions
# prioritize gcc version provided by gcc formula.
v = Formulary.factory(CompilerSelector.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
sig { params(_block: T.proc.params(arg0: Compiler).void).void }
def find_compiler(&_block)
compilers.each do |compiler|
case compiler
when :gnu
gnu_gcc_versions.reverse_each do |v|
executable = "gcc-#{v}"
version = compiler_version(executable)
yield Compiler.new(type: :gcc, name: executable, version:) unless version.null?
end
when :llvm
next # no-op. DSL supported, compiler is not.
else
version = compiler_version(compiler)
yield Compiler.new(type: compiler, name: compiler, version:) unless version.null?
end
end
end
sig { params(compiler: Compiler).returns(T::Boolean) }
def fails_with?(compiler)
failures.any? { |failure| failure.fails_with?(compiler) }
end
sig { params(name: T.any(String, Symbol)).returns(Version) }
def compiler_version(name)
case name.to_s
when "gcc", GNU_GCC_REGEXP
versions.gcc_version(name.to_s)
else
versions.send(:"#{name}_build_version")
end
end
end
require "extend/os/compilers"