夜末微凉 發表於 2022-3-22 15:34:15

教你如何在优麒麟上搭建 RISC-V 交叉编译环境

<p>由于 RISC-V 设备价格昂贵、不易采购等诸多原因,许多小伙伴虽然很感兴趣,但仍无法参与 RISC-V 开发工作,今天就教大家如何在优麒麟上搭建 RISC-V 交叉编译环境,快学起来吧!</p>
<p>交叉编译(Cross Compile)指编译代码的平台,和执行编译后源代码的平台是两个不同的平台,比如在 x86/Linux 平台下使用交叉编译工具链编译 ARM/Linux 平台下的可执行文件。今天我们要讲的就是在优麒麟(x86/Linux)上编译 RISC-V 架构可执行文件的方法。</p>
<p>我们为什么需要交叉编译呢,主要有以下考虑:</p>
<h3>01.性能与速度</h3>
<p>交叉编译的目标平台往往 CPU 性能较差,内存和磁盘性能也可能不能满足编译的要求,这时候就要依赖性能资源更好的编译主机进行编译。</p>
<h3>02.缺乏编译条件</h3>
<p>就算目标平台性能足够且资源充足,可以本地编译,但第一个在目标平台运行的本地编译器总是需要我们通过交叉编译获得。</p>
<h3>03.软件编译环境</h3>
<p>一个完整的 Linux 发行版需要由数百个包构成,而我们往往只关注需要在目标主机上安装的包,所以我们可以在交叉编译的主机上配置这些环境,而不是把时间浪费在配置目标主机的编译依赖上。</p>
<p>本文包含以下两部分:</p>
<p>1、如何搭建一个 RISC-V 的交叉编译环境。</p>
<p>2、交叉编译 Linux 内核。</p>
<h3>一、搭建 RISC-V 交叉编译环境。</h3>
<p>通常来讲,在搭建交叉编译环境时需要考虑不同体系架构的不同特性,包括 CPU 架构是 64 位还是 32 位系统、字节序是大端( big-endian )或小端( little-endian )、内存字节对齐方式等,不过好在 RISC-V 已经有完善的工具链,包含交叉编译所需的 binutils 、 gcc 和 glibc 3 个部分。</p>
<p>● 首先需要 RISC-V 交叉编译工具链,如果网络较慢,可以忽略其中的 qemu 子项目</p>
<div class="jb51code">
<pre class="brush:bash;">
git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
</pre>
</div>
<p>这是 RISC-V 的 C/C++ 交叉编译工具链,其支持两种构建模式:</p>
<p>1. 通用 ELF/Newlib 工具链</p>
<p>2. Linux-ELF/glibc 工具链</p>
<p>● 安装所需依赖包</p>
<div class="jb51code">
<pre class="brush:bash;">
sudo apt-get install -y autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev libncurses-dev device-tree-compiler libssl-dev gdisk swig</pre>
</div>
<p>● 接下来开始编译</p>
<div class="jb51code">
<pre class="brush:bash;">
cd riscv-gnu-toolchain
./configure --prefix=/opt/riscv(路径可以根据个人习惯自定义)
sudo make linux -j `nproc`</pre>
</div>
<p>编译完成后,刚才指定的路径 opt/riscv/bin 下会生成以下文件:</p>
<p style="text-align: center;"><img src="https://img.jbzj.com/file_images/article/202203/2022032215230581.jpg" title="优麒麟" data-="" alt="" /></p>
<p>交叉编译所需的工具,包括 Binutils(ld,as,ar 等,了解详情可参考https://sourceware.org/binutils/docs-2.37/binutils/index.html )、gcc 、gdb 等都在其中。</p>
<p>可以将这个路径添加到环境变量中:</p>
<div class="jb51code">
<pre class="brush:bash;">
export PATH=/opt/riscv/bin:$PATH
</pre>
</div>
<p>也可以添加到:</p>
<div class="jb51code">
<pre class="brush:bash;">
echo &quot;export PATH=/opt/riscv/bin:$PATH&quot; &gt;&gt; ~/.bashrc</pre>
</div>
<p>到这里我们就完成了交叉编译所需环境的搭建。</p>
<h3>二、编译 Linux 内核</h3>
<p>接下来我们以内核源码为例,了解一下上述交叉编译工具链的使用方法。</p>
<p>● 首先下载内核源码</p>
<div class="jb51code">
<pre class="brush:bash;">
git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git</pre>
</div>
<p>● 不过这里下载较慢,可以去国内的镜像站下载,比如清华大学镜像站</p>
<div class="jb51code">
<pre class="brush:bash;">
git clone https://mirrors.tuna.tsinghua.edu.cn/git/linux-stable.git</pre>
</div>
<p>● 下载完成后</p>
<div class="jb51code">
<pre class="brush:bash;">
git checkout</pre>
</div>
<p>接下来将需要打上你想要编译的开发板的 patch ,以 hifive unmatched 为例</p>
<p>● 首先下载</p>
<div class="jb51code">
<pre class="brush:bash;">
git clone https://github.com/sifive/meta-sifive</pre>
</div>
<p>这里面还包含了编译 OpenSBI 和 U-Boot 所需的 patch ,编译过程大同小异,如果想要自己构建一个系统镜像,可以分别编译这两个工具。这里以内核为例:</p>
<p>● 打上所有 patch</p>
<div class="jb51code">
<pre class="brush:bash;">
for f in path to /meta-sifive/recipes-kernel/linux/files/*.patch; do echo $f;patch -p1 &lt; $f;done
</pre>
</div>
<p>● 复制 defconfig 配置文件</p>
<div class="jb51code">
<pre class="brush:bash;">
cp path to /meta-sifive/recipes-kernel/linux/files/defconfig./.config
</pre>
</div>
<p>如果想避免产生额外后缀名,可以添加</p>
<div class="jb51code">
<pre class="brush:bash;">
touch .scmversion</pre>
</div>
<p>接下来开始编译内核,这里需要指定 make 的两个参数:</p>
<p>1、CROSS_COMPILE:交叉编译器的前缀,表示将代码编译编译成目标CPU指令的工具,如果不指定,make 会默认使用系统自带的 gcc 来编译,这里指定我们之前编译出来的 riscv64-unknown-linux-gnu- 为前缀。</p>
<p>2、ARCH:即 architecture ,用于选择编译哪种 CPU 架构,也就是编译 arch/ 目录下的哪个子目录,这里指定 ARCH=riscv ,arch/riscv 目录下也包含此架构特有的 Kconfig 配置文件,所以 make menuconfig 时也会用到这个目录。</p>
<div class="jb51code">
<pre class="brush:bash;">
make CROSS_COMPILE=riscv64-unknown-linux-gnu- ARCH=riscv olddefconfig
make CROSS_COMPILE=riscv64-unknown-linux-gnu- ARCH=riscv -j`nproc`</pre>
</div>
<p>也可以将内核和内核模块打成 tar 包或 deb 包</p>
<div class="jb51code">
<pre class="brush:bash;">
make CROSS_COMPILE=riscv64-unknown-linux-gnu- ARCH=riscv    INSTALL_MOD_STRIP=1 -j`nproc` tarbz2-pkg
make CROSS_COMPILE=riscv64-unknown-linux-gnu- ARCH=riscv INSTALL_MOD_STRIP=1 -j`nproc` bindeb-pkg</pre>
</div>
<p>添加版本号</p>
<div class="jb51code">
<pre class="brush:bash;">
version=`cat include/config/kernel.release`;echo $version</pre>
</div>
<p>编译完成后,会生成 path to/arch/riscv/boot/Image 内核镜像文件和 path to/arch/riscv/boot/dts/sifive/hifive-unmatched-a00.dtb 硬件 dtb 文件。</p>
<p>如果选择打成 deb 包,会生成三个 .deb 文件:</p>
<p>1. linux-headers-...</p>
<p>2. linux-libc-dev_...</p>
<p>3. linux-image-...</p>
<p>(其中省略号表示版本号)</p>
<p>到这里我们就完成了 RISC-V 架构 Linux 内核的编译,接下来便可以基于这个内核制作自己的系统镜像。</p>
<p>各位小伙伴,你学会了吗?</p>
<p>参考文档:</p>
<p><a href="https://github.com/carlosedp/riscv-bringup/tree/bbef412a4456acefdf814a9e14a75ce2778992ed/unmatched" target="_blank" rel="external nofollow">https://github.com/carlosedp/riscv-bringup/tree/bbef412a4456acefdf814a9e14a75ce2778992ed/unmatched</a></p>
<p><a href="https://github.com/sifive/meta-sifive/tree/2021.12/recipes-kernel/linux/files" target="_blank" rel="external nofollow">https://github.com/sifive/meta-sifive/tree/2021.12/recipes-kernel/linux/files</a></p>
<p><a href="https://github.com/riscv-collab/riscv-gnu-toolchain" target="_blank" rel="external nofollow">https://github.com/riscv-collab/riscv-gnu-toolchain</a></p>
<p><a href="https://github.com/sifive/freedom-u-sdk" target="_blank" rel="external nofollow">https://github.com/sifive/freedom-u-sdk</a></p>
<p>到此这篇关于手把手教你在优麒麟上搭建 RISC-V 交叉编译环境的文章就介绍到这了,更多相关优麒麟搭建RISC-V环境内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章,希望大家以后多多支持琼殿技术社区!</p>
頁: [1]
查看完整版本: 教你如何在优麒麟上搭建 RISC-V 交叉编译环境