-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
precompile.jl
207 lines (187 loc) · 6.9 KB
/
precompile.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
# This file is a part of Julia. License is MIT: https://julialang.org/license
module Precompile
# Can't use this during incremental: `@eval Module() begin``
import ..REPL
# Ugly hack for our cache file to not have a dependency edge on FakePTYs.
Base._track_dependencies[] = false
try
Base.include(@__MODULE__, joinpath(Sys.BINDIR, "..", "share", "julia", "test", "testhelpers", "FakePTYs.jl"))
import .FakePTYs: open_fake_pty
finally
Base._track_dependencies[] = true
end
using Base.Meta
import Markdown
## Debugging options
# Disable parallel precompiles generation by setting `false`
const PARALLEL_PRECOMPILATION = true
# View the code sent to the repl by setting this to `stdout`
const debug_output = devnull # or stdout
CTRL_C = '\x03'
CTRL_R = '\x12'
UP_ARROW = "\e[A"
DOWN_ARROW = "\e[B"
repl_script = """
2+2
print("")
printstyled("a", "b")
display([1])
display([1 2; 3 4])
foo(x) = 1
@time @eval foo(1)
; pwd
$CTRL_C
$CTRL_R$CTRL_C
? reinterpret
using Ra\t$CTRL_C
\\alpha\t$CTRL_C
\e[200~paste here ;)\e[201~"$CTRL_C
$UP_ARROW$DOWN_ARROW$CTRL_C
123\b\b\b$CTRL_C
\b\b$CTRL_C
f(x) = x03
f(1,2)
[][1]
cd("complet_path\t\t$CTRL_C
"""
julia_exepath() = joinpath(Sys.BINDIR, Base.julia_exename())
const JULIA_PROMPT = "julia> "
const PKG_PROMPT = "pkg> "
const SHELL_PROMPT = "shell> "
const HELP_PROMPT = "help?> "
blackhole = Sys.isunix() ? "/dev/null" : "nul"
procenv = Dict{String,Any}(
"JULIA_HISTORY" => blackhole,
"JULIA_PROJECT" => nothing, # remove from environment
"JULIA_LOAD_PATH" => "@stdlib",
"JULIA_DEPOT_PATH" => Sys.iswindows() ? ";" : ":",
"TERM" => "",
"JULIA_FALLBACK_REPL" => "0") # Turn REPL.jl on in subprocess
generate_precompile_statements() = try
# Extract the precompile statements from the precompile file
statements_step = Channel{String}(Inf)
step = @async mktemp() do precompile_file, precompile_file_h
# Collect statements from running a REPL process and replaying our REPL script
touch(precompile_file)
pts, ptm = open_fake_pty()
cmdargs = `-e 'import REPL; REPL.Terminals.is_precompiling[] = true'`
p = run(addenv(addenv(```$(julia_exepath()) -O0 --trace-compile=$precompile_file
--cpu-target=native --startup-file=no --compiled-modules=existing --color=yes -i $cmdargs```, procenv),
"JULIA_PKG_PRECOMPILE_AUTO" => "0"),
pts, pts, pts; wait=false)
Base.close_stdio(pts)
# Prepare a background process to copy output from process until `pts` is closed
output_copy = Base.BufferStream()
tee = @async try
while !eof(ptm)
l = readavailable(ptm)
write(debug_output, l)
Sys.iswindows() && (sleep(0.1); yield(); yield()) # workaround hang - probably a libuv issue?
write(output_copy, l)
end
catch ex
if !(ex isa Base.IOError && ex.code == Base.UV_EIO)
rethrow() # ignore EIO on ptm after pts dies
end
finally
close(output_copy)
close(ptm)
end
Base.errormonitor(tee)
repl_inputter = @async begin
# wait for the definitive prompt before start writing to the TTY
readuntil(output_copy, JULIA_PROMPT)
sleep(0.1)
readavailable(output_copy)
# Input our script
precompile_lines = split(repl_script::String, '\n'; keepempty=false)
curr = 0
for l in precompile_lines
sleep(0.1)
curr += 1
# consume any other output
bytesavailable(output_copy) > 0 && readavailable(output_copy)
# push our input
write(debug_output, "\n#### inputting statement: ####\n$(repr(l))\n####\n")
write(ptm, l, "\n")
readuntil(output_copy, "\n")
# wait for the next prompt-like to appear
readuntil(output_copy, "\n")
strbuf = ""
while !eof(output_copy)
strbuf *= String(readavailable(output_copy))
occursin(JULIA_PROMPT, strbuf) && break
occursin(PKG_PROMPT, strbuf) && break
occursin(SHELL_PROMPT, strbuf) && break
occursin(HELP_PROMPT, strbuf) && break
sleep(0.1)
end
end
write(ptm, "exit()\n")
wait(tee)
success(p) || Base.pipeline_error(p)
close(ptm)
write(debug_output, "\n#### FINISHED ####\n")
end
Base.errormonitor(repl_inputter)
n_step = 0
precompile_copy = Base.BufferStream()
buffer_reader = @async for statement in eachline(precompile_copy)
push!(statements_step, statement)
n_step += 1
end
open(precompile_file, "r") do io
while true
# We need to allways call eof(io) for bytesavailable(io) to work
eof(io) && istaskdone(repl_inputter) && eof(io) && break
if bytesavailable(io) == 0
sleep(0.1)
continue
end
write(precompile_copy, readavailable(io))
end
end
close(precompile_copy)
wait(buffer_reader)
close(statements_step)
return :ok
end
!PARALLEL_PRECOMPILATION && wait(step)
# Make statements unique
statements = Set{String}()
# Execute the precompile statements
for statement in statements_step
# Main should be completely clean
occursin("Main.", statement) && continue
Base.in!(statement, statements) && continue
try
ps = Meta.parse(statement)
if !isexpr(ps, :call)
# these are typically comments
@debug "skipping statement because it does not parse as an expression" statement
delete!(statements, statement)
continue
end
popfirst!(ps.args) # precompile(...)
ps.head = :tuple
# println(ps)
ps = eval(ps)
if !precompile(ps...)
@warn "Failed to precompile expression" form=statement _module=nothing _file=nothing _line=0
end
catch ex
# See #28808
@warn "Failed to precompile expression" form=statement exception=ex _module=nothing _file=nothing _line=0
end
end
fetch(step) == :ok || throw("Collecting precompiles failed.")
return nothing
finally
GC.gc(true); GC.gc(false); # reduce memory footprint
end
generate_precompile_statements()
# As a last step in system image generation,
# remove some references to build time environment for a more reproducible build.
Base.Filesystem.temp_cleanup_purge(force=true)
precompile(Tuple{typeof(getproperty), REPL.REPLBackend, Symbol})
end # Precompile