Skip to content

Commit f3b09c2

Browse files
authored
Merge pull request #13 from SimonDanisch/sd/fixes
fix #12
2 parents 713abb2 + 38dc02a commit f3b09c2

File tree

3 files changed

+54
-51
lines changed

3 files changed

+54
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ precompile.jl
1010
*.dll
1111
*.dylib
1212
*.o
13+
sysimg

src/PackageCompiler.jl

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,35 @@ function snoop(path, compilationfile, csv)
5353
end
5454
end
5555

56-
function clean_image(debug = false)
57-
build_sysimg(default_sysimg_path(debug), force = true)
56+
function copy_system_image(src, dest, ignore_missing = false)
57+
for file in sysimage_binaries
58+
# backup
59+
srcfile = joinpath(src, file)
60+
destfile = joinpath(dest, file)
61+
if !isfile(srcfile)
62+
ignore_missing && continue
63+
error("No file: $srcfile")
64+
end
65+
info("Copying system image: $srcfile to $destfile")
66+
cp(srcfile, destfile, remove_destination = true)
67+
end
68+
end
69+
70+
function build_clean_image(debug = false)
71+
backup = sysimgbackup_folder()
72+
build_sysimg(backup, "native")
73+
copy_system_image(backup, default_sysimg_path(debug))
5874
end
5975

76+
6077
function revert(debug = false)
6178
syspath = default_sysimg_path(debug)
62-
sysimg_backup = joinpath(@__DIR__, "..", "sysimg_backup")
79+
sysimg_backup = sysimgbackup_folder()
6380
if all(x-> isfile(joinpath(sysimg_backup, x)), sysimage_binaries) # we have a backup
64-
for file in sysimage_binaries
65-
# backup
66-
bfile = joinpath(sysimg_backup, file)
67-
if isfile(bfile)
68-
sfile = joinpath(dirname(syspath), file)
69-
isfile(sfile) && mv(sfile, sfile*".old", remove_destination = true)
70-
mv(bfile, sfile, remove_destination = false)
71-
else
72-
warn("No backup of $file found")
73-
end
74-
end
81+
copy_system_image(sysimg_backup, syspath)
7582
else
7683
warn("No backup found but restoring. Need to build a new system image from scratch")
77-
sysimg_backup = joinpath(@__DIR__, "..", "sysimg_backup") # build directly into backup
78-
isdir(sysimg_backup) || mkpath(sysimg_backup)
79-
build_sysimg(joinpath(sysimg_backup, "sys"))
80-
# now we should have a backup.
81-
# make sure that we have all files to not end up with an endless recursion!
82-
if all(x-> isfile(joinpath(sysimg_backup, x)), sysimage_binaries)
83-
revert(debug)
84-
else
85-
error("Revert went wrong")
86-
end
84+
build_clean_image(debug)
8785
end
8886
end
8987

@@ -96,10 +94,6 @@ function get_root_dir(path)
9694
end
9795
end
9896

99-
function snoop_package(package::String, rel_snoop_file, sysimg_tmp, reuse)
100-
precompile_file = joinpath(sysimg_tmp, "precompile.jl")
101-
102-
end
10397

