xref: /aosp_15_r20/external/libyuv/riscv_script/prepare_toolchain_qemu.sh (revision 4e366538070a3a6c5c163c31b791eab742e1657a)
1#!/bin/bash
2set -ev
3
4# Download & build RISC-V Clang toolchain & QEMU emulator.
5# RISC-V Clang is for cross compile with  the RISC-V Vector ISA.
6# RISC-V QEMU is used to run the test suite.
7#
8# Requirements: Linux host w/ working C++ compiler, git, cmake, ninja, wget, tar
9
10# NOTE: this script must be run from the top-level directory of the LIBYUV_SRC_DIR.
11
12RISCV_TRIPLE="riscv64-unknown-linux-gnu"
13RISCV_QEMU="qemu-riscv64"
14
15LIBYUV_SRC_DIR=$(pwd)
16BUILD_DIR="$LIBYUV_SRC_DIR"/build-toolchain-qemu
17INSTALL_QEMU="$BUILD_DIR"/riscv-qemu
18INSTALL_CLANG="$BUILD_DIR"/riscv-clang
19
20LLVM_VERSION="16.0.0"
21LLVM_NAME=llvm-project-"$LLVM_VERSION".src
22
23RISCV_GNU_TOOLCHAIN="$BUILD_DIR"/riscv-gnu-toolchain
24RISCV_CLANG_TOOLCHAIN="$BUILD_DIR"/"$LLVM_NAME"
25
26QEMU_NAME="qemu-7.0.0"
27
28mkdir -p "$BUILD_DIR"
29cd "$BUILD_DIR"
30
31# Download and install RISC-V GNU Toolchain (needed to build Clang)
32if [ ! -d "$RISCV_GNU_TOOLCHAIN" ]
33then
34  git clone git@github.com:riscv/riscv-gnu-toolchain.git
35  pushd "$RISCV_GNU_TOOLCHAIN"
36  git submodule update --init --recursive
37  ./configure --with-cmodel=medany --prefix="$INSTALL_CLANG"
38  ionice nice make linux -j `nproc` install
39  popd
40fi
41
42# Download Clang toolchain & build cross compiler
43if [ ! -d "$RISCV_CLANG_TOOLCHAIN" ]
44then
45  wget https://github.com/llvm/llvm-project/releases/download/llvmorg-"$LLVM_VERSION"/"$LLVM_NAME".tar.xz
46  tar xvJf "$LLVM_NAME".tar.xz
47  pushd "$RISCV_CLANG_TOOLCHAIN"
48	cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_CLANG" \
49      -DCMAKE_BUILD_TYPE=Release \
50      -DLLVM_TARGETS_TO_BUILD="RISCV" \
51      -DLLVM_ENABLE_PROJECTS="clang" \
52      -DLLVM_DEFAULT_TARGET_TRIPLE="$RISCV_TRIPLE" \
53      -DLLVM_INSTALL_TOOLCHAIN_ONLY=On \
54      -DDEFAULT_SYSROOT=../sysroot \
55      -G "Ninja" "$RISCV_CLANG_TOOLCHAIN"/llvm
56	ionice nice ninja -j `nproc`
57	ionice nice ninja -j `nproc` install
58  popd
59  pushd "$INSTALL_CLANG"/bin
60  ln -sf clang "$RISCV_TRIPLE"-clang
61  ln -sf clang++ "$RISCV_TRIPLE"-clang++
62  popd
63fi
64
65# Download QEMU and build the riscv64 Linux usermode emulator
66if [ ! -d "$QEMU_NAME" ]
67then
68  wget https://download.qemu.org/"$QEMU_NAME".tar.xz
69  tar xvJf "$QEMU_NAME".tar.xz
70  pushd "$QEMU_NAME"
71  ./configure --target-list=riscv64-linux-user --prefix="$INSTALL_QEMU"
72  ionice nice make -j `nproc` install
73  popd
74fi
75