RISC-V GNU toolchain debug for OpenJDK 8 on riscv64 environment

示例于 Ubuntu 22.04 LTS 上. 对于中国大陆的小伙伴来说,所有的 github.com 可以用 kgithub.com 代替.

Step 0: 预备

sudo apt 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 git \
                 libglib2.0-dev libfdt-dev libpixman-1-dev \
                 libncurses5-dev libncursesw5-dev ninja-build \
                 python3 autopoint pkg-config zip unzip screen \
                 make libxext-dev libxrender-dev libxtst-dev \
                 libxt-dev libcups2-dev libfreetype6-dev \
                 mercurial libasound2-dev cmake libfontconfig1-dev

Step 1: Toolchain 的编译准备

git clone https://github.com/riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain
git submodule update --init --recursive

Step 2: 编译 ToolChain

在这一步的时候因为源码都是从海外站点获取,中国大陆的小伙伴可能连接不上.此时我们善用镜像,编辑 .gitmodules 这个文件:

[submodule "binutils"]
        path = binutils
        url = https://gitee.com/mirrors/binutils-gdb.git
        branch = binutils-2_39-branch
[submodule "gcc"]
        path = gcc
        url = https://gitee.com/mirrors/gcc.git
        branch = releases/gcc-12
[submodule "glibc"]
        path = glibc
        url = https://kgithub.com/bminor/glibc.git
[submodule "dejagnu"]
        path = dejagnu
        url = https://kgithub.com/MIPS/dejagnu.git
        branch = dejagnu-1.6.3
[submodule "newlib"]
        path = newlib
        url = https://gitee.com/mirrors/newlib-cygwin.git
        branch = master
[submodule "gdb"]
        path = gdb
        url = https://gitee.com/mirrors/binutils-gdb.git
        branch = gdb-12-branch
[submodule "qemu"]
        path = qemu
        url = https://gitee.com/mirrors/qemu.git
[submodule "musl"]
        path = musl
        url = https://gitee.com/mirrors/musl.git
        branch = master
[submodule "spike"]
        path = spike
        url = https://github.com/riscv-software-src/riscv-isa-sim.git
        branch = master
[submodule "pk"]
        path = pk
        url = https://github.com/riscv-software-src/riscv-pk.git
        branch = master

然后再用 git submodule sync --recursive 这个命令同步一下即可.

mkdir ../riscv64
./configure --prefix=/home/ubuntu/riscv64
make linux -j

注意,如果是编译 jdk8 的话,可能需要较老版本的 glibc,先回退再编译: git checkout glibc-2.34

编译成功后写入你的环境变量:

export PATH="/home/ubuntu/riscv64/bin:$PATH"

Step 3: 编译 QEMU

Ubuntu 22.04 已经内置 QEMU 7,可不需要这一步.

cd ~
git clone https://github.com/qemu/qemu.git
cd qemu
./configure \
  --static \
  --enable-attr \
  --enable-tcg \
  --enable-linux-user \
  --target-list=riscv64-linux-user \
  --without-default-devices \
  --without-default-features \
  --disable-install-blobs \
  --disable-debug-info \
  --disable-debug-tcg \
  --disable-debug-mutex
make -j$(nproc)
sudo make install

Step 4: 编译额外库

我们需要安装一些额外库的 RISC-V 版本.

#!/bin/bash

mkdir build_ext_libs_riscv64 && cd build_ext_libs_riscv64

git clone --depth=1 https://github.com/libffi/libffi

git clone --depth=1 https://github.com/apple/cups

git clone --depth=1 https://github.com/libexpat/libexpat

git clone --depth=1 https://github.com/madler/zlib

git clone --depth=1 https://github.com/glennrp/libpng

wget https://download.savannah.gnu.org/releases/freetype/freetype-2.10.4.tar.gz && tar -xzvf freetype-2.10.4.tar.gz && mv freetype-2.10.4 freetype2 && rm -f freetype-2.10.4.tar.gz

