Skip to content

Commit

Permalink
安装musl toolchain以及riscv相关的工具链
Browse files Browse the repository at this point in the history
  • Loading branch information
fslongjin committed Nov 20, 2023
1 parent 99dbf38 commit 83f8829
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 26 deletions.
15 changes: 8 additions & 7 deletions tools/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ install_ubuntu_debian_pkg()
lsb-release \
llvm-dev libclang-dev clang gcc-multilib \
gcc build-essential fdisk dosfstools dnsmasq bridge-utils iptables libssl-dev pkg-config \
musl-tools sphinx
sphinx

# 如果python3没有安装
if [ -z "$(which python3)" ]; then
Expand Down Expand Up @@ -85,7 +85,7 @@ install_archlinux_pkg()
curl wget bridge-utils dnsmasq \
diffutils pkgconf which unzip util-linux dosfstools \
gcc make flex texinfo gmp mpfr qemu-base \
libmpc libssl-dev musl
libmpc libssl-dev

}

Expand Down Expand Up @@ -156,16 +156,15 @@ rustInstall() {
fi
echo "正在安装DragonOS所需的rust组件...首次安装需要一些时间来更新索引,请耐心等待..."
cargo install cargo-binutils
rustup toolchain install nightly
rustup default nightly
rustup toolchain install nightly-2023-01-21-x86_64-unknown-linux-gnu
rustup toolchain install nightly-2023-08-15-x86_64-unknown-linux-gnu
rustup component add rust-src --toolchain nightly-2023-01-21-x86_64-unknown-linux-gnu
rustup component add rust-src --toolchain nightly-2023-08-15-x86_64-unknown-linux-gnu
rustup component add rust-src
rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
rustup component add llvm-tools-preview
rustup target add x86_64-unknown-none
rustup target add x86_64-unknown-none --toolchain nightly-2023-01-21-x86_64-unknown-linux-gnu
rustup target add x86_64-unknown-none --toolchain nightly-2023-08-15-x86_64-unknown-linux-gnu

echo "Rust已经成功的在您的计算机上安装!请运行 source ~/.cargo/env 以使rust在当前窗口生效!"
fi
Expand Down Expand Up @@ -277,9 +276,11 @@ cargo install dadk || exit 1
# 创建磁盘镜像
bash create_hdd_image.sh
# 编译安装GCC交叉编译工具链
bash build_gcc_toolchain.sh -cs
bash build_gcc_toolchain.sh -cs -kb -kg || (echo "GCC交叉编译工具链安装失败" && exit 1)
# 编译安装musl交叉编译工具链
bash install_musl_gcc.sh || (echo "musl交叉编译工具链安装失败" && exit 1)
# 编译安装grub
bash grub_auto_install.sh
bash grub_auto_install.sh || (echo "grub安装失败" && exit 1)

# 解决kvm权限问题
USR=$USER
Expand Down
3 changes: 2 additions & 1 deletion tools/build_gcc_toolchain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ cd $INSTALL_POS
# compile binutils
BIN_UTILS="binutils-2.38"
BIN_UTILS_TAR="${BIN_UTILS}.tar.gz"

if [[ ! -n "$(find $PREFIX/bin/ -name ${TARGET_LD})" && ! -n "$(find $PREFIX/bin/ -name ${TARGET_AS})" ]] || [ ${KEEP_BINUTILS} -ne 1 ]; then
if [ ${KEEP_BINUTILS} -eq 1 ]; then
echo -e "\033[35m 没有检测到 ${TARGET_LD} 或 没有检测到 ${TARGET_AS}, -kb参数无效 \033[0m"
Expand Down Expand Up @@ -170,7 +171,7 @@ fi


# update PATH
if [ -n "$(grep -F "export DragonOS_GCC=$PREFIX/bin/" "$HOME/.$(basename $SHELL)rc")" ]; then
if [ -n "$(grep -F "export DragonOS_GCC" "$HOME/.$(basename $SHELL)rc")" ]; then
echo "[info] DragonOS_GCC has been in the "'$PATH'
else
echo 'export DragonOS_GCC='"$PREFIX"'/bin' >> "$HOME/.$(basename $SHELL)rc"
Expand Down
68 changes: 50 additions & 18 deletions tools/grub_auto_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ABS_PREFIX=/opt/dragonos-grub
grub_dir_i386_efi=${ABS_PREFIX}/arch/i386/efi/grub
grub_dir_i386_legacy=${ABS_PREFIX}/arch/i386/legacy/grub
grub_dir_x86_64_efi=${ABS_PREFIX}/arch/x86_64/efi/grub
grub_dir_riscv64_efi=${ABS_PREFIX}/arch/riscv64/efi/grub

sudo mkdir -p ${grub_dir_i386_efi}
sudo mkdir -p ${grub_dir_i386_legacy}
Expand All @@ -17,13 +18,13 @@ export OBJCOPY=objcopy


#检测grub是否已经安装
if [ -d ${grub_dir_i386_efi}/bin ] && [ -d ${grub_dir_i386_legacy}/bin ] && [ -d ${grub_dir_x86_64_efi}/bin ] ; then
if [ -d ${grub_dir_i386_efi}/bin ] && [ -d ${grub_dir_i386_legacy}/bin ] && [ -d ${grub_dir_x86_64_efi}/bin ] && [ -d ${grub_dir_riscv64_efi}/bin ] ; then
exit 0
fi
#仅支持Ubuntu/Debain, Arch下的自动安装
supported_package_manager="apt-get pacman"
packages=("make binutils bison gcc gettext flex bison automake autoconf wget" \
"make binutils bison gcc gettext flex bison automake autoconf wget")
packages=("make binutils bison gcc gettext flex bison automake autoconf wget gawk" \
"make binutils bison gcc gettext flex bison automake autoconf wget gawk")
update_options=("update" \
"-Sy")
install_options=("install -y" \
Expand Down Expand Up @@ -60,25 +61,56 @@ sudo ${pm} ${install_options[$pm_index]} ${packages[$pm_index]}
cd grub-2.06
echo "开始安装grub2.06"
#编译安装三个版本的grub
./configure --target=i386 --prefix=${grub_dir_i386_legacy} --disable-werror || exit 1
make -j $(nproc) || exit 1
sudo make install || exit 1
make clean || exit 1

./configure --target=i386 --with-platform=efi --prefix=${grub_dir_i386_efi} --disable-werror || exit 1
make -j $(nproc) || exit 1
sudo make install || exit 1
make clean || exit 1
# 如果不存在i386_legacy的grub,就编译安装
if [ ! -d ${grub_dir_i386_legacy}/bin ]; then
echo "开始编译安装i386_legacy版本的grub"
./configure --target=i386 --prefix=${grub_dir_i386_legacy} --disable-werror || exit 1
make -j $(nproc) || exit 1
sudo make install || exit 1
make clean || exit 1
sudo chmod -R 777 ${grub_dir_i386_legacy}
fi

# 如果不存在i386_efi的grub,就编译安装
if [ ! -d ${grub_dir_i386_efi}/bin ]; then
echo "开始编译安装i386_efi版本的grub"
./configure --target=i386 --with-platform=efi --prefix=${grub_dir_i386_efi} --disable-werror || exit 1
make -j $(nproc) || exit 1
sudo make install || exit 1
make clean || exit 1
sudo chmod -R 777 ${grub_dir_i386_efi}
fi

# 如果不存在x86_64_efi的grub,就编译安装
if [ ! -d ${grub_dir_x86_64_efi}/bin ]; then
echo "开始编译安装x86_64_efi版本的grub"
./configure --target=x86_64 --with-platform=efi --prefix=${grub_dir_x86_64_efi} --disable-werror || exit 1
make -j $(nproc) || exit 1
sudo make install || exit 1
make clean || exit 1
sudo chmod -R 777 ${grub_dir_x86_64_efi}
fi

# 如果不存在riscv64_efi的grub,就编译安装
if [ ! -d ${grub_dir_riscv64_efi}/bin ]; then
echo "开始编译安装riscv64_efi版本的grub"
# 使用which命令判断,如果不存在riscv64-linux-musl交叉编译工具链,则报错
if [ ! -n "$(which riscv64-linux-musl-gcc)" ]; then
echo "riscv64-linux-musl-gcc不存在,请先安装riscv64-linux-musl交叉编译工具链"
exit 1
fi

./configure --target=riscv64 --with-platform=efi --prefix=${grub_dir_riscv64_efi} --host=x86_64-linux-gnu --disable-werror BUILD_CC=gcc HOST_CC=x86_64-linux-gnu-gcc TARGET_CC=riscv64-linux-musl-gcc TARGET_OBJCOPY=riscv64-linux-musl-objcopy TARGET_STRIP=riscv64-linux-musl-strip TARGET_RANLIB=riscv64-linux-musl-ranlib TARGET_NM=riscv64-linux-musl-nm TARGET_LD=riscv64-linux-musl-ld
make -j $(nproc) || exit 1
sudo make install || exit 1
make clean || exit 1
sudo chmod -R 777 ${grub_dir_riscv64_efi}
fi

./configure --target=x86_64 --with-platform=efi --prefix=${grub_dir_x86_64_efi} --disable-werror || exit 1
make -j $(nproc) || exit 1
sudo make install || exit 1

cd ..
#解除权限限制
sudo chmod -R 777 ${grub_dir_i386_legacy}
sudo chmod -R 777 ${grub_dir_i386_efi}
sudo chmod -R 777 ${grub_dir_x86_64_efi}

rm -rf grub-2.06
rm grub-2.06.tar.xz*
echo "grub2.06安装完成"
75 changes: 75 additions & 0 deletions tools/install_musl_gcc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#########################################################################
# 这个脚本用于安装musl交叉编译工具链
# 该脚本会自动下载musl交叉编译工具链,并将其添加到PATH中
#########################################################################

MUSL_GCC_DATE="231114"
MUSL_GCC_VERSION="9.4.0"
MUSL_GCC_X86_64_TAR="x86_64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}-${MUSL_GCC_DATE}.tar.xz"
MUSL_GCC_RISCV64_TAR="riscv64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}-${MUSL_GCC_DATE}.tar.xz"
MUSL_GCC_X86_64_DOWNLOAD_URL="https://mirrors.dragonos.org.cn/pub/third_party/toolchain/gcc/${MUSL_GCC_X86_64_TAR}"
MUSL_GCC_RISCV64_DOWNLOAD_URL="https://mirrors.dragonos.org.cn/pub/third_party/toolchain/gcc/${MUSL_GCC_RISCV64_TAR}"


INSTALL_POS="$HOME/opt/"

get_shell_rc_file()
{
if [ -n "$ZSH_VERSION" ]; then
echo "$HOME/.zshrc"
elif [ -n "$BASH_VERSION" ]; then
echo "$HOME/.bashrc"
else
echo "$HOME/.profile"
fi
}

# 信号退出时清理下载的文件
trap_handler(){
rm -f $MUSL_GCC_X86_64_TAR
rm -f $MUSL_GCC_RISCV64_TAR
}

trap trap_handler EXIT
trap trap_handler SIGINT


SHELL_RC=$(get_shell_rc_file)

# 下载musl交叉编译工具链

# 如果x86_64-linux-musl-gcc或x86_64-linux-musl-g++不存在,则下载
if [ ! -n "$(which x86_64-linux-musl-gcc)" ] || [ ! -n "$(which x86_64-linux-musl-g++)" ]; then
echo "开始下载x86_64-linux-musl-gcc"
wget ${MUSL_GCC_X86_64_DOWNLOAD_URL} || exit 1
echo "下载完成"
echo "开始解压x86_64-linux-musl-gcc"
tar xvf $MUSL_GCC_X86_64_TAR -C $INSTALL_POS || exit 1
echo "PATH=\$PATH:$INSTALL_POS/x86_64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}-${MUSL_GCC_DATE}/bin" >> $SHELL_RC
echo "安装完成"
echo "开始清理x86_64-linux-musl-gcc的下载缓存"
rm -rf $MUSL_GCC_X86_64_TAR || exit 1
echo "清理完成"
else
echo "x86_64-linux-musl-gcc已经安装"
fi

# 如果riscv64-linux-musl-gcc或riscv64-linux-musl-g++不存在,则下载
if [ ! -n "$(which riscv64-linux-musl-gcc)" ] || [ ! -n "$(which riscv64-linux-musl-g++)" ]; then
echo "开始下载riscv64-linux-musl-gcc"
wget ${MUSL_GCC_RISCV64_DOWNLOAD_URL} || exit 1
echo "下载完成"
echo "开始解压riscv64-linux-musl-gcc"
tar xvf $MUSL_GCC_RISCV64_TAR -C $INSTALL_POS || exit 1
echo "export PATH=\"\$PATH:$INSTALL_POS/riscv64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}/bin\"" >> $SHELL_RC
echo "安装完成"
echo "开始清理riscv64-linux-musl-gcc的下载缓存"
rm -rf $MUSL_GCC_RISCV64_TAR || exit 1
echo "清理完成"
else
echo "riscv64-linux-musl-gcc已经安装"
fi

source $SHELL_RC

echo "musl交叉编译工具链安装完成,请运行 source $SHELL_RC 以使musl交叉编译工具链在当前窗口生效!"

0 comments on commit 83f8829

Please sign in to comment.