brenhinkeller / StaticTools.jl

Enabling StaticCompiler.jl-based compilation of (some) Julia code to standalone native binaries by avoiding GC allocations and llvmcall-ing all the things!
MIT License
167 stars 12 forks source link

Windows stdio #20

Closed mkitti closed 6 months ago

mkitti commented 2 years ago

Currently , stdoutp() will fail on Windows.

FATAL ERROR: Symbol "stdout"not found
signal (22): SIGABRT
in expression starting at ~\.julia\dev\StaticTools\test\testllvmio.jl:3
crt_sig_handler at /cygdrive/c/buildbot/worker/package_win64/build/src\signals-win.c:92
raise at C:\WINDOWS\System32\msvcrt.dll (unknown line)
abort at C:\WINDOWS\System32\msvcrt.dll (unknown line)
addModule at /cygdrive/c/buildbot/worker/package_win64/build/src\jitlayers.cpp:763

To get C stdio on Windows, I think we need to implement the equivalent of these calls. __iob_func is defined in msvcrt.dll.

ccall(:__iob_func, Ptr{Nothing}, (Cuint,), 0) #stdin
ccall(:__iob_func, Ptr{Nothing}, (Cuint,), 1) #stdout
ccall(:__iob_func, Ptr{Nothing}, (Cuint,), 2) #stderr

Do you have a method for converting these to their llvmcall equivalents?

brenhinkeller commented 2 years ago

Ah yeah, I currently haven't implemented any windows-specific options just because StaticCompiler.jl currently doesn't support Windows AFAIU (except I suppose via WSL). Of course, that's no reason we can't add them here in the meanwhile if we can figure out the right llvm IR!

Ah good question. My approach to figuring out the right LLVM IR to add has been looking at what gets generated either by (A) @code_llvm on equivalent Julia code or (B) getting hold of a C program that does what I want and compiling it to LLVM IR with e.g. clang -emit-llvm -S foo.c -o foo.ll

brenhinkeller commented 2 years ago

In principle (A) could work on those ccalls as long as you put them in a dummy function first so they can be compiled, but I think (A) also generally doesn't show you the variable and function definitions that come outside of the main function though so (B) is normally my go-to.

Thomas008 commented 11 months ago

Here is a link to further investigations to make StaticCompiler possible for Windows: https://discourse.julialang.org/t/how-to-compile-to-stand-alone-executable-using-staticcompiler-in-windows/106767

brenhinkeller commented 6 months ago

Closed by #58