查看: 2730|回复: 0
收起左侧

RISC-V GNU 工具链:安装与使用

[复制链接]

  离线 

  • TA的每日心情
    拍拍
    2022-6-27 11:09
  • 签到天数: 25 天

    [LV.4]

    发表于 2020-10-22 19:51:32 | 显示全部楼层 |阅读模式

    有人预言,RISC-V或将是继Intel和Arm之后的第三大主流处理器体系。欢迎访问全球首家只专注于RISC-V单片机行业应用的中文网站

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    本帖最后由 sky 于 2020-10-22 19:51 编辑

    1. 安装Wmware和unbuntu,我安装的是Wmware workstation pro 12.1.1 build-3770994, unbuntu 是18.04.2 amd版本, ubuntu-18.04.2-desktop-amd64.iso

    2. 安装好unbuntu后,在/home/{username}目录里面创建目录riscv, 我的用户名是kaguo,所以创建的目录就是/home/kaguo/riscv

    3.  cd /home/kaguo/riscv , 进入riscv目录后,执行以下命令,安装一些编译riscv工具链需要的库。
    1. sudo apt-get install 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
    复制代码



    建议把ubuntu的源镜像改为阿里云的,我开始用默认的会遇到 libtool库不能更新的问题。请参考


    可以用下面命令先安装vim和gvim
    1. sudo apt-get install vim
    2. sudo apt-get install vim-gtk
    复制代码

    4. 在riscv目录中执行以下命令,
    1.      sudo apt install git //首先安装git
    2.      git clone https://github.com/riscv/riscv-tools.git
    复制代码

    接着你可以用下面的命令
    1. sudo apt-get install 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
    复制代码

    将会下载下面的几个模块

    • Spike, the ISA simulator
    • riscv-tests, a battery of ISA-level tests
    • riscv-opcodes, the enumeration of all RISC-V opcodes executable by the simulator
    • riscv-pk, which contains bbl, a boot loader for Linux and similar OS kernels, and pk, a proxy kernel that services system calls for a target-machine application by forwarding them to the host machine

    但是如果网络环境不好,可以使用下面命令一个模块一个模块单独下载:
    1. git clone --recursive https://github.com/riscv/riscv-openocd.git
    2. git clone --recursive https://github.com/riscv/riscv-isa-sim.git
    3. git clone --recursive https://github.com/riscv/riscv-opcodes.git
    4. git clone --recursive https://github.com/riscv/riscv-pk.git
    5. git clone --recursive https://github.com/riscv/riscv-tests.git
    复制代码

    5.下载riscv gun toolchain,

    在目录/home/kaguo/riscv,执行
    1. git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
    复制代码

    或者
    1. git clone https://github.com/riscv/riscv-gnu-toolchain
    2. cd riscv-gnu-toolchain
    3. git submodule update --init --recursive
    复制代码

    将会下载riscv gun工具链,网络环境不好的化,可以按下面的方法,一个模块一个模块单独下载:
    1. git clone https://github.com/riscv/riscv-gnu-toolchain
    2. cd riscv-gnu-toolchain
    3. git clone --recursive https://github.com/riscv/riscv-qemu.git
    4. git clone --recursive https://github.com/riscv/riscv-newlib.git
    5. git clone --recursive https://github.com/riscv/riscv-binutils-gdb.git
    6. git clone --recursive https://github.com/riscv/riscv-dejagnu.git
    7. git clone --recursive https://github.com/riscv/riscv-glibc.git
    8. git clone --recursive https://github.com/riscv/riscv-gcc.git
    复制代码

    因为这样下载的目录和前一种方式不同,主要是qemu和gdb,binutils三个模块,可以进行一些copy工作,使得qemu, riscv-binutils,riscv-gdb三个目录有数据,否则编译会出错。
    1. cd riscv-qemu
    2. cp -a * ../qemu
    3. cd riscv-binutils-gdb
    4. cp -a * ../riscv-gdb
    5. cp -a * ../riscv-binutils
    复制代码

    6.先编译riscv gun toolchain,再编译riscv tools,否则pk模块会编译出错
    1. cd /home/kaguo/riscv/riscv-tools/riscv-tools/riscv-gnu-toolchain,
    2. vim ~/.bashrc
    复制代码

    在最后增加两行,
    1. export  RISCV="/home/kaguo/riscv/riscv-tools/riscv-gnu-toolchain"
    2. export PATH=$PATH:$RISCV/bin
    复制代码

    然后source ~/.bashrc

    现在用下面的命令开始编译riscv gun tool chain
    1. ./configure --prefix=$RISCV
    2. make
    复制代码

    https://github.com/riscv/riscv-gnu-toolchain

    这样会编译出64位的gcc riscv工具链,如果想同时产生32位工具链,可以用下面的命令
    1. ./configure --prefix=$RISCV --with-arch=rv32gc --with-abi=ilp32d
    2. make
    复制代码

    如果出现ifconfig不能找问题,安装net-tools
    1. sudo apt-get install net-tools
    复制代码


    7.  回到上一级目录,cd /home/kaguo/riscv/riscv-tools/riscv-tools/

    执行下面命令编译riscv tools

    1. ./build.sh
    复制代码

    编译完后,大部分可以执行文件都会安装到$RISCV/bin目录,单似乎pk的默认没有安装。

    如果遇到下面错误,可以删除openocd 目录,用git clone --recursive https://github.com/riscv/riscv-opcodes.git 重新下载。
    1. Configuring project riscv-openocd
    2. configure: error: cannot find install-sh, install.sh, or shtool in ".." "../.." "../../.."
    复制代码

    进入 cd /home/kaguo/riscv/riscv-tools/riscv-tools/riscv-pk/build
    1. cp pk dummy_payload config.status bbl_payload bbl /home/kaguo/riscv/riscv-tools/riscv-tools/riscv-gnu-toolchain/bin
    复制代码

    把pk的可执行文件copy到$RISCV/bin目录。

    8. 最后一步,测试运行。

    编写hello.c文件
    1. #include <stdio.h>
    2. int main(void)
    3. {
    4.         printf("Hello World!\n");
    5.         return 0;
    6. }
    复制代码

    编译hello.c,生产riscv的可执行文件hello
    1. riscv64-unknown-elf-gcc  -o  hello hello.c
    复制代码

    这时候的 hello,并不能用./hello执行,因为它是riscv asi的机器码,而我们的计算机是x86平台,我们可以spike模拟器来执行该文件。
    1. spike pk hello
    复制代码

    结果为:
    1. kaguo@ubuntu:~/riscv/riscv-tools/riscv-tools$ spike pk hello
    2. bbl loader
    3. Hello World!
    复制代码

    注意:因为spike默认要分配2G的内存,所以我们要给虚拟机分配2G以上的内存空间,否则会出现下面的错误,我为ubuntu分配的内存是4G。
    1. kaguo@ubuntu:~/riscv/riscv-tools/riscv-tools/riscv-gnu-toolchain$ spike
    2. terminate called after throwing an instance of 'std::runtime_error'
    3.   what():  couldn't allocate 2147483648 bytes of target memory
    4. Aborted (core dumped)
    复制代码
    1. spike -m128 pk hello
    复制代码

    或者我们可以用m参数,每次制定为spike分配的内存数量。







    上一篇:RISC-V与Zephyr OS
    下一篇:织女星开发板使用RISC-V核驱动GPIO
    RISCV作者优文
    全球首家只专注于RISC-V单片机行业应用的中文网站
    回复

    使用道具 举报

    高级模式
    B Color Image Link Quote Code Smilies

    本版积分规则

    关闭

    RISC-V单片机中文网上一条 /2 下一条



    版权及免责声明|RISC-V单片机中文网 |网站地图

    GMT+8, 2024-4-26 20:45 , Processed in 0.610976 second(s), 45 queries .

    快速回复 返回顶部 返回列表