Skip to content

Commit

Permalink
[Runner] Add helper function to set LD_LIBRARY_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Jul 14, 2021
1 parent 2260c0b commit feb9c9d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 40 deletions.
102 changes: 64 additions & 38 deletions src/Runner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,67 @@ end
# XXX: we want AnyPlatform to look like `x86_64-linux-musl` in the build environment.
aatriplet(p::AnyPlatform) = aatriplet(default_host_platform)

function ld_library_path(target::AbstractPlatform,
host::AbstractPlatform,
prefix::String="",
host_libdir::String="";
csl_paths::Bool=true)
# Helper for generating the library include path for a target. MacOS, as usual,
# puts things in slightly different place.
function target_lib_dir(p::AbstractPlatform)
t = aatriplet(p)
if Sys.isapple(p)
return "/opt/$(t)/$(t)/lib:/opt/$(t)/lib"
else
return "/opt/$(t)/$(t)/lib64:/opt/$(t)/$(t)/lib"
end
end

# Let's start
paths = String[]

# If requested, start with our CSL libraries for target/host, but only for architectures
# that can natively run within this environment
if csl_paths
# Ok, this is incredibly finicky. If the target has the same architecture as the
# host, we should have the host first then the target, otherwise we need to have
# target first.
platforms = arch(target) == arch(host) ? (host, target) : (target, host)
append!(paths,
unique("/usr/lib/csl-$(libc(p))-$(arch(p))" for p in platforms if Sys.islinux(p) && proc_family(p) == "intel"),
)
end

push!(paths,
# Then add default system paths
"/usr/local/lib64:/usr/local/lib:/usr/lib64:/usr/lib",
# Add our loader directories
"/lib64:/lib",
)

if !isempty(host_libdir)
push!(paths,
# Libdir of the host platform, to run programs in `HostBuildDependency`
host_libdir,
)
end

push!(paths,
# Add our target/host-specific library directories for compiler support libraries
target_lib_dir(host),
target_lib_dir(target),
)

# Finally, add dependencies in the prefix
if !isempty(prefix)
push!(paths,
"$(prefix)/lib64:$(prefix)/lib",
)
end

return join(paths, ":")
end

"""
generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::AbstractString,
host_platform::AbstractPlatform = $(repr(default_host_platform)),
Expand Down Expand Up @@ -396,7 +457,7 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
allow_ccache,
no_soft_float=arch(p) in ("armv6l", "armv7l"),
# Override `LD_LIBRARY_PATH` to avoid external settings mess it up.
env=Dict("LD_LIBRARY_PATH"=>""),
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)),
)
end

Expand All @@ -409,7 +470,7 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
link_only_flags=clang_link_flags!(p),
no_soft_float=arch(p) in ("armv6l", "armv7l"),
# Override `LD_LIBRARY_PATH` to avoid external settings mess it up.
env=Dict("LD_LIBRARY_PATH"=>""),
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)),
)
end

Expand Down Expand Up @@ -873,17 +934,6 @@ function platform_envs(platform::AbstractPlatform, src_name::AbstractString;
return mapping
end

# Helper for generating the library include path for a target. MacOS, as usual,
# puts things in slightly different place.
function target_lib_dir(p::AbstractPlatform)
t = aatriplet(p)
if Sys.isapple(p)
return "/opt/$(t)/$(t)/lib:/opt/$(t)/lib"
else
return "/opt/$(t)/$(t)/lib64:/opt/$(t)/$(t)/lib"
end
end

function GOARM(p::AbstractPlatform)
# See https://github.com/golang/go/wiki/GoArm#supported-architectures
if arch(p) == "armv6l"
Expand All @@ -895,16 +945,6 @@ function platform_envs(platform::AbstractPlatform, src_name::AbstractString;
end
end

# Generate CSL paths for target and host platforms, but only if these are platforms for
# which we can run executables, i.e. Intel penguins.
function csl_paths(target::AbstractPlatform, host::AbstractPlatform)
# Ok, this is incredibly finicky: if the target has the same architecture as the
# host, we should have the host first then the target, otherwise we need to have
# target first.
platforms = arch(target) == arch(host) ? (host, target) : (target, host)
return join(unique("/usr/lib/csl-$(libc(p))-$(arch(p))" for p in platforms if Sys.islinux(p) && proc_family(p) == "intel"), ":")
end

merge!(mapping, Dict(
"PATH" => join((
# First things first, our compiler wrappers trump all
Expand All @@ -922,21 +962,7 @@ function platform_envs(platform::AbstractPlatform, src_name::AbstractString;
mapping["bindir"],
), ":"),

"LD_LIBRARY_PATH" => join((
# Start with our CSL libraries for target/host, but only for architectures that can natively run within this environment
csl_paths(platform, host_platform),
# Then add default system paths
"/usr/local/lib64:/usr/local/lib:/usr/lib64:/usr/lib",
# Add our loader directories
"/lib64:/lib",
# Libdir of the host platform, to run programs in `HostBuildDependency`
"$(host_libdir)",
# Add our target/host-specific library directories for compiler support libraries
target_lib_dir(host_platform),
target_lib_dir(platform),
# Finally, dependencies
"$(prefix)/lib64:$(prefix)/lib",
), ":"),
"LD_LIBRARY_PATH" => ld_library_path(platform, host_platform, prefix, host_libdir),

# Default mappings for some tools
"CC" => "cc",
Expand Down
4 changes: 2 additions & 2 deletions test/runners.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ end
end

# This tests only that compilers for all platforms can build a simple C program
@testset "Compilation - $(platform)" for platform in platforms
@testset "Compilation - $(platform) - $(compiler)" for platform in platforms, compiler in ("gcc", "clang")
mktempdir() do dir
ur = preferred_runner()(dir; platform=platform)
iobuff = IOBuffer()
@test run(ur, `/bin/bash -c "echo 'int main() {return 0;}' | cc -x c -"`, iobuff; tee_stream=devnull)
@test run(ur, `/bin/bash -c "echo 'int main() {return 0;}' | $(compiler) -x c -"`, iobuff; tee_stream=devnull)
seekstart(iobuff)
@test split(String(read(iobuff)), "\n")[2] == ""
end
Expand Down

0 comments on commit feb9c9d

Please sign in to comment.