|
| 1 | +# Trimming tests ported from Base Julia test/trimming/ |
| 2 | + |
| 3 | +const TRIM_PROJ = abspath(joinpath(@__DIR__, "TrimmabilityProject")) |
| 4 | + |
| 5 | +@testset "Trimming: executable size check" begin |
| 6 | + # Uses the existing TEST_SRC (test.jl) hello world |
| 7 | + # Verifies the executable size stays reasonable |
| 8 | + outdir = mktempdir() |
| 9 | + exeout = joinpath(outdir, "hello") |
| 10 | + |
| 11 | + img = JuliaC.ImageRecipe( |
| 12 | + file = TEST_SRC, |
| 13 | + output_type = "--output-exe", |
| 14 | + project = TEST_PROJ, |
| 15 | + trim_mode = "safe", |
| 16 | + verbose = true, |
| 17 | + ) |
| 18 | + JuliaC.compile_products(img) |
| 19 | + link = JuliaC.LinkRecipe(image_recipe=img, outname=exeout) |
| 20 | + JuliaC.link_products(link) |
| 21 | + bun = JuliaC.BundleRecipe(link_recipe=link, output_dir=outdir) |
| 22 | + JuliaC.bundle_products(bun) |
| 23 | + |
| 24 | + actual_exe = Sys.iswindows() ? joinpath(outdir, "bin", basename(exeout) * ".exe") : joinpath(outdir, "bin", basename(exeout)) |
| 25 | + @test isfile(actual_exe) |
| 26 | + |
| 27 | + # Test that the executable size stays reasonable (< 2.5MB for the executable itself) |
| 28 | + @test filesize(actual_exe) < 2_500_000 |
| 29 | + |
| 30 | + print_tree_with_sizes(outdir) |
| 31 | +end |
| 32 | + |
| 33 | +@testset "Trimming: trimmability.jl (various constructs)" begin |
| 34 | + outdir = mktempdir() |
| 35 | + exeout = joinpath(outdir, "trimmability") |
| 36 | + |
| 37 | + # Build trimmability project as executable - tests OncePerProcess, Sockets, sort, map, etc. |
| 38 | + img = JuliaC.ImageRecipe( |
| 39 | + file = TRIM_PROJ, |
| 40 | + output_type = "--output-exe", |
| 41 | + trim_mode = "safe", |
| 42 | + verbose = true, |
| 43 | + ) |
| 44 | + JuliaC.compile_products(img) |
| 45 | + link = JuliaC.LinkRecipe(image_recipe=img, outname=exeout) |
| 46 | + JuliaC.link_products(link) |
| 47 | + bun = JuliaC.BundleRecipe(link_recipe=link, output_dir=outdir) |
| 48 | + JuliaC.bundle_products(bun) |
| 49 | + |
| 50 | + actual_exe = Sys.iswindows() ? joinpath(outdir, "bin", basename(exeout) * ".exe") : joinpath(outdir, "bin", basename(exeout)) |
| 51 | + @test isfile(actual_exe) |
| 52 | + |
| 53 | + # Test output - the program should output: |
| 54 | + # 1. "Hello, world!" (from OncePerProcess) |
| 55 | + # 2. PROGRAM_FILE (the path to the executable) |
| 56 | + # 3. arg1 |
| 57 | + # 4. arg2 |
| 58 | + # 5. The sum_areas result: 4.0 + pi = 7.141592653589793 |
| 59 | + output = readchomp(`$actual_exe arg1 arg2`) |
| 60 | + lines = split(output, '\n') |
| 61 | + @test length(lines) >= 5 |
| 62 | + @test lines[1] == "Hello, world!" |
| 63 | + @test lines[2] == actual_exe # PROGRAM_FILE |
| 64 | + @test lines[3] == "arg1" |
| 65 | + @test lines[4] == "arg2" |
| 66 | + @test parse(Float64, lines[5]) ≈ (4.0 + pi) |
| 67 | + |
| 68 | + print_tree_with_sizes(outdir) |
| 69 | +end |
| 70 | + |
| 71 | +@testset "Trimming: libsimple.jl C application test" begin |
| 72 | + outdir = mktempdir() |
| 73 | + libout = joinpath(outdir, "libsimple") |
| 74 | + |
| 75 | + # Build the libsimple library |
| 76 | + img = JuliaC.ImageRecipe( |
| 77 | + file = joinpath(@__DIR__, "libsimple.jl"), |
| 78 | + output_type = "--output-lib", |
| 79 | + project = TEST_LIB_PROJ, |
| 80 | + add_ccallables = true, |
| 81 | + trim_mode = "safe", |
| 82 | + verbose = true, |
| 83 | + ) |
| 84 | + JuliaC.compile_products(img) |
| 85 | + link = JuliaC.LinkRecipe(image_recipe=img, outname=libout) |
| 86 | + JuliaC.link_products(link) |
| 87 | + bun = JuliaC.BundleRecipe(link_recipe=link, output_dir=outdir) |
| 88 | + JuliaC.bundle_products(bun) |
| 89 | + |
| 90 | + # Library location differs by platform: bin/ on Windows, lib/ on Unix |
| 91 | + libdir = joinpath(outdir, Sys.iswindows() ? "bin" : "lib") |
| 92 | + libpath = joinpath(libdir, basename(libout) * "." * Base.BinaryPlatforms.platform_dlext()) |
| 93 | + @test isfile(libpath) |
| 94 | + |
| 95 | + # Compile and run the C application that uses libsimple |
| 96 | + # Use JuliaC's compiler (MinGW on Windows, system compiler on Unix) |
| 97 | + bindir = joinpath(outdir, "bin") |
| 98 | + mkpath(bindir) |
| 99 | + csrc = abspath(joinpath(@__DIR__, "c", "capplication.c")) |
| 100 | + exe = joinpath(bindir, Sys.iswindows() ? "capplication.exe" : "capplication") |
| 101 | + cc = JuliaC.get_compiler_cmd() |
| 102 | + |
| 103 | + if Sys.islinux() |
| 104 | + run(`$cc -o $exe $csrc -ldl`) |
| 105 | + else |
| 106 | + run(`$cc -o $exe $csrc`) |
| 107 | + end |
| 108 | + |
| 109 | + # Run the C application |
| 110 | + output = read(`$exe $libpath`, String) |
| 111 | + lines = split(strip(output), '\n') |
| 112 | + @test length(lines) == 2 |
| 113 | + @test lines[1] == "Sum of copied values: 6.000000" |
| 114 | + @test lines[2] == "Count of same vectors: 1" |
| 115 | +end |
0 commit comments