Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup julia_init options #9266

Merged
merged 7 commits into from
Dec 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -869,7 +868,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading