Skip to content

Commit

Permalink
Add script for RISC-V 64-bit cross compilation in GNU/Linux.
Browse files Browse the repository at this point in the history
Signed-off-by: Phoebe Chen <[email protected]>
  • Loading branch information
phoebesv committed Jan 23, 2024
1 parent d95dadc commit 9c1ddb8
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions tools/scripts/build_riscv64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/bin/bash
# Copyright (c) 2024 SiFive, Inc. All rights reserved.
# Copyright (c) 2024, Phoebe Chen <[email protected]>
# Licensed under the MIT License.


# The script is a sample for RISC-V 64-bit cross compilation in
# GNU/Linux, and you should ensure that your environment meets
# ORT requirements. You may need to make changes before using it.

set -e
set -o pipefail

# Get directory this script is in
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
OS=$(uname -s)

if [ "$OS" == "Linux" ]; then
LINUX_DISTRO=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"')
if [[ "${LINUX_DISTRO}" == "ubuntu" ]] ;then
DIR_OS="Linux"
else
echo "${LINUX_DISTRO} is not supported"
return 1
fi
else
echo "$OS is not supported"
return 1
fi

function cleanup {
if [ -d $WORK_DIR ]; then

Check warning on line 32 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 Double quote to prevent globbing and word splitting. Raw Output: ./tools/scripts/build_riscv64.sh:32:11: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
rm -rf $WORK_DIR

Check warning on line 33 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 Double quote to prevent globbing and word splitting. Raw Output: ./tools/scripts/build_riscv64.sh:33:12: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
fi
}

# The riscv toolchain, qemu and other platform related settings.
ORT_ROOT_DIR=$DIR/../..

PREBUILT_DIR="${ORT_ROOT_DIR}/riscv_tools"

read -p "Enter the riscv tools root path(press enter to use default path:${PREBUILT_DIR}): " INPUT_PATH

Check warning on line 42 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 read without -r will mangle backslashes. Raw Output: ./tools/scripts/build_riscv64.sh:42:1: info: read without -r will mangle backslashes. (ShellCheck.SC2162)
if [[ "${INPUT_PATH}" ]]; then
PREBUILT_DIR=${INPUT_PATH}
fi
echo "The riscv tool prefix path: ${PREBUILT_DIR}"

WORK_DIR=$DIR/.prebuilt

# The prebuit toolchain download from riscv-collab works with Ubuntu.
RISCV_GNU_TOOLCHAIN_URL="https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download"
TOOLCHAIN_VERSION="2023.11.20"
RISCV_TOOLCHAIN_FILE_NAME="riscv64-glibc-ubuntu-22.04-llvm-nightly-2023.11.20-nightly.tar.gz"
RISCV_TOOLCHAIN_FILE_SHA="98d6531b757fac01e065460c19abe8974976c607a8d88631cc5c1529d90ba7ba"

TOOLCHAIN_PATH_PREFIX=${PREBUILT_DIR}

execute () {
eval $1

Check warning on line 59 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 Double quote to prevent globbing and word splitting. Raw Output: ./tools/scripts/build_riscv64.sh:59:8: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
if [ $? -ne 0 ]; then

Check warning on line 60 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?. Raw Output: ./tools/scripts/build_riscv64.sh:60:8: info: Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?. (ShellCheck.SC2181)
echo "command:\"$1\" error"
exit 1
fi
}

execute "mkdir -p $WORK_DIR"

# Call the cleanup function when this tool exits.
trap cleanup EXIT

# Download and install the toolchain from
# https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download
download_file() {
local file_name="$1"
local install_path="$2"
local file_sha="$3"

echo "Install $1 to $2"
if [[ "$(ls -A $2)" ]]; then

Check warning on line 79 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 Double quote to prevent globbing and word splitting. Raw Output: ./tools/scripts/build_riscv64.sh:79:18: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
read -p "The file already exists. Keep it (y/n)? " replaced

Check warning on line 80 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 read without -r will mangle backslashes. Raw Output: ./tools/scripts/build_riscv64.sh:80:5: info: read without -r will mangle backslashes. (ShellCheck.SC2162)
case ${replaced:0:1} in
y|Y )
echo "Skip download $1."
return
;;
* )
rm -rf "$2"
;;
esac
fi

echo "Download ${file_name} ..."
mkdir -p $install_path

Check warning on line 93 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 Double quote to prevent globbing and word splitting. Raw Output: ./tools/scripts/build_riscv64.sh:93:12: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
wget --progress=bar:force:noscroll --directory-prefix="${WORK_DIR}" \
"${RISCV_GNU_TOOLCHAIN_URL}/${TOOLCHAIN_VERSION}/${file_name}" && \
echo "${file_sha} ${WORK_DIR}/${file_name}" | sha256sum -c -
echo "Extract ${file_name} ..."
tar -C "${install_path}" -xf "${WORK_DIR}/${file_name}" --no-same-owner \
--strip-components=1
}


read -p "Install RISCV toolchain(y/n)? " answer

Check warning on line 103 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 read without -r will mangle backslashes. Raw Output: ./tools/scripts/build_riscv64.sh:103:1: info: read without -r will mangle backslashes. (ShellCheck.SC2162)
case ${answer:0:1} in
y|Y )
download_file "${RISCV_TOOLCHAIN_FILE_NAME}" \
"${TOOLCHAIN_PATH_PREFIX}" \
"${RISCV_TOOLCHAIN_FILE_SHA}"
;;
* )
echo "Skip install RISCV toolchain."
;;
esac
echo "download finished."


# RISC-V cross compilation in GNU/Linux
RISCV_TOOLCHAIN_ROOT=${TOOLCHAIN_PATH_PREFIX}
RISCV_QEMU_PATH=${TOOLCHAIN_PATH_PREFIX}/bin/qemu-riscv64
python3 ${ORT_ROOT_DIR}/tools/ci_build/build.py \

Check warning on line 120 in tools/scripts/build_riscv64.sh

View workflow job for this annotation

GitHub Actions / Optional Lint

[shellcheck] reported by reviewdog 🐶 Double quote to prevent globbing and word splitting. Raw Output: ./tools/scripts/build_riscv64.sh:120:9: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
--build_dir ${ORT_ROOT_DIR}/build/${DIR_OS} \
--rv64 \
--parallel \
--skip_tests \
--config RelWithDebInfo \
--cmake_generator=Ninja \
--riscv_qemu_path=${RISCV_QEMU_PATH} \
--riscv_toolchain_root=${RISCV_TOOLCHAIN_ROOT} "$@"

0 comments on commit 9c1ddb8

Please sign in to comment.