install_musl_gcc.sh 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #########################################################################
  2. # 这个脚本用于安装musl交叉编译工具链
  3. # 该脚本会自动下载musl交叉编译工具链,并将其添加到PATH中
  4. #########################################################################
  5. export USE_GITHUB=${USE_GITHUB:=0}
  6. MUSL_GCC_DATE="231114"
  7. MUSL_GCC_VERSION="9.4.0"
  8. MUSL_GCC_X86_64_TAR=
  9. MUSL_GCC_RISCV64_TAR=
  10. MUSL_GCC_X86_64_DOWNLOAD_URL=""
  11. MUSL_GCC_RISCV64_DOWNLOAD_URL=""
  12. if [ $USE_GITHUB -eq 1 ]; then
  13. echo "Download from github"
  14. MUSL_GCC_X86_64_TAR=x86_64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}.tar.xz
  15. MUSL_GCC_RISCV64_TAR=riscv64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}.tar.xz
  16. MUSL_GCC_X86_64_DOWNLOAD_URL="https://github.com/DragonOS-Community/musl-cross-make/releases/download/${MUSL_GCC_VERSION}-${MUSL_GCC_DATE}/${MUSL_GCC_X86_64_TAR}"
  17. MUSL_GCC_RISCV64_DOWNLOAD_URL="https://github.com/DragonOS-Community/musl-cross-make/releases/download/${MUSL_GCC_VERSION}-${MUSL_GCC_DATE}/${MUSL_GCC_RISCV64_TAR}"
  18. https://github.com/DragonOS-Community/musl-cross-make/releases/download/9.4.0-231114/riscv64-linux-musl-cross-gcc-9.4.0.tar.xz
  19. else
  20. echo "Download from mirrors.dragonos.org.cn"
  21. MUSL_GCC_X86_64_TAR="x86_64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}-${MUSL_GCC_DATE}.tar.xz"
  22. MUSL_GCC_RISCV64_TAR="riscv64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}-${MUSL_GCC_DATE}.tar.xz"
  23. MUSL_GCC_X86_64_DOWNLOAD_URL="https://mirrors.dragonos.org.cn/pub/third_party/toolchain/gcc/${MUSL_GCC_X86_64_TAR}"
  24. MUSL_GCC_RISCV64_DOWNLOAD_URL="https://mirrors.dragonos.org.cn/pub/third_party/toolchain/gcc/${MUSL_GCC_RISCV64_TAR}"
  25. fi
  26. INSTALL_POS="$HOME/opt/"
  27. mkdir -p $INSTALL_POS
  28. get_shell_rc_file()
  29. {
  30. if [ -n "$ZSH_VERSION" ]; then
  31. echo "$HOME/.zshrc"
  32. elif [ -n "$BASH_VERSION" ]; then
  33. echo "$HOME/.bashrc"
  34. else
  35. echo "$HOME/.profile"
  36. fi
  37. }
  38. # 信号退出时清理下载的文件
  39. trap_handler(){
  40. rm -f $MUSL_GCC_X86_64_TAR
  41. rm -f $MUSL_GCC_RISCV64_TAR
  42. }
  43. trap trap_handler EXIT
  44. trap trap_handler SIGINT
  45. SHELL_RC=$(get_shell_rc_file)
  46. source $SHELL_RC
  47. # 下载musl交叉编译工具链
  48. # 如果x86_64-linux-musl-gcc或x86_64-linux-musl-g++不存在,则下载
  49. if [ ! -n "$(which x86_64-linux-musl-gcc)" ] || [ ! -n "$(which x86_64-linux-musl-g++)" ]; then
  50. echo "开始下载x86_64-linux-musl-gcc"
  51. wget ${MUSL_GCC_X86_64_DOWNLOAD_URL} || exit 1
  52. echo "下载完成"
  53. echo "开始解压x86_64-linux-musl-gcc"
  54. tar xvf $MUSL_GCC_X86_64_TAR -C $INSTALL_POS || exit 1
  55. echo "PATH=\$PATH:$INSTALL_POS/x86_64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}/bin" >> $SHELL_RC
  56. echo "安装完成"
  57. echo "开始清理x86_64-linux-musl-gcc的下载缓存"
  58. rm -rf $MUSL_GCC_X86_64_TAR || exit 1
  59. echo "清理完成"
  60. else
  61. echo "x86_64-linux-musl-gcc已经安装"
  62. fi
  63. # 如果riscv64-linux-musl-gcc或riscv64-linux-musl-g++不存在,则下载
  64. if [ ! -n "$(which riscv64-linux-musl-gcc)" ] || [ ! -n "$(which riscv64-linux-musl-g++)" ]; then
  65. echo "开始下载riscv64-linux-musl-gcc"
  66. wget ${MUSL_GCC_RISCV64_DOWNLOAD_URL} || exit 1
  67. echo "下载完成"
  68. echo "开始解压riscv64-linux-musl-gcc"
  69. tar xvf $MUSL_GCC_RISCV64_TAR -C $INSTALL_POS || exit 1
  70. echo "export PATH=\"\$PATH:$INSTALL_POS/riscv64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}/bin\"" >> $SHELL_RC
  71. echo "安装完成"
  72. echo "开始清理riscv64-linux-musl-gcc的下载缓存"
  73. rm -rf $MUSL_GCC_RISCV64_TAR || exit 1
  74. echo "清理完成"
  75. else
  76. echo "riscv64-linux-musl-gcc已经安装"
  77. fi
  78. source $SHELL_RC
  79. echo "musl交叉编译工具链安装完成,请运行 source $SHELL_RC 以使musl交叉编译工具链在当前窗口生效!"