Skip to content

Commit

Permalink
Make lld more first party in BBB (#345)
Browse files Browse the repository at this point in the history
* 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
gbaraldi authored Nov 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ac99aa5 commit fa22ac3
Showing 2 changed files with 42 additions and 19 deletions.
29 changes: 19 additions & 10 deletions src/BuildToolchains.jl
Original file line number Diff line number Diff line change
@@ -31,7 +31,14 @@ function cmake_os(p::AbstractPlatform)
end
end

function toolchain_file(bt::CMake, p::AbstractPlatform, host_platform::AbstractPlatform; is_host::Bool=false)
function linker_string(p::AbstractPlatform, clang_use_lld)
target = triplet(p)
aatarget = aatriplet(p)
lld_str = Sys.isapple(p) ? "ld64.lld" : "ld.lld"
return clang_use_lld ? "/opt/bin/$(target)/$(lld_str)" : "/opt/bin/$(target)/$(aatarget)-ld"
end

function toolchain_file(bt::CMake, p::AbstractPlatform, host_platform::AbstractPlatform; is_host::Bool=false, clang_use_lld::Bool=false)
target = triplet(p)
aatarget = aatriplet(p)

@@ -81,15 +88,14 @@ function toolchain_file(bt::CMake, p::AbstractPlatform, host_platform::AbstractP
set(CMAKE_SYSROOT /opt/$(aatarget)/$(aatarget)/sys-root/)
"""
end

file *= """
set(CMAKE_INSTALL_PREFIX \$ENV{prefix})
set(CMAKE_C_COMPILER /opt/bin/$(target)/$(aatarget)-$(c_compiler(bt)))
set(CMAKE_CXX_COMPILER /opt/bin/$(target)/$(aatarget)-$(cxx_compiler(bt)))
set(CMAKE_Fortran_COMPILER /opt/bin/$(target)/$(aatarget)-$(fortran_compiler(bt)))
set(CMAKE_LINKER /opt/bin/$(target)/$(aatarget)-ld)
set(CMAKE_LINKER $(linker_string(p, clang_use_lld)))
set(CMAKE_OBJCOPY /opt/bin/$(target)/$(aatarget)-objcopy)
set(CMAKE_AR /opt/bin/$(target)/$(aatarget)-ar)
@@ -162,18 +168,20 @@ function meson_cpu_family(p::AbstractPlatform)
end

function toolchain_file(bt::Meson, p::AbstractPlatform, envs::Dict{String,String};
is_host::Bool=false)
is_host::Bool=false, clang_use_lld::Bool=false)
target = triplet(p)
aatarget = aatriplet(p)

clang_use_lld=false #Meson tries is best to misuse lld so don't use it for now
return """
[binaries]
c = '/opt/bin/$(target)/$(aatarget)-$(c_compiler(bt))'
cpp = '/opt/bin/$(target)/$(aatarget)-$(cxx_compiler(bt))'
fortran = '/opt/bin/$(target)/$(aatarget)-$(fortran_compiler(bt))'
objc = '/opt/bin/$(target)/$(aatarget)-cc'
ar = '/opt/bin/$(target)/$(aatarget)-ar'
ld = '/opt/bin/$(target)/$(aatarget)-ld'
ld = '$(linker_string(p, clang_use_lld))'
cpp_ld = '$(linker_string(p, clang_use_lld))'
c_ld = '$(linker_string(p, clang_use_lld))'
nm = '/opt/bin/$(target)/$(aatarget)-nm'
strip = '/opt/bin/$(target)/$(aatarget)-strip'
pkgconfig = '/usr/bin/pkg-config'
@@ -211,6 +219,7 @@ end
function generate_toolchain_files!(platform::AbstractPlatform, envs::Dict{String,String},
toolchains_path::AbstractString;
host_platform::AbstractPlatform = default_host_platform,
clang_use_lld::Bool = false,
)

# Generate the files fot bot the host and the target platforms
@@ -221,13 +230,13 @@ function generate_toolchain_files!(platform::AbstractPlatform, envs::Dict{String
for compiler in (:clang, :gcc)
# Target toolchains
if platforms_match(p, platform)
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=false))
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=false))
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=false, clang_use_lld=clang_use_lld))
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=false, clang_use_lld=clang_use_lld))
end
# Host toolchains
if platforms_match(p, host_platform)
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=true))
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=true))
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=true, clang_use_lld=clang_use_lld))
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=true, clang_use_lld=clang_use_lld))
end
end

32 changes: 23 additions & 9 deletions src/Runner.jl
Original file line number Diff line number Diff line change
@@ -157,7 +157,9 @@ end
allow_unsafe_flags::Bool = false,
lock_microarchitecture::Bool = true,
gcc_version::Union{Nothing,VersionNumber}=nothing,
clang_version::Union{Nothing,VersionNumber}=nothing)
clang_version::Union{Nothing,VersionNumber}=nothing,
clang_use_lld::Bool = false,
)
We generate a set of compiler wrapper scripts within our build environment to force all
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
lock_microarchitecture::Bool = true,
bootstrap::Bool = !isempty(bootstrap_list),
gcc_version::Union{Nothing,VersionNumber}=nothing,
clang_version::Union{Nothing,VersionNumber}=nothing
clang_version::Union{Nothing,VersionNumber}=nothing,
clang_use_lld::Bool = false,
)
# Wipe that directory out, in case it already had compiler wrappers
rm(bin_path; recursive=true, force=true)
@@ -187,7 +190,7 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr

target = aatriplet(platform)
host_target = aatriplet(host_platform)
clang_use_lld = (!isnothing(gcc_version) && !isnothing(clang_version) && clang_version >= v"16" && gcc_version >= v"5")


function wrapper(io::IO,
prog::String;
@@ -344,7 +347,7 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
])
end
end
if Sys.islinux(p) && !isnothing(gcc_version) !isnothing(clang_version) && (clang_version >= v"16")
if Sys.islinux(p) && !isnothing(gcc_version) && !isnothing(clang_version) && (clang_version >= v"16")
append!(flags, ["--gcc-install-dir=/opt/$(aatriplet(p))/lib/gcc/$(aatriplet(p))/$(gcc_version)"])
end
if Sys.iswindows(p)
@@ -469,10 +472,12 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
end
# we want to use a particular linker with clang. But we want to avoid warnings about unused
# flags when just compiling, so we put it into "linker-only flags".
if !clang_use_lld
if !clang_use_lld #Clang with 16 or above is setup to use lld by default
push!(flags, "-fuse-ld=$(aatriplet(p))")
end

if Sys.isfreebsd(p) && clang_use_lld
push!(flags, "-L/opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/usr/local/lib")
end
sanitize_link_flags!(p, flags)

# 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
"""
wrapper(io, string("/opt/", aatriplet(p), "/bin/", string(aatriplet(p), "-dlltool")); allow_ccache=false, extra_cmds=extra_cmds, hash_args=true)
end

function lld(io::IO, p::AbstractPlatform)
lld_str = Sys.isapple(p) ? "ld64.lld" : "ld.lld"
return wrapper(io,
"/opt/$(host_target)/bin/$(lld_str)";
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)), allow_ccache=false,
)
end
# Write out a bunch of common tools
for tool in (:cpp, :ld, :nm, :libtool, :objcopy, :objdump, :otool,
:strip, :install_name_tool, :dlltool, :windres, :winmc, :lipo)
@@ -861,8 +872,10 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
# ld wrappers for clang's `-fuse-ld=$(target)`
if Sys.isapple(p)
write_wrapper(ld, p, "ld64.$(t)")
write_wrapper(lld,p,"ld64.lld")
else
write_wrapper(ld, p, "ld.$(t)")
write_wrapper(lld, p, "ld.lld")
end
write_wrapper(nm, p, "$(t)-nm")
write_wrapper(libtool, p, "$(t)-libtool")
@@ -1330,17 +1343,18 @@ function runner_setup!(workspaces, mappings, workspace_root, verbose, kwargs, pl

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

# JIT out some compiler wrappers, add it to our mounts
generate_compiler_wrappers!(platform; bin_path=compiler_wrapper_path, gcc_version, clang_version, compilers..., extract_kwargs(kwargs, (:allow_unsafe_flags,:lock_microarchitecture))...)
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))...)
push!(workspaces, compiler_wrapper_path => "/opt/bin")

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

# Generate directory where to write Cargo config files

0 comments on commit fa22ac3

Please sign in to comment.