@@ -5,9 +5,8 @@ using SnoopCompile
55
66include (" 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
5049end
5150
52- function revert ( )
53- build_sysimg (force = true )
51+ function clean_image (debug = false )
52+ build_sysimg (default_sysimg_path (debug), force = true )
5453end
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
66121end
67122
68123end # module
0 commit comments