This guide explains the steps needed to build the compiler and simulator toolchain for RISC-V ISA. It assumes RV32GC instruction extension.
- GNU GCC Compiler: https://github.com/riscv/riscv-gnu-toolchain
- LLVM Compiler: https://github.com/llvm/llvm-project.git
- Proxy Kernel (to map syscalls from binary to OS): https://github.com/riscv-software-src/riscv-pk
- Simulator (Spike): https://github.com/riscv-software-src/riscv-isa-sim
-
We will install the tools that we build manually into a folder called
riscv-tools
. We will point to this folder from now on with the environmnent variable$RISCV
. The repos will be cloned into another directory calledrepos
$ setenv RISCV=/path/to/install/riscv/tools $ mkdir repos; cd repos
-
Install the compiler prerequisites:
# For OpenSUSE $ sudo zypper install autoconf automake python3 libmpc3 mpfr-devel gmp-devel gawk bison flex texinfo patchutils gcc gcc-c++ zlib-devel libexpat-devel ninja # For Ubuntu $ sudo apt-get install autoconf automake autotools-dev curl python3 python3-pip libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build git cmake libglib2.0-dev
-
Clone and build the GCC compiler:
$ git clone https://github.com/riscv/riscv-gnu-toolchain --recursive $ cd riscv-gnu-toolchain $ mkdir build $ cd build $ ../configure --prefix=$RISCV --with-arch=rv32gc --enable-multilib $ make -j 4 $ cd ..
-
Clone and build the LLVM compiler:
$ git clone https://github.com/llvm/llvm-project.git riscv-llvm $ cd riscv-llvm $ mkdir build $ cd build $ cmake -G Ninja -DCMAKE_BUILD_TYPE="Release" \ -DCMAKE_INSTALL_PREFIX="$RISCV" \ -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld" \ -DLLVM_ENABLE_RUNTIMES="libc;libcxx;libcxxabi" \ -DLLVM_USE_SPLIT_DWARF=True \ -DLLVM_BUILD_TESTS=False \ -DLLVM_DEFAULT_TARGET_TRIPLE="riscv32-unknown-elf" \ -DLLVM_TARGETS_TO_BUILD="RISCV" \ -DLLVM_INSTALL_UTILS=ON \ ../llvm $ cmake --build . --target all $ cmake --build . --target install $ cd ..
-
Clone and build
riscv-pk
:$ git clone https://github.com/riscv-software-src/riscv-pk $ cd riscv-pk $ mkdir build $ cd build $ export PATH=$RISCV/bin:$PATH $ ../configure --prefix=$RISCV --host=riscv32-unknown-elf --with-arch=rv32gc $ make $ make install $ cd ..
-
Clone and build the simulator:
$ git clone https://github.com/riscv-software-src/riscv-isa-sim $ cd riscv-isa-sim $ mkdir build $ cd build $ ../configure --prefix=$RISCV $ make $ make install $ cd ..
$ cat hello.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[]) {
printf("Hello World from RISC-V!\n");
return EXIT_SUCCESS;
}
$ riscv32-unknown-elf-gcc hello.c
$ $RISCV/bin/spike --isa=RV32GC_zicntr $RISCV/riscv32-unknown-elf/bin/pk -s a.out
bbl loader
Hello World from RISC-V!
1200 ticks
56746 cycles
56748 instructions
0.99 CPI