Skip to content

Commit 5a95814

Browse files
committed
add juliac.jl to contrib and install it
hook up tests better
1 parent cdacfb2 commit 5a95814

File tree

5 files changed

+117
-7
lines changed

5 files changed

+117
-7
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ julia-deps: | $(DIRS) $(build_datarootdir)/julia/base $(build_datarootdir)/julia
8282
julia-stdlib: | $(DIRS) julia-deps
8383
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/stdlib
8484

85-
julia-base: julia-deps $(build_sysconfdir)/julia/startup.jl $(build_man1dir)/julia.1 $(build_datarootdir)/julia/julia-config.jl
85+
julia-base: julia-deps $(build_sysconfdir)/julia/startup.jl $(build_man1dir)/julia.1 $(build_datarootdir)/julia/julia-config.jl $(build_datarootdir)/julia/juliac.jl $(build_datarootdir)/julia/juliac-buildscript.jl
8686
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/base
8787

8888
julia-libccalltest: julia-deps
@@ -181,7 +181,7 @@ $(build_sysconfdir)/julia/startup.jl: $(JULIAHOME)/etc/startup.jl | $(build_sysc
181181
@echo Creating usr/etc/julia/startup.jl
182182
@cp $< $@
183183

184-
$(build_datarootdir)/julia/julia-config.jl: $(JULIAHOME)/contrib/julia-config.jl | $(build_datarootdir)/julia
184+
$(build_datarootdir)/julia/%: $(JULIAHOME)/contrib/% | $(build_datarootdir)/julia
185185
$(INSTALL_M) $< $(dir $@)
186186

187187
$(build_depsbindir)/stringreplace: $(JULIAHOME)/contrib/stringreplace.c | $(build_depsbindir)
File renamed without changes.

contrib/juliac.jl

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

test/trimming/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ endif
1616
# location of test source
1717
SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
1818
JULIAHOME := $(abspath $(SRCDIR)/../..)
19+
BUILDSCRIPT := $(BIN)/../share/julia/juliac-buildscript.jl
1920
include $(JULIAHOME)/Make.inc
2021

2122
# get the executable suffix, if any
@@ -31,8 +32,8 @@ LDFLAGS_ADD = -lm $(shell $(JULIA_CONFIG) --ldflags --ldlibs) -ljulia-internal
3132

3233
release: hello$(EXE)
3334

34-
hello.o: $(SRCDIR)/hello.jl $(SRCDIR)/buildscript.jl
35-
$(JULIA) -t 1 -J $(BIN)/../lib/julia/sys.so --startup-file=no --history-file=no --output-o $@ --output-incremental=no --strip-ir --strip-metadata --trim $(SRCDIR)/buildscript.jl $(SRCDIR)/hello.jl --output-exe true
35+
hello.o: $(SRCDIR)/hello.jl $(BUILDSCRIPT)
36+
$(JULIA) -t 1 -J $(BIN)/../lib/julia/sys.so --startup-file=no --history-file=no --output-o $@ --output-incremental=no --strip-ir --strip-metadata --trim $(BUILDSCRIPT) $(SRCDIR)/hello.jl --output-exe true
3637

3738
init.o: $(SRCDIR)/init.c
3839
$(CC) -c -o $@ $< $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS)
@@ -41,8 +42,7 @@ hello$(EXE): hello.o init.o
4142
$(CC) -o $@ $(WHOLE_ARCHIVE) hello.o $(NO_WHOLE_ARCHIVE) init.o $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)
4243

4344
check: hello$(EXE)
44-
$(JULIA) --depwarn=error $(SRCDIR)/trimming.jl ./hello$(EXE)
45-
@echo SUCCESS
45+
$(JULIA) --depwarn=error $(SRCDIR)/../runtests.jl $(SRCDIR)/trimming
4646

4747
clean:
4848
-rm -f hello$(EXE) init.o hello.o

test/trimming/trimming.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Test
22

3-
exe_path = ARGS[1]
3+
exe_path = joinpath(@__DIR__, "hello"*splitext(Base.julia_exename())[2])
44

55
@test readchomp(`$exe_path`) == "Hello, world!"
66

0 commit comments

Comments
 (0)