install.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2024, RT-Thread Development Team
  4. #
  5. # SPDX-License-Identifier: Apache-2.0
  6. #
  7. # Change Logs:
  8. # Date Author Notes
  9. # 2024-08-27 Supperthomas the first version
  10. # 2025-08-09 ThearchyHelios Fix Darwin detection and URL
  11. #
  12. #这个脚本用于安装RT-Thread开发环境 请确保网络畅通
  13. # 设置环境变量 如果希望生效请在当前shell中执行source install.sh
  14. export RTT_ROOT=$(pwd)
  15. export RTT_CC=gcc
  16. echo "RTT_ROOT is set to: $RTT_ROOT"
  17. check_if_china_ip() {
  18. # 默认情况下不使用gitee
  19. use_gitee=false
  20. # 尝试通过IP地址判断
  21. ip=$(curl -s https://ifconfig.me/ip)
  22. if [ -n "$ip" ]; then
  23. location=$(curl -s http://www.ip-api.com/json/$ip | grep -o '"country":"China"')
  24. if [ "$location" == '"country":"China"' ]; then
  25. use_gitee=true
  26. echo "Detected China IP. Using gitee."
  27. else
  28. echo "IP location is not in China."
  29. fi
  30. else
  31. echo "Failed to retrieve IP address. Falling back to timezone check."
  32. # 通过时区判断
  33. if [ $(($(date +%z)/100)) -eq 8 ]; then
  34. use_gitee=true
  35. echo "Detected timezone UTC+8. Using gitee."
  36. else
  37. echo "Timezone is not UTC+8."
  38. fi
  39. fi
  40. echo $use_gitee
  41. }
  42. # 检测操作系统类型和发行版
  43. detect_os() {
  44. if command -v uname >/dev/null 2>&1; then
  45. OS=$(uname -s)
  46. else
  47. if [ -f "/etc/os-release" ]; then
  48. OS="Linux"
  49. elif [ -f "/System/Library/CoreServices/SystemVersion.plist" ]; then
  50. OS="Darwin"
  51. elif [[ -d "/mnt/c/Windows" || -d "/c/Windows" ]]; then
  52. OS="WSL"
  53. else
  54. OS="UNKNOWN"
  55. fi
  56. fi
  57. if [ "$OS" == "Linux" ]; then
  58. if [ -f /etc/os-release ]; then
  59. . /etc/os-release
  60. DISTRO=$ID
  61. VERSION=$VERSION_ID
  62. elif [ -f /etc/lsb-release ]; then
  63. . /etc/lsb-release
  64. DISTRO=$DISTRIB_ID
  65. VERSION=$DISTRIB_RELEASE
  66. else
  67. DISTRO="UNKNOWN"
  68. VERSION="UNKNOWN"
  69. fi
  70. fi
  71. echo "Detected Operating System: $OS, Distribution: $DISTRO, Version: $VERSION"
  72. }
  73. # 修改的安装函数
  74. install_on_ubuntu() {
  75. echo "Installing on Debian/Ubuntu..."
  76. use_gitee=$(check_if_china_ip)
  77. # 根据检测结果决定是否使用--gitee参数
  78. if [ "$use_gitee" = true ]; then
  79. wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_ubuntu.sh
  80. chmod 777 install_ubuntu.sh
  81. echo "Installing on China gitee..."
  82. ./install_ubuntu.sh --gitee
  83. else
  84. wget https://raw.githubusercontent.com/RT-Thread/env/master/install_ubuntu.sh
  85. chmod 777 install_ubuntu.sh
  86. echo "Installing on no China..."
  87. ./install_ubuntu.sh
  88. fi
  89. rm install_ubuntu.sh
  90. }
  91. install_on_fedora() {
  92. echo "Installing on Fedora..."
  93. }
  94. install_on_centos() {
  95. echo "Installing on CentOS/RHEL..."
  96. }
  97. install_on_arch() {
  98. echo "Installing on Arch Linux..."
  99. }
  100. install_on_macos() {
  101. echo "Installing on macOS..."
  102. use_gitee=$(check_if_china_ip)
  103. # 根据检测结果决定是否使用--gitee参数
  104. if [ "$use_gitee" = true ]; then
  105. wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_macos.sh
  106. chmod 777 install_macos.sh
  107. echo "Installing on China gitee..."
  108. ./install_macos.sh --gitee
  109. else
  110. wget https://raw.githubusercontent.com/RT-Thread/env/master/install_macos.sh
  111. chmod 777 install_macos.sh
  112. echo "Installing on no China..."
  113. ./install_macos.sh
  114. fi
  115. rm ./install_macos.sh
  116. }
  117. install_on_wsl() {
  118. echo "Installing on Windows Subsystem for Linux (WSL)..."
  119. }
  120. install_on_windows() {
  121. echo "Installing on Windows using PowerShell..."
  122. use_gitee=$(check_if_china_ip)
  123. # 根据检测结果决定是否使用--gitee参数
  124. if [ "$use_gitee" = true ]; then
  125. wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_windows.ps1
  126. echo "Installing on China gitee..."
  127. ./install_windows.ps1 --gitee
  128. else
  129. wget https://raw.githubusercontent.com/RT-Thread/env/master/install_windows.ps1
  130. echo "Installing on no China..."
  131. ./install_windows.ps1
  132. fi
  133. rm ./install_windows.ps1
  134. }
  135. install_on_opensuse() {
  136. echo "Installing on openSUSE..."
  137. use_gitee=$(check_if_china_ip)
  138. if [ "$use_gitee" = true ]; then
  139. wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_suse.sh
  140. chmod 777 install_suse.sh
  141. echo "Installing on China gitee..."
  142. ./install_suse.sh --gitee
  143. else
  144. wget https://raw.githubusercontent.com/RT-Thread/env/master/install_suse.sh
  145. chmod 777 install_suse.sh
  146. echo "Installing on no China..."
  147. ./install_suse.sh
  148. fi
  149. rm ./install_suse.sh
  150. }
  151. # 主函数
  152. main() {
  153. detect_os
  154. case "$OS" in
  155. Linux)
  156. case "$DISTRO" in
  157. ubuntu|debian)
  158. install_on_ubuntu
  159. ;;
  160. fedora)
  161. install_on_fedora
  162. ;;
  163. centos|rhel)
  164. install_on_centos
  165. ;;
  166. arch)
  167. install_on_arch
  168. ;;
  169. *)
  170. echo "Unsupported Linux distribution: $DISTRO"
  171. exit 1
  172. ;;
  173. esac
  174. ;;
  175. Darwin)
  176. install_on_macos
  177. ;;
  178. WSL)
  179. install_on_wsl
  180. ;;
  181. Windows)
  182. install_on_windows
  183. ;;
  184. *)
  185. echo "Unsupported Operating System: $OS"
  186. exit 1
  187. ;;
  188. esac
  189. echo "Installation completed!"
  190. }
  191. # 执行主函数
  192. main