Skip to content

Commit 502e5fd

Browse files
committed
Merge pull request #9266 from JuliaLang/jn/init_opt_cleanup
cleanup julia_init options
2 parents e669ee3 + dc8c12a commit 502e5fd

25 files changed

+423
-327
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ else ifeq ($(OS), Linux)
306306
done
307307
endif
308308

309-
# Overwrite JL_SYSTEM_IMAGE_PATH in julia binaries
310-
for julia in $(DESTDIR)$(bindir)/julia* ; do \
309+
# Overwrite JL_SYSTEM_IMAGE_PATH in julia library
310+
for julia in $(DESTDIR)$(private_libdir)/libjulia*.$(SHLIB_EXT) ; do \
311311
$(call spawn,$(build_bindir)/stringreplace $$(strings -t x - $$julia | grep "sys.ji$$" | awk '{print $$1;}' ) "$(private_libdir_rel)/sys.ji" 256 $(call cygpath_w,$$julia)); \
312312
done
313313
endif

base/boot.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export
147147
# method reflection
148148
applicable, invoke, method_exists,
149149
# constants
150-
JULIA_HOME, nothing, Main,
150+
nothing, Main,
151151
# intrinsics module
152152
Intrinsics
153153
#ccall, cglobal, llvmcall, abs_float, add_float, add_int, and_int, ashr_int,

base/client.jl

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ function load_machine_file(path::AbstractString)
356356
end
357357

358358
function early_init()
359+
global const JULIA_HOME = ccall(:jl_get_julia_home, Any, ())
359360
Sys.init_sysinfo()
360361
if CPU_CORES > 8 && !("OPENBLAS_NUM_THREADS" in keys(ENV)) && !("OMP_NUM_THREADS" in keys(ENV))
361362
# Prevent openblas from stating to many threads, unless/until specifically requested
@@ -372,13 +373,10 @@ import .Terminals
372373
import .REPL
373374

374375
function _start()
375-
early_init()
376-
377376
try
378377
init_parallel()
379378
init_bind_addr(ARGS)
380379
any(a->(a=="--worker"), ARGS) || init_head_sched()
381-
init_load_path()
382380
(quiet,repl,startup,color_set,no_history_file) = process_options(copy(ARGS))
383381

384382
local term
@@ -412,32 +410,25 @@ function _start()
412410
# note: currently IOStream is used for file STDIN
413411
if isa(STDIN,File) || isa(STDIN,IOStream)
414412
# reading from a file, behave like include
415-
eval(parse_input_line(readall(STDIN)))
413+
eval(Main,parse_input_line(readall(STDIN)))
416414
else
417415
# otherwise behave repl-like
418416
while !eof(STDIN)
419417
eval_user_input(parse_input_line(STDIN), true)
420418
end
421419
end
422-
if have_color
423-
print(color_normal)
424-
end
425-
quit()
420+
else
421+
REPL.run_repl(active_repl)
426422
end
427-
REPL.run_repl(active_repl)
428423
end
429424
catch err
430425
display_error(err,catch_backtrace())
431426
println()
432427
exit(1)
433428
end
434-
if is_interactive
435-
if have_color
436-
print(color_normal)
437-
end
438-
println()
429+
if is_interactive && have_color
430+
print(color_normal)
439431
end
440-
ccall(:uv_atexit_hook, Void, ())
441432
end
442433

443434
const atexit_hooks = []

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export
163163
Inf,
164164
Inf16,
165165
Inf32,
166+
JULIA_HOME,
166167
LOAD_PATH,
167168
MS_ASYNC,
168169
MS_INVALIDATE,

base/sysimg.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ function __init__()
294294
reinit_stdio()
295295
Multimedia.reinit_displays() # since Multimedia.displays uses STDOUT as fallback
296296
fdwatcher_init()
297+
early_init()
298+
init_load_path()
297299
end
298300

299301
include("precompile.jl")

doc/manual/embedding.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,33 @@ We start with a simple C program that initializes Julia and calls some Julia cod
1818

1919
int main(int argc, char *argv[])
2020
{
21+
/* optional: randomize the stack guard */
22+
char a, b, c;
23+
SET_STACK_CHK_GUARD(a,b,c);
24+
25+
/* required: setup the julia context */
2126
jl_init(NULL);
22-
JL_SET_STACK_BASE;
2327

28+
/* run julia commands */
2429
jl_eval_string("print(sqrt(2.0))");
2530

31+
/* strongly recommended: notify julia that the
32+
program is about to terminate. this allows
33+
julia time to cleanup pending write requests
34+
and run all finalizers
35+
*/
36+
jl_atexit_hook();
37+
38+
/* if the stack guard is set: reset the stack guard */
39+
CLR_STACK_CHK_GUARD(a,b,c);
2640
return 0;
2741
}
2842

