RISC-V 编译环境搭建:riscv-gnu-toolchain 和 riscv-tools

riscv-gnu-toolchain 安装编译

1. 环境配置

这里我们需要使用 64 位基于 RV64G 的编译环境,选择编译 riscv64-unknown-linux-gnu-gcc

这里我们先添加我们需要安装的路径:

# 这里我们将路径添加到 .bashrc 中
vim ~/.bashrc
# 添加以下命令
export RISCV="/home/ppqppl/RISCV/riscv"
export PATH=$RISCV/bin:$PATH
# 保存退出后重新引用修改过的 .bashrc
source ~/.bashrc

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

当我们下载好 riscv-gnu-toolchain 后,就可以进行编译:

cd ~/RISCV/riscv-gnu-toolchain
mkdir build
cd build
../configure --prefix=/home/ppqppl/RISCV/riscv --enable-multilib
make linux -j8

这里编译需要大约 1 个小时,需要耐心等待,推荐使用多线程,可以缩短编译时间

这样我们就可以全局使用 riscv64-unknown-linux-gnu-gcc 命令了,我们可以使用相关命令查看版本号以确认我们是否安装好:

riscv64-unknown-linux-gnu-gcc -v

image

显示如上图,就说明我们的交叉编译环境已经配置好了,如果中间过程出现报错,我们就需要删除我们产生报错的工具包,重新下载再次编译即可

3. spike 环境编译与命令配置

我们需要进入到 riscv-gnu-toolchain 的 spike 路径下,然后进行编译:

cd ~/RISCV/riscv-gnu-toolchain/spike
# 安装需要的工具包
sudo apt-get install device-tree-compiler
# 创建存储编译过程文件的文件夹
mkdir build
cd build

因为之前已经把 RISCV 的交叉编译器安装到 /home/ppqppl/RISCV/riscv 文件夹了,所以有

../configure --prefix=/home/ppqppl/RISCV/riscv/spike

使用这个指令可以调用 8 个 cpu 核并行编译加速(我这个虚拟机只有 8 个,可以按需改数字),并把报错(2 类打印输出)写入到 error.log 文件里

sudo make -j8 2>error.log

如果 make 之后语言报错:则需要在 config 里面加上:

../configure--disable-nls

编译成功后安装:

sudo make install

最后一行应该会打印一个 done,表示安装完成,然后配置 spike 命令:

# 这里我们将命令路径添加到 .bashrc 中,注意这里要加上我们需要使用的工具
vim ~/.bashrc
# 我添加的命令如下,路径需要根据个人安装去修改
export SPIKE="/home/ppqppl/RISCV/riscv/spike"
export PATH=$SPIKE/bin:$PATH
# 退出保存后,重新引用修改共的 .bashrc
source ~/.bashrc

image

到此 spike 安装成功

4. pk 环境编译与命令配置

这里先进入到 riscv-gnu-toolchain 的 pk 路径下,然后使用如下命令安装:

cd ~/RISCV/riscv-gnu-toolchain/pk
mkdir build
cd build
# ../configure --prefix=/home/ppqppl/RISCV/riscv/ --with-arch=rv64gc --with-abi=lp64	# 这里没有指定 64 位环境,会报错
../configure --prefix=/home/ppqppl/RISCV/riscv/spike --host=riscv64-unknown-elf --with-arch=rv64gc_zifencei	# 需要加上 zifencei 扩展
sudo make -j8 2>error.log
sudo make install

编译 pk 之前必须编译 riscv-gnu-toolchain,否则报错:

gcc: error: unrecognized argument in option ‘-mcmodel=medany’
gcc: note: valid arguments to ‘-mcmodel=’ are: 32 kernel large medium small
make: *** [Makefile:336:file.o] 错误 1

环境配置:

# 这里我们将命令路径添加到 .bashrc 中,注意这里要加上我们需要使用的工具
vim ~/.bashrc
# 我添加的命令如下,路径需要根据个人安装去修改
export SPIKE="/home/ppqppl/RISCV/riscv/spike/pk"
export PATH=$SPIKE/riscv64-unknown-elf/bin:$PATH
# 退出保存后,重新引用修改共的 .bashrc
source ~/.bashrc

