Skip to content

Commit

Permalink
create a valid sys.dylib file for any compile
Browse files Browse the repository at this point in the history
renames the --bare [-b] flag to --build
the --build flag now requires an argument (where to save the system image)
the build flag triggers mode selection so that the code can get correctly generated and saved
  • Loading branch information
vtjnash committed Nov 28, 2013
1 parent b4eaf40 commit 1f51c45
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 67 deletions.
11 changes: 4 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $(foreach link,base test doc examples,$(eval $(call symlink_target,$(link),$(BUI
debug release: | $(DIRS) $(BUILD)/share/julia/base $(BUILD)/share/julia/test $(BUILD)/share/julia/doc $(BUILD)/share/julia/examples $(BUILD)/etc/julia/juliarc.jl
@$(MAKE) $(QUIET_MAKE) julia-$@
@export JL_PRIVATE_LIBDIR=$(JL_PRIVATE_LIBDIR) && \
$(MAKE) $(QUIET_MAKE) LD_LIBRARY_PATH=$(BUILD)/lib:$(LD_LIBRARY_PATH) JULIA_EXECUTABLE="$(JULIA_EXECUTABLE_$@)" $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys.dylib
$(MAKE) $(QUIET_MAKE) LD_LIBRARY_PATH=$(BUILD)/lib:$(LD_LIBRARY_PATH) JULIA_EXECUTABLE="$(JULIA_EXECUTABLE_$@)" $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys.$(SHLIB_EXT)

julia-debug-symlink:
@ln -sf $(BUILD)/bin/julia-debug-$(DEFAULT_REPL) julia
Expand Down Expand Up @@ -75,14 +75,11 @@ $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys%$(SHLIB_EXT): $(BUILD)/$(JL_PRIVATE_LIBDIR)/sy

$(BUILD)/$(JL_PRIVATE_LIBDIR)/sys0.bc:
@$(QUIET_JULIA) cd base && \
$(call spawn,$(JULIA_EXECUTABLE)) -bf sysimg.jl
mv $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys.ji $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys0.ji
mv $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys.bc $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys0.bc

$(call spawn,$(JULIA_EXECUTABLE)) --build $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys0 -f sysimg.jl
$(BUILD)/$(JL_PRIVATE_LIBDIR)/sys.bc: VERSION base/*.jl base/pkg/*.jl base/linalg/*.jl base/sparse/*.jl $(BUILD)/share/julia/helpdb.jl $(BUILD)/share/man/man1/julia.1 $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys0.dylib
@$(QUIET_JULIA) cd base && \
$(call spawn,$(JULIA_EXECUTABLE)) -f \
`[ -e $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys.ji ] || echo -J"$(BUILD)/$(JL_PRIVATE_LIBDIR)/sys0.ji"` sysimg.jl \
$(call spawn,$(JULIA_EXECUTABLE)) --build $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys -f \
-J$(BUILD)/$(JL_PRIVATE_LIBDIR)/$$([ -e $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys.ji ] && echo sys.ji || echo sys0.ji) sysimg.jl \
|| (echo "*** This error is usually fixed by running 'make clean'. If the error persists, try 'make cleanall'. ***" && false)

run-julia-debug run-julia-release: run-julia-%:
Expand Down
9 changes: 0 additions & 9 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,3 @@ begin
end

end # baremodule Base

using Base.get, Base.ENV

let JL_PRIVATE_LIBDIR = get(ENV, "JL_PRIVATE_LIBDIR", "lib/julia")
# create system image file
ccall(:jl_save_system_image, Void, (Ptr{Uint8},),
"$JULIA_HOME/../$JL_PRIVATE_LIBDIR/sys.ji")
ccall(:jl_dump_bitcode, Void, (Ptr{Uint8},), "$JULIA_HOME/../$JL_PRIVATE_LIBDIR/sys.bc")
end
6 changes: 3 additions & 3 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,10 @@ struct jl_varinfo_t {
};

// --- helpers for reloading IR image
extern "C" DLLEXPORT
void jl_set_imaging_mode(uint8_t stat)
extern "C"
void jl_set_imaging_mode(int stat)
{
imaging_mode = (stat == 1);
imaging_mode = !!stat;
}

static void jl_gen_llvm_gv_array();
Expand Down
14 changes: 8 additions & 6 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -894,19 +894,21 @@ extern void jl_get_system_hooks(void);
extern void jl_get_uv_hooks(void);

DLLEXPORT
void jl_restore_system_image(char *fname)
void jl_restore_system_image(char *fname, int build_mode)
{
ios_t f;
char *fpath = fname;
if (ios_file(&f, fpath, 1, 0, 0, 0) == NULL) {
JL_PRINTF(JL_STDERR, "System image file \"%s\" not found\n", fname);
exit(1);
}
char *fname_shlib = alloca(strlen(fname));
strcpy(fname_shlib, fname);
char *fname_shlib_dot = strrchr(fname_shlib, '.');
if (fname_shlib_dot != NULL) *fname_shlib_dot = 0;
jl_load_sysimg_so(fname_shlib);
if (!build_mode) {
char *fname_shlib = alloca(strlen(fname));
strcpy(fname_shlib, fname);
char *fname_shlib_dot = strrchr(fname_shlib, '.');
if (fname_shlib_dot != NULL) *fname_shlib_dot = 0;
jl_load_sysimg_so(fname_shlib);
}
#ifdef JL_GC_MARKSWEEP
int en = jl_gc_is_enabled();
jl_gc_disable();
Expand Down
19 changes: 15 additions & 4 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,10 @@ DLLEXPORT kern_return_t catch_exception_raise

#endif

void julia_init(char *imageFile)
void julia_init(char *imageFile, int build_mode)
{
if (build_mode)
jl_set_imaging_mode(1);
jl_page_size = jl_getpagesize();
jl_find_stack_bottom();
jl_dl_handle = jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT);
Expand Down Expand Up @@ -644,7 +646,6 @@ void julia_init(char *imageFile)
jl_init_serializer();

if (!imageFile) {
jl_set_imaging_mode(1);
jl_main_module = jl_new_module(jl_symbol("Main"));
jl_main_module->parent = jl_main_module;
jl_core_module = jl_new_module(jl_symbol("Core"));
Expand All @@ -666,7 +667,7 @@ void julia_init(char *imageFile)

if (imageFile) {
JL_TRY {
jl_restore_system_image(imageFile);
jl_restore_system_image(imageFile, build_mode);
}
JL_CATCH {
JL_PRINTF(JL_STDERR, "error during init:\n");
Expand Down Expand Up @@ -792,7 +793,7 @@ DLLEXPORT void jl_install_sigint_handler()

extern void * __stack_chk_guard;

DLLEXPORT int julia_trampoline(int argc, char **argv, int (*pmain)(int ac,char *av[]))
DLLEXPORT int julia_trampoline(int argc, char **argv, int (*pmain)(int ac,char *av[]), char *build_path)
{
#if defined(_OS_WINDOWS_) //&& !defined(_WIN64)
SetUnhandledExceptionFilter(exception_handler);
Expand All @@ -813,6 +814,16 @@ DLLEXPORT int julia_trampoline(int argc, char **argv, int (*pmain)(int ac,char *
}
#endif
int ret = pmain(argc, argv);
if (build_path) {
char *build_ji;
asprintf(&build_ji, "%s.ji",build_path);
jl_save_system_image(build_ji);
free(build_ji);
char *build_bc;
asprintf(&build_bc, "%s.bc",build_path);
jl_dump_bitcode(build_bc);
free(build_bc);
}
p[sizeof(__stack_chk_guard)-1] = a;
p[sizeof(__stack_chk_guard)-2] = b;
p[0] = c;
Expand Down
2 changes: 1 addition & 1 deletion src/jlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DLLEXPORT void jl_init(char *julia_home_dir)
{
libsupport_init();
char *image_file = jl_locate_sysimg(julia_home_dir, JL_SYSTEM_IMAGE_PATH);
julia_init(image_file);
julia_init(image_file, 0);
jl_set_const(jl_core_module, jl_symbol("JULIA_HOME"),
jl_cstr_to_string(julia_home));
jl_module_export(jl_core_module, jl_symbol("JULIA_HOME"));
Expand Down
12 changes: 6 additions & 6 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,8 @@ jl_value_t *jl_no_method_error(jl_function_t *f, jl_value_t **args, size_t na);
void jl_check_type_tuple(jl_tuple_t *t, jl_sym_t *name, const char *ctx);

// initialization functions
DLLEXPORT void julia_init(char *imageFile);
DLLEXPORT int julia_trampoline(int argc, char *argv[], int (*pmain)(int ac,char *av[]));
DLLEXPORT void julia_init(char *imageFile, int build_mode);
DLLEXPORT int julia_trampoline(int argc, char *argv[], int (*pmain)(int ac,char *av[]), char* build_mode);
void jl_init_types(void);
void jl_init_box_caches(void);
DLLEXPORT void jl_init_frontend(void);
Expand All @@ -886,11 +886,11 @@ void jl_init_intrinsic_functions(void);
void jl_init_tasks(void *stack, size_t ssize);
void jl_init_serializer(void);

DLLEXPORT void jl_save_system_image(char *fname);
DLLEXPORT void jl_restore_system_image(char *fname);
DLLEXPORT void jl_dump_bitcode(char *fname);
void jl_save_system_image(char *fname);
void jl_restore_system_image(char *fname, int build_mode);
void jl_dump_bitcode(char *fname);
const char *jl_get_llvmname(void *func);
DLLEXPORT void jl_set_imaging_mode(uint8_t stat);
void jl_set_imaging_mode(int stat);
int32_t jl_get_llvm_gv(jl_value_t *p);

// front end interface
Expand Down
8 changes: 8 additions & 0 deletions test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

macro test(x)
:( println( "hello ", $x ) )
end

@test "world"


This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Nov 29, 2013

Member

I think you didn't mean to commit this.

55 changes: 24 additions & 31 deletions ui/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
#define WHOLE_ARCHIVE
#include "../src/julia.h"

#ifndef JL_SYSTEM_IMAGE_PATH
#define JL_SYSTEM_IMAGE_PATH ".." PATHSEPSTRING "lib" PATHSEPSTRING "julia" PATHSEPSTRING "sys.ji"
#endif

static int lisp_prompt = 0;
static char *program = NULL;
char *image_file = "sys.ji";
char *image_file;
int tab_width = 2;
char *build_mode;

static const char *usage = "julia [options] [program] [args...]\n";
static const char *opts =
Expand All @@ -37,22 +42,20 @@ static const char *opts =
" -h --help Print this message\n";

void parse_opts(int *argcp, char ***argvp) {
static char* shortopts = "+H:T:bhJ:";
static char* shortopts = "+H:T:hJ:";
static struct option longopts[] = {
{ "home", required_argument, 0, 'H' },
{ "tab", required_argument, 0, 'T' },
{ "bare", no_argument, 0, 'b' },
{ "build", required_argument, 0, 'b' },
{ "lisp", no_argument, &lisp_prompt, 1 },
{ "help", no_argument, 0, 'h' },
{ "sysimage", required_argument, 0, 'J' },
{ 0, 0, 0, 0 }
};
int c;
opterr = 0;
int ind = 1;
#ifdef JL_SYSTEM_IMAGE_PATH
int imagepathspecified=0;
#endif
image_file = JL_SYSTEM_IMAGE_PATH;
while ((c = getopt_long(*argcp,*argvp,shortopts,longopts,0)) != -1) {
switch (c) {
case 0:
Expand All @@ -61,23 +64,19 @@ void parse_opts(int *argcp, char ***argvp) {
break;
case 'H':
julia_home = strdup(optarg);
ind+=2;
break;
case 'T':
// TODO: more robust error checking.
tab_width = atoi(optarg);
ind+=2;
break;
case 'b':
image_file = NULL;
ind+=1;
build_mode = strdup(optarg);
if (!imagepathspecified)
image_file = NULL;
break;
case 'J':
image_file = optarg;
#ifdef JL_SYSTEM_IMAGE_PATH
image_file = strdup(optarg);
imagepathspecified = 1;
#endif
ind+=2;
break;
case 'h':
printf("%s%s", usage, opts);
Expand All @@ -100,35 +99,29 @@ void parse_opts(int *argcp, char ***argvp) {
free(julia_path);
}
}
*argvp += ind;
*argcp -= ind;
*argvp += optind;
*argcp -= optind;
if (image_file==NULL && *argcp > 0) {
if (strcmp((*argvp)[0], "-")) {
program = (*argvp)[0];
}
}
if (image_file) {
int build_time_path = 0;
#ifdef JL_SYSTEM_IMAGE_PATH
if (!imagepathspecified) {
image_file = JL_SYSTEM_IMAGE_PATH;
build_time_path = 1;
}
#endif
if (image_file[0] != PATHSEP) {
struct stat stbuf;
char path[512];
if (build_time_path) {
if (!imagepathspecified) {
// build time path relative to JULIA_HOME
snprintf(path, sizeof(path), "%s%s%s",
julia_home, PATHSEPSTRING, JL_SYSTEM_IMAGE_PATH);
snprintf(path, sizeof(path), "%s%s",
julia_home, PATHSEPSTRING JL_SYSTEM_IMAGE_PATH);
image_file = strdup(path);
}
else if (jl_stat(image_file, (char*)&stbuf) != 0) {
// otherwise try julia_home/../lib/julia/%s
snprintf(path, sizeof(path), "%s%s..%slib%sjulia%s%s",
julia_home, PATHSEPSTRING, PATHSEPSTRING,
PATHSEPSTRING, PATHSEPSTRING, image_file);
snprintf(path, sizeof(path), "%s%s%s",
julia_home,
PATHSEPSTRING ".." PATHSEPSTRING "lib" PATHSEPSTRING "julia" PATHSEPSTRING,
image_file);
image_file = strdup(path);
}
}
Expand Down Expand Up @@ -283,6 +276,6 @@ int main(int argc, char *argv[])
jl_lisp_prompt();
return 0;
}
julia_init(lisp_prompt ? NULL : image_file);
return julia_trampoline(argc, argv, true_main);
julia_init(lisp_prompt ? NULL : image_file, build_mode!=NULL);
return julia_trampoline(argc, argv, true_main, build_mode);
}

2 comments on commit 1f51c45

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

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

I like this since --bare hasn't really worked as a standalone thing for quite some time. We might want to allow multiple --bulid flags in the future so that people can include their own stuff in a custom build. That could be one interface to statically compiling Julia code.

@vtjnash
Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, in order to static-compile efficiently, the compiler needed to know that it is building a system image. I think --bare hasn't worked since modules were added, so it made sense to co-opt it.

Please sign in to comment.