Skip to content

Commit df35e16

Browse files
committed
improve build options/process
1 parent 5b8f781 commit df35e16

File tree

2 files changed

+67
-45
lines changed

2 files changed

+67
-45
lines changed

src/PackageCompiler.jl

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ using SnoopCompile
55

66
include("build_sysimg.jl")
77

8-
function snoop(path, compilationfile, reuse)
8+
function snoop(path, compilationfile, csv, reuse)
99
cd(@__DIR__)
10-
csv = "precompile.csv"
1110
# Snoop compiler can't handle the path as a variable, so we just create a file
1211
if !reuse
1312
open(joinpath("snoopy.jl"), "w") do io
@@ -49,20 +48,76 @@ function snoop(path, compilationfile, reuse)
4948
end
5049
end
5150

52-
function revert()
53-
build_sysimg(force = true)
51+
function clean_image(debug = false)
52+
build_sysimg(default_sysimg_path(debug), force = true)
5453
end
5554

56-
function compile_package(package, force = false, reuse = false)
55+
function revert(debug = false)
56+
syspath = default_sysimg_path(debug)
57+
sysimg_backup = joinpath(@__DIR__, "..", "sysimg_backup")
58+
sysfiles = ("sys.o", "sys.so", "sys.ji", "inference.o", "inference.ji")
59+
if all(x-> isfile(joinpath(sysimg_backup, x)), sysfiles) # we have a backup
60+
for file in sysfiles
61+
# backup
62+
bfile = joinpath(sysimg_backup, file)
63+
if isfile(bfile)
64+
sfile = joinpath(dirname(syspath), file)
65+
isfile(sfile) && rm(sfile)
66+
mv(bfile, sfile)
67+
else
68+
warn("No backup of $file found")
69+
end
70+
end
71+
else
72+
warn("No backup found but restoring. Need to build a new system image from scratch")
73+
sysimg_backup = joinpath(@__DIR__, "..", "sysimg_backup") # build directly into backup
74+
build_sysimg(joinpath(sysimg_backup, "sys"))
75+
# now we should have a backup.
76+
# make sure that we have all files to not end up with an endless recursion!
77+
if all(x-> isfile(joinpath(sysimg_backup, x)), sysfiles)
78+
revert(debug)
79+
else
80+
error("Revert went wrong")
81+
end
82+
end
83+
end
84+
85+
function compile_package(package, force = false, reuse = false; debug = false)
5786
realpath = if ispath(package)
5887
normpath(abspath(package))
5988
else
6089
Pkg.dir(package)
6190
end
6291
testroot = joinpath(realpath, "test")
6392
precompile_file = joinpath(testroot, "precompile.jl")
64-
snoop(joinpath(testroot, "runtests.jl"), precompile_file, reuse)
65-
build_sysimg(default_sysimg_path(), "native", precompile_file, force = force)
93+
sysimg_tmp = joinpath(@__DIR__, "..", "sysimg_tmp", basename(realpath))
94+
snoop(joinpath(testroot, "runtests.jl"), precompile_file, joinpath(sysimg_tmp, "snooped.csv"), reuse)
95+
!isdir(sysimg_tmp) && mkdir(sysimg_tmp)
96+
build_sysimg(joinpath(sysimg_tmp, "sys"), "native", precompile_file)
97+
sysimg_backup = joinpath(@__DIR__, "..", "sysimg_backup")
98+
if force
99+
try
100+
syspath = default_sysimg_path(debug)
101+
for file in ("sys.o", "sys.so", "sys.ji", "inference.o", "inference.ji")
102+
# backup
103+
bfile = joinpath(sysimg_backup, file)
104+
sfile = joinpath(dirname(syspath), file)
105+
if !isfile(bfile) # use the one that is already there
106+
mv(sfile, bfile, remove_destination = true)
107+
else
108+
rm(sfile) # remove so we don't overwrite (seems to be problematic on windows)
109+
end
110+
mv(joinpath(sysimg_tmp, file), sfile)
111+
end
112+
catch e
113+
warn("An error has occured while replacing sysimg files:")
114+
warn(e)
115+
info("Recovering old system image from backup")
116+
revert(debug)
117+
end
118+
else
119+
info("Not forcing system image. You can start julia with julia -J $(joinpath(sysimg_tmp, "sys")) to load the compiled files.")
120+
end
66121
end
67122

68123
end # module

src/build_sysimg.jl

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ end
3333

3434
system_compiler() = gcc
3535

