| 1 |
# Building RISC-V Target with Cmake # |
| 2 |
|
| 3 |
> **Warning** |
| 4 |
> Runtime rvv detection (using `hwcap`) requires linux kernel 6.5 or newer. |
| 5 |
> |
| 6 |
> When running on older kernels, we fall back to compile-time detection, potentially this can cause crashes if rvv is enabled at compile but not supported by the target cpu. |
| 7 |
> Therefore if older kernel support is needed, rvv should be disabled if the target cpu does not support it. |
| 8 |
## Prerequisite: Build RISC-V Clang Toolchain and QEMU ## |
| 9 |
|
| 10 |
If you don't have prebuilt clang and riscv64 qemu, you can refer to the [script](https://github.com/sifive/prepare-riscv-toolchain-qemu/blob/main/prepare_riscv_toolchain_qemu.sh) to get the source. Copy the script to the zlib-ng root directory, and run it to download the source and build them. Modify the content according to your conditions (e.g., toolchain version). |
| 11 |
|
| 12 |
```bash |
| 13 |
./prepare_riscv_toolchain_qemu.sh |
| 14 |
``` |
| 15 |
|
| 16 |
After running script, clang & qemu are built in `build-toolchain-qemu/riscv-clang/` & `build-toolchain-qemu/riscv-qemu/`. |
| 17 |
|
| 18 |
`build-toolchain-qemu/riscv-clang/` is your `TOOLCHAIN_PATH`. |
| 19 |
`build-toolchain-qemu/riscv-qemu/bin/qemu-riscv64` is your `QEMU_PATH`. |
| 20 |
|
| 21 |
You can also download the prebuilt toolchain & qemu from [the release page](https://github.com/sifive/prepare-riscv-toolchain-qemu/releases), and enjoy using them. |
| 22 |
|
| 23 |
## Cross-Compile for RISC-V Target ## |
| 24 |
|
| 25 |
```bash |
| 26 |
cmake -G Ninja -B ./build-riscv \ |
| 27 |
-D CMAKE_TOOLCHAIN_FILE=./cmake/toolchain-riscv.cmake \ |
| 28 |
-D CMAKE_INSTALL_PREFIX=./build-riscv/install \ |
| 29 |
-D TOOLCHAIN_PATH={TOOLCHAIN_PATH} \ |
| 30 |
-D QEMU_PATH={QEMU_PATH} \ |
| 31 |
. |
| 32 |
|
| 33 |
cmake --build ./build-riscv |
| 34 |
``` |
| 35 |
|
| 36 |
Disable the option if there is no RVV support: |
| 37 |
``` |
| 38 |
-D WITH_RVV=OFF |
| 39 |
``` |
| 40 |
|
| 41 |
## Run Unittests on User Mode QEMU ## |
| 42 |
|
| 43 |
```bash |
| 44 |
cd ./build-riscv && ctest --verbose |
| 45 |
``` |