Skip to content

Commit 454090a

Browse files
add exit_save_sysimage
1 parent 7303b45 commit 454090a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

base/Base.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ include("loading.jl")
391391
# misc useful functions & macros
392392
include("timing.jl")
393393
include("util.jl")
394+
include("output.jl")
394395

395396
include("asyncmap.jl")
396397

base/output.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
"""
4+
exit_save_sysimage(fpath::String)
5+
6+
Save the current state as a sysimage. The sysimage is saved after julia exit, which this
7+
function triggers.
8+
"""
9+
function exit_save_sysimage(fpath::String)
10+
fpath = abspath(fpath)
11+
ccall(:jl_options_set_outputo, Cvoid, (Cstring,), fpath)
12+
ccall(:jl_restore_module_init_order_cache, Cvoid, ())
13+
@info "Julia exiting. A sysimage will be generated at $(repr(fpath))"
14+
exit()
15+
end

src/init.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ extern BOOL (WINAPI *hSymRefreshModuleList)(HANDLE);
4444

4545
// list of modules being deserialized with __init__ methods
4646
jl_array_t *jl_module_init_order;
47+
jl_array_t *jl_module_init_order_cache;
48+
49+
JL_DLLEXPORT void jl_restore_module_init_order_cache(void)
50+
{
51+
jl_module_init_order = jl_module_init_order_cache;
52+
}
53+
4754

4855
JL_DLLEXPORT size_t jl_page_size;
4956

@@ -771,6 +778,7 @@ static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_
771778
if (jl_options.image_file && (!jl_generating_output() || jl_options.incremental) && jl_module_init_order) {
772779
jl_array_t *init_order = jl_module_init_order;
773780
JL_GC_PUSH1(&init_order);
781+
jl_module_init_order_cache = jl_module_init_order; // used when output is requested later than init
774782
jl_module_init_order = NULL;
775783
int i, l = jl_array_len(init_order);
776784
for (i = 0; i < l; i++) {

src/precompile.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ JL_DLLEXPORT int jl_generating_output(void)
2222

2323
static void *jl_precompile(int all);
2424

25-
void jl_write_compiler_output(void)
25+
JL_DLLEXPORT void jl_write_compiler_output(void)
2626
{
2727
if (!jl_generating_output()) {
2828
return;
@@ -36,7 +36,6 @@ void jl_write_compiler_output(void)
3636
jl_printf(JL_STDERR, "WARNING: --output requested, but no modules defined during run\n");
3737
return;
3838
}
39-
4039
jl_array_t *worklist = jl_module_init_order;
4140
JL_GC_PUSH1(&worklist);
4241
jl_module_init_order = jl_alloc_vec_any(0);
@@ -110,6 +109,11 @@ void jl_write_compiler_output(void)
110109
JL_GC_POP();
111110
}
112111

112+
JL_DLLEXPORT void jl_options_set_outputo(const char * path)
113+
{
114+
jl_options.outputo = path;
115+
}
116+
113117
// f{<:Union{...}}(...) is a common pattern
114118
// and expanding the Union may give a leaf function
115119
static void _compile_all_tvar_union(jl_value_t *methsig)

0 commit comments

Comments
 (0)