RSIC-V

一、编译安装riscv-gnu-toolchain

方式一、手动编译安装

详细可参考:riscv-gnu-toolchain仓库目录

1、Clone riscv-toolchain

全部一次性下载 (预计几个小时,而且容易失败,与网络有关)

git clone --recursive https://github.com/riscv-collab/riscv-gnu-toolchain.git

分开下载(推荐使用这个)

  • 先下载主仓库
git clone https://gitee.com/mirrors/riscv-gnu-toolchain.git
或
git clone https://github.com/riscv/riscv-gnu-toolchain
  • 查看子仓库的分支
cd riscv-gnu-toolchain
cat .gitmodules

子仓库分支查看

  • 根据子仓库分支分别下载子仓库
  1. binutils 下载 binutils-2_40-branch 分支
git clone -b binutils-2_40-branch https://github.com/bminor/binutils-gdb.git
rm -rf binutils
mv binutils-gdb binutils

  1. gcc 下载 releases/gcc-12 分支
git clone -b releases/gcc-12 https://github.com/gcc-mirror/gcc.git
  1. glibc 下载主分支
git clone https://mirrors.tuna.tsinghua.edu.cn/git/glibc.git
  1. dejagnu 下载主分支,不使用.gitmodules中的分支
git clone https://github.com/riscvarchive/riscv-dejagnu.git
rm -rf dejagnu
mv riscv-dejagnu dejagnu
  1. newlib 下载主分
git clone https://gitee.com/mirrors/riscv-newlib
rm -rf newlib
mv riscv-newlib newlib
  1. gdb 下载 gdb-12-branch 分支
git clone -b gdb-12-branch https://github.com/bminor/binutils-gdb.git
rm -rf gdb
mv binutils-gdb gdb
  1. qemu 下载主分支
git clone https://mirrors.tuna.tsinghua.edu.cn/git/qemu.git
  1. musl 下载主分支
git clone https://gitee.com/mirrors/musl.git
  1. spike 下载主分支
git clone https://github.com/riscv-software-src/riscv-isa-sim.git
rm -rf spike
mv riscv-isa-sim spike
  1. pk 下载主分支
git clone https://github.com/riscv-software-src/riscv-pk.git
rm -rf pk
mv riscv-pk pk

2、安装相关依赖库

  • Ubuntu
sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build
  • Fedora/CentOS/RHEL OS
sudo yum install autoconf automake python3 libmpc-devel mpfr-devel gmp-devel gawk  bison flex texinfo patchutils gcc gcc-c++ zlib-devel expat-devel
  • Arch Linux
sudo pacman -Syyu autoconf automake curl python3 libmpc mpfr gmp gawk base-devel bison flex texinfo gperf libtool patchutils bc zlib expat

3、创建riscv文件夹,并设置权限

sudo mkdir /opt/riscv
sudo chmod 777 /opt/riscv

4、设置环境变量

#编辑 ~/.bashrc文件
sudo vim ~/.bashrc

#在 ~/.bashrc文件中加入这两句
export RISCV="/opt/riscv"
export PATH=$PATH:$RISCV/bin

#让环境变量生效
source ~/.bashrc

在这里插入图片描述

5、编译(预计三个小时左右)

  1. 配置安装路径
  • 构建同时支持 32 位和 64 位的交叉编译器
./configure --prefix=/opt/riscv --enable-multilib
  • 构建64 位的交叉编译器
./configure --prefix=/opt/riscv
  • 构建32 位的交叉编译器
./configure --prefix=/opt/riscv --with-arch=rv32gc --with-abi=ilp32d
  1. 编译工具链

riscv-gnu-toolchain 工具链分elf-gcc,linux-gnu-gcc两个版本,以及他们对应的32位和64位版本。两个的主要区别在于:
○ riscv32-unknown-elf-gcc,riscv64-unknown-elf-gcc:使用的riscv-newlib库(面向嵌入式的C库),只支持静态链接,不支持动态链接。
○ riscv32-unknown-linux-gnu-gcc,riscv64-unknown-linux-gnu-gcc:使用的是glibc标准库,支持动态链接。

如果是编译简单,较小的elf程序,使用elf-gcc版本即可,如果编译比较大的程序或者需要动态库(比如编译linux,或opencv库等),推荐使用linux-gnu-gcc版本。

# Newlib 执行这个
sudo make -j4

# linux 执行这个
sudo make linux -j4
  1. 检查生成的二进制文件
