From 83f8829e698471fde9ea7160d60e7db967578f1a Mon Sep 17 00:00:00 2001 From: longjin Date: Mon, 20 Nov 2023 05:50:01 +0000 Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E8=A3=85musl=20toolchain=E4=BB=A5?= =?UTF-8?q?=E5=8F=8Ariscv=E7=9B=B8=E5=85=B3=E7=9A=84=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/bootstrap.sh | 15 ++++---- tools/build_gcc_toolchain.sh | 3 +- tools/grub_auto_install.sh | 68 +++++++++++++++++++++++--------- tools/install_musl_gcc.sh | 75 ++++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 tools/install_musl_gcc.sh diff --git a/tools/bootstrap.sh b/tools/bootstrap.sh index b3389fe6b..76798ea8a 100644 --- a/tools/bootstrap.sh +++ b/tools/bootstrap.sh @@ -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 @@ -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 } @@ -156,8 +156,6 @@ 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 @@ -165,7 +163,8 @@ rustInstall() { 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 @@ -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 diff --git a/tools/build_gcc_toolchain.sh b/tools/build_gcc_toolchain.sh index c0f187fee..3674e1645 100644 --- a/tools/build_gcc_toolchain.sh +++ b/tools/build_gcc_toolchain.sh @@ -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" @@ -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" diff --git a/tools/grub_auto_install.sh b/tools/grub_auto_install.sh index d2f4d9f27..a65ebfc6a 100644 --- a/tools/grub_auto_install.sh +++ b/tools/grub_auto_install.sh @@ -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} @@ -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" \ @@ -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安装完成" diff --git a/tools/install_musl_gcc.sh b/tools/install_musl_gcc.sh new file mode 100644 index 000000000..54b5621e3 --- /dev/null +++ b/tools/install_musl_gcc.sh @@ -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交叉编译工具链在当前窗口生效!"