tshort / StaticCompiler.jl

Compiles Julia code to a standalone library (experimental)
Other
489 stars 31 forks source link

Feature Request - Ability to compile multiple functions with `compile_executable` and `compile_shlib` #93

Closed VarLad closed 1 year ago

VarLad commented 1 year ago

It'd be nice if compile_executable could take a Tuple of functions, with the first element being the main function, and use the other function's object files to make the final executable. The use case being, I'm trying to find an alternative of @cfunction for using bindings to a Rust library with StaticCompiler. The discussion took place in JuliaLang Zulip's general > StaticTools Other than that, having similar functionality for comiple_shlib would make sense as well, since it'd make single shlibs exposing mulitple functions/symbols quite easier. (as they're usually used)

For the Rust binding: This can be solved via making shlibs out of the said functions and getting a pointer to them via dlsym. But this, while it works, would make the user experience a pain, both while developing and distributing. Another solution is to use dlopen(NULL) in the program itself, and get a pointer to the required function via dlsym. The issue with this approach being, the function name being called gets mangled for some reason. Here's an example of the latter (from Zulip):

using StaticTools, StaticCompiler

@noinline function add(a,b)
    printf(c"That works!\n")
    return a+b
end

function main()
    c = add(1,1)
    mode = StaticTools.RTLD_LOCAL | StaticTools.RTLD_LAZY
    d = @symbolcall dlopen(C_NULL::Ptr{Nothing}, mode::Int32)::Ptr{StaticTools.DYLIB}
    addptr = StaticTools.dlsym(d, c"julia_add")
    x = @ptrcall addptr(4::Int, 6::Int)::Int
    printf(c"Sum: ", x)
end

compile_executable(main, (), "./")

This gives segfault when executed because the symbol name in the final binary gets mangled to julia_add_RandomNumberHere

brenhinkeller commented 1 year ago

Yeah, I think we should be able to do something like this. We actually already have this for compille_shlib thanks to #79, which we could probably build on to add this for compile_executable as well

MasonProtter commented 1 year ago

@VarLad have you tried out compile_shlib yet? It does what you're asking for here. I'm not sure it makes any sense to have multiple functions exposed from compile_executable since that is just meant to produce a binary, not a library like you seem to want here.

brenhinkeller commented 1 year ago

Completed by #122