tshort / StaticCompiler.jl

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

How to compile a Whole Julia project by parts #161

Open kylincaster opened 1 month ago

kylincaster commented 1 month ago

Dear all,

I am working on developing and compiling a large Julia project. To facilitate the development process, I hope to compile the project incrementally, similar to the procedure used in C and C++ projects.

For instance, in a C++ project with three files (a.cpp, b.cpp, c.cpp), we can compile each file into object files (a.o, b.o, c.o) using a Makefile. Then, we link them together to create an executable program. This allows us to easily modify individual files.

Using StaticCompiler in Julia, we can generate IR code from the required functions and compile them into object files. However, there is a challenge: to compile a function, all the functions it invokes must be provided. For example, consider the following function that calls a fib function:

function double(x::Int64)
    return 2 * fib(x)
end

If we want to compile the double function separately, we need a way to defer the compilation of the fib function. This means compiling double function into an object file without requiring fib to be defined at the same time. One possible solution is to use a function declaration for fib, similar to how header files work in C or C++.

If anyone has suggestions or improvements, they would be greatly appreciated.

tshort commented 1 month ago

You might try replacing fib with a ccall invocation.

kylincaster commented 1 month ago

You might try replacing fib with a ccall invocation.

Thank you for your reply.

I have tried the @ccall macro, which invokes a dynamic link with the external library. I understand that Julia is designed to use dynamic linking rather than static linking. Significant modifications would be required to achieve static linking, either by changing the StaticCompiler code or by modifying the generated LLVM IR code.

Therefore, I propose managing the incremental compilation using Julia itself. By checking the modification times of the Julia code, IR code, and object files, Julia program can automatically determine which IR code, containing certain functions, needs to be updated.