NAC_Blockchain/scripts/setup_env.sh

188 lines
5.3 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
# NAC区块链开发环境自动配置脚本
# 作者NAC公链开发小组
# 版本v1.0.0
# 日期2026-02-17
set -e
echo "========================================="
echo " NAC区块链开发环境配置脚本"
echo "========================================="
echo ""
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 检测操作系统
OS="$(uname -s)"
case "${OS}" in
Linux*) MACHINE=Linux;;
Darwin*) MACHINE=Mac;;
*) MACHINE="UNKNOWN:${OS}"
esac
echo -e "${GREEN}检测到操作系统: ${MACHINE}${NC}"
echo ""
# 1. 安装Rust工具链
echo "========================================="
echo "1. 安装Rust工具链"
echo "========================================="
if command -v rustc &> /dev/null; then
RUST_VERSION=$(rustc --version | awk '{print $2}')
echo -e "${YELLOW}已安装Rust版本: ${RUST_VERSION}${NC}"
else
echo "正在安装Rust..."
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source $HOME/.cargo/env
echo -e "${GREEN}✓ Rust安装完成${NC}"
fi
# 安装指定版本的Rust
REQUIRED_RUST_VERSION="1.75.0"
echo "正在安装Rust ${REQUIRED_RUST_VERSION}..."
rustup install ${REQUIRED_RUST_VERSION}
rustup default ${REQUIRED_RUST_VERSION}
# 安装必要组件
echo "正在安装Rust组件..."
rustup component add rustfmt clippy rust-src
# 添加WASM目标
echo "正在添加WASM编译目标..."
rustup target add wasm32-unknown-unknown
echo -e "${GREEN}✓ Rust工具链配置完成${NC}"
echo ""
# 2. 安装Go工具链可选
echo "========================================="
echo "2. 安装Go工具链可选"
echo "========================================="
if command -v go &> /dev/null; then
GO_VERSION=$(go version | awk '{print $3}')
echo -e "${YELLOW}已安装Go版本: ${GO_VERSION}${NC}"
else
echo "Go未安装正在安装..."
REQUIRED_GO_VERSION="1.21.0"
if [ "$MACHINE" == "Linux" ]; then
wget https://go.dev/dl/go${REQUIRED_GO_VERSION}.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go${REQUIRED_GO_VERSION}.linux-amd64.tar.gz
rm go${REQUIRED_GO_VERSION}.linux-amd64.tar.gz
# 添加到PATH
if ! grep -q "/usr/local/go/bin" ~/.bashrc; then
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
echo 'export PATH=$PATH:$HOME/go/bin' >> ~/.bashrc
fi
export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:$HOME/go/bin
echo -e "${GREEN}✓ Go安装完成${NC}"
elif [ "$MACHINE" == "Mac" ]; then
if command -v brew &> /dev/null; then
brew install go@1.21
echo -e "${GREEN}✓ Go安装完成${NC}"
else
echo -e "${YELLOW}请手动安装Homebrew后再运行此脚本${NC}"
fi
fi
fi
echo ""
# 3. 安装系统依赖
echo "========================================="
echo "3. 安装系统依赖"
echo "========================================="
if [ "$MACHINE" == "Linux" ]; then
echo "正在安装Linux系统依赖..."
if command -v apt-get &> /dev/null; then
sudo apt-get update
sudo apt-get install -y build-essential pkg-config libssl-dev git curl wget
echo -e "${GREEN}✓ 系统依赖安装完成${NC}"
elif command -v yum &> /dev/null; then
sudo yum groupinstall -y "Development Tools"
sudo yum install -y openssl-devel git curl wget
echo -e "${GREEN}✓ 系统依赖安装完成${NC}"
fi
elif [ "$MACHINE" == "Mac" ]; then
echo "正在安装macOS系统依赖..."
if command -v brew &> /dev/null; then
brew install openssl pkg-config git curl wget
echo -e "${GREEN}✓ 系统依赖安装完成${NC}"
else
echo -e "${YELLOW}请先安装Homebrew: /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"${NC}"
fi
fi
echo ""
# 4. 编译Charter编译器
echo "========================================="
echo "4. 编译Charter编译器"
echo "========================================="
if [ -d "charter-compiler" ]; then
echo "正在编译Charter编译器..."
cd charter-compiler
cargo build --release
cd ..
echo -e "${GREEN}✓ Charter编译器编译完成${NC}"
echo "Charter编译器位置: ./charter-compiler/target/release/charter-compiler"
else
echo -e "${YELLOW}未找到charter-compiler目录跳过${NC}"
fi
echo ""
# 5. 验证安装
echo "========================================="
echo "5. 验证安装"
echo "========================================="
echo "Rust版本:"
rustc --version
echo ""
echo "Cargo版本:"
cargo --version
echo ""
if command -v go &> /dev/null; then
echo "Go版本:"
go version
echo ""
fi
echo "Git版本:"
git --version
echo ""
# 6. 环境变量提示
echo "========================================="
echo "环境配置完成!"
echo "========================================="
echo ""
echo -e "${GREEN}✓ 所有依赖已安装完成${NC}"
echo ""
echo "请运行以下命令使环境变量生效:"
echo -e "${YELLOW}source ~/.bashrc${NC}"
echo ""
echo "或者重新打开终端。"
echo ""
echo "接下来可以运行:"
echo " ./scripts/build_all.sh # 编译所有模块"
echo " ./scripts/test_all.sh # 运行所有测试"
echo " ./scripts/verify_env.sh # 验证环境配置"
echo ""