xref: /DragonOS/tools/install_musl_gcc.sh (revision 52da9a59374752b4d01907b052135a0d317781dd)
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
36get_shell_rc_file()
37{
38    if [ -n "$ZSH_VERSION" ]; then
39        echo "$HOME/.zshrc"
40    elif [ -n "$BASH_VERSION" ]; then
41        echo "$HOME/.bashrc"
42    else
43        echo "$HOME/.profile"
44    fi
45}
46
47# 信号退出时清理下载的文件
48trap_handler(){
49    rm -f $MUSL_GCC_X86_64_TAR
50    rm -f $MUSL_GCC_RISCV64_TAR
51}
52
53trap trap_handler EXIT
54trap trap_handler SIGINT
55
56
57SHELL_RC=$(get_shell_rc_file)
58source $SHELL_RC
59
60# 下载musl交叉编译工具链
61
62# 如果x86_64-linux-musl-gcc或x86_64-linux-musl-g++不存在,则下载
63if [ ! -n "$(which x86_64-linux-musl-gcc)" ] || [ ! -n "$(which x86_64-linux-musl-g++)" ]; then
64    echo "开始下载x86_64-linux-musl-gcc"
65    wget ${MUSL_GCC_X86_64_DOWNLOAD_URL} || exit 1
66    echo "下载完成"
67    echo "开始解压x86_64-linux-musl-gcc"
68    tar xvf $MUSL_GCC_X86_64_TAR -C $INSTALL_POS || exit 1
69    echo "PATH=\$PATH:$INSTALL_POS/x86_64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}/bin" >> $SHELL_RC
70    echo "安装完成"
71    echo "开始清理x86_64-linux-musl-gcc的下载缓存"
72    rm -rf $MUSL_GCC_X86_64_TAR || exit 1
73    echo "清理完成"
74else
75    echo "x86_64-linux-musl-gcc已经安装"
76fi
77
78# 如果riscv64-linux-musl-gcc或riscv64-linux-musl-g++不存在,则下载
79if [ ! -n "$(which riscv64-linux-musl-gcc)" ] || [ ! -n "$(which riscv64-linux-musl-g++)" ]; then
80    echo "开始下载riscv64-linux-musl-gcc"
81    wget ${MUSL_GCC_RISCV64_DOWNLOAD_URL} || exit 1
82    echo "下载完成"
83    echo "开始解压riscv64-linux-musl-gcc"
84    tar xvf $MUSL_GCC_RISCV64_TAR -C $INSTALL_POS || exit 1
85    echo "export PATH=\"\$PATH:$INSTALL_POS/riscv64-linux-musl-cross-gcc-${MUSL_GCC_VERSION}/bin\"" >> $SHELL_RC
86    echo "安装完成"
87    echo "开始清理riscv64-linux-musl-gcc的下载缓存"
88    rm -rf $MUSL_GCC_RISCV64_TAR || exit 1
89    echo "清理完成"
90else
91    echo "riscv64-linux-musl-gcc已经安装"
92fi
93
94source $SHELL_RC
95
96echo "musl交叉编译工具链安装完成,请运行 source $SHELL_RC 以使musl交叉编译工具链在当前窗口生效!"
97