-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathcompiler_abi.jl
262 lines (229 loc) · 9.89 KB
/
compiler_abi.jl
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import Base.BinaryPlatforms: detect_libstdcxx_version, detect_cxxstring_abi
using ObjectFile
csl_warning(lib) = @warn(
"""
To ensure that the correct version of $(lib) is found at runtime, add the following entry to the list of dependencies of this builder
Dependency(PackageSpec(name="CompilerSupportLibraries_jll", uuid="e66e0078-7015-5450-92f7-15fbd957f2ae"))
""")
"""
detect_libgfortran_version(oh::ObjectHandle, platform::AbstractPlatform)
Given an ObjectFile, examine its dynamic linkage to discover which (if any)
`libgfortran` it's linked against. The major SOVERSION will determine which
GCC version we're restricted to.
"""
function detect_libgfortran_version(oh::ObjectHandle, platform::AbstractPlatform)
# We look for linkage to libgfortran
libs = basename.(path.(DynamicLinks(oh)))
fortran_libs = filter(l -> occursin("libgfortran", l), libs)
if isempty(fortran_libs)
return nothing
end
# If we find one, pass it off to `parse_dl_name_version`
name, version = parse_dl_name_version(first(fortran_libs), os(platform))
return version
end
function check_libgfortran_version(oh::ObjectHandle, platform::AbstractPlatform; verbose::Bool = false,
has_csl::Bool = true)
version = nothing
try
version = detect_libgfortran_version(oh, platform)
catch e
if isa(e, InterruptException)
rethrow(e)
end
@warn "$(path(oh)) could not be scanned for libgfortran dependency!" exception=(e, catch_backtrace())
return true
end
if verbose && version !== nothing
@info("$(path(oh)) locks us to libgfortran v$(version)")
end
if !has_csl && version !== nothing
csl_warning("libgfortran")
end
if libgfortran_version(platform) === nothing && version !== nothing
msg = strip(replace("""
$(path(oh)) links to libgfortran! This causes incompatibilities across
major versions of GCC. To remedy this, you must build a tarball for
each major version of GCC. To do this, immediately after your `platforms`
definition in your `build_tarballs.jl` file, add the line:
""", '\n' => ' '))
msg *= "\n\n platforms = expand_gfortran_versions(platforms)"
@warn(msg)
return false
end
if libgfortran_version(platform) !== nothing !== version && libgfortran_version(platform) != version
msg = strip(replace("""
$(path(oh)) links to libgfortran$(version.major), but we are supposedly building
for libgfortran$(libgfortran_version(platform).major). This usually indicates that
the build system is somehow ignoring our choice of compiler!
""", '\n' => ' '))
@warn(msg)
return false
end
return true
end
function check_csl_libs(oh::ObjectHandle, platform::AbstractPlatform; verbose::Bool=false,
has_csl::Bool=true, csl_libs::Vector{String}=["libgomp", "libatomic"])
if has_csl
# No need to do any check, CompilerSupportLibraries_jll is already a dependency
return true
end
# Collect list of dependencies
libs = try
basename.(path.(DynamicLinks(oh)))
catch e
if isa(e, InterruptException)
rethrow(e)
end
@warn "$(path(oh)) could not be scanned for $(lib) dependency!" exception=(e, catch_backtrace())
return true
end
# If any of the libs is a library provided by
# `CompilerSupportLibraries_jll`, suggest to add the package as dependency
for lib in csl_libs
if length(filter(l -> occursin(lib, l), libs)) >= 1
csl_warning(lib)
return false
end
end
return true
end
"""
detect_libstdcxx_version(oh::ObjectHandle, platform::AbstractPlatform)
Given an ObjectFile, examine its dynamic linkage to discover which (if any)
`libgfortran` it's linked against. The major SOVERSION will determine which
GCC version we're restricted to.
"""
function detect_libstdcxx_version(oh::ObjectHandle, platform::AbstractPlatform)
# We look for linkage to libstdc++
libs = basename.(path.(DynamicLinks(oh)))
libstdcxx_libs = filter(l -> occursin("libstdc++", l), libs)
if isempty(libstdcxx_libs)
return nothing
end
# Extract all pieces of `.gnu.version_d` from libstdc++.so, find the `GLIBCXX_*`
# symbols, and use the maximum version of that to find the GLIBCXX ABI version number
version_symbols = readmeta(first(libstdcxx_libs)) do oh
unique(vcat((x -> x.names).(ELFVersionData(oh))...))
end
version_symbols = filter(x -> startswith(x, "GLIBCXX_"), version_symbols)
if isempty(version_symbols)
# This would be weird, but let's be prepared
return nothing
end
return maximum([VersionNumber(split(v, "_")[2]) for v in version_symbols])
end
function check_libstdcxx_version(oh::ObjectHandle, platform::AbstractPlatform; verbose::Bool = false)
libstdcxx_version = nothing
try
libstdcxx_version = detect_libstdcxx_version(oh, platform)
catch e
if isa(e, InterruptException)
rethrow(e)
end
@warn "$(path(oh)) could not be scanned for libstdcxx dependency!" exception=(e, catch_backtrace())
return true
end
if verbose && libstdcxx_version != nothing
@info("$(path(oh)) locks us to libstdc++ v$(libstdcxx_version)+")
end
# This actually isn't critical, so we don't complain. Yet.
# if libstdcxx_version(platform) === nothing && libstdcxx_version != nothing
# msg = strip(replace("""
# $(path(oh)) links to libstdc++! This causes incompatibilities across
# major versions of GCC. To remedy this, you must build a tarball for
# each major version of GCC. To do this, immediately after your `platforms`
# definition in your `build_tarballs.jl` file, add the line:
# """, '\n' => ' '))
# msg *= "\n\n platforms = expand_cxxstring_abis(platforms)"
# warn(io, msg)
# return false
# end
return true
end
function cppfilt(symbol_names::Vector, platform::AbstractPlatform; strip_underscore::Bool=false)
input = IOBuffer()
for name in symbol_names
println(input, name)
end
seekstart(input)
output = IOBuffer()
mktempdir() do dir
ur = preferred_runner()(dir; cwd="/workspace/", platform=platform)
cmd = Cmd(`/opt/bin/$(triplet(ur.platform))/c++filt`; ignorestatus=true)
if strip_underscore
cmd = `$(cmd) --strip-underscore`
end
run_interactive(ur, cmd; stdin=input, stdout=output)
end
return filter!(s -> !isempty(s), split(String(take!(output)), "\n"))
end
"""
detect_cxxstring_abi(oh::ObjectHandle, platform::AbstractPlatform)
Given an ObjectFile, examine its symbols to discover which (if any) C++11
std::string ABI it's using. We do this by scanning the list of exported
symbols, triggering off of instances of `St7__cxx11` or `_ZNSs` to give
evidence toward a constraint on `cxx11`, `cxx03` or neither.
"""
function detect_cxxstring_abi(oh::ObjectHandle, platform::AbstractPlatform)
try
# First, if this object doesn't link against `libstdc++`, it's a `:cxxany`
if !any(occursin("libstdc++", l) for l in ObjectFile.path.(DynamicLinks(oh)))
return nothing
end
# GCC on macOS prepends an underscore to symbols, strip it.
symbol_names = cppfilt(symbol_name.(Symbols(oh)), platform; strip_underscore=Sys.isapple(platform))
# Shove the symbol names through c++filt (since we don't want to have to
# reimplement the parsing logic in Julia). If anything has `cxx11` tags,
# then mark it as such.
if any(occursin("[abi:cxx11]", c) || occursin("std::__cxx11", c) for c in symbol_names)
return "cxx11"
end
# Otherwise, if we still have `std::string`'s or `std::list`'s in there, it's implicitly a
# `cxx03` binary, even though we don't have a __cxx03 namespace or something. Mark it.
if any(occursin("std::string", c) || occursin("std::basic_string", c) ||
occursin("std::list", c) for c in symbol_names)
return "cxx03"
end
catch e
if isa(e, InterruptException)
rethrow(e)
end
@warn "$(path(oh)) could not be scanned for cxx11 ABI!" exception=(e, catch_backtrace())
end
return nothing
end
function check_cxxstring_abi(oh::ObjectHandle, platform::AbstractPlatform; io::IO = stdout, verbose::Bool = false)
# First, check the stdlibc++ string ABI to see if it is a superset of `platform`. If it's
# not, then we have a problem!
cxx_abi = detect_cxxstring_abi(oh, platform)
# If no std::string symbols found, just exit out immediately
if cxx_abi == nothing
return true
end
if verbose && cxx_abi != nothing
@info("$(path(oh)) locks us to $(cxx_abi)")
end
if cxxstring_abi(platform) == nothing && cxx_abi != nothing
msg = strip(replace("""
$(path(oh)) contains std::string values! This causes incompatibilities across
the GCC 4/5 version boundary. To remedy this, you must build a tarball for
both GCC 4 and GCC 5. To do this, immediately after your `platforms`
definition in your `build_tarballs.jl` file, add the line:
""", '\n' => ' '))
msg *= "\n\n platforms = expand_cxxstring_abis(platforms)"
@warn(msg)
return false
end
if cxxstring_abi(platform) != cxx_abi
msg = strip(replace("""
$(path(oh)) contains $(cxx_abi) ABI std::string values within its public interface,
but we are supposedly building for $(cxxstring_abi(platform)) ABI. This usually
indicates that the build system is somehow ignoring our choice of compiler, as we manually
insert the correct compiler flags for this ABI choice!
""", '\n' => ' '))
@warn(msg)
return false
end
return true
end