RISC-V Toolchain

Motivation

The reason for writing this post is to capture my understanding of how embedded toolchains work. I primarily work on embedded RISC-V cores, so I will explain how to get the correct RISC-V toolchain on both Windows and WSL, because these are my primary development environments.

Toolchain

A toolchain basically contains all the required tools and libraries to compile, link, and manipulate object files and binaries. It is the basic first step when starting to work with embedded systems, or with native development in general.

Installation

Here, I will list a few possible sources you can use to get or download the correct toolchain for your needs.

riscv-gnu-toolchain

Windows

Compiling the whole toolchain from source is not well documented on Windows because it requires many dependencies, including GNU/LLVM tools, which are not straightforward to set up unless you already have MinGW/MSYS2 installed on your system. Even the official riscv-gnu-toolchain README.md does not explain how to build or obtain a native Windows toolchain. In theory, you could cross-compile a Windows toolchain from another environment such as WSL, but I would recommend using one of the other sources mentioned below.

WSL

xPack

xPack Binary Development Tools is a cross-platform binary tools for software development, intended for reproducible builds, with an emphasis on C/C++ and embedded projects.

xPack supports many packages, but here we are interested in embedded RISC-V, so we will focus on riscv-none-elf-gcc-xpack, which is a binary distribution of the GNU RISC-V Embedded GCC toolchain. Documentation is available here.

There are two ways to install these xPack toolchains:

  1. xpm
  1. GitHub Releases

Embecosm

Embecosm provides free GNU and Clang/LLVM compiler tool chain packages for the convenience of the open source software community.

There are a few different packages listed on their tool-chain-downloads page. For this use case, we want the embedded RISC-V toolchains, which means choosing either RISC-V Embedded stable release compilers or RISC-V Embedded top-of-tree compilers. I generally use the stable releases (GCC/LLVM), but you are free to choose based on your development needs.

Multilib Support

Note

Compiling for the correct variant, depending on which RISC-V core you use, can be done by using the march and mabi compiler flags:

$ riscv32-unknown-elf-gcc -march=rv32imac -mabi=ilp32 main.c

References

/riscv/ /windows/ /wsl/