Skip to content

Commit e32fd1f

Browse files
committed
refactor
1 parent 0ed06a0 commit e32fd1f

File tree

9 files changed

+381
-482
lines changed

9 files changed

+381
-482
lines changed

examples/program.c

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,51 @@ JULIA_DEFINE_FAST_TLS()
1313
#endif
1414

1515
// Declare C prototype of a function defined in Julia
16-
extern void julia_main();
16+
extern int julia_main(jl_array_t*);
1717

18+
// main function (windows UTF16 -> UTF8 argument conversion code copied from julia's ui/repl.c)
19+
#ifndef _OS_WINDOWS_
1820
int main(int argc, char *argv[])
1921
{
20-
intptr_t v;
21-
22-
// Initialize Julia
22+
int retcode;
23+
int i;
2324
uv_setup_args(argc, argv); // no-op on Windows
25+
#else
26+
int wmain(int argc, wchar_t *wargv[], wchar_t *envp[])
27+
{
28+
int retcode;
29+
int i;
30+
char **argv;
31+
for (i = 0; i < argc; i++) { // convert the command line to UTF8
32+
wchar_t *warg = argv[i];
33+
size_t len = WideCharToMultiByte(CP_UTF8, 0, warg, -1, NULL, 0, NULL, NULL);
34+
if (!len) return 1;
35+
char *arg = (char*)alloca(len);
36+
if (!WideCharToMultiByte(CP_UTF8, 0, warg, -1, arg, len, NULL, NULL)) return 1;
37+
argv[i] = arg;
38+
}
39+
#endif
40+
41+
// initialization
2442
libsupport_init();
43+
// jl_options.compile_enabled = JL_OPTIONS_COMPILE_OFF;
2544
// JULIAC_PROGRAM_LIBNAME defined on command-line for compilation
2645
jl_options.image_file = JULIAC_PROGRAM_LIBNAME;
2746
julia_init(JL_IMAGE_JULIA_HOME);
2847

29-
// Do some work
30-
julia_main();
31-
32-
// Cleanup and graceful exit
33-
jl_atexit_hook(0);
34-
return 0;
48+
// build arguments array: `String[ unsafe_string(argv[i]) for i in 1:argc ]`
49+
jl_array_t *ARGS = jl_alloc_array_1d(jl_apply_array_type(jl_string_type, 1), 0);
50+
JL_GC_PUSH1(&ARGS);
51+
jl_array_grow_end(ARGS, argc - 1);
52+
for (i = 1; i < argc; i++) {
53+
jl_value_t *s = (jl_value_t*)jl_cstr_to_string(argv[i]);
54+
jl_arrayset(ARGS, s, i - 1);
55+
}
56+
// call the work function, and get back a value
57+
retcode = julia_main(ARGS);
58+
JL_GC_POP();
59+
60+
// Cleanup and gracefully exit
61+
jl_atexit_hook(retcode);
62+
return retcode;
3563
}

examples/program2.c

Lines changed: 0 additions & 63 deletions
This file was deleted.

juliastatic.jl

Lines changed: 0 additions & 131 deletions
This file was deleted.

src/PackageCompiler.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ else
1414
end
1515

1616
using SnoopCompile
17-
using ArgParse
18-
julia_v07 &&
19-
is_windows() && using WinRPM
2017

18+
iswindows() && using WinRPM
2119

22-
include("juliac.jl")
23-
include("build_sysimg.jl")
20+
21+
include("static_julia.jl")
22+
include("api.jl")
2423
include("snooping.jl")
2524

2625
const sysimage_binaries = (
@@ -62,7 +61,7 @@ Can also be used to build a native system image for a downloaded cross compiled
6261
"""
6362
function build_clean_image(debug = false)
6463
backup = sysimgbackup_folder()
65-
build_sysimg(backup, "native")
64+
build_sysimg(backup, joinpath(@__DIR__, "empty_userimg.jl"))
6665
copy_system_image(backup, default_sysimg_path(debug))
6766
end
6867

@@ -145,7 +144,7 @@ function compile_package(packages::Tuple{String, String}...; force = false, reus
145144
end
146145
!isfile(userimg) && reuse && error("Nothing to reuse. Please run `compile_package(reuse = true)`")
147146
image_path = sysimg_folder()
148-
build_sysimg(image_path, "native", userimg)
147+
build_sysimg(image_path, userimg)
149148
imgfile = joinpath(image_path, "sys.$(Libdl.dlext)")
150149
if force
151150
try
@@ -178,7 +177,6 @@ function compile_package(packages::Tuple{String, String}...; force = false, reus
178177
end
179178

180179

181-
182180
export compile_package, revert, build_clean_image
183181

184182

src/api.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
"""
3+
build_sysimg(sysimg_path=default_sysimg_path(), cpu_target="native", userimg_path=nothing; force=false)
4+
5+
Rebuild the system image. Store it in `sysimg_path`, which defaults to a file named `sys.ji`
6+
that sits in the same folder as `libjulia.{so,dylib}`, except on Windows where it defaults
7+
to `JULIA_HOME/../lib/julia/sys.ji`. Use the cpu instruction set given by `cpu_target`.
8+
Valid CPU targets are the same as for the `-C` option to `julia`, or the `-march` option to
9+
`gcc`. Defaults to `native`, which means to use all CPU instructions available on the
10+
current processor. Include the user image file given by `userimg_path`, which should contain
11+
directives such as `using MyPackage` to include that package in the new system image. New
12+
system image will not replace an older image unless `force` is set to true.
13+
"""
14+
function build_sysimg(sysimg_path, userimg_path = nothing;
15+
verbose = false, quiet = false,
16+
cpu_target = nothing, optimize = nothing,
17+
debug = nothing, inline = nothing, check_bounds = nothing,
18+
math_mode = nothing
19+
)
20+
julia_compile(
21+
userimg_path, julia_program_basename = "sys",
22+
23+
cpu_target = cpu_target, optimize = optimize,
24+
debug = debug, inline = inline, check_bounds = check_bounds,
25+
math_mode = math_mode, verbose = verbose, quiet = quiet,
26+
27+
cprog = nothing, builddir = sysimg_path,
28+
clean = false, sysimage = nothing,
29+
compile = nothing, depwarn = nothing, autodeps = false,
30+
object = true, shared = true, executable = false, julialibs = false,
31+
)
32+
end
33+
34+
35+
function build_shared_lib(
36+
library, library_name;
37+
verbose = false, quiet = false,
38+
cpu_target = "native", optimize = nothing, debug = nothing,
39+
inline = nothing, check_bounds = nothing, math_mode = nothing
40+
)
41+
julia_compile(
42+
43+
library, julia_program_basename = library_name,
44+
45+
cpu_target = cpu_target, optimize = optimize,
46+
debug = debug, inline = inline, check_bounds = check_bounds,
47+
math_mode = math_mode, verbose = verbose, quiet = quiet,
48+
49+
cprog = nothing, builddir = sysimg_path,
50+
clean = false, sysimage = nothing,
51+
compile = nothing, depwarn = nothing, autodeps = false,
52+
object = true, shared = true, executable = false, julialibs = true,
53+
)
54+
end

src/build_sysimg.jl

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)