Skip to content

Commit

Permalink
Implement linking for more than x86 linux
Browse files Browse the repository at this point in the history
Co-authored-by: Mosè Giordano <[email protected]>
  • Loading branch information
vchuravy and giordano committed Oct 18, 2022
1 parent e0c96b0 commit 112c66b
Showing 1 changed file with 61 additions and 13 deletions.
74 changes: 61 additions & 13 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1756,11 +1756,7 @@ const MAX_NUM_PRECOMPILE_FILES = Ref(10)
module Linking

const lld_path = Ref{String}()
if Sys.iswindows()
const lld_exe = "lld.exe"
else
const lld_exe = "lld"
end
const lld_exe = Sys.iswindows() ? "lld.exe" : "lld"

function __init__()
# Prefer our own bundled lld, but if we don't have one, pick it up off of the PATH
Expand All @@ -1781,27 +1777,79 @@ function lld()
return Cmd([lld_path[]])
end


function ld()
default_args = ``
@static if Sys.iswindows()
flavor = "link"
elseif Sys.isapple()
flavor = "darwin"
arch = Sys.ARCH == :aarch64 ? :arm64 : Sys.ARCH
# TODO: gently handle failure in `xcrun` (command not found, garbage being returned, etc...)
sysroot = readchomp(`xcrun --sdk macosx --show-sdk-path`)
default_args = `-arch $arch -syslibroot $(sysroot) -lSystem -platform_version macos 10 11`
else
flavor = "gnu"
end
`$(lld()) -flavor $flavor`

`$(lld()) -flavor $flavor $default_args`
end

const WHOLE_ARCHIVE = if Sys.isapple()
`-all_load`
elseif Sys.iswindows()
`/WHOLEARCHIVE`
else
`--whole-archive`
end

const NO_WHOLE_ARCHIVE = if Sys.isapple() || Sys.iswindows()
``
else
`--no-whole-archive`
end

const SHARED = if Sys.isapple()
`-dylib`
elseif Sys.iswindows()
`/DLL`
else
`-shared`
end

function libdir(path)
@static if Sys.iswindows()
`/LIBPATH:$(path)`
else
`-L$(path)`
end
end

function output(path)
@static if Sys.isapple()
`-o $(path)`
elseif Sys.iswindows()
`/OUT:$(path)`
else
`--output=$(path)`
end
end

function l(path)
@static if Sys.iswindows()
path
else
"-l$path"
end
end

is_debug() = ccall(:jl_is_debugbuild, Cint, ()) == 1

function link_jilib(path, out, internal_stderr::IO = stderr, internal_stdout::IO = stdout, args = ``)
LIBDIR = joinpath(Sys.BINDIR, "..", "lib")
LIBS = is_debug() ? `-ljulia-debug -ljulia-internal-debug` : `-ljulia -ljulia-internal`
WHOLE_ARCHIVE = Sys.isapple() ? `-all_load` : `--whole-archive`
NO_WHOLE_ARCHIVE = Sys.isapple() ? `` : `--no-whole-archive`
function link_jilib(path, out, internal_stderr::IO = stderr, internal_stdout::IO = stdout)
LIBDIR = libdir(joinpath(Sys.BINDIR, "..", "lib"))
libs = is_debug() ? ("julia-debug", "julia-internal-debug") : ("julia", "julia-internal")
LIBS = map(l, libs)

run(`$(ld()) --shared --output=$out $WHOLE_ARCHIVE $path $NO_WHOLE_ARCHIVE -L$(LIBDIR) $LIBS $args`,
run(`$(ld()) $SHARED $(output(out)) $WHOLE_ARCHIVE $path $NO_WHOLE_ARCHIVE $LIBDIR $LIBS`,
Base.DevNull(), internal_stdout, internal_stderr)
end
end # module Linking
Expand Down

0 comments on commit 112c66b

Please sign in to comment.