最近尝试根据https://github.com/riscv-collab/riscv-gnu-toolchain上的步骤来编译支持多arch和abi的risc-v toolchain。目标是同时支持生成Ubuntu和Windows上都能用的工具链。
以下是经过摸索后可以用来生成linux和windows的工具链的脚本。脚本分多个部分:
安装依赖包
# Install dependenceis first
sudo apt install -y autoconf automake autotools-dev curl \
python3 python3-pip \
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
# Install cross-compiling toolchains
sudo apt install -y autoconf automake autotools-dev bc bison \
build-essential curl dejagnu expect flex gawk gperf \
libtool patchutils texinfo python3 zip \
mingw-w64 gdb-mingw-w64 libz-mingw-w64-dev
编译Windows依赖的库
#!/bin/bash
########################################################################
#
#
# Resolve dependencies on Windows OS first
#
#
########################################################################
BUILD_HOST=x86_64-w64-mingw32
if [ ! -f gmp-6.2.1.tar.xz ]; then
wget https://gmplib.org/download/gmp/gmp-6.2.1.tar.xz
fi
if [ ! -f "expat-2.4.8.tar.bz2" ]; then
wget https://github.com/libexpat/libexpat/releases/download/R_2_4_8/expat-2.4.8.tar.bz2
fi
if [ ! -f "gmp-6.2.1.tar.xz" ]; then
wget https://gmplib.org/download/gmp/gmp-6.2.1.tar.xz
fi
if [ ! -f "mpfr-4.2.0.tar.bz2" ]; then
wget https://www.mpfr.org/mpfr-current/mpfr-4.2.0.tar.bz2
fi
if [ ! -f "mpc-1.3.1.tar.gz" ]; then
wget https://ftp.gnu.org/gnu/mpc/mpc-1.3.1.tar.gz
fi
# Build GMP
tar -xf gmp-6.2.1.tar.xz
cd gmp-6.2.1
./configure --host=${BUILD_HOST} --prefix=/usr/${BUILD_HOST} LDFLAGS="-static" --enable-static --disable-shared
make clean
make -j8
sudo make install
cd ..
# Build MPFR
tar -jxvf mpfr-4.2.0.tar.bz2
cd mpfr-4.2.0
./configure --host=${BUILD_HOST} --prefix=/usr/${BUILD_HOST} LDFLAGS="-static" --enable-static --disable-shared
make clean
make -j8
sudo make install
cd ..
# Build MPC
tar -zxvf mpc-1.3.1.tar.gz
cd mpc-1.3.1
./configure --host=${BUILD_HOST} --prefix=/usr/${BUILD_HOST} LDFLAGS="-static" --enable-static --disable-shared
make clean
make -j8
sudo make install
cd ..
# Build expat
tar -jxvf expat-2.4.8.tar.bz2
cd expat-2.4.8
./configure --host=${BUILD_HOST} --prefix=/usr/${BUILD_HOST} LDFLAGS="-static" --enable-static --disable-shared
make clean
make -j8
sudo make install
cd ..
克隆代码仓库并切换到对应的tag
git clone https://github.com/riscv-collab/riscv-gnu-toolchain.git -b 2023.07.07
cd riscv-gnu-toolchain
正式编译部分
PREFIX_COMMON=${HOME}/riscv32-unknown-elf-gcc-${TOOLCHAIN_TAG}
TARGET=riscv32-unknown-elf
MULTILIB_LIST='rv32imac-ilp32--;\
rv32imafc-ilp32--;rv32imafc-ilp32f--;\
rv32imafdc-ilp32--;rv32imafdc-ilp32f--;rv32imafdc-ilp32d--'
########################################################################
#
#
# Build Linux toolchain
#
#
########################################################################
BUILD_HOST=x86_64-linux-gnu
PREFIX=${PREFIX_COMMON}-linux
cd gcc
./contrib/download_prerequisites
cd ..
if [ -d ${PREFIX} ]; then
rm -rf ${PREFIX}
fi
make distclean
#export PATH=${PATH}:${PREFIX}:${PREFIX}/bin
./configure --prefix=${PREFIX} \
--build={BUILD_HOST} \
--target=${TARGET} \
--with-isa-spec=2.2 \
--with-abi=ilp32 \
--with-arch=rv32imac\
--enable-multilib \
--with-multilib-generator="${MULTILIB_LIST}" \
CFLAGS_FOR_TARGET='-Os -mstrict-align -mcmodel=medlow ' \
CXXFLAGS_FOR_TARGET='-Os -mstrict-align -mcmodel=medlow ' \
LDFLAGS="-static" \
--with-gcc-src=`pwd`/gcc
make -j16
# Strip
strip -s `find $PREFIX/ -type f -executable`
########################################################################
#
#
# Build wondows toolchain
#
#
########################################################################
BUILD_HOST=x86_64-w64-mingw32
PREFIX=${PREFIX_COMMON}-win
export PATH=${PATH}:${PREFIX_COMMON}-linux:${PREFIX_COMMON}-linux/bin
cd gcc
./contrib/download_prerequisites
cd ..
if [ -d $PREFIX ]; then
rm -rf $PREFIX
fi
make clean
./configure --prefix=${PREFIX} \
--with-host=${BUILD_HOST} \
--target=${TARGET} \
--with-isa-spec=2.2 \
--with-abi=ilp32 \
--with-arch=rv32imac\
--enable-multilib \
--with-multilib-generator="${MULTILIB_LIST}" \
CFLAGS_FOR_TARGET='-Os -mstrict-align -mcmodel=medlow ' \
CXXFLAGS_FOR_TARGET='-Os -mstrict-align -mcmodel=medlow ' \
LDFLAGS="-static"
--with-gcc-src=`pwd`/gcc
make -j16
if [ ! -d build-gdb-newlib ]; then
mkdir build-gdb-newlib
fi
cd build-gdb-newlib
make distclean
../gdb/configure --target=${TARGET} \
--host=${BUILD_HOST} \
--prefix=${PREFIX} \
--disable-werror --with-expat=yes --enable-gdb --disable-gas --disable-binutils --disable-ld --disable-gold --disable-gprof \
--disable-shared --with-static-standard-libraries --disable-source-highlight LDFLAGS="-static"
make -j16
make install
cd $PREFIX
# Strip unused symbols in toolchain binaries
for i in `find . -name *.dll`; do x86_64-w64-mingw32-strip -s $i ; done
for i in `find . -name *.exe`; do x86_64-w64-mingw32-strip -s $i ; done
cd ..