Skip to content

Commit

Permalink
add juliac.jl to contrib and install it
Browse files Browse the repository at this point in the history
hook up tests better
  • Loading branch information
JeffBezanson committed Aug 28, 2024
1 parent 8120b64 commit d596feb
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ julia-deps: | $(DIRS) $(build_datarootdir)/julia/base $(build_datarootdir)/julia
julia-stdlib: | $(DIRS) julia-deps
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/stdlib

julia-base: julia-deps $(build_sysconfdir)/julia/startup.jl $(build_man1dir)/julia.1 $(build_datarootdir)/julia/julia-config.jl
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
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/base

julia-libccalltest: julia-deps
Expand Down Expand Up @@ -181,7 +181,7 @@ $(build_sysconfdir)/julia/startup.jl: $(JULIAHOME)/etc/startup.jl | $(build_sysc
@echo Creating usr/etc/julia/startup.jl
@cp $< $@

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

$(build_depsbindir)/stringreplace: $(JULIAHOME)/contrib/stringreplace.c | $(build_depsbindir)
Expand Down
File renamed without changes.
110 changes: 110 additions & 0 deletions contrib/juliac.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Julia compiler wrapper script
# NOTE: The interface and location of this script are considered unstable/experimental

cmd = Base.julia_cmd()
cmd = `$cmd --startup-file=no --history-file=no`
output_type = nothing # exe, sharedlib, sysimage
trim = nothing
outname = nothing
file = nothing
add_ccallables = false

help = findfirst(x->x == "--help", ARGS)
if help !== nothing
println(
"""
Usage: julia juliac.jl [--output-exe | --output-lib | --output-sysimage] <name> [options] <file.jl>
--trim=<no,safe,unsafe,unsafe-warn> Only output code statically determined to be reachable
--compile-ccallable Include all methods marked `@ccallable` in output
--verbose Request verbose output
""")
exit(0)
end

let i = 1
while i <= length(ARGS)
arg = ARGS[i]
if arg == "--output-exe" || arg == "--output-lib" || arg == "--output-sysimage"
isnothing(output_type) || error("Multiple output types specified")
global output_type = arg
i == length(ARGS) && error("Output specifier requires an argument")
global outname = ARGS[i+1]
i += 1
elseif startswith(arg, "--trim")
arg = split(arg, '=')
if length(arg) == 1
global trim = "safe"
else
global trim = arg[2]
end
elseif arg == "--compile-ccallable"
global add_ccallables = true
else
if arg[1] == '-' || !isnothing(file)
println("Unexpected argument `$arg`")
exit(1)
end
global file = arg
end
i += 1
end
end

isnothing(outname) && error("No output file specified")
isnothing(file) && error("No input file specified")

absfile = abspath(file)
cflags = readchomp(`$(cmd) $(joinpath(Sys.BINDIR, Base.DATAROOTDIR,"julia", "julia-config.jl")) --cflags `)
cflags = Base.shell_split(cflags)
allflags = readchomp(`$(cmd) $(joinpath(Sys.BINDIR, Base.DATAROOTDIR,"julia", "julia-config.jl")) --allflags`)
allflags = Base.shell_split(allflags)
tmpdir = mktempdir(cleanup=false)
initsrc_path = joinpath(tmpdir, "init.c")
init_path = joinpath(tmpdir, "init.a")
img_path = joinpath(tmpdir, "img.a")
bc_path = joinpath(tmpdir, "img-bc.a")

open(initsrc_path, "w") do io
print(io, """
#include <julia.h>
__attribute__((constructor)) void static_init(void) {
if (jl_is_initialized())
return;
julia_init(JL_IMAGE_IN_MEMORY);
jl_exception_clear();
}
""")
end

static_call_graph_arg() = isnothing(trim) ? `` : `--trim=$(trim)`
is_verbose() = verbose ? `--verbose-compilation=yes` : ``
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)

if !success(pipeline(cmd; stdout, stderr))
println(stderr, "\nFailed to compile $file")
exit(1)
end

run(`cc $(cflags) -g -c -o $init_path $initsrc_path`)

if output_type == "--output-lib" || output_type == "--output-sysimage"
of, ext = splitext(outname)
soext = "." * Base.BinaryPlatforms.platform_dlext()
if ext == ""
outname = of * soext
end
end

julia_libs = Base.shell_split(Base.isdebugbuild() ? "-ljulia-debug -ljulia-internal-debug" : "-ljulia -ljulia-internal")
try
if output_type == "--output-lib"
run(`cc $(allflags) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $init_path $(julia_libs)`)
elseif output_type == "--output-sysimage"
run(`cc $(allflags) -o $outname -shared -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $(julia_libs)`)
else
run(`cc $(allflags) -o $outname -Wl,$(Base.Linking.WHOLE_ARCHIVE) $img_path -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $init_path $(julia_libs)`)
end
catch
println("\nCompilation failed.")
exit(1)
end
8 changes: 4 additions & 4 deletions test/trimming/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ endif
# location of test source
SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
JULIAHOME := $(abspath $(SRCDIR)/../..)
BUILDSCRIPT := $(BIN)/../share/julia/juliac-buildscript.jl
include $(JULIAHOME)/Make.inc

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

release: hello$(EXE)

hello.o: $(SRCDIR)/hello.jl $(SRCDIR)/buildscript.jl
$(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
hello.o: $(SRCDIR)/hello.jl $(BUILDSCRIPT)
$(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

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

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

clean:
-rm -f hello$(EXE) init.o hello.o
Expand Down
2 changes: 1 addition & 1 deletion test/trimming/trimming.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Test

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

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

Expand Down

0 comments on commit d596feb

Please sign in to comment.