MC3302_SDK_V1.0.3_20251215_SRC/sdk/X-AIOS-LT00/install_xos_env.sh
2026-01-08 12:15:44 +08:00

152 lines
4.9 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -euo pipefail
# 颜色定义(便于输出提示)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 支持的 Ubuntu 版本列表
SUPPORTED_VERSIONS=("20.04" "22.04" "24.04")
# 检查是否为 root/有 sudo 权限
check_sudo() {
if [ "$(id -u)" -ne 0 ]; then
echo -e "${YELLOW}提示:需要 sudo 权限,即将请求管理员密码...${NC}"
if ! sudo -v; then
echo -e "${RED}错误:无 sudo 权限,无法继续安装${NC}"
exit 1
fi
fi
}
# 检测 Ubuntu 版本并验证是否支持
detect_ubuntu_version() {
if [ -f /etc/os-release ]; then
. /etc/os-release
if [ "$ID" != "ubuntu" ]; then
echo -e "${RED}错误:仅支持 Ubuntu 系统,当前系统为 $ID${NC}"
exit 1
fi
UBUNTU_VERSION=$VERSION_ID
echo -e "${GREEN}检测到 Ubuntu 版本:$UBUNTU_VERSION${NC}"
# 验证版本是否支持
if ! [[ " ${SUPPORTED_VERSIONS[@]} " =~ " $UBUNTU_VERSION " ]]; then
echo -e "${RED}错误:仅支持 Ubuntu ${SUPPORTED_VERSIONS[*]},当前版本为 $UBUNTU_VERSION,不支持安装${NC}"
exit 1
fi
else
echo -e "${RED}错误:无法检测系统版本${NC}"
exit 1
fi
}
# 通用工具安装20.04/22.04/24.04 共用)
install_common_packages() {
echo -e "${GREEN}===== 安装通用编译工具 =====${NC}"
sudo apt update -y
sudo apt install -y \
cmake mtools scons curl flex bison gperf build-essential zip \
device-tree-compiler libsdl2-dev libpng-dev libcurl4-openssl-dev libssl-dev \
gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib \
g++-multilib libc6-dev-i386 x11proto-core-dev libx11-dev ccache \
libgl1-mesa-dev libxml2-utils xsltproc unzip liblz4-tool gdb u-boot-tools git \
python3 python3-pip python-is-python3 lib32z-dev fakeroot mtd-utils \
android-sdk-libsparse-utils cpio
# 修复可能的依赖问题
sudo apt -f install -y
}
# Ubuntu 20.04 专属安装
install_ubuntu_2004() {
echo -e "${GREEN}===== 安装 Ubuntu 20.04 专属依赖 =====${NC}"
# 安装 ncurses 相关
sudo apt install -y libncurses5 lib32ncurses-dev
# 安装 xlrd 1.2.0(突破系统环境限制)
echo -e "${YELLOW}安装 xlrd 1.2.0...${NC}"
pip3 install xlrd==1.2.0 --break-system-packages
}
# Ubuntu 22.04 专属安装(适配逻辑同 20.04ncurses 兼容 + xlrd 1.2.0
install_ubuntu_2204() {
echo -e "${GREEN}===== 安装 Ubuntu 22.04 专属依赖 =====${NC}"
# 22.04 兼容 20.04 的 ncurses 依赖,同时支持 xlrd 1.2.0
sudo apt install -y libncurses5 lib32ncurses-dev
# 安装 xlrd 1.2.0(突破系统环境限制)
echo -e "${YELLOW}安装 xlrd 1.2.0...${NC}"
pip3 install xlrd==1.2.0
}
# Ubuntu 24.04 专属安装
install_ubuntu_2404() { echo -e "${GREEN}===== 安装 Ubuntu 24.04 专属依赖 =====${NC}"
# 添加 i386 架构
sudo dpkg --add-architecture i386
sudo apt update -y
# 安装 ncurses 相关
sudo apt install -y libncurses6 libncurses6:i386 libncurses-dev libncurses-dev:i386
# 安装 openpyxl
sudo apt install -y python3-openpyxl
}
# 验证安装结果
verify_installation() {
echo -e "${GREEN}===== 验证安装结果 =====${NC}"
# 检查核心工具
local tools=("cmake" "scons" "dtc" "python3" "pip3" "cpio" "fakeroot")
for tool in "${tools[@]}"; do
if command -v "$tool" &>/dev/null; then
echo -e "${GREEN}[✓] $tool 安装成功${NC}"
else
echo -e "${RED}[✗] $tool 安装失败${NC}"
fi
done
# 检查 Python 库
if [ "$UBUNTU_VERSION" = "20.04" ] || [ "$UBUNTU_VERSION" = "22.04" ]; then
if python3 -c "import xlrd; assert xlrd.__version__ == '1.2.0'" &>/dev/null; then
echo -e "${GREEN}[✓] xlrd 1.2.0 安装成功${NC}"
else
echo -e "${RED}[✗] xlrd 1.2.0 安装失败${NC}"
fi
elif [ "$UBUNTU_VERSION" = "24.04" ]; then
if python3 -c "import openpyxl" &>/dev/null; then
echo -e "${GREEN}[✓] openpyxl 安装成功${NC}"
else
echo -e "${RED}[✗] openpyxl 安装失败${NC}"
fi
fi
}
# 主流程
main() {
echo -e "${GREEN}===== 开始搭建 XOS 编译环境 =====${NC}"
check_sudo
detect_ubuntu_version
install_common_packages
case "$UBUNTU_VERSION" in
20.04)
install_ubuntu_2004
;;
22.04)
install_ubuntu_2204
;;
24.04)
install_ubuntu_2404
;;
*)
# 理论上不会走到这里(已在 detect_ubuntu_version 验证)
echo -e "${RED}错误:不支持的 Ubuntu 版本${NC}"
exit 1
;;
esac
verify_installation
echo -e "${GREEN}===== XOS 编译环境搭建完成 =====${NC}"
}
# 执行主流程
main