# Build riscv-gnu-toolchain
This is a step-by-step tutorial for build gnu toolchain.
- Tested on Ubuntu 22.04 LTS (recommanded).
- Optional: spike and qemu
## Step 0, Install packages needed
```
$ sudo apt-get 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
```
## Step 2, Create a installation dir and add it to PATH
As an example, I put it under /opt/riscv (just an example)
```
$ sudo mkdir /opt/riscv
```
Add install dir to PATH, use vim as editor in this example
```
$ sudo vim ~/.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 install path in local machine (user defined)
```
### For RV32IMZf RTL simulation users
```
(Put toolchain in dir <yout path>)
$ ../configure --prefix=<your path> --with-arch=rv32imzicsr --with-abi=ilp32 --disable-gdb --disable-qemu
```
Details of the options
- rv32imzicsr
- some csr instruction was included in rv32im
- 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 will be needed for prefix like /opt/...
```
$ sudo make (for bare-metal)
$ sudo make linux (for linux)
```
## 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
```
$ ../configure –-prefix=/opt/riscv --with-arch=rv32im --with-abi=ilp32 --disable-gdb --disable-qemu
```
## Examples for eztb users
### For cv32e40p and rv32im(P2 machine)
```
(install at /opt/riscv)
$ cd riscv-gnu-toolchain
$ ../configure --prefix=/opt/riscv --with-arch=rv32im --with-abi=ilp32
```
### For cva6
```
(install at /opt/riscv64-linux)
$ ../configure --prefix=/opt/riscv64-linux --with-arch=rv64gc --with-abi=lp64d --enable-linux
$ sudo make linux
```
rv64gc supports rv64i with extensions a, m, f, d, g; gc means general purpose uses on 32 bits with support for compressed instructions
### For cv32a6
```
(install at /opt/riscv32-linux)
$ ../configure --prefix=/opt/riscv32-linux --with-arch=rv32gc --with-abi=ilp32d --enable-linux
$ sudo make linux
```
cva6 include 2 configures, cv32a6 emphasize it as **32bit**
### For RV32IMF
```
(install at /opt/riscv32-linux)
$ ../configure --prefix=/opt/riscv32-linux --with-arch=rv32imf_zicsr --with-abi=ilp32f --disable-gdb --disable-qemu-system --enable-multilib
$ sudo make
```
### For RV32IMZfinx
```
(install at /opt/riscv32-linux)
$ ../configure --prefix=/opt/riscv32-linux --with-arch=rv32im_zfinx_zicsr --with-abi=ilp32 --disable-gdb --disable-qemu-system --enable-multilib
$ sudo make
```
Notice: with-abi=ilp32
### For multilib
in riscv-gnu-toolchain, multilib support compiler with both 32bit and 64 bit, not use in this project
## 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
###### tags: `tutorials` `RISC-V` `toolchain`