Skip to content

Commit

Permalink
Merge pull request #9266 from JuliaLang/jn/init_opt_cleanup
Browse files Browse the repository at this point in the history
cleanup julia_init options
  • Loading branch information
JeffBezanson committed Dec 14, 2014
2 parents e669ee3 + dc8c12a commit 502e5fd
Show file tree
Hide file tree
Showing 25 changed files with 423 additions and 327 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -306,8 +306,8 @@ else ifeq ($(OS), Linux)
done
endif

# Overwrite JL_SYSTEM_IMAGE_PATH in julia binaries
for julia in $(DESTDIR)$(bindir)/julia* ; do \
# Overwrite JL_SYSTEM_IMAGE_PATH in julia library
for julia in $(DESTDIR)$(private_libdir)/libjulia*.$(SHLIB_EXT) ; do \
$(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)); \
done
endif
Expand Down
2 changes: 1 addition & 1 deletion base/boot.jl
Expand Up @@ -147,7 +147,7 @@ export
# method reflection
applicable, invoke, method_exists,
# constants
JULIA_HOME, nothing, Main,
nothing, Main,
# intrinsics module
Intrinsics
#ccall, cglobal, llvmcall, abs_float, add_float, add_int, and_int, ashr_int,
Expand Down
21 changes: 6 additions & 15 deletions base/client.jl
Expand Up @@ -356,6 +356,7 @@ function load_machine_file(path::AbstractString)
end

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

function _start()
early_init()

try
init_parallel()
init_bind_addr(ARGS)
any(a->(a=="--worker"), ARGS) || init_head_sched()
init_load_path()
(quiet,repl,startup,color_set,no_history_file) = process_options(copy(ARGS))

local term
Expand Down Expand Up @@ -412,32 +410,25 @@ function _start()
# note: currently IOStream is used for file STDIN
if isa(STDIN,File) || isa(STDIN,IOStream)
# reading from a file, behave like include
eval(parse_input_line(readall(STDIN)))
eval(Main,parse_input_line(readall(STDIN)))
else
# otherwise behave repl-like
while !eof(STDIN)
eval_user_input(parse_input_line(STDIN), true)
end
end
if have_color
print(color_normal)
end
quit()
else
REPL.run_repl(active_repl)
end
REPL.run_repl(active_repl)
end
catch err
display_error(err,catch_backtrace())
println()
exit(1)
end
if is_interactive
if have_color
print(color_normal)
end
println()
if is_interactive && have_color
print(color_normal)
end
ccall(:uv_atexit_hook, Void, ())
end

const atexit_hooks = []
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Expand Up @@ -163,6 +163,7 @@ export
Inf,
Inf16,
Inf32,
JULIA_HOME,
LOAD_PATH,
MS_ASYNC,
MS_INVALIDATE,
Expand Down
2 changes: 2 additions & 0 deletions base/sysimg.jl
Expand Up @@ -294,6 +294,8 @@ function __init__()
reinit_stdio()
Multimedia.reinit_displays() # since Multimedia.displays uses STDOUT as fallback
fdwatcher_init()
early_init()
init_load_path()
end

include("precompile.jl")
Expand Down
18 changes: 16 additions & 2 deletions doc/manual/embedding.rst
Expand Up @@ -18,19 +18,33 @@ We start with a simple C program that initializes Julia and calls some Julia cod

int main(int argc, char *argv[])
{
/* optional: randomize the stack guard */
char a, b, c;
SET_STACK_CHK_GUARD(a,b,c);

/* required: setup the julia context */
jl_init(NULL);
JL_SET_STACK_BASE;

/* run julia commands */
jl_eval_string("print(sqrt(2.0))");

/* strongly recommended: notify julia that the
program is about to terminate. this allows
julia time to cleanup pending write requests
and run all finalizers
*/
jl_atexit_hook();

/* if the stack guard is set: reset the stack guard */
CLR_STACK_CHK_GUARD(a,b,c);
return 0;
}

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::

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

