RISC-V GNU Toolchain

repository·master·Indexed 26 days ago

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

C and C++ cross-compilers for RISC-V architectures, supporting bare-metal (Newlib) and Linux (glibc/musl) environments. Includes instructions for building Newlib and Linux cross-compilers, configuring multilib, big endian support, and LLVM/Clang integration. Provides installation prerequisites for Ubuntu, Fedora/CentOS/RHEL, Arch Linux, and macOS.

Tokens
1.9K
Snippets
13
Records
15
Agent score
39%

What's inside riscv-gnu-toolchain

  1. Install prerequisites for Fedora/CentOS/RHEL

    master

    Install the required standard packages using yum to prepare your system for building the toolchain.

    sudo yum install autoconf automake python3 libmpc-devel mpfr-devel gmp-devel gawk  bison flex texinfo patchutils gcc gcc-c++ zlib-devel expat-devel libslirp-devel ncurses-devel ninja-build cmake
  2. Run the Dejagnu test suite

    master

    The Dejagnu test suite can be run using a simulator (e.g., qemu, gdb, or spike) by setting the SIM variable. For GCC regression tests, use make report. You can restrict tests using the RUNTESTFLAGS environment variable.

    # Run with QEMU for Linux toolchain
    ./configure --prefix=$RISCV
    make linux
    make report-linux SIM=qemu
    
    # Restrict to RISC-V specific tests
    RUNTESTFLAGS="riscv.exp" make report
    
    # Run Binutils tests
    make check-binutils
  3. Install prerequisites for macOS via Homebrew

    master

    Install the required dependencies using brew. Note: When building the glibc (Linux) toolchain on macOS, you must build within a case-sensitive file system (e.g., a mounted case-sensitive disk image).

    brew install python3 gawk gnu-sed make gmp mpfr libmpc isl zlib expat texinfo flock libslirp ncurses ninja cmake bison m4 wget
  4. Build the Linux cross-compiler (glibc or musl)

    master

    To build a Linux-ELF toolchain, use make linux for glibc or make musl for musl libc. By default, it targets RV64GC. To target 32-bit RV32GC, specify the architecture and ABI during configuration.

    # Default RV64GC with glibc
    ./configure --prefix=/opt/riscv
    make linux
    
    # 32-bit RV32GC
    ./configure --prefix=/opt/riscv --with-arch=rv32gc --with-abi=ilp32d
    make linux
    
    # Linux with musl libc
    ./configure --prefix=/opt/riscv
    make musl
  5. Configure LLVM/Clang support

    master

    You can build LLVM with C and C++ support alongside the GNU toolchain using the --enable-llvm flag. Note that combining --enable-llvm with multilib configuration is not supported. LLVM builds use the Ninja CMake generator by default.

    # Build LLVM on top of a RV64 Linux toolchain
    ./configure --prefix=$RISCV --enable-llvm --enable-linux
    make -j$(nproc) all
    
    # To use make instead of Ninja, set LLVM_GENERATOR
    make -j$(nproc) LLVM_GENERATOR="Unix Makefiles" all
  6. Install prerequisites for Ubuntu

    master

    Install the required standard packages using apt-get to prepare your system for building the toolchain.

    sudo apt-get install autoconf automake autotools-dev curl python3 python3-pip python3-tomli 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 libslirp-dev libncurses-dev
  7. Build the toolchain on MacOS ARM

    master

    Building on MacOS ARM requires specific steps:

    1. Clone the repository into a case-sensitive volume.
    2. Source macos.zsh to set up the PATH for Homebrew tools.
    3. Run ulimit -n 65536 to raise the open files limit.
    4. It is recommended to run make check-binutils first to verify the environment before a full build.
    source macos.zsh
    ./configure --prefix=/Volumes/case-sensitive/opt/riscv --with-arch=rv64gc_zifencei --with-abi=lp64d --enable-linux --disable-gdb
    ulimit -n 65536
    make check-binutils
    make linux