5. 测试

这里我们编写一个 helloworld.c 进行测试:

#include<stdio.h>
int main(void){
    printf("Hello ppqppl!\n");
    return 0;
}

编译运行:

riscv64-unknown-linux-gnu-gcc -o helloworld helloworld.c 
./helloworld

由于这里我们采用 RV64G 指令集进行编译,所以二进制文件无法在 ubuntu 上运行,这里会报错,需要采用 spike 进行运行仿真:

spike pk helloworld

image

这里有些直接运行会报错(没有按照上述路径安装):

terminate called after throwing an instance of 'std::runtime_error'
  what():  could not open pk; searched paths:
	. (current directory)
	/home/ppqppl/RISCV/spike/riscv64-unknown-elf/bin/ (based on configured --prefix and --with-target)

这里意思是说 spike 没有发现 pk,pk(proxy kernel) 包含了一个 boot loader 和一个能处理系统调用的代理内核,pk 需要安装在与 spike 同一路径下。

riscv-tools 安装编译

需要安装哪个包就自行安装即可,下面给出所有的包和对应的地址:

THIS REPO IS NOW ARCHIVED. If you would like to make an addition or change, please contact content@riscv.org
RISC-V Software Ecosystem Overview

This document captures the status of the RISC-V Software Ecosystem. Please add
to the list and fix inaccuracies by making a Pull Request against the software list repository on GitHub.

We would like to enlist community help for the software ports in the Help Wanted section.

