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