2943
In order to build this program you have to put the path to the Julia header into the include path and link against ``libjulia``. For instance, when Julia is installed to ``$JULIA_DIR``, one can compile the above test program ``test.c`` with gcc using::
3044

3145
gcc -o test -I$JULIA_DIR/include/julia -L$JULIA_DIR/usr/lib -ljulia test.c
3246

33-
Alternatively, look at the ``embedding.c`` program in the julia source tree in the ``examples/`` folder.
47+
Alternatively, look at the ``embedding.c`` program in the julia source tree in the ``examples/`` folder. The file ``ui/repl.c`` program is another simple example of how to set ``jl_compileropts`` options while linking against libjulia.
3448

3549
The first thing that has to be done before calling any other Julia C function is to initialize Julia. This is done by calling ``jl_init``, which takes as argument a C string (``const char*``) to the location where Julia is installed. When the argument is ``NULL``, Julia tries to determine the install location automatically.
3650

examples/embedding.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ double my_c_sqrt(double x)
99

1010
int main()
1111
{
12+
char a, b, c;
13+
SET_STACK_CHK_GUARD(a,b,c);
1214
jl_init(NULL);
13-
JL_SET_STACK_BASE;
1415

1516
{
1617
// Simple running Julia code
@@ -94,5 +95,7 @@ int main()
9495
}
9596
}
9697

98+
jl_atexit_hook();
99+
CLR_STACK_CHK_GUARD(a,b,c);
97100
return 0;
98101
}

src/ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static builtinspec_t julia_flisp_ast_ext[] = {
114114
{ NULL, NULL }
115115
};
116116

