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