1. 准备开发环境

1 安装Rust开发环境

开发机器使用的是 Ubuntu 24.04 操作系统。

1.1 分三步

  1. 设置 rustup 镜像

    export RUSTUP_DIST_SERVER="https://rsproxy.cn"
    export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
    
  2. Install Rust

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

  3. 设置 crates.io 镜像

# 放到 `$HOME/.cargo/config` 文件中
[source.crates-io]
replace-with = 'rsproxy-sparse'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true

1.2 参考资料

2 RISC-V GNU toolchain

2.1 从镜像克隆仓库

curl https://mirror.iscas.ac.cn/riscv-toolchains/git/riscv-collab/riscv-gnu-toolchain.sh | bash

2.2 安装依赖

  • $ sudo apt-get install autoconf automake autotools-dev curl python3 python3-pip libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build git cmake libglib2.0-dev libslirp-dev
  • 让命令行能够直接使用 python :sudo ln -s /usr/bin/python3 /usr/bin/python

2.3 build & install

  • --enable-debug-info
#!/bin/bash

set -x # We use the -x parameter when debugging our scripts to determine the output of individual commands.
set -e # When a query returns a non-zero status, the -e flag stops the script.

# Set install paths
INSTALL_PATH=~/xtools/riscv

# Build and install toolchain
cd riscv-gnu-toolchain
mkdir -p build_dir
cd build_dir
../configure --prefix=$INSTALL_PATH --with-arch=rv64gc --with-abi=lp64d --enable-multilib --enable-debug-info
make -j$(nproc)
make install

echo "Toolchains installed to $INSTALL_PATH"
# 编译为使用 ilp32 ABI 的 32 位程序
riscv64-unknown-elf-gcc -march=rv32gc -mabi=ilp32 -o hello_ilp32 hello.c

2.4 参考资料

3 Spike

build Spike

# We assume that the RISCV environment variable is set to the RISC-V tools install path.

$ sudo apt-get install device-tree-compiler libboost-regex-dev libboost-system-dev
$ mkdir build
$ cd build
# $ ../configure --prefix=$RISCV
$ CFLAGS=-g CXXFLAGS=-g ../configure --prefix=$RISCV
$ make
$ [sudo] make install

build pk

$ mkdir build
$ cd build
$ ../configure --prefix=$RISCV --host=riscv64-unknown-elf
$ make
$ make install
../configure --prefix=$RISCV --host=riscv64-unknown-elf --with-arch=rv32gc --with-abi=ilp32

test Spike & pk

spike --isa=rv32gc /home/zsw/xtools/riscv/riscv32-unknown-elf/bin/pk  hello_ilp32
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容