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
NameLinksLicenseMaintainers
riscvOVPsimPluswebsiteProprietary freewareImperas
DBT-RISE-RISCVgithubBSD 3-ClauseMINRES Technologies
FireSimwebsite, mailing list, github, ISCA 2018 PaperBSDSagar Karandikar, Howard Mao, Donggyu Kim, David Biancolin, Alon Amid, Berkeley Architecture Research
gem5SW-dev thread, repositoryBSD-styleAlec Roelke (University of Virginia)
OVPsimwebsite, githubProprietary (core simulation platform), Apache License (processor / platform model)Imperas
jor1kwebsite, githubBSD 2-ClauseSebastian Macke
JupitergithubGPL-3.0Andrés Castellanos
MARSS-RISCVgithubMITGaurav N Kothari, Parikshit P Sarnaik, Gokturk Yuksek (State University of New York at Binghamton)
QEMUupstreamGPLSagar Karandikar (University of California, Berkeley), Bastian Koppelmann (University of Paderborn), Alex Suykov, Stefan O’Rear and Michael Clark (SiFive)
RARSgithubMITBenjamin Landers
Renodewebsite, githubMITAntmicro
RipesgithubMITMorten Borup Petersen
RISC-V Virtual Prototypewebsite, githubMITVladimir Herdt (University of Bremen, AGRA)
TinyEMUwebsiteMITFabrice Bellard
SpikegithubBSD 3-clauseAndrew Waterman & Yunsup Lee (SiFive)
Swerv-ISSgithubGPL - 3Joseph Rahmeh (Western Digital)
VLABVLAB WorksProprietaryASTC
WebRISC-VgithubBSD 3-clauseGianfranco Mariotti, Roberto Giorgi (University of Siena)
PQSEwebsiteProprietaryPQShield
riscv-rustwebsite githubMITTakahiro Aoyagi
terminusgithubMITYang Li
VulcangithubMITVictor Miguel de Morais Costa
riscv-vmgithubMITAidan Dodds
EmulsiVWorking Instance, GitHubOpen (MPL2.0)Guillaume Savaton
Object toolchain
NameLinksLicenseMaintainers
BinutilsRISC-V repo, Upstream reposGPLv2Andrew Waterman (SiFive), Palmer Dabbelt (Google) & Jim Wilson (SiFive)
LLVMGitHub mirrorApache 2.0 with LLVM exceptionAlex Bradbury (lowRISC)
CraneliftGitHubApache 2.0Cranelift core team
Debugging
NameLinksLicenseMaintainers
GDBUpstreamGPLv2Andrew Burgess (Embecosm), Palmer Dabbelt (Google)
OpenOCDUpstream repo, RISC-V repoGPLv2Tim Newsome (SiFive), Megan Wachs (SiFive), Palmer Dabbelt (Google)
GNU MCU Eclipse OpenOCDWebsite, GitHubGPLv2Liviu Ionescu
TRACE32 JTAG DebuggerWebsiteTRACE32 licenseLauterbach
Ozone - the J-Link DebuggerWebsiteSEGGER commercial license (J-Link PLUS)SEGGER
Ashling RiscFree DebuggerWebsiteAshling commercial licenseAshling
Imperas Multi Processor DebuggerWebsiteImperas Commercial LicenseImperas
TCF DebuggerWebsite, GitHubEclipse Distribution License 1.0 (BSD) / Eclipse Public License 1.0Sanimir Agovic
PlatformIO Unified DebuggerDocsApache 2.0PlatformIO
Green Hills ProbeWebsiteProprietaryGreen Hills
GRMONWebsiteEvaluation license, ProprietaryCobham Gaisler
C compilers and libraries
NameLinksLicenseMaintainers
GCCUpstream, RISC-V repositoryGPLv3Andrew 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 installerEclipse Public LicenseLiviu lonescu
Clang/LLVMUpstreamApache 2.0Alex Bradbury (lowRISC)
CompCertUpstreamINRIA Non-Commercial License AgreementXavier Leroy
GlibcUpstream, RISC-V repositoryGPLv2Palmer Dabbelt (Google), Andrew Waterman (SiFive), DJ Delorie (Red Hat), Darius Rad(Bluespec)
NewlibUpstream, RISC-V repositoryGPLv2Kito Cheng (SiFive)
SEGGER Runtime LibraryWebsiteSEGGER commercial licenseSEGGER
SEGGER Floating Point LibraryWebsiteSEGGER commercial licenseSEGGER
MuslGitHubMIT-
Green Hills Optimizing C/C++ CompilersWebsiteProprietaryGreen Hills
OpenArkCompiler (MAPLE)UpstreamMulanPSL-2.0Futurewei
Andes MCUlibwebsiteAndes commercial licenseAndes
Boot loaders and monitors
NameLinksLicenseMaintainers
corebootUpstreamGPLv2Ron Minnich (Google), Jonathan Neuschäfer
U-BootUpstreamGPLv2Rick Chen (Andes)
Proxy Kernel/BBLGitHubBSD 3-clauseSiFive
OpenSBIGitHubBSD 2-clauseAnup Patel (Western Digital), Atish Patra (Western Digital)
RustSBIGitHubMulan PSL v2Luo Jia (Huazhong Univ. of Sci. & Tech.)
UEFI edk2GitHubBSD 2-Clause PatentAbner Chang (Hewlett Packard Enterprise), Daniel Schaefer (Hewlett Packard Enterprise)
Hypervisors and related tools
NameLinksLicenseBriefMaintainers
XvisorWebsite, Upstream, RISC-V RepoGPLv2Type-1 baremetal monolithic hypervisorAnup Patel (Western Digital)
KVMWebsite, RISC-V Repo, RISC-V WikiGPLv2Type-2 hypervisorAnup Patel (Western Digital), Atish Patra (Western Digital)
KVMTOOLUpstream, RISC-V RepoGPLv2Userspace tool for the KVM hypervisorAnup Patel (Western Digital), Atish Patra (Western Digital)
RVirtGitHubApache-2.0Trap-and-emulate hypervisorJonathan Behrens (MIT)
OS and OS kernels
Linux built from source
NameLinksLicenseMaintainers
Linux Kernelgithub, kernel.orgGPLv2Andrew Waterman (SiFive), Albert Ou (SiFive), Palmer Dabbelt (Google)
Yocto Project/OpenEmbeddedgithubMITKhem Raj
Buildrootbusybox.netMark Corbin (Embecosm)
Linux distributions
NameLinksLicenseMaintainers
Fedorafedoraproject.orgRichard WM Jones, Stefan O’Rear, David Abdurachmanov
Debiandebian wiki, mit.edu, riscv.org, AnncManuel A. Fernandez Montecelo
OpenMandrivaopenmandriva.org, openmandriva.orgBernhard “Bero” Rosenkränzer
openSUSEopensuse.orgAndreas Schwab (SUSE)
GentoogithubPalmer Dabbelt (University of California, Berkeley)
Parabola GNU/Linux-libregithub, parabola.nuAndreas Grapentin (University of Potsdam, HPI)
Ataraxia Linuxgithubprotonesso
Slackware LinuxgithubÁlvaro Figueroa (of Greencore Solutions)
Ubuntuubuntu.comxnox
Real-time Operating Systems
NameLinksLicenseMaintainers
RT-Threadgithub, rt-thread.ioApache 2.0RT-Thread
embOSWebsite, RISC-V portSEGGER commercial license, free for non-commercial useSEGGER
RTEMSrtems.org, docs.rtems.orgHesham Almatary
FreeRTOSsourceforge, freertos.orgMITAWS
Zephyrgithub, docsApache 2.0Karol Gugala (Antmicro), Peter Gielda (Antmicro), Nathaniel Graff (SiFive)
LiteOSgithub, docsChaifangming (Huawei), Pengzhouhu (Huawei), Huerjia (Huawei)
NuttXbitbucket.org, nuttx.org
Apache Mynewtriscv.orgApache 2.0James Pace, Runtime
OpenWrtgithub, binary repoZoltan Herpai
seL4github, seL4.systemsGPLv2 (kernel and proofs), various open-source licenses (userland)Trustworthy Systems group, Data61 CSIRO
PikeOSPress releaseProprietarySYSGO
VxWorksPress releaseProprietaryWind River
Emboxgithub, embox.rocksEmbox
Phoenix-RTOSgithub, documentationBSDPhoenix Systems
BSD distributions
NameLinksLicenseMaintainers
FreeBSDgithub, wiki.freebsd.org, presentationRuslan Bukin (FreeBSD)
NetBSDnetbsd.org, githubMatt Thomas (NetBSD), Reinoud Zandijk (NetBSD)
Compilers and runtimes for other languages
NameLinksLicenseMaintainers
GoUpstream, RISC-V repository, Stef’s forkBSD 3-clauseBenjamin Barenblat (Google), Michael Pratt (Google), Stef O’Rear
OcamlUpstream, RISC-V repositoryLGPLNicolás Ojeda Bär
Maxine VM (Java Virtual Machine)UpstreamGPLv2Maxine team
Jikes RVM (Java Virtual Machine)UpstreamEclipse 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)UpstreamEclipse Public License 2.0 (EPLv2) with ClassPath Exception & Apache 2.0Cheng Jin
BishengJDK/HotSpot (Java Virtual Machine)UpstreamGPLv2 with Classpath ExceptionYadong Wang
Free PascalUpstream?Jeppe Johansen and others
NimUpstreamMITAndreas Rumpf and others
Ada (GNAT)UpstreamGPLv3 with linking exceptionAdaCore
RustUpstreamApache and MITRust Project
muForthUpstream?David Frech
ibForthUpstreamGPLv3Lars Brinkhoff
Mecrisp-Quintis Forth kernelUpstream?Matthias Koch
MonoInitial support in upstreamMITAlex Rønne Petersen
ZenZen-Lang.orgCommercial, AGPLv3connectFree Corporation
V8 (JS)Upstream githubBSDRIOS, Futurewei, PLCT Lab
Node.jsgithubMITRIOS, Futurewei, PLCT Lab
OpenArkCompiler (MAPLE)UpstreamMulanPSL-2.0Futurewei
Eclipse OMRgithubEclipse Public License 2.0 (EPLv2) with ClassPath and Assembly Exception & Apache 2.0Eclipse OMR Project
IDEs, SDKs and binary toolchain distributions
NameLinksLicenseMaintainers
riscv-gnu-toolchainWebsiteGPLv3RISC-V International
JianChi CDKwebsiteAlibaba commercial licenseT-Head (Alibaba group)
Imperas M*SDKWebsiteProprietaryImperas
GNU MCU EclipseWebsite, Repositories, Binary distributionEPL-1.0 / variousLiviu Ionescu
RT-Thread StudioWebsiteEPL-1.0 / variousRT-Thread
AndeSight IDEwebsiteAndes comercial LicenseAndes
Embedded StudioWebsite, RISC-V specSEGGER commercial license, free for non-commercial useSEGGER
IAR Embedded WorkbenchWebsitecommercialIAR Systems
PlatformIOWebsite, IDE, DocsApache 2.0PlatformIO
Freedom StudioWebsiteEPL 1.0/variousSiFive
Nuclei StudioWebsiteEPL 1.0/variousNuclei System Technology
Ashling RiscFreeTM IDEWebsiteAshling commercial licenseAshling
SoftConsoleWebsiteVarious, see RNMicrochip
GCC Sourcery CodeBench LiteWebsiteGPLv3Mentor, a Siemens Business
LLVM Sourcery CodeBench LiteWebsiteApache 2.0Mentor, a Siemens Business
MULTI IDEWebsiteProprietaryGreen Hills
Security
NameLinksLicenseMaintainers
emCryptWebsiteCommercial, free for non-commercial useSEGGER
CoreGuardWebsiteProprietaryDover Microsystems, Inc.
MultiZone APIGitHubISCHex Five Security Inc.
Secure IoT StackGitHubMIT, GPLv2, GPLv3, Evaluation licenseHex Five Security Inc.
MultiZone Security TEE & EnclaveSDK, EnclaveEvaluation licenseHex Five Security Inc.
Keystone EnclaveWebsite, RepositoriesBSD 3-clauseKeystone Team
SecureRFWebsite, SDKProprietarySecureRF Corp.
IntrinsicIDQuiddikeyProprietaryIntrinsic ID
Penglai EnclaveWebsite, GitHubMulan PSL v1IPADS
PQSLIB / PQSoCWebsiteProprietaryPQShield
GhidraWebsite, GithubApache 2.0National Security Agency
Machine Learning / AI
NameLinksLicenseMaintainers
TF Litedemo, blog noteApache 2.0Antmicro / Google TF Lite team
ncnnGitHubBSD 3-clauseTencent Open Source
Andes NN LibrarywebsiteAndes commercial licenseAndes
Configuration
NameLinksLicenseMaintainers
RISC-V ExplorerWebsiteProprietaryBlueSpec
Verification Tools
NameLinksLicenseMaintainers
OneSpin RISC-V Verification AppWebsiteProprietaryOneSpin
OneSpin Floating-Point Unit AppWebsiteProprietaryOneSpin
RISCV-DVGitHubApache 2.0CHIPS Alliance Tools/DV workgroup
STINGWebsiteProprietaryValtrix Systems
ImperasDV SystemVerilog HW DVWebsiteProprietaryImperas
Axiomise formalISA appWebsiteProprietaryAxiomise
Accelerated Libraries
NameLinksLicenseMaintainers
OpenBLAS (Support RISC-V V extension 0.7.1)GitHubBSD 3-clausePerfXLab
Andes DSP LibrarywebsiteAndes commercial licenseAndes
Andes Vector LibrarywebsiteAndes commercial licenseAndes
Help Wanted
  • Dart

编译安装报错处理

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

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

上网工具推荐

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

参考资料

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

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

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