build_gcc_toolchain.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/bin/bash
  2. if test -n "$ZSH_VERSION"; then
  3. CURRENT_SHELL=zsh
  4. elif test -n "$BASH_VERSION"; then
  5. CURRENT_SHELL=bash
  6. elif test -n "$KSH_VERSION"; then
  7. CURRENT_SHELL=ksh
  8. elif test -n "$FCEDIT"; then
  9. CURRENT_SHELL=ksh
  10. elif test -n "$PS3"; then
  11. CURRENT_SHELL=unknown
  12. else
  13. CURRENT_SHELL=sh
  14. fi
  15. source "$HOME/.$CURRENT_SHELL"rc
  16. # init something here
  17. current_path=$PATH
  18. current_pwd=$PWD
  19. # 不建议自行选择安装的位置, 如果要修改请自行修改 INSTALL_POS
  20. STRUCTURE="x86_64" # 这里选择 x86_64 (64位),而不是选择 i686 架构(32位)
  21. INSTALL_POS="$HOME/opt/dragonos-gcc"
  22. PREFIX="$INSTALL_POS/gcc-$STRUCTURE-unknown-none"
  23. TARGET="${STRUCTURE}-elf"
  24. PATH="$PREFIX/bin:$PATH"
  25. TARGET_GCC="$STRUCTURE-elf-gcc"
  26. TARGET_LD="$STRUCTURE-elf-ld"
  27. TARGET_AS="$STRUCTURE-elf-as"
  28. # 获取选项
  29. KEEP_BINUTILS=0
  30. KEEP_GCC=0
  31. CHANGE_SOURCE=0
  32. FORCE=0
  33. SAVE_CACHE=0
  34. while true; do
  35. if [ ! -n "$1" ]; then
  36. break
  37. fi
  38. case "$1" in
  39. "-save-cache")
  40. SAVE_CACHE=1
  41. ;;
  42. "-rebuild")
  43. echo "清除${INSTALL_POS}目录下的所有信息"
  44. rm -rf "${INSTALL_POS}"
  45. ;;
  46. "-kb")
  47. KEEP_BINUTILS=1
  48. ;;
  49. "-kg")
  50. KEEP_GCC=1
  51. ;;
  52. "-cs")
  53. CHANGE_SOURCE=1
  54. ;;
  55. "-f")
  56. FORCE=1
  57. ;;
  58. "-help")
  59. echo "脚本选项如下:"
  60. echo "-save-cache: 保留最后的下载压缩包"
  61. echo "-rebuild: 清除上一次安装的全部信息, 即删掉$INSTALL_POS目录下的所有内容, 然后重新构建gcc工具链."
  62. echo "-kg(keep-gcc): 您确保${STRUCTURE}-gcc已被编译安装, 本次调用脚本不重复编译安装gcc. 如果没有安装,脚本仍然会自动安装."
  63. echo "-kb(keep-binutils): 您确保binutils已被编译安装, 本次调用脚本不重复编译安装binutils. 如果没有安装,脚本仍然会自动安装."
  64. echo "-cs(change source): 如果包含该选项, 使用清华源下载gcc和binutils. 否则默认官方源."
  65. echo "-f(force): 如果包含该选项, 可以强制使用root权限安装在/root/目录下."
  66. ;;
  67. *)
  68. echo "不认识参数$1"
  69. ;;
  70. esac
  71. shift 1
  72. done
  73. # check: Don't install the gcc-toolchain in /root/*
  74. if [ "${HOME:0:5}" = "/root" ] && [ $FORCE -eq 0 ]; then
  75. echo -e "\033[35m 不要把GCC交叉编译工具链安装在/root/目录下, 或者请不要使用sudo \033[0m"
  76. echo -e "\033[35m gcc交叉编译工具链默认安装在: /home/<your_usr_name>/opt/dragonos-gcc/ \033[0m"
  77. echo -e "\033[35m 如果想要在/root/目录下安装(或者您的操作系统只有root用户), 请使用指令: sudo bash build_gcc_toolchain.sh -f \033[0m"
  78. exit 0
  79. else
  80. # 安装开始[提示]
  81. echo -e "\033[35m [开始安装] \033[0m"
  82. echo -e "\033[33m gcc交叉编译工具链默认安装在: /home/<your_usr_name>/opt/dragonos-gcc/, 整个过程耗时: 5-30mins \033[0m"
  83. sleep 0.3s
  84. fi
  85. # install prerequisited
  86. # 注意texinfo和binutils的版本是否匹配
  87. # 注意gmp/mpc/mpfr和gcc/g++的版本是否匹配
  88. echo "Start installing prerequisited packages"
  89. case `cat /etc/os-release | grep '^NAME=' | cut -d'"' -f2` in
  90. "Debian"* | "Ubuntu"*)
  91. sudo apt-get install -y \
  92. g++ \
  93. gcc \
  94. make \
  95. texinfo \
  96. libgmp3-dev \
  97. libmpc-dev \
  98. libmpfr-dev \
  99. flex \
  100. wget
  101. ;;
  102. "Arch"*)
  103. sudo pacman -S --needed --noconfirm \
  104. gcc make flex wget texinfo libmpc gmp mpfr \
  105. diffutils pkgconf which unzip
  106. ;;
  107. *)
  108. ;;
  109. esac
  110. # build the workspace
  111. mkdir -p $HOME/opt
  112. mkdir -p $INSTALL_POS
  113. mkdir -p $PREFIX
  114. cd $INSTALL_POS
  115. # compile binutils
  116. BIN_UTILS="binutils-2.38"
  117. BIN_UTILS_TAR="${BIN_UTILS}.tar.gz"
  118. if [[ ! -n "$(find $PREFIX/bin/ -name ${TARGET_LD})" && ! -n "$(find $PREFIX/bin/ -name ${TARGET_AS})" ]] || [ ${KEEP_BINUTILS} -ne 1 ]; then
  119. if [ ${KEEP_BINUTILS} -eq 1 ]; then
  120. echo -e "\033[35m 没有检测到 ${TARGET_LD} 或 没有检测到 ${TARGET_AS}, -kb参数无效 \033[0m"
  121. echo -e "\033[35m 开始安装binutils \033[0m"
  122. sleep 1s
  123. fi
  124. if [ ! -d "$BIN_UTILS" ]; then
  125. if [ ! -f "$BIN_UTILS_TAR" ]; then
  126. echo -e "\033[33m [提醒] 如果使用的是国外源, 下载时间可能偏久. 如果需要使用清华源, 请以输入参数-cs, 即: bash build_gcc_toolchain.sh -cs \033[0m "
  127. if [ ${CHANGE_SOURCE} -eq 1 ]; then
  128. # 国内源
  129. wget "https://mirrors.ustc.edu.cn/gnu/binutils/${BIN_UTILS_TAR}" -P "$INSTALL_POS"
  130. else
  131. # 官方网站
  132. wget https://ftp.gnu.org/gnu/binutils/${BIN_UTILS_TAR} -P "$INSTALL_POS"
  133. fi
  134. fi
  135. tar zxvf "$BIN_UTILS_TAR"
  136. fi
  137. # 开始编译
  138. mkdir build-binutils
  139. cd build-binutils
  140. ../${BIN_UTILS}/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror
  141. make -j $(nproc) || exit 1
  142. make install || exit 1
  143. cd ..
  144. fi
  145. # compile GCC
  146. GCC_FILE="gcc-11.3.0"
  147. GCC_FILE_TAR="${GCC_FILE}.tar.gz"
  148. if [ ! -n "$(find $PREFIX/bin/* -name $TARGET_GCC)" ] || [ ${KEEP_GCC} -ne 1 ]; then
  149. if [ $KEEP_GCC -eq 1 ]; then
  150. echo -e "\033[35m 没有检测到 $TARGET_GCC, -kg参数无效 \033[0m"
  151. echo -e "\033[35m 开始安装gcc \033[0m"
  152. sleep 1s
  153. fi
  154. if [ ! -d "$GCC_FILE" ]; then
  155. if [ ! -f "$GCC_FILE_TAR" ]; then
  156. echo -e "\033[33m [提醒] 如果使用的是国外源, 下载时间可能偏久. 如果需要使用清华源, 请以输入参数-cs, 即: bash build_gcc_toolchain.sh -cs \033[0m "
  157. if [ ${CHANGE_SOURCE} -eq 1 ]; then
  158. # 国内源
  159. wget "https://mirrors.ustc.edu.cn/gnu/gcc/${GCC_FILE}/${GCC_FILE_TAR}" -P "$INSTALL_POS"
  160. else
  161. # 官方网站
  162. wget "http://ftp.gnu.org/gnu/gcc/${GCC_FILE}/${GCC_FILE_TAR}" -P "$INSTALL_POS"
  163. fi
  164. fi
  165. tar zxvf "$GCC_FILE_TAR"
  166. fi
  167. # 开始编译安装
  168. mkdir build-gcc
  169. cd build-gcc
  170. ../${GCC_FILE}/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers
  171. make MAKEINFO=true all-gcc -j $(nproc) || exit 1
  172. make MAKEINFO=true all-target-libgcc -j $(nproc) || exit 1
  173. make MAKEINFO=true install-gcc -j $(nproc) || exit 1
  174. make MAKEINFO=true install-target-libgcc -j $(nproc) || exit 1
  175. cd ..
  176. fi
  177. # update PATH
  178. if [ -n "$(grep -F "export DragonOS_GCC" "$HOME/.$(basename $SHELL)rc")" ]; then
  179. echo "[info] DragonOS_GCC has been in the "'$PATH'
  180. else
  181. echo 'export DragonOS_GCC='"$PREFIX"'/bin' >> "$HOME/.$(basename $SHELL)rc"
  182. echo 'export PATH="$DragonOS_GCC:$PATH"' >> "$HOME/.$(basename $SHELL)rc"
  183. echo "[info] Add DragonOS_GCC into PATH successfully."
  184. fi
  185. source "$HOME/.$CURRENT_SHELL"rc
  186. # final check
  187. if [ -n "$(find $PREFIX/bin/* -name $TARGET_GCC)" ] &&
  188. [ -n "$(find $PREFIX/bin/* -name $TARGET_LD)" ] &&
  189. [ -n "$(find $PREFIX/bin/* -name $TARGET_AS)" ]; then
  190. if [ ${SAVE_CACHE} -eq 0 ]; then
  191. # 删除临时文件
  192. rm -rf "$BIN_UTILS"
  193. rm -rf "$BIN_UTILS_TAR"
  194. rm -rf "build-binutils"
  195. rm -rf "$GCC_FILE"
  196. rm -rf "$GCC_FILE_TAR"
  197. rm -rf "build-gcc"
  198. fi
  199. echo -e "\033[42;37m [构建成功] Build Successfully.(请重新打开另一个Shell窗口或者重新打开你的IDE以获取新的环境变量) \033[0m"
  200. else
  201. echo -e "\033[31m [错误] 未找到$STRUCTURE-elf-gcc, $STRUCTURE-elf-ld和$STRUCTURE-elf-as. \033[0m"
  202. echo -e "\033[31m [构建失败] 请尝试重新运行build_gcc_toolchain.sh, 或者查看输出,找到错误的原因. \033[0m"
  203. fi
  204. cd "$current_pwd"