Zig Makes Rust Cross-compilation Just Work · Um, actually...

submited by
Style Pass
2021-05-22 11:30:06

During a recent stage 2 meeting Jakub Konka wanted to demo his progress on WASM support, but ran into problems with screen-sharing on Linux. After switching to his Mac to run it there, he found out that Wasmtime (a WASM runtime) doesn’t ship aarch64-macos pre-built binaries.

But Wasmtime is an open-source Rust project, I should be able to clone the repo and build it for Jakub myself. The problem? I don’t have an M1 Mac on hand. I will have to cross-compile.

Some Rust crates depend on C code, but the Rust toolchain does not include a C compiler. This is the same problem Loris encountered with CGO and, luckily for us, it has the same solution. Create a shell script which wraps Zig1

We’re now successfully compiling Wasmtime’s objects, but we’re failing at the last step - linking the final binary.

The Rust toolchain includes the Rust standard library and a compiler to produce object files for the target, but it does not include a linker to link them into the final executable. Furthermore, the cargo documentation instructs us to provide it with a target linker in .cargo/config.toml. However, we can see that cargo is attempting to invoke cc like it did before with wasmtime-fiber. Can we reuse the same wrapper we used before?

Leave a Comment