|
| 1 | +# Julia compiler wrapper script |
| 2 | +# NOTE: The interface and location of this script are considered unstable/experimental |
| 3 | + |
| 4 | +cmd = Base.julia_cmd() |
| 5 | +cmd = `$cmd --startup-file=no --history-file=no` |
| 6 | +output_type = nothing # exe, sharedlib, sysimage |
| 7 | +trim = nothing |
| 8 | +outname = nothing |
| 9 | +file = nothing |
| 10 | +add_ccallables = false |
| 11 | + |
| 12 | +help = findfirst(x->x == "--help", ARGS) |
| 13 | +if help !== nothing |
| 14 | + println( |
| 15 | + """ |
| 16 | + Usage: julia juliac.jl [--output-exe | --output-lib | --output-sysimage] <name> [options] <file.jl> |
| 17 | + --trim=<no,safe,unsafe,unsafe-warn> Only output code statically determined to be reachable |
| 18 | + --compile-ccallable Include all methods marked `@ccallable` in output |
| 19 | + --verbose Request verbose output |
| 20 | + """) |
| 21 | + exit(0) |
| 22 | +end |
| 23 | + |
| 24 | +let i = 1 |
| 25 | + while i <= length(ARGS) |
| 26 | + arg = ARGS[i] |
| 27 | + if arg == "--output-exe" || arg == "--output-lib" || arg == "--output-sysimage" |
| 28 | + isnothing(output_type) || error("Multiple output types specified") |
| 29 | + global output_type = arg |
| 30 | + i == length(ARGS) && error("Output specifier requires an argument") |
| 31 | + global outname = ARGS[i+1] |
| 32 | + i += 1 |
| 33 | + elseif startswith(arg, "--trim") |
| 34 | + arg = split(arg, '=') |
| 35 | + if length(arg) == 1 |
| 36 | + global trim = "safe" |
| 37 | + else |
| 38 | + global trim = arg[2] |
| 39 | + end |
| 40 | + elseif arg == "--compile-ccallable" |
| 41 | + global add_ccallables = true |
| 42 | + else |
| 43 | + if arg[1] == '-' || !isnothing(file) |
| 44 | + println("Unexpected argument `$arg`") |
| 45 | + exit(1) |
| 46 | + end |
| 47 | + global file = arg |
| 48 | + end |
| 49 | + i += 1 |
| 50 | + end |
| 51 | +end |
| 52 | + |
| 53 | +isnothing(outname) && error("No output file specified") |
| 54 | +isnothing(file) && error("No input file specified") |
| 55 | + |
| 56 | +absfile = abspath(file) |
| 57 | +cflags = readchomp(`$(cmd) $(joinpath(Sys.BINDIR, Base.DATAROOTDIR,"julia", "julia-config.jl")) --cflags `) |
| 58 | +cflags = Base.shell_split(cflags) |
| 59 | +allflags = readchomp(`$(cmd) $(joinpath(Sys.BINDIR, Base.DATAROOTDIR,"julia", "julia-config.jl")) --allflags`) |
| 60 | +allflags = Base.shell_split(allflags) |
| 61 | +tmpdir = mktempdir(cleanup=false) |
| 62 | +initsrc_path = joinpath(tmpdir, "init.c") |
| 63 | +init_path = joinpath(tmpdir, "init.a") |
| 64 | +img_path = joinpath(tmpdir, "img.a") |
| 65 | +bc_path = joinpath(tmpdir, "img-bc.a") |
| 66 | + |
| 67 | +open(initsrc_path, "w") do io |
| 68 | + print(io, """ |
| 69 | + #include <julia.h> |
| 70 | + __attribute__((constructor)) void static_init(void) { |
| 71 | + if (jl_is_initialized()) |
| 72 | + return; |
| 73 | + julia_init(JL_IMAGE_IN_MEMORY); |
| 74 | + jl_exception_clear(); |
| 75 | + } |
| 76 | + """) |
| 77 | +end |
| 78 | + |
| 79 | +static_call_graph_arg() = isnothing(trim) ? `` : `--trim=$(trim)` |
| 80 | +is_verbose() = verbose ? `--verbose-compilation=yes` : `` |
| 81 | +cmd = addenv(`$cmd --project=$(Base.active_project()) --output-o $img_path --output-incremental=no --strip-ir --strip-metadata $(static_call_graph_arg()) $(joinpath(@__DIR__,"juliac-buildscript.jl")) $absfile $output_type $add_ccallables`, "OPENBLAS_NUM_THREADS" => 1, "JULIA_NUM_THREADS" => 1) |
| 82 | + |
| 83 | +if !success(pipeline(cmd; stdout, stderr)) |
| 84 | + println(stderr, "\nFailed to compile $file") |
| 85 | + exit(1) |
| 86 | +end |
| 87 | + |
| 88 | +run(`cc $(cflags) -g -c -o $init_path $initsrc_path`) |
| 89 | + |
| 90 | +if output_type == "--output-lib" || output_type == "--output-sysimage" |
| 91 | + of, ext = splitext(outname) |
| 92 | + soext = "." * Base.BinaryPlatforms.platform_dlext() |
| 93 | + if ext == "" |
| 94 | + outname = of * soext |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +julia_libs = Base.shell_split(Base.isdebugbuild() ? "-ljulia-debug -ljulia-internal-debug" : "-ljulia -ljulia-internal") |
| 99 | +try |
| 100 | + if output_type == "--output-lib" |
| 101 | + run(`cc $(allflags) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $init_path $(julia_libs)`) |
| 102 | + elseif output_type == "--output-sysimage" |
| 103 | + run(`cc $(allflags) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $(julia_libs)`) |
| 104 | + else |
| 105 | + run(`cc $(allflags) -o $outname -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $init_path $(julia_libs)`) |
| 106 | + end |
| 107 | +catch |
| 108 | + println("\nCompilation failed.") |
| 109 | + exit(1) |
| 110 | +end |
0 commit comments