Enable RISC-V cmake build & qemu test

1. Provide script to build qemu & clang locally
2. Enable risc-v cmake build with clang
3. Enable test with qemu
4. Update doc to introduce how to cross-build & test with qemu

Signed-off-by: Bruce Lai <bruce.lai@sifive.com>
Change-Id: I7ce8315993b0e8300e8fd5b1632610a91a4e9f52
Reviewed-on: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/4401740
Reviewed-by: Frank Barchard <fbarchard@chromium.org>
Commit-Queue: Frank Barchard <fbarchard@chromium.org>
This commit is contained in:
Bruce Lai 2023-03-22 00:53:16 -07:00 committed by libyuv LUCI CQ
parent 0200037a5a
commit 646fc1b214
4 changed files with 174 additions and 0 deletions

View File

@ -220,6 +220,39 @@ Install cmake: http://www.cmake.org/
make -j4 make -j4
make package make package
## Building RISC-V target with cmake
### Prerequisite: build risc-v clang toolchain and qemu
If you don't have prebuilt clang and riscv64 qemu, run the script to download source and build them.
./riscv_script/prepare_toolchain_qemu.sh
After running script, clang & qemu are built in `build-toolchain-qemu/riscv-clang/` & `build-toolchain-qemu/riscv-qemu/`.
### Cross-compile for RISC-V target
cmake -B out/Release/ -DTEST=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE="./riscv_script/riscv-clang.cmake" \
-DTOOLCHAIN_PATH={TOOLCHAIN_PATH} \
-DUSE_RVV=ON .
cmake --build out/Release/
### Run on QEMU
To test RVV on user mode QEMU, we need to hack `source/cpu_id.cc`. By forcing `RiscvCpuCaps` to read from a dummy cpuinfo file instead of the host `/proc/cpuinfo`. Because the program detects CPU Caps from `/proc/cpuinfo` and determines whether to use RVV originally.
sed -i 's+RiscvCpuCaps("/proc/cpuinfo+RiscvCpuCaps("../../unit_test/testdata/riscv64_rvv.txt+g' source/cpu_id.cc
#### Run libyuv_unittest on QEMU
cd out/Release/
USE_RVV=ON \
TOOLCHAIN_PATH={TOOLCHAIN_PATH} \
QEMU_PREFIX_PATH={QEMU_PREFIX_PATH} \
../../riscv_script/run_qemu.sh libyuv_unittest
## Setup for Arm Cross compile ## Setup for Arm Cross compile
See also https://www.ccoderun.ca/programming/2015-12-20_CrossCompiling/index.html See also https://www.ccoderun.ca/programming/2015-12-20_CrossCompiling/index.html

View File

@ -0,0 +1,74 @@
#!/bin/bash
set -ev
# Download & build RISC-V Clang toolchain & QEMU emulator.
# RISC-V Clang is for cross compile with the RISC-V Vector ISA.
# RISC-V QEMU is used to run the test suite.
#
# Requirements: Linux host w/ working C++ compiler, git, cmake, ninja, wget, tar
# NOTE: this script must be run from the top-level directory of the LIBYUV_SRC_DIR.
RISCV_TRIPLE="riscv64-unknown-linux-gnu"
RISCV_QEMU="qemu-riscv64"
LIBYUV_SRC_DIR=$(pwd)
BUILD_DIR="$LIBYUV_SRC_DIR"/build-toolchain-qemu
INSTALL_QEMU="$BUILD_DIR"/riscv-qemu
INSTALL_CLANG="$BUILD_DIR"/riscv-clang
LLVM_VERSION="16.0.0"
LLVM_NAME=llvm-project-"$LLVM_VERSION".src
RISCV_GNU_TOOLCHAIN="$BUILD_DIR"/riscv-gnu-toolchain
RISCV_CLANG_TOOLCHAIN="$BUILD_DIR"/"$LLVM_NAME"
QEMU_NAME="qemu-7.0.0"
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"
# Download and install RISC-V GNU Toolchain (needed to build Clang)
if [ ! -d "$RISCV_GNU_TOOLCHAIN" ]
then
git clone git@github.com:riscv/riscv-gnu-toolchain.git
pushd "$RISCV_GNU_TOOLCHAIN"
git submodule update --init --recursive
./configure --with-cmodel=medany --prefix="$INSTALL_CLANG"
ionice nice make linux -j `nproc` install
popd
fi
# Download Clang toolchain & build cross compiler
if [ ! -d "$RISCV_CLANG_TOOLCHAIN" ]
then
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-"$LLVM_VERSION"/"$LLVM_NAME".tar.xz
tar xvJf "$LLVM_NAME".tar.xz
pushd "$RISCV_CLANG_TOOLCHAIN"
cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_CLANG" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_TARGETS_TO_BUILD="RISCV" \
-DLLVM_ENABLE_PROJECTS="clang" \
-DLLVM_DEFAULT_TARGET_TRIPLE="$RISCV_TRIPLE" \
-DLLVM_INSTALL_TOOLCHAIN_ONLY=On \
-DDEFAULT_SYSROOT=../sysroot \
-G "Ninja" "$RISCV_CLANG_TOOLCHAIN"/llvm
ionice nice ninja -j `nproc`
ionice nice ninja -j `nproc` install
popd
pushd "$INSTALL_CLANG"/bin
ln -sf clang "$RISCV_TRIPLE"-clang
ln -sf clang++ "$RISCV_TRIPLE"-clang++
popd
fi
# Download QEMU and build the riscv64 Linux usermode emulator
if [ ! -d "$QEMU_NAME" ]
then
wget https://download.qemu.org/"$QEMU_NAME".tar.xz
tar xvJf "$QEMU_NAME".tar.xz
pushd "$QEMU_NAME"
./configure --target-list=riscv64-linux-user --prefix="$INSTALL_QEMU"
ionice nice make -j `nproc` install
popd
fi

