Skip to content

Commit 1809ac2

Browse files
authored
add precompile statements for functions called from main in apps (#649)
remove dependency on at-eval in main
1 parent 6db6760 commit 1809ac2

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/PackageCompiler.jl

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ function create_sysimg_object_file(object_file::String,
249249
precompile_statements_file::Vector{String},
250250
cpu_target::String,
251251
script::Union{Nothing, String},
252-
sysimage_build_args::Cmd)
252+
sysimage_build_args::Cmd,
253+
extra_precompiles::String)
253254
# Handle precompilation
254255
precompile_files = String[]
255256
@debug "running precompilation execution script..."
@@ -301,6 +302,9 @@ function create_sysimg_object_file(object_file::String,
301302
@debug "failed to execute \$statement"
302303
end
303304
end
305+
@eval PrecompileStagingArea begin
306+
$extra_precompiles
307+
end
304308
end # module
305309
"""
306310

@@ -418,6 +422,7 @@ function create_sysimage(packages::Union{Nothing, Symbol, Vector{String}, Vector
418422
version=nothing,
419423
soname=nothing,
420424
compat_level::String="major",
425+
extra_precompiles::String = "",
421426
)
422427

423428
if filter_stdlibs && incremental
@@ -506,7 +511,8 @@ function create_sysimage(packages::Union{Nothing, Symbol, Vector{String}, Vector
506511
precompile_statements_file,
507512
cpu_target,
508513
script,
509-
sysimage_build_args)
514+
sysimage_build_args,
515+
extra_precompiles)
510516
object_files = [object_file]
511517
if julia_init_c_file !== nothing
512518
push!(object_files, compile_c_init_julia(julia_init_c_file, basename(sysimage_path)))
@@ -700,14 +706,24 @@ function create_app(package_dir::String,
700706
package_name = ctx.env.pkg.name
701707
project = dirname(ctx.env.project_file)
702708

709+
# add precompile statements for functions that will be called from the C main() wrapper
710+
precompiles = String[]
711+
for (_, julia_main) in executables
712+
push!(precompiles, "isdefined($package_name, :$julia_main) && precompile(Tuple{typeof($package_name.$julia_main)})")
713+
end
714+
push!(precompiles, "precompile(Tuple{typeof(append!), Vector{String}, Vector{Any}})")
715+
push!(precompiles, "precompile(Tuple{typeof(empty!), Vector{String}})")
716+
push!(precompiles, "precompile(Tuple{typeof(popfirst!), Vector{String}})")
717+
703718
create_sysimage([package_name]; sysimage_path, project,
704719
incremental,
705720
filter_stdlibs,
706721
precompile_execution_file,
707722
precompile_statements_file,
708723
cpu_target,
709724
sysimage_build_args,
710-
include_transitive_dependencies)
725+
include_transitive_dependencies,
726+
extra_precompiles = join(precompiles, "\n"))
711727

712728
for (app_name, julia_main) in executables
713729
create_executable_from_sysimg(joinpath(app_dir, "bin", app_name), c_driver_program, string(package_name, ".", julia_main))
@@ -717,7 +733,7 @@ end
717733

718734
function create_executable_from_sysimg(exe_path::String,
719735
c_driver_program::String,
720-
julia_main::String)
736+
julia_main::String)
721737
c_driver_program = abspath(c_driver_program)
722738
mkpath(dirname(exe_path))
723739
flags = Base.shell_split(join((cflags(), ldflags(), ldlibs()), " "))

src/embedding_wrapper.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ int main(int argc, char *argv[]) {
102102

103103
// Update ARGS and PROGRAM_FILE
104104
checked_eval_string("append!(empty!(Base.ARGS), Core.ARGS)");
105-
checked_eval_string("@eval Base PROGRAM_FILE = popfirst!(ARGS)");
105+
jl_value_t *firstarg = checked_eval_string("popfirst!(ARGS)");
106+
JL_GC_PUSH1(&firstarg);
107+
jl_set_global(jl_base_module, jl_symbol("PROGRAM_FILE"), firstarg);
108+
JL_GC_POP();
106109

107110
// call the work function, and get back a value
108111
jl_value_t *jl_retcode = checked_eval_string(JULIA_MAIN "()");

0 commit comments

Comments
 (0)