forked from zlib-ng/zlib-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add supporting RISC-V cross compilation workflows
Add RISC-V cross-compilation test Enable RVV support at compile time
- Loading branch information
Showing
10 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Building RISC-V Target with Cmake # | ||
|
||
> **Warning** | ||
> We cannot detect rvv support at runtime, running the rvv code on a no-rvv target is a risk. Users should disable the rvv when the target does not support it. | ||
> | ||
> We will have a better solution when the kernels update `hwcap` or `hwprobe` for risc-v. | ||
## Prerequisite: Build RISC-V Clang Toolchain and QEMU ## | ||
|
||
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). | ||
|
||
```bash | ||
./prepare_riscv_toolchain_qemu.sh | ||
``` | ||
|
||
After running script, clang & qemu are built in `build-toolchain-qemu/riscv-clang/` & `build-toolchain-qemu/riscv-qemu/`. | ||
|
||
`build-toolchain-qemu/riscv-clang/` is your `TOOLCHAIN_PATH`. | ||
`build-toolchain-qemu/riscv-qemu/bin/qemu-riscv64` is your `QEMU_PATH`. | ||
|
||
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. | ||
|
||
## Cross-Compile for RISC-V Target ## | ||
|
||
```bash | ||
cmake -G Ninja -B ./build-riscv \ | ||
-D CMAKE_TOOLCHAIN_FILE=./cmake/toolchain-riscv.cmake \ | ||
-D CMAKE_INSTALL_PREFIX=./build-riscv/install \ | ||
-D TOOLCHAIN_PATH={TOOLCHAIN_PATH} \ | ||
-D QEMU_PATH={QEMU_PATH} \ | ||
. | ||
|
||
cmake --build ./build-riscv | ||
``` | ||
|
||
Disable the option if there is no RVV support: | ||
``` | ||
-D WITH_RVV=OFF | ||
``` | ||
|
||
## Run Unittests on User Mode QEMU ## | ||
|
||
```bash | ||
cd ./build-riscv && ctest --verbose | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "../../zbuild.h" | ||
#include "riscv_features.h" | ||
|
||
/* TODO: detect risc-v cpu info at runtime when the kernel updates hwcap or hwprobe for risc-v */ | ||
void Z_INTERNAL riscv_check_features(struct riscv_cpu_features *features) { | ||
#if defined(__riscv_v) && defined(__linux__) | ||
features->has_rvv = 1; | ||
#else | ||
features->has_rvv = 0; | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* riscv_features.h -- check for riscv features. | ||
* | ||
* Copyright (C) 2023 SiFive, Inc. All rights reserved. | ||
* Contributed by Alex Chiang <[email protected]> | ||
* | ||
* For conditions of distribution and use, see copyright notice in zlib.h | ||
*/ | ||
|
||
#ifndef RISCV_H_ | ||
#define RISCV_H_ | ||
|
||
struct riscv_cpu_features { | ||
int has_rvv; | ||
}; | ||
|
||
void Z_INTERNAL riscv_check_features(struct riscv_cpu_features *features); | ||
|
||
#endif /* RISCV_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
set(CMAKE_CROSSCOMPILING TRUE) | ||
set(CMAKE_SYSTEM_NAME "Linux") | ||
set(CMAKE_SYSTEM_PROCESSOR "riscv64") | ||
|
||
# Avoid to use system path for cross-compile | ||
set(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH FALSE) | ||
|
||
set(TOOLCHAIN_PATH "" CACHE STRING "The toolchain path.") | ||
if(NOT TOOLCHAIN_PATH) | ||
set(TOOLCHAIN_PATH ${CMAKE_SOURCE_DIR}/prebuilt-riscv-toolchain-qemu/riscv-clang) | ||
endif() | ||
|
||
set(TOOLCHAIN_PREFIX "riscv64-unknown-linux-gnu-" CACHE STRING "The toolchain prefix.") | ||
set(QEMU_PATH "" CACHE STRING "The qemu path.") | ||
if(NOT QEMU_PATH) | ||
set(QEMU_PATH ${CMAKE_SOURCE_DIR}/prebuilt-riscv-toolchain-qemu/riscv-qemu/bin/qemu-riscv64) | ||
endif() | ||
|
||
# toolchain setting | ||
set(CMAKE_C_COMPILER "${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}clang") | ||
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}clang++") | ||
|
||
# disable auto-vectorizer | ||
add_compile_options(-fno-vectorize -fno-slp-vectorize) | ||
|
||
# emulator setting | ||
set(QEMU_CPU_OPTION "rv64,zba=true,zbb=true,zbc=true,zbs=true,v=true,vlen=512,elen=64,vext_spec=v1.0") | ||
set(CMAKE_CROSSCOMPILING_EMULATOR ${QEMU_PATH} -cpu ${QEMU_CPU_OPTION} -L ${TOOLCHAIN_PATH}/sysroot/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters