@@ -53,37 +53,35 @@ function snoop(path, compilationfile, csv)
5353 end
5454end
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))
5874end
5975
76+
6077function 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
8886end
8987
@@ -96,10 +94,6 @@ function get_root_dir(path)
9694 end
9795end
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
10498function 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
189191end
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)
205202end
206203
207204
208205end # module
206+
207+
0 commit comments