File size: 3,630 Bytes
3c2af29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash

# 设置错误时退出
set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

info() {
    echo -e "${BLUE}[INFO] $1${NC}"
}

error() {
    echo -e "${RED}[ERROR] $1${NC}"
    exit 1
}

# 检查是否为 root 用户(FreeBSD 和 Linux)
if [ "$(uname)" != "Darwin" ] && [ "$EUID" -ne 0 ]; then
    error "请使用 root 权限运行此脚本 (sudo ./setup.sh)"
fi

# 检测包管理器
if command -v brew &> /dev/null; then
    PKG_MANAGER="brew"
    info "检测到 macOS/Homebrew 系统"
elif command -v pkg &> /dev/null; then
    PKG_MANAGER="pkg"
    info "检测到 FreeBSD 系统"
elif command -v apt-get &> /dev/null; then
    PKG_MANAGER="apt-get"
    info "检测到 Debian/Ubuntu 系统"
elif command -v dnf &> /dev/null; then
    PKG_MANAGER="dnf"
    info "检测到 Fedora/RHEL 系统"
elif command -v yum &> /dev/null; then
    PKG_MANAGER="yum"
    info "检测到 CentOS 系统"
else
    error "未检测到支持的包管理器"
fi

# 更新包管理器缓存
info "更新包管理器缓存..."
case $PKG_MANAGER in
    "brew")
        brew update
        ;;
    "pkg")
        pkg update
        ;;
    *)
        $PKG_MANAGER update -y
        ;;
esac

# 安装基础构建工具
info "安装基础构建工具..."
case $PKG_MANAGER in
    "brew")
        brew install \
            protobuf \
            pkg-config \
            openssl \
            curl \
            git \
            node
        ;;
    "pkg")
        pkg install -y \
            gmake \
            protobuf \
            pkgconf \
            openssl \
            curl \
            git \
            node
        ;;
    "apt-get")
        $PKG_MANAGER install -y --no-install-recommends \
            build-essential \
            protobuf-compiler \
            pkg-config \
            libssl-dev \
            ca-certificates \
            curl \
            tzdata \
            git
        ;;
    *)
        $PKG_MANAGER install -y \
            gcc \
            gcc-c++ \
            make \
            protobuf-compiler \
            pkg-config \
            openssl-devel \
            ca-certificates \
            curl \
            tzdata \
            git
        ;;
esac

# 安装 Node.js 和 npm(如果还没有通过包管理器安装)
if ! command -v node &> /dev/null && [ "$PKG_MANAGER" != "brew" ] && [ "$PKG_MANAGER" != "pkg" ]; then
    info "安装 Node.js 和 npm..."
    if [ "$PKG_MANAGER" = "apt-get" ]; then
        curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
        $PKG_MANAGER install -y nodejs
    else
        curl -fsSL https://rpm.nodesource.com/setup_lts.x | bash -
        $PKG_MANAGER install -y nodejs
    fi
fi

# 安装 Rust(如果未安装)
if ! command -v rustc &> /dev/null; then
    info "安装 Rust..."
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
    . "$HOME/.cargo/env"
fi

# 添加目标平台
info "添加 Rust 目标平台..."
case "$(uname)" in
    "FreeBSD")
        rustup target add x86_64-unknown-freebsd
        ;;
    "Darwin")
        rustup target add x86_64-apple-darwin aarch64-apple-darwin
        ;;
    *)
        rustup target add x86_64-unknown-linux-gnu
        ;;
esac

# 清理包管理器缓存
case $PKG_MANAGER in
    "apt-get")
        rm -rf /var/lib/apt/lists/*
        ;;
    "pkg")
        pkg clean -y
        ;;
esac

# 设置时区(除了 macOS)
if [ "$(uname)" != "Darwin" ]; then
    info "设置时区为 Asia/Shanghai..."
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
fi

echo -e "${GREEN}安装完成!${NC}"