# Test that a JLL dependency works under trimming, at both build- and run-time
module BasicJLL

using Libdl
using Zstd_jll # Note this uses the vendored older non-LazyLibrary version of Zstd_jll

# JLL usage at build-time should function as expected
Zstd_jll.__init__()
const build_ver = unsafe_string(ccall((:ZSTD_versionString, libzstd), Cstring, ()))

function print_string(fptr::Ptr{Cvoid})
    println(Core.stdout, unsafe_string(ccall(fptr, Cstring, ())))
end

version_str::String = "1.2.3"

function @main(args::Vector{String})::Cint
    # Test the basic "Hello, world!"
    println(Core.stdout, "Julia! Hello, world!")

    # JLL usage at run-time should function as expected
    ver = unsafe_string(ccall((:ZSTD_versionString, libzstd), Cstring, ()))
    println(Core.stdout, ver)
    @assert ver == build_ver

    parsed_ver = VersionNumber(version_str)
    @assert parsed_ver == v"1.2.3"

    sleep(0.01)

    # Add an indirection via `@cfunction` / 1-arg ccall
    cfunc = @cfunction(print_string, Cvoid, (Ptr{Cvoid},))
    fptr = dlsym(Zstd_jll.libzstd_handle, :ZSTD_versionString)
    ccall(cfunc, Cvoid, (Ptr{Cvoid},), fptr)
    return 0
end

end