Simulators
Name Links License Maintainers
riscvOVPsimPlus website Proprietary freeware Imperas
DBT-RISE-RISCV github BSD 3-Clause MINRES Technologies
FireSim website, mailing list, github, ISCA 2018 Paper BSD Sagar Karandikar, Howard Mao, Donggyu Kim, David Biancolin, Alon Amid, Berkeley Architecture Research
gem5 SW-dev thread, repository BSD-style Alec Roelke (University of Virginia)
OVPsim website, github Proprietary (core simulation platform), Apache License (processor / platform model) Imperas
jor1k website, github BSD 2-Clause Sebastian Macke
Jupiter github GPL-3.0 Andrés Castellanos
MARSS-RISCV github MIT Gaurav N Kothari, Parikshit P Sarnaik, Gokturk Yuksek (State University of New York at Binghamton)
QEMU upstream GPL Sagar Karandikar (University of California, Berkeley), Bastian Koppelmann (University of Paderborn), Alex Suykov, Stefan O’Rear and Michael Clark (SiFive)
RARS github MIT Benjamin Landers
Renode website, github MIT Antmicro
Ripes github MIT Morten Borup Petersen
RISC-V Virtual Prototype website, github MIT Vladimir Herdt (University of Bremen, AGRA)
TinyEMU website MIT Fabrice Bellard
Spike github BSD 3-clause Andrew Waterman & Yunsup Lee (SiFive)
Swerv-ISS github GPL - 3 Joseph Rahmeh (Western Digital)
VLAB VLAB Works Proprietary ASTC
WebRISC-V github BSD 3-clause Gianfranco Mariotti, Roberto Giorgi (University of Siena)
PQSE website Proprietary PQShield
riscv-rust website github MIT Takahiro Aoyagi
terminus github MIT Yang Li
Vulcan github MIT Victor Miguel de Morais Costa
riscv-vm github MIT Aidan Dodds
EmulsiV Working Instance, GitHub Open (MPL2.0) Guillaume Savaton
Object toolchain
Name Links License Maintainers
Binutils RISC-V repo, Upstream repos GPLv2 Andrew Waterman (SiFive), Palmer Dabbelt (Google) & Jim Wilson (SiFive)
LLVM GitHub mirror Apache 2.0 with LLVM exception Alex Bradbury (lowRISC)
Cranelift GitHub Apache 2.0 Cranelift core team
Debugging
Name Links License Maintainers
GDB Upstream GPLv2 Andrew Burgess (Embecosm), Palmer Dabbelt (Google)
OpenOCD Upstream repo, RISC-V repo GPLv2 Tim Newsome (SiFive), Megan Wachs (SiFive), Palmer Dabbelt (Google)
GNU MCU Eclipse OpenOCD Website, GitHub GPLv2 Liviu Ionescu
TRACE32 JTAG Debugger Website TRACE32 license Lauterbach
Ozone - the J-Link Debugger Website SEGGER commercial license (J-Link PLUS) SEGGER
Ashling RiscFree Debugger Website Ashling commercial license Ashling
Imperas Multi Processor Debugger Website Imperas Commercial License Imperas
TCF Debugger Website, GitHub Eclipse Distribution License 1.0 (BSD) / Eclipse Public License 1.0 Sanimir Agovic
PlatformIO Unified Debugger Docs Apache 2.0 PlatformIO
Green Hills Probe Website Proprietary Green Hills
GRMON Website Evaluation license, Proprietary Cobham Gaisler
C compilers and libraries
Name Links License Maintainers
GCC Upstream, RISC-V repository GPLv3 Andrew Waterman (SiFive), Palmer Dabbelt (Google), Jim Wilson (SiFive), Kito Cheng (SiFive)
GNU MCU Eclipse RISC-V Embedded GCC (riscv-non-embed-gcc) Web, Repository, Build, Binary package installer Eclipse Public License Liviu lonescu
Clang/LLVM Upstream Apache 2.0 Alex Bradbury (lowRISC)
CompCert Upstream INRIA Non-Commercial License Agreement Xavier Leroy
Glibc Upstream, RISC-V repository GPLv2 Palmer Dabbelt (Google), Andrew Waterman (SiFive), DJ Delorie (Red Hat), Darius Rad(Bluespec)
Newlib Upstream, RISC-V repository GPLv2 Kito Cheng (SiFive)
SEGGER Runtime Library Website SEGGER commercial license SEGGER
SEGGER Floating Point Library Website SEGGER commercial license SEGGER
Musl GitHub MIT -
Green Hills Optimizing C/C++ Compilers Website Proprietary Green Hills
OpenArkCompiler (MAPLE) Upstream MulanPSL-2.0 Futurewei
Andes MCUlib website Andes commercial license Andes
Boot loaders and monitors
Name Links License Maintainers
coreboot Upstream GPLv2 Ron Minnich (Google), Jonathan Neuschäfer
U-Boot Upstream GPLv2 Rick Chen (Andes)
Proxy Kernel/BBL GitHub BSD 3-clause SiFive
OpenSBI GitHub BSD 2-clause Anup Patel (Western Digital), Atish Patra (Western Digital)
RustSBI GitHub Mulan PSL v2 Luo Jia (Huazhong Univ. of Sci. & Tech.)
UEFI edk2 GitHub BSD 2-Clause Patent Abner Chang (Hewlett Packard Enterprise), Daniel Schaefer (Hewlett Packard Enterprise)
Hypervisors and related tools
Name Links License Brief Maintainers
Xvisor Website, Upstream, RISC-V Repo GPLv2 Type-1 baremetal monolithic hypervisor Anup Patel (Western Digital)
KVM Website, RISC-V Repo, RISC-V Wiki GPLv2 Type-2 hypervisor Anup Patel (Western Digital), Atish Patra (Western Digital)
KVMTOOL Upstream, RISC-V Repo GPLv2 Userspace tool for the KVM hypervisor Anup Patel (Western Digital), Atish Patra (Western Digital)
RVirt GitHub Apache-2.0 Trap-and-emulate hypervisor Jonathan Behrens (MIT)
OS and OS kernels
Linux built from source
Name Links License Maintainers
Linux Kernel github, kernel.org GPLv2 Andrew Waterman (SiFive), Albert Ou (SiFive), Palmer Dabbelt (Google)
Yocto Project/OpenEmbedded github MIT Khem Raj
Buildroot busybox.net Mark Corbin (Embecosm)
Linux distributions
Name Links License Maintainers
Fedora fedoraproject.org Richard WM Jones, Stefan O’Rear, David Abdurachmanov
Debian debian wiki, mit.edu, riscv.org, Annc Manuel A. Fernandez Montecelo
OpenMandriva openmandriva.org, openmandriva.org Bernhard “Bero” Rosenkränzer
openSUSE opensuse.org Andreas Schwab (SUSE)
Gentoo github Palmer Dabbelt (University of California, Berkeley)
Parabola GNU/Linux-libre github, parabola.nu Andreas Grapentin (University of Potsdam, HPI)
Ataraxia Linux github protonesso
Slackware Linux github Álvaro Figueroa (of Greencore Solutions)
Ubuntu ubuntu.com xnox
Real-time Operating Systems
Name Links License Maintainers
RT-Thread github, rt-thread.io Apache 2.0 RT-Thread
embOS Website, RISC-V port SEGGER commercial license, free for non-commercial use SEGGER
RTEMS rtems.org, docs.rtems.org Hesham Almatary
FreeRTOS sourceforge, freertos.org MIT AWS
Zephyr github, docs Apache 2.0 Karol Gugala (Antmicro), Peter Gielda (Antmicro), Nathaniel Graff (SiFive)
LiteOS github, docs Chaifangming (Huawei), Pengzhouhu (Huawei), Huerjia (Huawei)
NuttX bitbucket.org, nuttx.org
Apache Mynewt riscv.org Apache 2.0 James Pace, Runtime
OpenWrt github, binary repo Zoltan Herpai
seL4 github, seL4.systems GPLv2 (kernel and proofs), various open-source licenses (userland) Trustworthy Systems group, Data61 CSIRO
PikeOS Press release Proprietary SYSGO
VxWorks Press release Proprietary Wind River
Embox github, embox.rocks Embox
Phoenix-RTOS github, documentation BSD Phoenix Systems
BSD distributions
Name Links License Maintainers
FreeBSD github, wiki.freebsd.org, presentation Ruslan Bukin (FreeBSD)
NetBSD netbsd.org, github Matt Thomas (NetBSD), Reinoud Zandijk (NetBSD)
Compilers and runtimes for other languages
Name Links License Maintainers
Go Upstream, RISC-V repository, Stef’s fork BSD 3-clause Benjamin Barenblat (Google), Michael Pratt (Google), Stef O’Rear
Ocaml Upstream, RISC-V repository LGPL Nicolás Ojeda Bär
Maxine VM (Java Virtual Machine) Upstream GPLv2 Maxine team
Jikes RVM (Java Virtual Machine) Upstream Eclipse Public License (EPL) Martin Maas (University of California, Berkeley)
OpenJDK/HotSpot (Java Virtual Machine) ? ? Alexey Baturo, Michael Knysnek, Martin Maas
OpenJDK/OpenJ9 (Java Virtual Machine) Upstream Eclipse Public License 2.0 (EPLv2) with ClassPath Exception & Apache 2.0 Cheng Jin
BishengJDK/HotSpot (Java Virtual Machine) Upstream GPLv2 with Classpath Exception Yadong Wang
Free Pascal Upstream ? Jeppe Johansen and others
Nim Upstream MIT Andreas Rumpf and others
Ada (GNAT) Upstream GPLv3 with linking exception AdaCore
Rust Upstream Apache and MIT Rust Project
muForth Upstream ? David Frech
ibForth Upstream GPLv3 Lars Brinkhoff
Mecrisp-Quintis Forth kernel Upstream ? Matthias Koch
Mono Initial support in upstream MIT Alex Rønne Petersen
Zen Zen-Lang.org Commercial, AGPLv3 connectFree Corporation
V8 (JS) Upstream github BSD RIOS, Futurewei, PLCT Lab
Node.js github MIT RIOS, Futurewei, PLCT Lab
OpenArkCompiler (MAPLE) Upstream MulanPSL-2.0 Futurewei
Eclipse OMR github Eclipse Public License 2.0 (EPLv2) with ClassPath and Assembly Exception & Apache 2.0 Eclipse OMR Project
IDEs, SDKs and binary toolchain distributions
Name Links License Maintainers
riscv-gnu-toolchain Website GPLv3 RISC-V International
JianChi CDK website Alibaba commercial license T-Head (Alibaba group)
Imperas M*SDK Website Proprietary Imperas
GNU MCU Eclipse Website, Repositories, Binary distribution EPL-1.0 / various Liviu Ionescu
RT-Thread Studio Website EPL-1.0 / various RT-Thread
AndeSight IDE website Andes comercial License Andes
Embedded Studio Website, RISC-V spec SEGGER commercial license, free for non-commercial use SEGGER
IAR Embedded Workbench Website commercial IAR Systems
PlatformIO Website, IDE, Docs Apache 2.0 PlatformIO
Freedom Studio Website EPL 1.0/various SiFive
Nuclei Studio Website EPL 1.0/various Nuclei System Technology
Ashling RiscFreeTM IDE Website Ashling commercial license Ashling
SoftConsole Website Various, see RN Microchip
GCC Sourcery CodeBench Lite Website GPLv3 Mentor, a Siemens Business
LLVM Sourcery CodeBench Lite Website Apache 2.0 Mentor, a Siemens Business
MULTI IDE Website Proprietary Green Hills
Security
Name Links License Maintainers
emCrypt Website Commercial, free for non-commercial use SEGGER
CoreGuard Website Proprietary Dover Microsystems, Inc.
MultiZone API GitHub ISC Hex Five Security Inc.
Secure IoT Stack GitHub MIT, GPLv2, GPLv3, Evaluation license Hex Five Security Inc.
MultiZone Security TEE & Enclave SDK, Enclave Evaluation license Hex Five Security Inc.
Keystone Enclave Website, Repositories BSD 3-clause Keystone Team
SecureRF Website, SDK Proprietary SecureRF Corp.
IntrinsicID Quiddikey Proprietary Intrinsic ID
Penglai Enclave Website, GitHub Mulan PSL v1 IPADS
PQSLIB / PQSoC Website Proprietary PQShield
Ghidra Website, Github Apache 2.0 National Security Agency
Machine Learning / AI
Name Links License Maintainers
TF Lite demo, blog note Apache 2.0 Antmicro / Google TF Lite team
ncnn GitHub BSD 3-clause Tencent Open Source
Andes NN Library website Andes commercial license Andes
Configuration
Name Links License Maintainers
RISC-V Explorer Website Proprietary BlueSpec
Verification Tools
Name Links License Maintainers
OneSpin RISC-V Verification App Website Proprietary OneSpin
OneSpin Floating-Point Unit App Website Proprietary OneSpin
RISCV-DV GitHub Apache 2.0 CHIPS Alliance Tools/DV workgroup
STING Website Proprietary Valtrix Systems
ImperasDV SystemVerilog HW DV Website Proprietary Imperas
Axiomise formalISA app Website Proprietary Axiomise
Accelerated Libraries
Name Links License Maintainers
OpenBLAS (Support RISC-V V extension 0.7.1) GitHub BSD 3-clause PerfXLab
Andes DSP Library website Andes commercial license Andes
Andes Vector Library website Andes commercial license Andes
Help Wanted
  • Dart

编译安装报错处理

这里可以选择只删除 make 产生的文件,或同时删除 make 产生的文件和 configure 产生的文件

sudo make clean			# 只删除 make 产生的文件
sudo make distclean		# 同时删除 make 和 configure 产生的文件

上网工具推荐

点击连接跳转注册使用:魔界

参考资料

riscv各种版本gcc工具链编译与安装 #链接库说明

GCC RISCV环境搭建 #另一篇指导

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