bazel rules for a riscv64 bare metal rust compiler

2024/08/31

bazel rules for a riscv64 bare metal rust compiler

This article assumes that you have bazel installed via the bazelisk method.

This is the latest example in my repository at: https://github.com/filmil/bazel-experiments

The subdirectory https://github.com/filmil/bazel-experiments/tree/main/riscv64-rust-baremetal shows how to build a bare-metal binary for 64-bit RISC-V, using bazel and a hermetic Rust compiler.

It leans onto the set of bazel bare metal rules for 64-bit RISC-V, since the Rust compiler uses GNU ld for its linker. Unfortnately, the know-how for building the toolchain has been lost to time it seems. I might need to find a way to rebuild this from scratch, possibly using the instructions from here and here

For details, you can check the README.md, but in practice it is very easy to get started.

Check the code out and do:

bazel build //prg:my_program

bazel will download and prepare everything you need for the compilation and produce the binary.

You can build a disassembly listing of the demo binary using:

bazel build //prg:disasm \
  && cat bazel-bin/prg/disasm.S

The rules use rules_rust for the heavy lifting.

References