|
| 1 | +# This testset makes sure that the `examples/MyLib` example does not bitrot. |
| 2 | + |
| 3 | +if Sys.iswindows() |
| 4 | + @info "Skipping the examples/MyLib test on Windows" |
| 5 | + @test_skip false |
| 6 | + # TODO: Figure out how to get this testset to work on Windows. |
| 7 | +else |
| 8 | + rootdir_testdir = @__DIR__ |
| 9 | + rootdir = dirname(rootdir_testdir) |
| 10 | + rootdir_examples = joinpath(rootdir, "examples") |
| 11 | + rootdir_examples_MyLib = joinpath(rootdir_examples, "MyLib") |
| 12 | + |
| 13 | + run_julia_code = (project, code; env = ENV) -> begin |
| 14 | + julia_binary = Base.julia_cmd()[1] |
| 15 | + env2 = copy(env) |
| 16 | + env2["JULIA_PROJECT"] = project |
| 17 | + cmd = `$(julia_binary) --startup-file=no -e "$(code)"` |
| 18 | + return run(setenv(cmd, env2)) |
| 19 | + end |
| 20 | + run_julia_script = (project, scriptfile; env = ENV) -> begin |
| 21 | + julia_binary = Base.julia_cmd()[1] |
| 22 | + env2 = copy(env) |
| 23 | + env2["JULIA_PROJECT"] = project |
| 24 | + cmd = `$(julia_binary) --startup-file=no "$(scriptfile)"` |
| 25 | + return run(setenv(cmd, env2)) |
| 26 | + end |
| 27 | + |
| 28 | + mktempdir() do mytmp |
| 29 | + # The PackageCompiler.jl source code directory might be read-only. So let's copy |
| 30 | + # examples/MyLib to a temp directory so that we can write stuff to it. |
| 31 | + MyLib_tmp = joinpath(mytmp, "MyLib") |
| 32 | + cp(rootdir_examples_MyLib, MyLib_tmp) |
| 33 | + |
| 34 | + cd(MyLib_tmp) do |
| 35 | + # Go into MyLib_tmp/build/ and dev this copy of PackageCompiler.jl |
| 36 | + run_julia_code("./build", """ |
| 37 | + import Pkg |
| 38 | + Pkg.develop(; path = "$(rootdir)") |
| 39 | + """) |
| 40 | + |
| 41 | + # Instantiate and precompile the `MyLib/` and `MyLib/build/` environments. |
| 42 | + run_julia_code(".", "import Pkg; Pkg.instantiate(); Pkg.precompile()") |
| 43 | + run_julia_code("./build", "import Pkg; Pkg.instantiate(); Pkg.precompile()") |
| 44 | + |
| 45 | + # We don't want to assume that the machine running the tests has `make` installed |
| 46 | + # and available in the PATH. Therefore, we just run the relevant commands directly. |
| 47 | + |
| 48 | + # build-library |
| 49 | + run_julia_script("./build", "build/build.jl") |
| 50 | + |
| 51 | + # build-executable |
| 52 | + CC = PackageCompiler.get_compiler_cmd() |
| 53 | + TARGET = joinpath(MyLib_tmp, "MyLibCompiled") |
| 54 | + INCLUDE_DIR = joinpath(TARGET, "include") |
| 55 | + cmd = `$(CC) my_application.c -o $(TARGET)/my_application.out -I$(INCLUDE_DIR) -L$(TARGET)/lib -ljulia -lmylib` |
| 56 | + run(cmd) |
| 57 | + |
| 58 | + # Run `./my_application.out` |
| 59 | + env2 = copy(ENV) |
| 60 | + if Sys.isapple() |
| 61 | + env2["DYLD_FALLBACK_LIBRARY_PATH"] = "./MyLibCompiled/lib/:./MyLibCompiled/lib/julia/" |
| 62 | + else |
| 63 | + env2["LD_LIBRARY_PATH"] = "./MyLibCompiled/lib/" |
| 64 | + end |
| 65 | + cmd = `$(TARGET)/my_application.out` |
| 66 | + @test success(run(setenv(cmd, env2))) |
| 67 | + observed_str = strip(read(setenv(cmd, env2), String)) |
| 68 | + expected_str = "Incremented count: 4 (Cint)\nIncremented value: 4" |
| 69 | + @test observed_str == expected_str |
| 70 | + end |
| 71 | + |
| 72 | + |
| 73 | + end |
| 74 | + |
| 75 | +end # if-elseif-else-end |
0 commit comments