git clone -b json-c-0.13 --depth=1 https://github.com/json-c/json-c

git clone --depth=1 https://gitlab.freedesktop.org/fontconfig/fontconfig

git clone --depth=1 https://github.com/alsa-project/alsa-lib

git clone --depth=1 https://github.com/karelzak/util-linux

mkdir xorg && cd xorg && wget https://gist.githubusercontent.com/misaka00251/a8990410d9e0fb565aa7cf7992d178d1/raw/749dfce2dfc1f116c8ebec536dcc7bfe4a1011b7/xorg_modules && git clone --depth=1 https://github.com/misaka00251/xorg-util-modular.git util/modular

cd ..

然后,在刚刚创建的 build_ext_libs_risv64 里面保存如下脚本

#!/bin/bash

# exit on error
set -e

if [ ! -n "$1" ];then
    echo "Please designate riscv toolchain path"
    exit 1
else
    riscvpath=$1
    echo "riscv toolchian path was set as: $riscvpath"
fi

export PATH=$riscvpath/bin:$PATH
export sysroot=$riscvpath/sysroot
export prefix=$sysroot/usr

# libffi
cd libffi && ./autogen.sh && ./configure --host=riscv64-unknown-linux-gnu --prefix=$prefix 

make && make install

cd -

# cups
cd cups && ./configure --host=riscv64-unknown-linux-gnu --disable-ssl --disable-gssapi --disable-avahi --disable-libusb --disable-dbus --disable-systemd

make CFLAGS="-Wno-error=sign-conversion -Wno-error=format-truncation" CXXFLAGS="-Wno-error=sign-conversion -Wno-error=format-truncation" && make install DSTROOT=$sysroot

cd -

# libexpat
cd libexpat/expat && ./buildconf.sh &&./configure --host=riscv64-unknown-linux-gnu --prefix=$prefix

make && make install

cd -

# zlib
cd zlib && CHOST=riscv64 CC=riscv64-unknown-linux-gnu-gcc AR=riscv64-unknown-linux-gnu-ar RANLIB=riscv64-unknown-linux-gnu-ranlib ./configure  --prefix=$prefix

make && make install

cd -

# libpng
cd libpng && ./configure --host=riscv64-unknown-linux-gnu --prefix=$prefix

make && make install

cd -

# freetype2
cd freetype2 && ./autogen.sh && ./configure --host=riscv64-unknown-linux-gnu --prefix=$prefix --with-brotli=no --with-harfbuzz=no --with-bzip2=no

make && make install

cd -

# json-c
cd json-c && ./autogen.sh &&  ./configure --host=riscv64-unknown-linux-gnu --prefix=$prefix

make && make install

cd -

# fontconfig
cd fontconfig && PKG_CONFIG_PATH=$prefix/lib/pkgconfig ./autogen.sh --host=riscv64-unknown-linux-gnu --prefix=$prefix

make && make install

cd -

# alsa-lib
cd alsa-lib && libtoolize --force --copy --automake && aclocal && autoheader && automake --foreign --copy --add-missing && autoconf && ./configure --host=riscv64-unknown-linux-gnu --prefix=$prefix

make && make install

cd -

# util-linux
cd util-linux && ./autogen.sh && ./configure --host=riscv64-unknown-linux-gnu --prefix=$prefix --disable-all-programs --enable-libuuid

make && make install || true

cd -

# xorg
cd xorg && CONFFLAGS="--host=riscv64-unknown-linux-gnu --disable-malloc0returnsnull" ./util/modular/build.sh --modfile ./xorg_modules --clone $prefix

echo "Success. exit"

执行的时候要输入工具链的完整路径:

sh build.sh /home/ubuntu/riscv64

如果喜欢本文,欢迎点击下方的「鼓掌」按钮!

如果上面没有加载出任何东西,可以点击这里