-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbootstrap.jl
244 lines (204 loc) · 6.06 KB
/
bootstrap.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using PackageCompiler
using Setfield
using Pkg
const trace_dir = abspath(@__DIR__,"../traces/")
const trace_file = Vector{UInt8}()
function trace()
global trace_dir,trace_file
!isdir(trace_dir) && mkdir(trace_dir)
empty!(trace_file)
for c in joinpath(trace_dir,"trace_$(rand(UInt32)).jl")
push!(trace_file,UInt8(c))
end
push!(trace_file,C_NULL)
opts = Base.JLOptions()
opts = @set opts.trace_compile = pointer(trace_file)
opts = @set opts.compile_enabled = 2
unsafe_store!(Base.cglobal(:jl_options,Base.JLOptions),opts)
return nothing
end
function untrace()
global trace_dir,cstr
opts = Base.JLOptions()
opts = @set opts.trace_compile = C_NULL
opts = @set opts.compile_enabled = 1
unsafe_store!(Base.cglobal(:jl_options,Base.JLOptions),opts)
return nothing
end
function grep_possible_packages!(package_set::Set{Symbol},expr::Expr)
(length(expr.args) == 0) && return
s = expr.args[1]
if expr.head == :.
if typeof(s) === Symbol && s !== :.
push!(package_set,s)
end
end
foreach(expr.args) do a
if typeof(a) == Expr
grep_possible_packages!(package_set,a)
end
end
nothing
end
semantic_fix(line) = begin
line = replace(line,"(\"" => "(raw\"")
line = replace(line,"DatePart{Char(" => "DatePart{(")
line = replace(line,r"(?<!#)#s(?=\d)" => "T")
line = replace(line,")()" => ")")
end
try_parse_line(line,linenum = 0,filename ="") = begin
try
expr = Meta.parse(line, raise = true)
if expr.head != :incomplete
return expr
else
@warn "skipping line with parsing error" linenum filename
return nothing
end
catch e
@warn "skipping line with parsing error" linenum filename
return nothing
end
end
function brute_build_julia(;clear_traces = true)
!isdir(trace_dir) && begin
@info "no trace files found"
return
end
blacklist = push!(Fezzik.blacklist(),"Main","##benchmark#","###compiledcall")
statements = Set{String}()
packages = Set{Symbol}()
for fname in readdir(trace_dir)
counter = 0
fname = abspath(trace_dir,fname)
for line in eachline(fname)
try
counter += 1
any(occursin.(blacklist,[line])) && continue
line = semantic_fix(line);
expr = try_parse_line(line,counter,fname);
isnothing(expr) && continue
grep_possible_packages!(packages,expr)
push!(statements,line);
catch e
statement = line
@info "Failed to parse statement" statement
end
end
end
clear_traces && isdir(trace_dir) && for f in readdir(trace_dir)
file = joinpath(trace_dir,f)
try
rm(file;force = true)
catch err
@warn "failed to remove trace file" err file
end
end
out_file = abspath(@__DIR__,"../","precomp.jl")
@info "generating precompile"
my_env = Base.ACTIVE_PROJECT.x
env = """
import Pkg
Pkg.activate()
import Random
Base.PCRE.__init__()
Random.__init__()
"""
usings = """
import Fezzik
packages = $(repr(packages))
Fezzik.brute_import_packages!(packages,@__MODULE__)
"""
#dry run
temp_mod = Module()
include_string(temp_mod,usings)
if my_env == nothing
Pkg.activate()
else
Pkg.activate(my_env)
end
open(out_file, "w") do io
println(io, """
$env
# recursively bring dependencies of used packages and standard libraries into namespace
$usings
Fezzik.reset_count()
""")
for line in statements
println(io, "Fezzik.@compile $line ")
end
println(io, "Fezzik.compile_summary()")
end
@info "used $(length(statements)) precompile statements"
@show out_file
untrace()
println("\n\n\n Compiling...")
compile_incremental(out_file)
@info "DONE!!"
exit()
nothing
end
export brute_build_julia
import Libdl
sysimage_size() = stat(PackageCompiler.default_sysimg_path()).size/(1024*1024)
export sysimage_size
revert(;force = false) = begin
PackageCompiler.restore_default_sysimage()
end
compile_incremental(file) = begin
PackageCompiler.create_sysimage(precompile_execution_file = file , replace_default = true; cpu_target="generic")
end
function PackageCompiler.create_sysimg_object_file(object_file::String, packages::Vector{String};
project::String,
base_sysimage::String,
precompile_execution_file::Vector{String},
precompile_statements_file::Vector{String},
cpu_target::String,
script::Nothing,
isapp::Bool)
@debug "creating object file at $object_file"
@info "PackageCompiler: creating system image object file, this might take a while..."
code = PrecompileCommand(precompile_execution_file[1])
cmd = `$(PackageCompiler.get_julia_cmd()) --cpu-target=$cpu_target
--sysimage=$base_sysimage --output-o=$(object_file) -e $code`
@debug "running $cmd"
run(cmd)
end
#old packagecompiler
"""
Init basic C libraries
"""
function InitBase()
"""
Base.__init__()
Sys.__init__() #fix https://github.com/JuliaLang/julia/issues/30479
@eval Sys BINDIR = ccall(:jl_get_julia_bindir, Any, ())::String
Base.init_load_path()
Base.init_depot_path()
"""
end
"""
# Initialize REPL module for Docs
"""
function InitREPL()
"""
using REPL
Base.REPL_MODULE_REF[] = REPL
"""
end
function Include(path)
"""
Mod = Module()
Base.include(Mod,$(repr(path)))
empty!(LOAD_PATH)
empty!(DEPOT_PATH)
"""
end
"""
The command to pass to julia --output-o, that runs the julia code in `path` during compilation.
"""
function PrecompileCommand(path)
InitBase() *
InitREPL() *
Include(path)
end