xref: /DragonOS/tools/install_musl_gcc.sh (revision c75089286e9d49cef8d039446bf570c1bd4d2550)
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)
38
39# 下载musl交叉编译工具链
40
41# 如果x86_64-linux-musl-gcc或x86_64-linux-musl-g++不存在,则下载
42if [ ! -n "$(which x86_64-linux-musl-gcc)" ] || [ ! -n "$(which x86_64-linux-musl-g++)" ]; then
43    echo "开始下载x86_64-linux-musl-gcc"
44    wget ${MUSL_GCC_X86_64_DOWNLOAD_URL} || exit 1
45    echo "下载完成"
46    echo "开始解压x86_64-linux-musl-gcc"
47    tar xvf $MUSL_GCC_X86_64_TAR -C $INSTALL_POS || exit 1
48    echo "PATH=\$PATH:$INSTALL_POS/x86_64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}-${MUSL_GCC_DATE}/bin" >> $SHELL_RC
49    echo "安装完成"
50    echo "开始清理x86_64-linux-musl-gcc的下载缓存"
51    rm -rf $MUSL_GCC_X86_64_TAR || exit 1
52    echo "清理完成"
53else
54    echo "x86_64-linux-musl-gcc已经安装"
55fi
56
57# 如果riscv64-linux-musl-gcc或riscv64-linux-musl-g++不存在,则下载
58if [ ! -n "$(which riscv64-linux-musl-gcc)" ] || [ ! -n "$(which riscv64-linux-musl-g++)" ]; then
59    echo "开始下载riscv64-linux-musl-gcc"
60    wget ${MUSL_GCC_RISCV64_DOWNLOAD_URL} || exit 1
61    echo "下载完成"
62    echo "开始解压riscv64-linux-musl-gcc"
63    tar xvf $MUSL_GCC_RISCV64_TAR -C $INSTALL_POS || exit 1
64    echo "export PATH=\"\$PATH:$INSTALL_POS/riscv64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}/bin\"" >> $SHELL_RC
65    echo "安装完成"
66    echo "开始清理riscv64-linux-musl-gcc的下载缓存"
67    rm -rf $MUSL_GCC_RISCV64_TAR || exit 1
68    echo "清理完成"
69else
70    echo "riscv64-linux-musl-gcc已经安装"
71fi
72
73source $SHELL_RC
74
75echo "musl交叉编译工具链安装完成,请运行 source $SHELL_RC 以使musl交叉编译工具链在当前窗口生效!"
76