owned this note
owned this note
Published
Linked with GitHub
# Build riscv-gnu-toolchain
This is a step-by-step tutorial for build gnu toolchain.
- Tested on Ubuntu 22.04 LTS.
- Recommended version for reproducibility:
- riscv-gnu-toolchain: 2023.01.03
- Spike: 1.1.0
## Step 0, Install packages needed
```
sudo apt install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build device-tree-compiler
```
## Step 1, Clone from riscv-gnu-toolchain
```
git clone https://github.com/riscv/riscv-gnu-toolchain
git checkout 2023.01.03
```
## Step 2, Create a installation dir and add it to PATH
As an example, I put it under /opt/riscv :
```
sudo mkdir /opt/riscv
```
Add install dir to PATH, use vim as editor in this example:
```
sudo vim ~/.bashrc
```
Add:
```
export PATH=/opt/riscv/bin:$PATH
```
After editing ~/.bashrc, run:
```
source ~/.bashrc
```
## Step 3, Generate Makefile according to target system
```
cd riscv-gnu-toolchain
mkdir build
cd build
../configure --prefix=/opt/riscv --with-arch=rv32im --with-abi=ilp32
```
Prefix will be the install path in local machine (user defined).
### For RV32IM RTL simulation users
```
../configure --prefix=/opt/riscv --with-arch=rv32imzicsr --with-abi=ilp32 --disable-gdb --disable-qemu
```
Details of the options:
- rv32im**zicsr**
- csr instructions moved to zicsr
- disable gdb and qemu
- to speed up if gdb and qemu are not needed
- with-arch
- with computer architecture, for riscv, it may be rv32i with submodules
- with-abi
- ABI(application binary interface)
- depend on arch
- interface between two binary program modules
- size of int/long/pointer
- ilp32 for example
- 32bit int, long, pointer
- 64bit long long
- 8bit char
- 16bit short
- notice, ilp32 only supported for 32-bit target system
## Step 4, After makefile is done, build toolchain
```
sudo make (for bare-metal)
sudo make linux (for linux)
```
## Optional: Installing Spike (RISC-V ISA Simulator)
If you want to run your binaries on Spike:
## Step X1, Clone and build spike
```
git clone https://github.com/riscv/riscv-isa-sim.git
cd riscv-isa-sim
git checkout v1.1.0
mkdir build
cd build
../configure --prefix=/opt/riscv
make
sudo make install
```
Spike will be installed in /opt/riscv/bin/spike.
## Optional: Installing Proxy Kernel (pk)
If you want to run bare-metal binaries on Spike using pk:
### Step X2, Clone and build riscv-pk
```bash
git clone https://github.com/riscv/riscv-pk.git
cd riscv-pk
git checkout v1.0.0
mkdir build
cd build
../configure --prefix=/home/yutung/RISCV-TOOLS/RV32IMZf --host=riscv32-unknown-elf --with-arch=rv32im_zicsr_zfinx
make
make install
```
pk Will be installed in:
```
/home/yutung/RISCV-TOOLS/RV32IMZf/riscv32-unknown-elf/bin/pk
```
## More options
To save time, there are some options may be disabled when configure
- gdb
- gnu debugger
- ```--disable-gdb```
- qemu-system
- open source simulator
- ```--disable-qemu```
example
```
mkdir build
cd build
../configure –-prefix=/opt/riscv --with-arch=rv32im --with-abi=ilp32 --disable-gdb --disable-qemu
```
## For users with different ISA
### For cva6 users
rv64gc supports rv64i with extensions a, m, f, d, g; gc means general purpose uses on 32 bits with support for compressed instructions.
```
mkdir build
cd build
../configure --prefix=/opt/riscv --with-arch=rv64gc --with-abi=lp64d --enable-linux
sudo make linux
```
### For cv32a6 users
cva6 include 2 configures, cv32a6 emphasize it as **32bit**.
```
mkdir build
cd build
../configure --prefix=/opt/riscv --with-arch=rv32gc --with-abi=ilp32d --enable-linux
sudo make linux
```
### For RV32IMF users
```
mkdir build
cd build
../configure --prefix=/opt/riscv --with-arch=rv32imf_zicsr --with-abi=ilp32f --disable-gdb --disable-qemu --enable-multilib
sudo make
```
### For RV32IMZfinx users
```
mkdir build
cd build
../configure --prefix=/opt/riscv --with-arch=rv32im_zfinx_zicsr --with-abi=ilp32 --disable-gdb --disable-qemu --enable-multilib
sudo make
```
Notice: with-abi=ilp32
## More info
### How to verify the installation?
use flag **-v** to check the configuration details:
```
riscv32-unknown-elf-gcc -v
```

### What is difference between newlib and glibc?
- glibc
- release by gnu
- linux bottom api
- unix protocol
- newlib
- open source c lib
- light-weight
- embedded system recommended
## Reference
- [RISC-V Collaboration](https://github.com/riscv-collab)
- [riscv-gnu-toolchain github](https://github.com/riscv/riscv-gnu-toolchain)
- [explain riscv-gnu-toolchain](https://www.rvmcu.com/article-article-show-id-404.html)
- [riscv extensions](https://gist.github.com/dominiksalvet/2a982235957012c51453139668e21fce)
- [riscv-isa-sim (Spike)](https://github.com/riscv/riscv-isa-sim)
###### tags: `tutorials` `RISC-V` `toolchain`