|
| 1 | +# This file is a part of Julia. License is MIT: https://julialang.org/license |
| 2 | + |
| 3 | +# Prevent this from being put into the Main namespace |
| 4 | +let |
| 5 | +M = Module() |
| 6 | +@eval M begin |
| 7 | +if !isdefined(Base, :uv_eventloop) |
| 8 | + Base.reinit_stdio() |
| 9 | +end |
| 10 | +Base.include(@__MODULE__, joinpath(Sys.BINDIR, "..", "share", "julia", "test", "testhelpers", "FakePTYs.jl")) |
| 11 | +import .FakePTYs: with_fake_pty |
| 12 | + |
| 13 | +CTRL_C = '\x03' |
| 14 | +UP_ARROW = "\e[A" |
| 15 | +DOWN_ARROW = "\e[B" |
| 16 | + |
| 17 | +precompile_script = """ |
| 18 | +2+2 |
| 19 | +print("") |
| 20 | +@time 1+1 |
| 21 | +; pwd |
| 22 | +? reinterpret |
| 23 | +using Ra\t$CTRL_C |
| 24 | +\\alpha\t$CTRL_C |
| 25 | +\e[200~paste here ;)\e[201~"$CTRL_C |
| 26 | +$UP_ARROW$DOWN_ARROW$CTRL_C |
| 27 | +123\b\b\b$CTRL_C |
| 28 | +\b\b$CTRL_C |
| 29 | +f(x) = x03 |
| 30 | +f(1,2) |
| 31 | +[][1] |
| 32 | +cd("complet_path\t\t$CTRL_C |
| 33 | +""" |
| 34 | + |
| 35 | +julia_cmd() = (julia = joinpath(Sys.BINDIR, Base.julia_exename()); `$julia`) |
| 36 | +have_repl = haskey(Base.loaded_modules, |
| 37 | + Base.PkgId(Base.UUID("3fa0cd96-eef1-5676-8a61-b3b8758bbffb"), "REPL")) |
| 38 | +have_pkg = haskey(Base.loaded_modules, |
| 39 | + Base.PkgId(Base.UUID("44cfe95a-1eb2-52ea-b672-e2afdf69b78f"), "Pkg")) |
| 40 | + |
| 41 | +if have_pkg |
| 42 | + precompile_script *= """ |
| 43 | + tmp = mktempdir() |
| 44 | + cd(tmp) |
| 45 | + touch("Project.toml") |
| 46 | + ] activate . |
| 47 | + st |
| 48 | + $CTRL_C |
| 49 | + rm(tmp; recursive=true) |
| 50 | + """ |
| 51 | +end |
| 52 | + |
| 53 | +function generate_precompile_statements() |
| 54 | + start_time = time() |
| 55 | + |
| 56 | + # Precompile a package |
| 57 | + mktempdir() do prec_path |
| 58 | + push!(DEPOT_PATH, prec_path) |
| 59 | + push!(LOAD_PATH, prec_path) |
| 60 | + pkgname = "__PackagePrecompilationStatementModule" |
| 61 | + mkpath(joinpath(prec_path, pkgname, "src")) |
| 62 | + write(joinpath(prec_path, pkgname, "src", "$pkgname.jl"), |
| 63 | + """ |
| 64 | + module $pkgname |
| 65 | + end |
| 66 | + """) |
| 67 | + @eval using __PackagePrecompilationStatementModule |
| 68 | + empty!(LOAD_PATH) |
| 69 | + empty!(DEPOT_PATH) |
| 70 | + end |
| 71 | + |
| 72 | + # Create a staging area where all the loaded packages are available |
| 73 | + PrecompileStagingArea = Module() |
| 74 | + for (_pkgid, _mod) in Base.loaded_modules |
| 75 | + if !(_pkgid.name in ("Main", "Core", "Base")) |
| 76 | + eval(PrecompileStagingArea, :($(Symbol(_mod)) = $_mod)) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # TODO: Implement REPL replayer for Windows |
| 81 | + @static if !Sys.iswindows() |
| 82 | + print("Generating precompile statements...") |
| 83 | + sysimg = isempty(ARGS) ? joinpath(dirname(Sys.BINDIR), "lib", "julia", "sys.ji") : ARGS[1] |
| 84 | + |
| 85 | + # Run a repl process and replay our script |
| 86 | + stdout_accumulator, stderr_accumulator = IOBuffer(), IOBuffer() |
| 87 | + with_fake_pty() do slave, master |
| 88 | + with_fake_pty() do slave_err, master_err |
| 89 | + done = false |
| 90 | + withenv("JULIA_HISTORY" => tempname(), "JULIA_PROJECT" => nothing, |
| 91 | + "TERM" => "") do |
| 92 | + p = run(`$(julia_cmd()) -O0 --trace-compile=yes --sysimage $sysimg |
| 93 | + --startup-file=no --color=yes`, |
| 94 | + slave, slave, slave_err; wait=false) |
| 95 | + readuntil(master, "julia>", keep=true) |
| 96 | + for (tty, accumulator) in (master => stdout_accumulator, |
| 97 | + master_err => stderr_accumulator) |
| 98 | + @async begin |
| 99 | + while true |
| 100 | + done && break |
| 101 | + write(accumulator, readavailable(tty)) |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + if have_repl |
| 106 | + for l in split(precompile_script, '\n'; keepempty=false) |
| 107 | + write(master, l, '\n') |
| 108 | + end |
| 109 | + end |
| 110 | + write(master, "exit()\n") |
| 111 | + wait(p) |
| 112 | + done = true |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + stderr_output = String(take!(stderr_accumulator)) |
| 118 | + # println(stderr_output) |
| 119 | + # stdout_output = String(take!(stdout_accumulator)) |
| 120 | + # println(stdout_output) |
| 121 | + |
| 122 | + # Extract the precompile statements from stderr |
| 123 | + statements = Set{String}() |
| 124 | + for statement in split(stderr_output, '\n') |
| 125 | + m = match(r"(precompile\(Tuple{.*)", statement) |
| 126 | + m === nothing && continue |
| 127 | + statement = m.captures[1] |
| 128 | + occursin(r"Main.", statement) && continue |
| 129 | + push!(statements, statement) |
| 130 | + end |
| 131 | + |
| 132 | + # Load the precompile statements |
| 133 | + statements_ordered = join(sort(collect(statements)), '\n') |
| 134 | + # println(statements_ordered) |
| 135 | + if have_repl |
| 136 | + # Seems like a reasonable number right now, adjust as needed |
| 137 | + @assert length(statements) > 700 |
| 138 | + end |
| 139 | + |
| 140 | + Base.include_string(PrecompileStagingArea, statements_ordered) |
| 141 | + print(" $(length(statements)) generated in ") |
| 142 | + Base.time_print((time() - start_time) * 10^9) |
| 143 | + println() |
| 144 | + end |
| 145 | + |
| 146 | + # Fall back to explicit list on Windows, might as well include them |
| 147 | + # for everyone though |
| 148 | + Base.include(PrecompileStagingArea, "precompile_explicit.jl") |
| 149 | + |
| 150 | + return |
| 151 | +end |
| 152 | + |
| 153 | +generate_precompile_statements() |
| 154 | + |
| 155 | +end # @eval |
| 156 | +end # let |
0 commit comments