diff --git a/test/JuliaC/MyApp/src/MyApp.jl b/test/JuliaC/MyApp/src/MyApp.jl index 3d2c400a94..ac787d5e17 100644 --- a/test/JuliaC/MyApp/src/MyApp.jl +++ b/test/JuliaC/MyApp/src/MyApp.jl @@ -19,11 +19,14 @@ function @main(args::Vector{String})::Cint if head == "--mathoptformat" return run_mathoptformat(args) end + # The following commands are not traced by `test_juliac.jl`: the compiled + # binary must run them with the methods resolved at compile time. return 1 # Incorrect head end -function run_mathoptformat(args::Vector{String})::Cint - filename = only(args) +# The model of the traced command. Each command that is not traced writes this +# model with one difference, so that it tests one code path that is not traced. +function _model() model = MOI.FileFormats.MOF.Model() x = MOI.add_variable(model) MOI.set(model, MOI.VariableName(), x, "x") @@ -37,8 +40,18 @@ function run_mathoptformat(args::Vector{String})::Cint objective = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0) MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) MOI.set(model, MOI.ObjectiveFunction{typeof(objective)}(), objective) + return model, x +end + +function _write(model, args::Vector{String})::Cint + filename = only(args) open(io -> write(io, model), filename, "w") return 0 end -end # module MOFWriter +function run_mathoptformat(args::Vector{String})::Cint + model, _ = _model() + return _write(model, args) +end + +end # module MyApp diff --git a/test/JuliaC/entry.jl b/test/JuliaC/entry.jl new file mode 100644 index 0000000000..37968a8155 --- /dev/null +++ b/test/JuliaC/entry.jl @@ -0,0 +1,28 @@ +# Copyright (c) 2017: Miles Lubin and contributors +# Copyright (c) 2017: Google Inc. +# +# Use of this source code is governed by an MIT-style license that can be found +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. + +# The entry point of the compiled MyApp. `test_juliac.jl` copies it next to +# `dispatch.trace`, the `--trace-dispatch` output of a run of MyApp in ordinary +# Julia. Each method reached through dynamic dispatch in that run becomes an +# entry point of the image, compiled with the methods it statically reaches. + +import MyApp.main + +# The signatures of the trace refer to the modules by name. +for M in values(Base.loaded_modules) + if !isdefined(@__MODULE__, nameof(M)) + Core.eval(@__MODULE__, :(const $(nameof(M)) = $M)) + end +end + +for line in eachline(joinpath(@__DIR__, "dispatch.trace")) + signature = try + Core.eval(@__MODULE__, Meta.parse(line).args[2]) + catch + continue # For instance, a closure that only exists in the traced run + end + Base.Experimental.entrypoint(signature) +end diff --git a/test/JuliaC/test_juliac.jl b/test/JuliaC/test_juliac.jl index 5130bb520d..13bc9d5cbc 100644 --- a/test/JuliaC/test_juliac.jl +++ b/test/JuliaC/test_juliac.jl @@ -24,13 +24,29 @@ using Test import JSON import JuliaC +# Trace the methods that MyApp reaches through dynamic dispatch, so that +# `entry.jl` keeps them in the trimmed image. Only `--mathoptformat` is traced, +# the other commands test the methods that are resolved at compile time. +function trace(output_dir) + project = joinpath(@__DIR__, "MyApp") + trace = joinpath(output_dir, "dispatch.trace") + output = joinpath(output_dir, "trace.mof.json") + run( + `$(Base.julia_cmd()) --project=$project --trace-dispatch=$trace -m MyApp --mathoptformat $output`, + ) + entry = joinpath(output_dir, "entry.jl") + cp(joinpath(@__DIR__, "entry.jl"), entry) + return entry +end + function compile() output_dir = mktempdir() outname = joinpath(output_dir, "MyApp") image_recipe = JuliaC.ImageRecipe( output_type = "--output-exe", - file = joinpath(@__DIR__, "MyApp"), - trim_mode = "no", + file = trace(output_dir), + project = joinpath(@__DIR__, "MyApp"), + trim_mode = "unsafe-warn", add_ccallables = false, verbose = true, ) @@ -45,25 +61,32 @@ end function runtests() output_dir = compile() is_test(name) = startswith("$name", "test_") - @testset "$name" for name in filter(is_test, names(@__MODULE__; all = true)) - getfield(@__MODULE__, name)(output_dir) + # The outer testset runs every command, even after one fails. + @testset "JuliaC" begin + @testset "$name" for name in + filter(is_test, names(@__MODULE__; all = true)) + getfield(@__MODULE__, name)(output_dir) + end end return end -function test_mathoptformat(output_dir) - # Run via the compiled binary - output = joinpath(output_dir, "compiled.mof.json") - run(`$(output_dir)/bin/MyApp --mathoptformat $output`) - # Compare against running as `-m` - reference = joinpath(output_dir, "reference.mof.json") +# Run the command `head` of MyApp with the compiled binary and with Julia, and +# return both outputs. +function _run(output_dir, head) + output = joinpath(output_dir, "compiled$head.mof.json") + run(`$(output_dir)/bin/MyApp $head $output`) + reference = joinpath(output_dir, "reference$head.mof.json") project = joinpath(@__DIR__, "MyApp") - run( - `$(Base.julia_cmd()) --project=$project -m MyApp --mathoptformat $reference`, - ) - @test read(output, String) == read(reference, String) + run(`$(Base.julia_cmd()) --project=$project -m MyApp $head $reference`) + return read(output, String), read(reference, String) +end + +function test_mathoptformat(output_dir) + compiled, reference = _run(output_dir, "--mathoptformat") + @test compiled == reference # Manually test the output - object = JSON.parsefile(output) + object = JSON.parse(compiled) @test object["variables"] == [Dict("name" => "x")] @test object["objective"] == Dict( "sense" => "min",