Stream: general

Topic: getting started with AoT compilation?


view this post on Zulip Sam Estep (Aug 19 2024 at 18:17):

Hello! :wave: I was able to successfully follow the Cranelift JIT demo to get a working Cranelift JIT hello world example, but I'm having trouble finding a place to look to make a similar "hello world" example for ahead-of-time compilation. I tried using someone's example Brainfuck compiler, but I got linking errors on all my machines (x86 WSL2, ARM64 macOS, and i386 Linux) so I'm not exactly sure how to adapt it so it'll work. I assume there already exists some simple example for producing a static binary using Cranelift; where can I find it?

JIT compiler and runtime for a toy language, using Cranelift - bytecodealliance/cranelift-jit-demo
Cranelift JIT hello world. GitHub Gist: instantly share code, notes, and snippets.
Contribute to ctiedt/cranefuck development by creating an account on GitHub.

view this post on Zulip bjorn3 (Aug 19 2024 at 19:40):

Are you passing -lc when linking? And are you defining a main function which you mark as Linkage::Export?

view this post on Zulip Sam Estep (Sep 04 2024 at 14:56):

@bjorn3 sorry for being slow to respond; thankfully the cranefuck author helped me figure out how to get linking working though!

This PR fixes some typos in build.sh. Unfortunately, on both my WSL2 machine and my M1 MacBook, even with the fixes it still fails with this error: No crt1.o found

view this post on Zulip bjorn3 (Sep 04 2024 at 14:58):

Wait, why are you directly using ld instead of using gcc or clang? Gcc and clang pass a bunch of flags to the linker to tell it where to find system libraries, which libraries are unconditionally required and so on.

view this post on Zulip bjorn3 (Sep 04 2024 at 14:59):

Pretty much everyone on Unix systems uses gcc, clang or whatever C compiler is the default for the platform instead of directly running the linker.

view this post on Zulip bjorn3 (Sep 04 2024 at 15:00):

This is both a lot more portable and much simpler than reproducing all this logic for every OS you want to target.

view this post on Zulip bjorn3 (Sep 04 2024 at 15:00):

@Sam Estep

view this post on Zulip Sam Estep (Sep 04 2024 at 15:06):

@bjorn3 that makes a lot of sense, sounds like I should be using gcc or clang instead! More generally, I'd like to distribute my compiler as a standalone binary (via musl etc) which itself can produce standalone binaries, so I don't really want to just assume that the user already has gcc or clang installed on their system. What should I do in this case? For instance, it seems like the Rust compiler itself comes with a linker, but I assume that's because Rust bundles LLVM. Is there a way I can achieve this same UX but with Cranelift?

view this post on Zulip bjorn3 (Sep 04 2024 at 15:10):

Do you mean rust-lld? On all Unix systems we (rustc) actually tell gcc/clang to use rust-lld as linker rather than directly invoking rust-lld.

view this post on Zulip Sam Estep (Sep 04 2024 at 15:11):

OK gotcha; so, what happens when someone installs Rust but doesn't already have gcc or clang installed?

view this post on Zulip bjorn3 (Sep 04 2024 at 15:11):

You get an error that gcc or clang was not found.

view this post on Zulip Sam Estep (Sep 04 2024 at 15:12):

What about on Windows?

view this post on Zulip Sam Estep (Sep 04 2024 at 15:14):

Sounds like you're saying I essentially can't just distribute one standalone binary for my compiler and also produce standalone binaries from user programs, is that accurate?

view this post on Zulip bjorn3 (Sep 04 2024 at 15:14):

For wasm and embedded systems without OS rustc does directly invoke rust-lld as for those there is no libc and such.

For MSVC Windows by default we expect link.exe in the PATH, but can run rust-lld in link.exe mode. Even so you need the Windows SDK installed (which we legally can't redistribute) for import libraries and a couple of things that get statically linked into the executable. For MinGW Windows we ship a copy of MinGW.


Last updated: Nov 22 2024 at 17:03 UTC