Alternatively, look at the ``embedding.c`` program in the julia source tree in the ``examples/`` folder.
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.

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.

Expand Down
5 changes: 4 additions & 1 deletion examples/embedding.c
Expand Up @@ -9,8 +9,9 @@ double my_c_sqrt(double x)

int main()
{
char a, b, c;
SET_STACK_CHK_GUARD(a,b,c);
jl_init(NULL);
JL_SET_STACK_BASE;

{
// Simple running Julia code
Expand Down Expand Up @@ -94,5 +95,7 @@ int main()
}
}

jl_atexit_hook();
CLR_STACK_CHK_GUARD(a,b,c);
return 0;
}
2 changes: 1 addition & 1 deletion src/ast.c
Expand Up @@ -114,7 +114,7 @@ static builtinspec_t julia_flisp_ast_ext[] = {
{ NULL, NULL }
};

DLLEXPORT void jl_init_frontend(void)
void jl_init_frontend(void)
{
fl_init(4*1024*1024);
value_t img = cvalue(iostreamtype, sizeof(ios_t));
Expand Down
9 changes: 4 additions & 5 deletions src/codegen.cpp
Expand Up @@ -121,7 +121,7 @@ extern "C" {

#include "builtin_proto.h"

void *__stack_chk_guard = NULL;
DLLEXPORT void *__stack_chk_guard = NULL;

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

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

void write_log_data(logdata_t logData, const char *extension)
{
std::string base = std::string(julia_home);
std::string base = std::string(jl_compileropts.julia_home);
base = base + "/../share/julia/base/";
logdata_t::iterator it = logData.begin();
for (; it != logData.end(); it++) {
Expand Down
2 changes: 1 addition & 1 deletion src/disasm.cpp
Expand Up @@ -482,7 +482,7 @@ void jl_dump_function_asm(const char *Fptr, size_t Fsize,
#ifdef LLVM35
if (MCIA->evaluateBranch(Inst, Index, insSize, addr))
#else
if ((addr = MCIA->evaluateBranch(Inst, Index, insSize)) != -1)
if ((addr = MCIA->evaluateBranch(Inst, Index, insSize)) != (uint64_t)-1)
#endif
DisInfo.insertAddress(addr);
}
Expand Down
2 changes: 1 addition & 1 deletion src/dlload.c
Expand Up @@ -116,7 +116,7 @@ static uv_lib_t *jl_load_dynamic_library_(char *modname, unsigned flags, int thr
ext = extensions[i];
path[0] = '\0';
handle->handle = NULL;
if (dl_path[len-1] == PATHSEP)
if (dl_path[len-1] == PATHSEPSTRING[0])
snprintf(path, PATHBUF, "%s%s%s", dl_path, modname, ext);
else
snprintf(path, PATHBUF, "%s" PATHSEPSTRING "%s%s", dl_path, modname, ext);
Expand Down
30 changes: 8 additions & 22 deletions src/dump.c
Expand Up @@ -452,21 +452,14 @@ static void jl_serialize_module(ios_t *s, jl_module_t *m)
jl_serialize_value(s, m->parent);
if (ref_only)
return;
// set on every startup; don't save value
jl_sym_t *jhsym = jl_symbol("JULIA_HOME");
size_t i;
void **table = m->bindings.table;
for(i=1; i < m->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (b->owner == m || m != jl_main_module) {
jl_serialize_value(s, b->name);
if (table[i-1] == jhsym && m == jl_core_module) {
jl_serialize_value(s, NULL);
}
else {
jl_serialize_value(s, b->value);
}
jl_serialize_value(s, b->value);
jl_serialize_value(s, b->type);
jl_serialize_value(s, b->owner);
write_int8(s, (b->constp<<2) | (b->exportp<<1) | (b->imported));
Expand Down Expand Up @@ -1305,7 +1298,7 @@ void jl_deserialize_lambdas_from_mod(ios_t *s)

extern jl_array_t *jl_module_init_order;

DLLEXPORT void jl_save_system_image(char *fname)
DLLEXPORT void jl_save_system_image(const char *fname)
{
jl_gc_collect();
jl_gc_collect();
Expand Down Expand Up @@ -1337,10 +1330,8 @@ DLLEXPORT void jl_save_system_image(char *fname)
// save module initialization order
if (jl_module_init_order != NULL) {
for(i=0; i < jl_array_len(jl_module_init_order); i++) {
// NULL out any modules that weren't saved
jl_value_t *mod = jl_cellref(jl_module_init_order, i);
if (ptrhash_get(&backref_table, mod) == HT_NOTFOUND)
jl_cellset(jl_module_init_order, i, NULL);
// verify that all these modules were saved
assert(ptrhash_get(&backref_table, jl_cellref(jl_module_init_order, i)) != HT_NOTFOUND);
}
}
jl_serialize_value(&f, jl_module_init_order);
Expand All @@ -1360,11 +1351,10 @@ extern void jl_get_system_hooks(void);
extern void jl_get_uv_hooks();

DLLEXPORT
void jl_restore_system_image(char *fname)
void jl_restore_system_image(const char *fname)
{
ios_t f;
char *fpath = fname;
if (ios_file(&f, fpath, 1, 0, 0, 0) == NULL) {
if (ios_file(&f, fname, 1, 0, 0, 0) == NULL) {
JL_PRINTF(JL_STDERR, "System image file \"%s\" not found\n", fname);
exit(1);
}
Expand Down Expand Up @@ -1432,14 +1422,10 @@ void jl_restore_system_image(char *fname)
//ios_printf(ios_stderr, "backref_list.len = %d\n", backref_list.len);
arraylist_free(&backref_list);
ios_close(&f);
if (fpath != fname) free(fpath);

#ifdef JL_GC_MARKSWEEP
if (en) jl_gc_enable();
#endif
// restore the value of our "magic" JULIA_HOME variable/constant
jl_get_binding_wr(jl_core_module, jl_symbol("JULIA_HOME"))->value =
jl_cstr_to_string(julia_home);
mode = last_mode;
jl_update_all_fptrs();
}
Expand Down Expand Up @@ -1543,7 +1529,7 @@ jl_value_t *jl_uncompress_ast(jl_lambda_info_t *li, jl_value_t *data)
}

DLLEXPORT
int jl_save_new_module(char *fname, jl_module_t *mod)
int jl_save_new_module(const char *fname, jl_module_t *mod)
{
ios_t f;
if (ios_file(&f, fname, 1, 1, 1, 1) == NULL) {
Expand Down Expand Up @@ -1583,7 +1569,7 @@ jl_function_t *jl_method_cache_insert(jl_methtable_t *mt, jl_tuple_t *type,
jl_function_t *method);

DLLEXPORT
jl_module_t *jl_restore_new_module(char *fname)
jl_module_t *jl_restore_new_module(const char *fname)
{
ios_t f;
if (ios_file(&f, fname, 1, 0, 0, 0) == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/gc.c
Expand Up @@ -712,7 +712,7 @@ static void gc_mark_task(jl_task_t *ta, int d)
gc_mark_stack(jl_pgcstack, offset, d);
}
else {
offset = (char *)ta->stkbuf - ((char *)ta->stackbase - ta->ssize);
offset = (char *)ta->stkbuf - ((char *)jl_stackbase - ta->ssize);
gc_mark_stack(ta->gcstack, offset, d);
}
#else
Expand Down

1 comment on commit 502e5fd

@staticfloat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit broke 32-bit builds:

task.c: In function ‘ctx_switch’:
task.c:352:13: warning: asm operand 1 probably doesn’t match constraints [enabled by default]
task.c:352:13: error: impossible constraint in ‘asm’
make[2]: *** [task.o] Error 1

Please sign in to comment.