36-
function default_sysimg_path(debug=false)
36+
function default_sysimg_path(debug = false)
3737
if is_unix()
3838
splitext(Libdl.dlpath(debug ? "sys-debug" : "sys"))[1]
3939
else
@@ -53,31 +53,17 @@ current processor. Include the user image file given by `userimg_path`, which sh
5353
directives such as `using MyPackage` to include that package in the new system image. New
5454
system image will not replace an older image unless `force` is set to true.
5555
"""
56-
function build_sysimg(sysimg_path = nothing, cpu_target="native", userimg_path=nothing; force=false, debug=false)
57-
if sysimg_path === nothing
58-
sysimg_path = default_sysimg_path(debug)
59-
end
60-
61-
# Quit out if a sysimg is already loaded and is in the same spot as sysimg_path, unless forcing
62-
sysimg = Libdl.dlopen_e("sys")
63-
if sysimg != C_NULL
64-
if !force && Base.samefile(Libdl.dlpath(sysimg), "$(sysimg_path).$(Libdl.dlext)")
65-
info("System image already loaded at $(Libdl.dlpath(sysimg)), set force=true to override.")
66-
return nothing
67-
end
68-
end
56+
function build_sysimg(sysimg_path, cpu_target = "native", userimg_path = nothing; debug=false)
6957

7058
# Canonicalize userimg_path before we enter the base_dir
7159
if userimg_path !== nothing
7260
userimg_path = abspath(userimg_path)
7361
end
74-
7562
# Enter base and setup some useful paths
7663
base_dir = dirname(Base.find_source_file("sysimg.jl"))
7764
cd(base_dir) do
7865
julia = joinpath(JULIA_HOME, debug ? "julia-debug" : "julia")
7966
cc = system_compiler()
80-
8167
# Ensure we have write-permissions to wherever we're trying to write to
8268
try
8369
touch("$sysimg_path.ji")
@@ -111,15 +97,6 @@ function build_sysimg(sysimg_path = nothing, cpu_target="native", userimg_path=n
11197

11298
link_sysimg(sysimg_path, cc, debug)
11399

114-
if !Base.samefile("$(default_sysimg_path(debug)).ji", "$sysimg_path.ji")
115-
if Base.isfile("$sysimg_path.$(Libdl.dlext)")
116-
info("To run Julia with this image loaded, run: `julia -J $sysimg_path.$(Libdl.dlext)`.")
117-
else
118-
info("To run Julia with this image loaded, run: `julia -J $sysimg_path.ji`.")
119-
end
120-
else
121-
info("Julia will automatically load this system image at next startup.")
122-
end
123100
finally
124101
# Cleanup userimg.jl
125102
if userimg_path !== nothing && isfile("userimg.jl")
@@ -130,10 +107,8 @@ function build_sysimg(sysimg_path = nothing, cpu_target="native", userimg_path=n
130107
end
131108

132109
# Link sys.o into sys.$(dlext)
133-
function link_sysimg(sysimg_path = nothing, cc = system_compiler(), debug = false)
134-
if sysimg_path === nothing
135-
sysimg_path = default_sysimg_path(debug)
136-
end
110+
function link_sysimg(sysimg_path, cc = system_compiler(), debug = false)
111+
137112
julia_libdir = dirname(Libdl.dlpath(debug ? "libjulia-debug" : "libjulia"))
138113

139114
FLAGS = ["-L$julia_libdir"]
@@ -148,13 +123,5 @@ function link_sysimg(sysimg_path = nothing, cc = system_compiler(), debug = fals
148123
info("Linking sys.$(Libdl.dlext)")
149124
info("$cc $(join(FLAGS, ' ')) -o $sysimg_file $sysimg_path.o")
150125
# Windows has difficulties overwriting a file in use so we first link to a temp file
151-
if is_windows() && isfile(sysimg_file)
152-
if success(pipeline(`$cc $FLAGS -o $sysimg_path.tmp $sysimg_path.o`; stdout=STDOUT, stderr=STDERR))
153-
mv(sysimg_file, "$sysimg_file.old"; remove_destination = true)
154-
mv("$sysimg_path.tmp", sysimg_file; remove_destination = true)
155-
end
156-
else
157-
run(`$cc $FLAGS -o $sysimg_file $sysimg_path.o`)
158-
end
159-
info("System image successfully built at $sysimg_path.$(Libdl.dlext)")
126+
run(`$cc $FLAGS -o $sysimg_file $sysimg_path.o`)
160127
end

0 commit comments

Comments
 (0)