View File

@ -0,0 +1,52 @@
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_SYSTEM_NAME "Linux")
set(CMAKE_SYSTEM_PROCESSOR "riscv64")
option(USE_RVV "Enable riscv vector or not." ON)
option(USE_AUTO_VECTORIZER "Enable riscv auto vectorizer or not." OFF)
# Avoid to use system path for cross-compile
set(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH FALSE)
set(TOOLCHAIN_PATH "" CACHE STRING "The toolcahin path.")
if(NOT TOOLCHAIN_PATH)
set(TOOLCHAIN_PATH ${CMAKE_SOURCE_DIR}/build-toolchain-qemu/riscv-clang)
endif()
set(TOOLCHAIN_PREFIX "riscv64-unknown-linux-gnu-" CACHE STRING "The toolcahin prefix.")
# toolchain setting
set(CMAKE_C_COMPILER "${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}clang")
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}clang++")
# CMake will just use the host-side tools for the following tools, so we setup them here.
set(CMAKE_C_COMPILER_AR "${TOOLCHAIN_PATH}/bin/llvm-ar")
set(CMAKE_CXX_COMPILER_AR "${TOOLCHAIN_PATH}/bin/llvm-ar")
set(CMAKE_C_COMPILER_RANLIB "${TOOLCHAIN_PATH}/bin/llvm-ranlib")
set(CMAKE_CXX_COMPILER_RANLIB "${TOOLCHAIN_PATH}/bin/llvm-ranlib")
set(CMAKE_OBJDUMP "${TOOLCHAIN_PATH}/bin/llvm-objdump")
set(CMAKE_OBJCOPY "${TOOLCHAIN_PATH}/bin/llvm-objcopy")
# compile options
message(STATUS "USE_RVV: ${USE_RVV}")
message(STATUS "USE_AUTO_VECTORIZER: ${USE_AUTO_VECTORIZER}")
set(RISCV_COMPILER_FLAGS)
if(USE_RVV)
list(APPEND RISCV_COMPILER_FLAGS "-march=rv64gcv")
if(NOT USE_AUTO_VECTORIZER)
# Disable auto-vectorizer
add_compile_options(-fno-vectorize -fno-slp-vectorize)
endif()
else()
list(APPEND RISCV_COMPILER_FLAGS "-march=rv64gc")
endif()
message(STATUS "RISCV_COMPILER_FLAGS: ${RISCV_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS "${RISCV_COMPILER_FLAGS} ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${RISCV_COMPILER_FLAGS} ${CMAKE_CXX_FLAGS}")
set(RISCV_LINKER_FLAGS "-lstdc++ -lpthread -lm -ldl")
set(RISCV_LINKER_FLAGS_EXE)
set(CMAKE_SHARED_LINKER_FLAGS "${RISCV_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "${RISCV_LINKER_FLAGS} ${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${RISCV_LINKER_FLAGS} ${RISCV_LINKER_FLAGS_EXE} ${CMAKE_EXE_LINKER_FLAGS}")

15
riscv_script/run_qemu.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/bash
set -x
set -e
USE_RVV="${USE_RVV:-OFF}"
TOOLCHAIN_PATH="${TOOLCHAIN_PATH:-../../build-toolchain-qemu/riscv-clang}"
QEMU_PREFIX_PATH="${QEMU_PREFIX_PATH:-../../build-toolchain-qemu/riscv-qemu/}"
if [ "${USE_RVV}" = "ON" ];then
QEMU_OPTION="-cpu rv64,zba=true,zbb=true,zbc=true,zbs=true,v=true,vlen=512,elen=64,vext_spec=v1.0 -L ${TOOLCHAIN_PATH}/sysroot"
else
QEMU_OPTION="-cpu rv64,zba=true,zbb=true,zbc=true,zbs=true,v=true -L ${TOOLCHAIN_PATH}/sysroot"
fi
$QEMU_PREFIX_PATH/bin/qemu-riscv64 $QEMU_OPTION $@