Skip to content

Commit 2bb3852

Browse files
committed
[Runner] Add helper function to set LD_LIBRARY_PATH
1 parent 2260c0b commit 2bb3852

File tree

2 files changed

+67
-40
lines changed

2 files changed

+67
-40
lines changed

src/Runner.jl

+64-38
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,67 @@ end
4747
# XXX: we want AnyPlatform to look like `x86_64-linux-musl` in the build environment.
4848
aatriplet(p::AnyPlatform) = aatriplet(default_host_platform)
4949

50+
function ld_library_path(target::AbstractPlatform,
51+
host::AbstractPlatform,
52+
prefix::String="",
53+
host_libdir::String="";
54+
csl_paths::Bool=true)
55+
# Helper for generating the library include path for a target. MacOS, as usual,
56+
# puts things in slightly different place.
57+
function target_lib_dir(p::AbstractPlatform)
58+
t = aatriplet(p)
59+
if Sys.isapple(p)
60+
return "/opt/$(t)/$(t)/lib:/opt/$(t)/lib"
61+
else
62+
return "/opt/$(t)/$(t)/lib64:/opt/$(t)/$(t)/lib"
63+
end
64+
end
65+
66+
# Let's start
67+
paths = String[]
68+
69+
# If requested, start with our CSL libraries for target/host, but only for architectures
70+
# that can natively run within this environment
71+
if csl_paths
72+
# Ok, this is incredibly finicky. If the target has the same architecture as the
73+
# host, we should have the host first then the target, otherwise we need to have
74+
# target first.
75+
platforms = arch(target) == arch(host) ? (host, target) : (target, host)
76+
append!(paths,
77+
unique("/usr/lib/csl-$(libc(p))-$(arch(p))" for p in platforms if Sys.islinux(p) && proc_family(p) == "intel"),
78+
)
79+
end
80+
81+
push!(paths,
82+
# Then add default system paths
83+
"/usr/local/lib64:/usr/local/lib:/usr/lib64:/usr/lib",
84+
# Add our loader directories
85+
"/lib64:/lib",
86+
)
87+
88+
if !isempty(host_libdir)
89+
push!(paths,
90+
# Libdir of the host platform, to run programs in `HostBuildDependency`
91+
host_libdir,
92+
)
93+
end
94+
95+
push!(paths,
96+
# Add our target/host-specific library directories for compiler support libraries
97+
target_lib_dir(host),
98+
target_lib_dir(target),
99+
)
100+
101+
# Finally, add dependencies in the prefix
102+
if !isempty(prefix)
103+
push!(paths,
104+
"$(prefix)/lib64:$(prefix)/lib",
105+
)
106+
end
107+
108+
return join(paths, ":")
109+
end
110+
50111
"""
51112
generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::AbstractString,
52113
host_platform::AbstractPlatform = $(repr(default_host_platform)),
@@ -396,7 +457,7 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
396457
allow_ccache,
397458
no_soft_float=arch(p) in ("armv6l", "armv7l"),
398459
# Override `LD_LIBRARY_PATH` to avoid external settings mess it up.
399-
env=Dict("LD_LIBRARY_PATH"=>""),
460+
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)),
400461
)
401462
end
402463

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

@@ -873,17 +934,6 @@ function platform_envs(platform::AbstractPlatform, src_name::AbstractString;
873934
return mapping
874935
end
875936

876-
# Helper for generating the library include path for a target. MacOS, as usual,
877-
# puts things in slightly different place.
878-
function target_lib_dir(p::AbstractPlatform)
879-
t = aatriplet(p)
880-
if Sys.isapple(p)
881-
return "/opt/$(t)/$(t)/lib:/opt/$(t)/lib"
882-
else
883-
return "/opt/$(t)/$(t)/lib64:/opt/$(t)/$(t)/lib"
884-
end
885-
end
886-
887937
function GOARM(p::AbstractPlatform)
888938
# See https://github.com/golang/go/wiki/GoArm#supported-architectures
889939
if arch(p) == "armv6l"
@@ -895,16 +945,6 @@ function platform_envs(platform::AbstractPlatform, src_name::AbstractString;
895945
end
896946
end
897947

898-
# Generate CSL paths for target and host platforms, but only if these are platforms for
899-
# which we can run executables, i.e. Intel penguins.
900-
function csl_paths(target::AbstractPlatform, host::AbstractPlatform)
901-
# Ok, this is incredibly finicky: if the target has the same architecture as the
902-
# host, we should have the host first then the target, otherwise we need to have
903-
# target first.
904-
platforms = arch(target) == arch(host) ? (host, target) : (target, host)
905-
return join(unique("/usr/lib/csl-$(libc(p))-$(arch(p))" for p in platforms if Sys.islinux(p) && proc_family(p) == "intel"), ":")
906-
end
907-
908948
merge!(mapping, Dict(
909949
"PATH" => join((
910950
# First things first, our compiler wrappers trump all
@@ -922,21 +962,7 @@ function platform_envs(platform::AbstractPlatform, src_name::AbstractString;
922962
mapping["bindir"],
923963
), ":"),
924964

925-
"LD_LIBRARY_PATH" => join((
926-
# Start with our CSL libraries for target/host, but only for architectures that can natively run within this environment
927-
csl_paths(platform, host_platform),
928-
# Then add default system paths
929-
"/usr/local/lib64:/usr/local/lib:/usr/lib64:/usr/lib",
930-
# Add our loader directories
931-
"/lib64:/lib",
932-
# Libdir of the host platform, to run programs in `HostBuildDependency`
933-
"$(host_libdir)",
934-
# Add our target/host-specific library directories for compiler support libraries
935-
target_lib_dir(host_platform),
936-
target_lib_dir(platform),
937-
# Finally, dependencies
938-
"$(prefix)/lib64:$(prefix)/lib",
939-
), ":"),
965+
"LD_LIBRARY_PATH" => ld_library_path(platform, host_platform, prefix, host_libdir),
940966

941967
# Default mappings for some tools
942968
"CC" => "cc",

test/runners.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ end
9696
end
9797

9898
# This tests only that compilers for all platforms can build a simple C program
99-
@testset "Compilation - $(platform)" for platform in platforms
99+
# TODO: for the time being we only test `cc`, eventually we want to run `gcc` and `clang` separately
100+
@testset "Compilation - $(platform) - $(compiler)" for platform in platforms, compiler in ("cc",)
100101
mktempdir() do dir
101102
ur = preferred_runner()(dir; platform=platform)
102103
iobuff = IOBuffer()
103-
@test run(ur, `/bin/bash -c "echo 'int main() {return 0;}' | cc -x c -"`, iobuff; tee_stream=devnull)
104+
@test run(ur, `/bin/bash -c "echo 'int main() {return 0;}' | $(compiler) -x c -"`, iobuff; tee_stream=devnull)
104105
seekstart(iobuff)
105106
@test split(String(read(iobuff)), "\n")[2] == ""
106107
end

0 commit comments

Comments
 (0)