Skip to content

Commit fa22ac3

Browse files
authored
Make lld more first party in BBB (#345)
* Make lld more first party in BBB * Make sure meson doesn't try to use lld because it sucks at it * Fix typo and add freebsd /usr/local/lib search path * Apply suggestions from review * Fix typo
1 parent ac99aa5 commit fa22ac3

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

src/BuildToolchains.jl

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ function cmake_os(p::AbstractPlatform)
3131
end
3232
end
3333

34-
function toolchain_file(bt::CMake, p::AbstractPlatform, host_platform::AbstractPlatform; is_host::Bool=false)
34+
function linker_string(p::AbstractPlatform, clang_use_lld)
35+
target = triplet(p)
36+
aatarget = aatriplet(p)
37+
lld_str = Sys.isapple(p) ? "ld64.lld" : "ld.lld"
38+
return clang_use_lld ? "/opt/bin/$(target)/$(lld_str)" : "/opt/bin/$(target)/$(aatarget)-ld"
39+
end
40+
41+
function toolchain_file(bt::CMake, p::AbstractPlatform, host_platform::AbstractPlatform; is_host::Bool=false, clang_use_lld::Bool=false)
3542
target = triplet(p)
3643
aatarget = aatriplet(p)
3744

@@ -81,15 +88,14 @@ function toolchain_file(bt::CMake, p::AbstractPlatform, host_platform::AbstractP
8188
set(CMAKE_SYSROOT /opt/$(aatarget)/$(aatarget)/sys-root/)
8289
"""
8390
end
84-
8591
file *= """
8692
set(CMAKE_INSTALL_PREFIX \$ENV{prefix})
8793
8894
set(CMAKE_C_COMPILER /opt/bin/$(target)/$(aatarget)-$(c_compiler(bt)))
8995
set(CMAKE_CXX_COMPILER /opt/bin/$(target)/$(aatarget)-$(cxx_compiler(bt)))
9096
set(CMAKE_Fortran_COMPILER /opt/bin/$(target)/$(aatarget)-$(fortran_compiler(bt)))
9197
92-
set(CMAKE_LINKER /opt/bin/$(target)/$(aatarget)-ld)
98+
set(CMAKE_LINKER $(linker_string(p, clang_use_lld)))
9399
set(CMAKE_OBJCOPY /opt/bin/$(target)/$(aatarget)-objcopy)
94100
95101
set(CMAKE_AR /opt/bin/$(target)/$(aatarget)-ar)
@@ -162,18 +168,20 @@ function meson_cpu_family(p::AbstractPlatform)
162168
end
163169

164170
function toolchain_file(bt::Meson, p::AbstractPlatform, envs::Dict{String,String};
165-
is_host::Bool=false)
171+
is_host::Bool=false, clang_use_lld::Bool=false)
166172
target = triplet(p)
167173
aatarget = aatriplet(p)
168-
174+
clang_use_lld=false #Meson tries is best to misuse lld so don't use it for now
169175
return """
170176
[binaries]
171177
c = '/opt/bin/$(target)/$(aatarget)-$(c_compiler(bt))'
172178
cpp = '/opt/bin/$(target)/$(aatarget)-$(cxx_compiler(bt))'
173179
fortran = '/opt/bin/$(target)/$(aatarget)-$(fortran_compiler(bt))'
174180
objc = '/opt/bin/$(target)/$(aatarget)-cc'
175181
ar = '/opt/bin/$(target)/$(aatarget)-ar'
176-
ld = '/opt/bin/$(target)/$(aatarget)-ld'
182+
ld = '$(linker_string(p, clang_use_lld))'
183+
cpp_ld = '$(linker_string(p, clang_use_lld))'
184+
c_ld = '$(linker_string(p, clang_use_lld))'
177185
nm = '/opt/bin/$(target)/$(aatarget)-nm'
178186
strip = '/opt/bin/$(target)/$(aatarget)-strip'
179187
pkgconfig = '/usr/bin/pkg-config'
@@ -211,6 +219,7 @@ end
211219
function generate_toolchain_files!(platform::AbstractPlatform, envs::Dict{String,String},
212220
toolchains_path::AbstractString;
213221
host_platform::AbstractPlatform = default_host_platform,
222+
clang_use_lld::Bool = false,
214223
)
215224

216225
# Generate the files fot bot the host and the target platforms
@@ -221,13 +230,13 @@ function generate_toolchain_files!(platform::AbstractPlatform, envs::Dict{String
221230
for compiler in (:clang, :gcc)
222231
# Target toolchains
223232
if platforms_match(p, platform)
224-
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=false))
225-
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=false))
233+
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=false, clang_use_lld=clang_use_lld))
234+
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=false, clang_use_lld=clang_use_lld))
226235
end
227236
# Host toolchains
228237
if platforms_match(p, host_platform)
229-
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=true))
230-
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=true))
238+
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=true, clang_use_lld=clang_use_lld))
239+
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=true, clang_use_lld=clang_use_lld))
231240
end
232241
end
233242

src/Runner.jl

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ end
157157
allow_unsafe_flags::Bool = false,
158158
lock_microarchitecture::Bool = true,
159159
gcc_version::Union{Nothing,VersionNumber}=nothing,
160-
clang_version::Union{Nothing,VersionNumber}=nothing)
160+
clang_version::Union{Nothing,VersionNumber}=nothing,
161+
clang_use_lld::Bool = false,
162+
)
161163
162164
We generate a set of compiler wrapper scripts within our build environment to force all
163165
build systems to honor the necessary sets of compiler flags to build for our systems.
@@ -174,7 +176,8 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
174176
lock_microarchitecture::Bool = true,
175177
bootstrap::Bool = !isempty(bootstrap_list),
176178
gcc_version::Union{Nothing,VersionNumber}=nothing,
177-
clang_version::Union{Nothing,VersionNumber}=nothing
179+
clang_version::Union{Nothing,VersionNumber}=nothing,
180+
clang_use_lld::Bool = false,
178181
)
179182
# Wipe that directory out, in case it already had compiler wrappers
180183
rm(bin_path; recursive=true, force=true)
@@ -187,7 +190,7 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
187190

188191
target = aatriplet(platform)
189192
host_target = aatriplet(host_platform)
190-
clang_use_lld = (!isnothing(gcc_version) && !isnothing(clang_version) && clang_version >= v"16" && gcc_version >= v"5")
193+
191194

192195
function wrapper(io::IO,
193196
prog::String;
@@ -344,7 +347,7 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
344347
])
345348
end
346349
end
347-
if Sys.islinux(p) && !isnothing(gcc_version) !isnothing(clang_version) && (clang_version >= v"16")
350+
if Sys.islinux(p) && !isnothing(gcc_version) && !isnothing(clang_version) && (clang_version >= v"16")
348351
append!(flags, ["--gcc-install-dir=/opt/$(aatriplet(p))/lib/gcc/$(aatriplet(p))/$(gcc_version)"])
349352
end
350353
if Sys.iswindows(p)
@@ -469,10 +472,12 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
469472
end
470473
# we want to use a particular linker with clang. But we want to avoid warnings about unused
471474
# flags when just compiling, so we put it into "linker-only flags".
472-
if !clang_use_lld
475+
if !clang_use_lld #Clang with 16 or above is setup to use lld by default
473476
push!(flags, "-fuse-ld=$(aatriplet(p))")
474477
end
475-
478+
if Sys.isfreebsd(p) && clang_use_lld
479+
push!(flags, "-L/opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/usr/local/lib")
480+
end
476481
sanitize_link_flags!(p, flags)
477482

478483
# On macos, we need to pass `-headerpad_max_install_names` so that we have lots of space
@@ -782,7 +787,13 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
782787
"""
783788
wrapper(io, string("/opt/", aatriplet(p), "/bin/", string(aatriplet(p), "-dlltool")); allow_ccache=false, extra_cmds=extra_cmds, hash_args=true)
784789
end
785-
790+
function lld(io::IO, p::AbstractPlatform)
791+
lld_str = Sys.isapple(p) ? "ld64.lld" : "ld.lld"
792+
return wrapper(io,
793+
"/opt/$(host_target)/bin/$(lld_str)";
794+
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)), allow_ccache=false,
795+
)
796+
end
786797
# Write out a bunch of common tools
787798
for tool in (:cpp, :ld, :nm, :libtool, :objcopy, :objdump, :otool,
788799
:strip, :install_name_tool, :dlltool, :windres, :winmc, :lipo)
@@ -861,8 +872,10 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
861872
# ld wrappers for clang's `-fuse-ld=$(target)`
862873
if Sys.isapple(p)
863874
write_wrapper(ld, p, "ld64.$(t)")
875+
write_wrapper(lld,p,"ld64.lld")
864876
else
865877
write_wrapper(ld, p, "ld.$(t)")
878+
write_wrapper(lld, p, "ld.lld")
866879
end
867880
write_wrapper(nm, p, "$(t)-nm")
868881
write_wrapper(libtool, p, "$(t)-libtool")
@@ -1330,17 +1343,18 @@ function runner_setup!(workspaces, mappings, workspace_root, verbose, kwargs, pl
13301343

13311344
clang = filter(s -> s.name == "LLVMBootstrap", shards)
13321345
clang_version = length(clang) == 1 ? only(clang).version : nothing
1346+
clang_use_lld = (!isnothing(gcc_version) && !isnothing(clang_version) && clang_version >= v"16" && gcc_version >= v"6")
13331347
# Construct environment variables we'll use from here on out
13341348
platform::Platform = get_concrete_platform(platform; compilers..., extract_kwargs(kwargs, (:preferred_gcc_version,:preferred_llvm_version))...)
13351349
envs::Dict{String,String} = merge(platform_envs(platform, src_name; rust_version, verbose, compilers...), extra_env)
13361350

13371351
# JIT out some compiler wrappers, add it to our mounts
1338-
generate_compiler_wrappers!(platform; bin_path=compiler_wrapper_path, gcc_version, clang_version, compilers..., extract_kwargs(kwargs, (:allow_unsafe_flags,:lock_microarchitecture))...)
1352+
generate_compiler_wrappers!(platform; bin_path=compiler_wrapper_path, gcc_version, clang_version, clang_use_lld, compilers..., extract_kwargs(kwargs, (:allow_unsafe_flags,:lock_microarchitecture))...)
13391353
push!(workspaces, compiler_wrapper_path => "/opt/bin")
13401354

13411355
if isempty(bootstrap_list)
13421356
# Generate CMake and Meson files, only if we are not bootstrapping
1343-
generate_toolchain_files!(platform, envs, toolchains_path)
1357+
generate_toolchain_files!(platform, envs, toolchains_path; clang_use_lld=clang_use_lld)
13441358
push!(workspaces, toolchains_path => "/opt/toolchains")
13451359

13461360
# Generate directory where to write Cargo config files

0 commit comments

Comments
 (0)