Is it possible to import the Crystal compiler library into my own Crystal application, in order to enable it to compile other Crystal apps by their source code?
The scenario is that we want to provide an autonomous framework on a single executable, that will generate Crystal code and compile it when needed (without the extra step of installing Crystal itself).
I’m not discarding the possibility to bring the Crystal compiler binary externally (like as an installation dependency on Linux, for example), but I just would like to know how complex the “embed” approach would be.
This is a fascinating use case. Is the idea to be able to compile an entire new program for your app to shell out to or to embed the newly built Crystal code as a
.so file or something?
The idea is to compile an entire new program.
A developer will be able to choose between a CLI or a desktop app of our framework to:
- scaffold new applications (for our specific architecture and framework)
- start a development mode using Crystal interpreter, also providing “live-reload”
- run the build command, where the referenced application is compiled (and, here, we use the compiler)
You can use the compiler as a library, but you’ll need to have LLVM somewhere in the server. I don’t remember how is all tied up, but the best thing to do is to just try
requireing the compiler’s code, using it, and seeing what errors you get and go from there.
Since the idea is to provide a single executable to develop new applications and compile them, without the need to install Crystal itself (nor any other dependency), having LLVM as an external dependency seems more complicated than having Crystal compiler as one.
LibLLVM shouldn’t be an issue. You can just link it statically into your binary, the same way we’re doing it on the compiler.
However, you’ll need lib dependencies for the standard library (and any other code you’re building). That’s
They need to be available as actual library files (shared or static doesn’t matter) in order to link the compiled code. The linker needs to be able to find them.
Also probably worth mentioning, the last step of the compilation is linking, and
clang (whatever is in the CC env variable) is used. So you also need a linker on the server… which is probably already there, but…
Guys, I think I found it: GitHub - bcardiff/crystal-tool-demo: Demo of how to create your own crystal tools without forking the compiler
If I understand it correctly, I can compile my framework as an extension of the Crystal code, before compiling Crystal.
With it, the final executable (to the final users) will run fine without dependencies on LLVM or any other dependency when compiling Crystal from source, since we will compile it in advance, as Crystal itself.
(Of course, in my computer, it is necessary that I have all the pre-requisites to compile Crystal in order to be able to compile my framework if I use this approach)