Skip to content

Commit

Permalink
Attempt windows compatibility, don't require linking, allow for inclu…
Browse files Browse the repository at this point in the history
…sion of usrimg.jl
  • Loading branch information
staticfloat committed Oct 25, 2014
1 parent 18f023a commit cacfba3
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions contrib/build_sysimg.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# By default, put the system image next to libjulia
build_sysimg(;force=false, cpu_target="native") = build_sysimg(joinpath(dirname(Sys.dlpath("libjulia")),"sys"), force=force, cpu_target=cpu_target)
build_sysimg(; userimg_path=nothing, force=false, cpu_target="native") = build_sysimg(joinpath(dirname(Sys.dlpath("libjulia")),"sys"), userimg_path=userimg_path, force=force, cpu_target=cpu_target)

# Build a system image binary at sysimg_path.dlext. If a system image is already loaded, error out, or continue if force = true
function build_sysimg(sysimg_path; force=false, cpu_target="native")
function build_sysimg(sysimg_path; userimg_path=nothing, force=false, cpu_target="native")
# Unless force == true, quit out if a sysimg is already loadable
sysimg = dlopen_e("sys")
if !force && sysimg != C_NULL
println("System image already loaded at $(Sys.dlpath(sysimg)), pass \"force=true\" to override")
return;
end

# Canonicalize userimg_path before we enter the base_dir
userimg_path = abspath(userimg_path)

# Enter base/ and setup some useful paths
base_dir = dirname(Base.find_source_file("sysimg.jl"))
cd(base_dir) do
Expand All @@ -18,10 +21,18 @@ function build_sysimg(sysimg_path; force=false, cpu_target="native")
ld = find_system_linker()

# Ensure we have write-permissions to wherever we're trying to write to
if !iswritable("$sysimg_path.$(Sys.dlext)")
if !success(`touch $sysimg_path.$(Sys.dlext)`)

This comment has been minimized.

Copy link
@tkelman

tkelman Oct 27, 2014

Contributor

I think we have a native-julia touch now, at least on 0.4 (need to check whether it got backported)

edit - was there a problem with iswritable? does it not work if the file doesn't exist or something?

This comment has been minimized.

Copy link
@staticfloat

staticfloat Oct 27, 2014

Author Member

Exactly, if the file doesn't exist, it fails.

This comment has been minimized.

Copy link
@staticfloat

staticfloat Oct 27, 2014

Author Member

Hahaha, "native-julia touch"

julia> touch("/this/is/not/a/file")
touch: /this/is/not/a/file: No such file or directory
ERROR: failed process: Process(`touch /this/is/not/a/file`, ProcessExited(1)) [1]
 in pipeline_error at process.jl:502
 in touch at file.jl:74

This comment has been minimized.

Copy link
@tkelman

tkelman Oct 27, 2014

Contributor

#8594 ?

This comment has been minimized.

Copy link
@staticfloat

staticfloat Oct 27, 2014

Author Member

Ah, I ran that on 0.3.2. My bad. I was planning on using touch() anyway, but good to know that it is actually julia native.

error("$sysimg_path unwritable, ensure parent directory exists and is writable! (Do you need to run this with sudo?)")
end

# Copy in userimg.jl if it exists...
if userimg_path != nothing
if !isreadable(userimg_path)
error("$userimg_path is not readable, ensure it is an absolute path!")
end
cp(userimg_path, "userimg.jl")
end

# Start by building sys0.{ji,o}
sys0_path = joinpath(dirname(sysimg_path), "sys0")
println("Building sys0.o...")
Expand All @@ -45,9 +56,18 @@ function build_sysimg(sysimg_path; force=false, cpu_target="native")
end
@windows_only append!(FLAGS, ["-L$JULIA_HOME", "-ljulia", "-lssp"])

println("Linking sys.$(Sys.dlext)")
run(`$ld $FLAGS -o $sysimg_path.$(Sys.dlext) $sysimg_path.o`)
if ld != nothing
println("Linking sys.$(Sys.dlext)")
run(`$ld $FLAGS -o $sysimg_path.$(Sys.dlext) $sysimg_path.o`)
end

# Cleanup userimg.jl
try
rm("userimg.jl")
end
end

println("System image built; run julia -J $sysimg_path.ji")
end

# Search for a linker to link sys.o into sys.dl_ext. Honor LD environment variable, otherwise search for something we know works
Expand All @@ -59,17 +79,27 @@ function find_system_linker()
return ENV["LD"]
end

poss_linkers = ["ld", "link"]
# On Windows, check to see if WinRPM is installed, and if so, see if binutils is installed
@windows_only try
using WinRPM
if WinRPM.installed("binutils")
ENV["PATH"] = "$(ENV["PATH"]):$(joinpath(WinRPM.installdir,"usr","$(Sys.ARCH)-w64-mingw32","sys-root","mingw","bin"))"
else
throw()
end
catch
warn("binutils package not installed! Install via WinRPM.install(\"binutils\") for faster sysimg load times" )
end

for linker in poss_linkers
try
if success(`which $linker`)
return linker
end

# See if `ld` exists
try
if success(`which ld`)

This comment has been minimized.

Copy link
@tkelman

tkelman Oct 27, 2014

Contributor

we don't have a working which on windows unless we make another copy of busybox

This comment has been minimized.

Copy link
@staticfloat

staticfloat Oct 27, 2014

Author Member

Hmmm. I could just try running it instead?

This comment has been minimized.

Copy link
@staticfloat

staticfloat Oct 27, 2014

Author Member
success(`ld -v`)

would that work for you on windows?

This comment has been minimized.

Copy link
@tkelman

tkelman Oct 27, 2014

Contributor

ah, too many things going on simultaneously. probably?

This comment has been minimized.

Copy link
@staticfloat

staticfloat Oct 27, 2014

Author Member

I know, my inbox just exploded.

This comment has been minimized.

Copy link
@tkelman

tkelman Oct 27, 2014

Contributor

(largely my fault, huh)

return "ld"
end
end

error( "No supported linker found (tried $(join(poss_linkers, ", "))), override with LD environment variable!" )
warn( "No supported linker found; sysimg load times will be longer!" )
end

if !isinteractive()
Expand Down

0 comments on commit cacfba3

Please sign in to comment.