117-
DLLEXPORT void jl_init_frontend(void)
117+
void jl_init_frontend(void)
118118
{
119119
fl_init(4*1024*1024);
120120
value_t img = cvalue(iostreamtype, sizeof(ios_t));

src/codegen.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ extern "C" {
121121

122122
#include "builtin_proto.h"
123123

124-
void *__stack_chk_guard = NULL;
124+
DLLEXPORT void *__stack_chk_guard = NULL;
125125

126126
#if defined(_OS_WINDOWS_) && !defined(_COMPILER_MINGW_)
127127
void __stack_chk_fail()
@@ -130,9 +130,8 @@ void __attribute__(()) __stack_chk_fail()
130130
#endif
131131
{
132132
/* put your panic function or similar in here */
133-
fprintf(stderr, "warning: stack corruption detected\n");
134-
//assert(0 && "stack corruption detected");
135-
//abort();
133+
fprintf(stderr, "fatal error: stack corruption detected\n");
134+
abort(); // end with abort, since the compiler destroyed the stack upon entry to this function
136135
}
137136
}
138137

@@ -877,7 +876,7 @@ static void coverageVisitLine(std::string filename, int line)
877876

878877
void write_log_data(logdata_t logData, const char *extension)
879878
{
880-
std::string base = std::string(julia_home);
879+
std::string base = std::string(jl_compileropts.julia_home);
881880
base = base + "/../share/julia/base/";
882881
logdata_t::iterator it = logData.begin();
883882
for (; it != logData.end(); it++) {

src/disasm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ void jl_dump_function_asm(const char *Fptr, size_t Fsize,
482482
#ifdef LLVM35
483483
if (MCIA->evaluateBranch(Inst, Index, insSize, addr))
484484
#else
485-
if ((addr = MCIA->evaluateBranch(Inst, Index, insSize)) != -1)
485+
if ((addr = MCIA->evaluateBranch(Inst, Index, insSize)) != (uint64_t)-1)
486486
#endif
487487
DisInfo.insertAddress(addr);
488488
}

src/dlload.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static uv_lib_t *jl_load_dynamic_library_(char *modname, unsigned flags, int thr
116116
ext = extensions[i];
117117
path[0] = '\0';
118118
handle->handle = NULL;
119-
if (dl_path[len-1] == PATHSEP)
119+
if (dl_path[len-1] == PATHSEPSTRING[0])
120120
snprintf(path, PATHBUF, "%s%s%s", dl_path, modname, ext);
121121
else
122122
snprintf(path, PATHBUF, "%s" PATHSEPSTRING "%s%s", dl_path, modname, ext);

src/dump.c

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -452,21 +452,14 @@ static void jl_serialize_module(ios_t *s, jl_module_t *m)
452452
jl_serialize_value(s, m->parent);
453453
if (ref_only)
454454
return;
455-
// set on every startup; don't save value
456-
jl_sym_t *jhsym = jl_symbol("JULIA_HOME");
457455
size_t i;
458456
void **table = m->bindings.table;
459457
for(i=1; i < m->bindings.size; i+=2) {
460458
if (table[i] != HT_NOTFOUND) {
461459
jl_binding_t *b = (jl_binding_t*)table[i];
462460
if (b->owner == m || m != jl_main_module) {
463461
jl_serialize_value(s, b->name);
464-
if (table[i-1] == jhsym && m == jl_core_module) {
465-
jl_serialize_value(s, NULL);
466-
}
467-
else {
468-
jl_serialize_value(s, b->value);
469-
}
462+
jl_serialize_value(s, b->value);
470463
jl_serialize_value(s, b->type);
471464
jl_serialize_value(s, b->owner);
472465
write_int8(s, (b->constp<<2) | (b->exportp<<1) | (b->imported));
@@ -1305,7 +1298,7 @@ void jl_deserialize_lambdas_from_mod(ios_t *s)
13051298

13061299
extern jl_array_t *jl_module_init_order;
13071300

1308-
DLLEXPORT void jl_save_system_image(char *fname)
1301+
DLLEXPORT void jl_save_system_image(const char *fname)
13091302
{
13101303
jl_gc_collect();
13111304
jl_gc_collect();
@@ -1337,10 +1330,8 @@ DLLEXPORT void jl_save_system_image(char *fname)
13371330
// save module initialization order
13381331
if (jl_module_init_order != NULL) {
13391332
for(i=0; i < jl_array_len(jl_module_init_order); i++) {
1340-
// NULL out any modules that weren't saved
1341-
jl_value_t *mod = jl_cellref(jl_module_init_order, i);
1342-
if (ptrhash_get(&backref_table, mod) == HT_NOTFOUND)
1343-
jl_cellset(jl_module_init_order, i, NULL);
1333+
// verify that all these modules were saved
1334+
assert(ptrhash_get(&backref_table, jl_cellref(jl_module_init_order, i)) != HT_NOTFOUND);
13441335
}
13451336
}
13461337
jl_serialize_value(&f, jl_module_init_order);
@@ -1360,11 +1351,10 @@ extern void jl_get_system_hooks(void);
13601351
extern void jl_get_uv_hooks();
13611352

13621353
DLLEXPORT
1363-
void jl_restore_system_image(char *fname)
1354+
void jl_restore_system_image(const char *fname)
13641355
{
13651356
ios_t f;
1366-
char *fpath = fname;
1367-
if (ios_file(&f, fpath, 1, 0, 0, 0) == NULL) {
1357+
if (ios_file(&f, fname, 1, 0, 0, 0) == NULL) {
13681358
JL_PRINTF(JL_STDERR, "System image file \"%s\" not found\n", fname);
13691359
exit(1);
13701360
}
@@ -1432,14 +1422,10 @@ void jl_restore_system_image(char *fname)
14321422
//ios_printf(ios_stderr, "backref_list.len = %d\n", backref_list.len);
14331423
arraylist_free(&backref_list);
14341424
ios_close(&f);
1435-
if (fpath != fname) free(fpath);
14361425

14371426
#ifdef JL_GC_MARKSWEEP
14381427
if (en) jl_gc_enable();
14391428
#endif
1440-
// restore the value of our "magic" JULIA_HOME variable/constant
1441-
jl_get_binding_wr(jl_core_module, jl_symbol("JULIA_HOME"))->value =
1442-
jl_cstr_to_string(julia_home);
14431429
mode = last_mode;
14441430
jl_update_all_fptrs();
14451431
}
@@ -1543,7 +1529,7 @@ jl_value_t *jl_uncompress_ast(jl_lambda_info_t *li, jl_value_t *data)
15431529
}
15441530

15451531
DLLEXPORT
1546-
int jl_save_new_module(char *fname, jl_module_t *mod)
1532+
int jl_save_new_module(const char *fname, jl_module_t *mod)
15471533
{
15481534
ios_t f;
15491535
if (ios_file(&f, fname, 1, 1, 1, 1) == NULL) {
@@ -1583,7 +1569,7 @@ jl_function_t *jl_method_cache_insert(jl_methtable_t *mt, jl_tuple_t *type,
15831569
jl_function_t *method);
15841570

15851571
DLLEXPORT
1586-
jl_module_t *jl_restore_new_module(char *fname)
1572+
jl_module_t *jl_restore_new_module(const char *fname)
15871573
{
15881574
ios_t f;
15891575
if (ios_file(&f, fname, 1, 0, 0, 0) == NULL) {

src/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ static void gc_mark_task(jl_task_t *ta, int d)
712712
gc_mark_stack(jl_pgcstack, offset, d);
713713
}
714714
else {
715-
offset = (char *)ta->stkbuf - ((char *)ta->stackbase - ta->ssize);
715+
offset = (char *)ta->stkbuf - ((char *)jl_stackbase - ta->ssize);
716716
gc_mark_stack(ta->gcstack, offset, d);
717717
}
718718
#else

0 commit comments

Comments
 (0)