install.sh 5.7 KB

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