FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

Official GCCでのRISC-Vコンパイル環境構築方法 (Linux Cross-compilation向け)

RISC-V 向けGCCを作るのに、glibcを用いたcross-compilerの作り方をメモしておく。

  • Binutilsのコンパイル
curl -L ftp://ftp.gnu.org/gnu/binutils/binutils-2.40.tar.gz | tar xz && \
    cd binutils-2.40 && \
    mkdir build && \
    cd build && \
    ../configure --prefix=${RISCV} \
    --target=riscv64-unknown-linux-gnu \
    --enable-languages=c,c++ \
    --disable-multilib && make -j$(nproc) && \
    make install && \
    rm -rf build
  • Linux-headersのインストール
curl -L https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.4.tar.gz | tar xz && \
    cd linux-6.4 && \
    make ARCH=riscv INSTALL_HDR_PATH=${RISCV}/sysroot/usr headers_install
  • GCCのビルド(1回目)
curl -L http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-13.1.0/gcc-13.1.0.tar.gz | tar xz && \
    mkdir -p gcc-13.1.0/build-gcc-linux-stage1 && \
    cd gcc-13.1.0/build-gcc-linux-stage1 && \
    ../configure --prefix=${RISCV} \
    --target=riscv64-unknown-linux-gnu \
    --with-sysroot=${RISCV}/sysroot \
    --with-newlib \
    --without-headers \
    --disable-shared \
    --disable-threads \
    --with-system-zlib \
    --enable-tls \
    --enable-languages=c \
    --disable-libatomic \
    --disable-libmudflap \
    --disable-libssp \
    --disable-libquadmath \
    --disable-libgomp \
    --disable-nls \
    --disable-bootstrap \
    --src=../ \
    --disable-multilib \
    CFLAGS_FOR_TARGET="-O2   -mcmodel=medlow" \
    CXXFLAGS_FOR_TARGET="-O2   -mcmodel=medlow" && \
    make -j$(nproc) && \
    make install
  • Glibcのビルド
curl -L https://ftp.gnu.org/gnu/glibc/glibc-2.38.tar.gz | tar xz
cd glibc-2.38 && \
    mkdir -p build-glibc-linux-rv64imafdc-lp64d && \
    cd build-glibc-linux-rv64imafdc-lp64d && \
    LD_LIBRARY_PATH="" \
    CC="riscv64-unknown-linux-gnu-gcc " \
    CXX="this-is-not-the-compiler-youre-looking-for" \
    CFLAGS="  -mcmodel=medlow -g -O2 " \
    CXXFLAGS="  -mcmodel=medlow -g -O2 " \
    ASFLAGS=" -mcmodel=medlow " \
    ../configure \
    --host=riscv64-unknown-linux-gnu \
    --prefix=/usr \
    --disable-werror \
    --enable-shared \
    --enable-obsolete-rpc \
    --with-headers=${RISCV}/sysroot/usr/include \
    --disable-multilib \
    --enable-kernel=3.0.0 \
    --libdir=/usr/lib libc_cv_slibdir=/lib libc_cv_rtlddir=/lib && \
     make -j$(nproc) && \
     make install install_root=${RISCV}/sysroot && \
     make install-headers install_root=${RISCV}/sysroot
  • GCCのビルド(2回目)
mkdir -p gcc-13.1.0/build-gcc-linux-stage2 && \
    cd gcc-13.1.0/build-gcc-linux-stage2 && \
    ../configure --prefix=${RISCV} \
    --target=riscv64-unknown-linux-gnu \
    --with-sysroot=${RISCV}/sysroot \
    --with-system-zlib \
    --enable-shared \
    --enable-tls \
    --enable-languages=c,c++,fortran \
    --disable-libmudflap \
    --disable-libssp \
    --disable-libquadmath \
    --disable-libsanitizer \
    --disable-nls \
    --disable-bootstrap \
    --disable-multilib \
    CFLAGS_FOR_TARGET="-O2   -mcmodel=medlow" \
    CXXFLAGS_FOR_TARGET="-O2   -mcmodel=medlow" && \
    make -j$(nproc) && \
    make install

Baremetal向け:

msyksphinz.hatenablog.com