ls /opt/riscv/bin -lt

在这里插入图片描述

方式二、编译压缩包安装(只适用于ubuntu)

如果不想自己编译,也可以选择使用 Release 页面中预先编译好的压缩包。

1、下载压缩包

链接:https://github.com/riscv-collab/riscv-gnu-toolchain/releases
直接下载 riscv64-elf-ubuntu- 开头,且与读者 Ubuntu 版本相符的压缩包。64表示编译的是64位版本,elf代表c运行库是newlib,glibc代表c运行库是glibc,musl代表c运行库是musl。
在这里插入图片描述
解压:

# riscv 64位 elf 版本
tar -xzvf riscv64-elf-ubuntu-20.04-nightly-2023.05.24-nightly.tar.gz 

# riscv 32位 elf 版本
tar -xzvf riscv32-elf-ubuntu-20.04-nightly-2023.05.24-nightly.tar.gz

解压后会在当前目录得到一个riscv的文件夹,需拷贝到opt目录:

sudo cp -rf riscv /opt/riscv
sudo chmod 777 /opt/riscv

2、安装相关依赖库

sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build

3、设置环境变量

sudo vim ~/.bashrc

#加入:
export RISCV="/opt/riscv"
export PATH=$PATH:$RISCV/bin

#让环境变量生效
source ~/.bashrc

在这里插入图片描述

二、下载并编译riscv-fesvr

git clone https://github.com/riscv/riscv-fesvr.git
cd riscv-fesvr
mkdir build && cd build

#riscv 64位 elf 版本执行这个
../configure --prefix=/opt/riscv --target=riscv64-unknown-elf

#riscv 32位 elf 版本执行这个
../configure --prefix=/opt/riscv --target=riscv32-unknown-elf

sudo make install

三、安装riscv-tools

1、下载riscv-tools

git clone https://github.com/riscv-software-src/riscv-tools
cd riscv-tools
git submodule update --init --recursive

2、三个修改

如果是安装 riscv 32位 elf 版本只需要修改riscv-tools/riscv-pk/util/string.c就行。

修改build.sh文件

编译到riscv-pk时,会有以下报错:

../machine/flush_icache.c:4: Error: unrecognized opcode `fence.i', extension `zifencei' required

需修改build.sh文件,在riscv-pk的编译配置后加入 --with-arch=rv64gc_zifencei,参考

#在riscv-tools目录下
vim build.sh

在这里插入图片描述

修改riscv-tests/env/v/vm.c文件

不修改,编译时,riscv-tests会报错,错误信息具体可参考

在riscv-tests/env/v/vm.c文件的tohost、fromhost 前加入extern

#在riscv-tools目录下
vim riscv-tests/env/v/vm.c

在这里插入图片描述

修改riscv-tools/riscv-pk/util/string.c

使用spike pk运行可执行文件时,遇到以下错误:
在这里插入图片描述
修改方法,在memset函数前后加入这三句 #pragma,参考

#pragma GCC push_options
#pragma GCC optimize ("no-tree-loop-distribute-patterns")
void* memset(void* dest, int byte, size_t len)
{
   .......
}
#pragma GCC pop_options
#在riscv-tools目录下
vim riscv-pk/util/string.c

在这里插入图片描述

3、安装依赖

#ubuntu
sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev libusb-1.0-0-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev device-tree-compiler pkg-config libexpat-dev

4、编译

  • riscv 64位 elf 版本执行这个
#在riscv-tools目录下 使用root权限
su
export RISCV=/opt/riscv
./build.sh
  • riscv 32位 elf 版本执行这个
#在riscv-tools目录下 使用root权限
su
export RISCV=/opt/riscv
./build-rv32ima.sh

5分钟左右安装成功,提示:RISC-V Toolchain installation completed!

四、测试

新建一个hello.c文件,输入以下内容:

#include <stdio.h>
int main(){
    printf("hello world!\n");
    return 0;
}
  • riscv 64位 elf 版本执行这个
riscv64-unknown-elf-gcc -o hello hello.c
spike pk ./hello
  • riscv 32位 elf 版本执行这个
    参考
 riscv32-unknown-elf-gcc -o hello hello.c
spike --isa=rv32imafdc /opt/riscv/riscv32-unknown-elf/bin/pk hello

在这里插入图片描述

到这里,RSIC-V的编译工具链和模拟器都以及安装成功。

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值