10498
function sysimg_folder(files...)
10599
base_path = normpath(abspath(joinpath(@__DIR__, "..", "sysimg")))
@@ -169,7 +163,7 @@ function compile_package(packages::Tuple{String, String}...; force = false, reus
169163
build_sysimg(image_path, "native", userimg)
170164
if force
171165
try
172-
replace_jl_sysimg(debug)
166+
replace_jl_sysimg(sysimg_folder(), debug)
173167
info(
174168
"Replaced system image successfully. Next start of julia will load the newly compiled system image.
175169
If you encounter any errors with the new julia image, try `PackageCompiler.revert([debug = false])`"
@@ -178,7 +172,15 @@ function compile_package(packages::Tuple{String, String}...; force = false, reus
178172
warn("An error has occured while replacing sysimg files:")
179173
warn(e)
180174
info("Recovering old system image from backup")
181-
revert(debug)
175+
# if any file is missing in default system image, revert!
176+
syspath = default_sysimg_path(debug)
177+
for file in sysimage_binaries
178+
if !isfile(joinpath(syspath, file))
179+
info("$(joinpath(syspath, file)) missing. Reverting!")
180+
revert(debug)
181+
break
182+
end
183+
end
182184
end
183185
else
184186
info("""
@@ -189,20 +191,17 @@ function compile_package(packages::Tuple{String, String}...; force = false, reus
189191
end
190192

191193

192-
function replace_jl_sysimg(debug = false)
194+
function replace_jl_sysimg(image_path, debug = false)
193195
syspath = default_sysimg_path(debug)
194-
for file in sysimage_binaries
195-
# backup
196-
bfile = sysimgbackup_folder(file)
197-
sfile = joinpath(dirname(syspath), file)
198-
if !isfile(bfile) # use the one that is already there
199-
mv(sfile, bfile, remove_destination = true)
200-
else
201-
mv(sfile, sfile*".old", remove_destination = true) # remove so we don't overwrite (seems to be problematic on windows)
202-
end
203-
mv(joinpath(sysimg_tmp, file), sfile, remove_destination = false)
204-
end
196+
backup = sysimgbackup_folder()
197+
# create a backup
198+
# if syspath has missing files, ignore, since it will get replaced anyways
199+
copy_system_image(syspath, backup, true)
200+
info("Overwriting system image!")
201+
copy_system_image(image_path, syspath)
205202
end
206203

207204

208205
end # module
206+
207+

src/build_sysimg.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ end
3333
system_compiler() = gcc
3434

3535
function default_sysimg_path(debug = false)
36+
ext = debug ? "sys-debug" : "sys"
3637
if is_unix()
37-
splitext(Libdl.dlpath(debug ? "sys-debug" : "sys"))[1]
38+
dirname(splitext(Libdl.dlpath(ext))[1])
3839
else
39-
joinpath(dirname(JULIA_HOME), "lib", "julia", debug ? "sys-debug" : "sys")
40+
joinpath(dirname(JULIA_HOME), "lib", "julia")
4041
end
4142
end
4243

@@ -52,7 +53,7 @@ current processor. Include the user image file given by `userimg_path`, which sh
5253
directives such as `using MyPackage` to include that package in the new system image. New
5354
system image will not replace an older image unless `force` is set to true.
5455
"""
55-
function build_sysimg(sysimg_path, cpu_target, userimg_path; debug = false)
56+
function build_sysimg(sysimg_path, cpu_target, userimg_path = nothing; debug = false)
5657
# Enter base and setup some useful paths
5758
base_dir = dirname(Base.find_source_file("sysimg.jl"))
5859
cd(base_dir) do
@@ -68,13 +69,15 @@ function build_sysimg(sysimg_path, cpu_target, userimg_path; debug = false)
6869
end
6970

7071
# Copy in userimg.jl if it exists
71-
if !isfile(userimg_path)
72-
error("$userimg_path is not found, ensure it is an absolute path.")
73-
end
74-
if isfile("userimg.jl")
75-
error("$base_dir/userimg.jl already exists, delete manually to continue.")
72+
if userimg_path != nothing
73+
if !isfile(userimg_path)
74+
error("$userimg_path is not found, ensure it is an absolute path.")
75+
end
76+
if isfile("userimg.jl")
77+
error("$base_dir/userimg.jl already exists, delete manually to continue.")
78+
end
79+
cp(userimg_path, "userimg.jl")
7680
end
77-
cp(userimg_path, "userimg.jl")
7881
try
7982
# Start by building inference.{ji,o}
8083
inference_path = joinpath(dirname(sysimg_path), "inference")

0 commit comments

Comments
 (0)