|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# ============================================================================= |
| 5 | +# RocketMQ Node.js Addon - 全平台构建脚本 |
| 6 | +# ============================================================================= |
| 7 | + |
| 8 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | +cd "$SCRIPT_DIR" |
| 10 | + |
| 11 | +RELEASE_DIR="$SCRIPT_DIR/Release" |
| 12 | +LOG_DIR="$SCRIPT_DIR/.build-logs" |
| 13 | + |
| 14 | +# 颜色输出 |
| 15 | +RED='\033[0;31m' |
| 16 | +GREEN='\033[0;32m' |
| 17 | +YELLOW='\033[1;33m' |
| 18 | +BLUE='\033[0;34m' |
| 19 | +NC='\033[0m' # No Color |
| 20 | + |
| 21 | +info() { echo -e "${BLUE}[INFO]${NC} $*"; } |
| 22 | +success() { echo -e "${GREEN}[OK]${NC} $*"; } |
| 23 | +warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } |
| 24 | +error() { echo -e "${RED}[ERROR]${NC} $*"; } |
| 25 | + |
| 26 | +# 检测当前架构 |
| 27 | +detect_arch() { |
| 28 | + case "$(uname -m)" in |
| 29 | + x86_64|amd64) echo "amd64" ;; |
| 30 | + arm64|aarch64) echo "arm64" ;; |
| 31 | + *) echo "unknown" ;; |
| 32 | + esac |
| 33 | +} |
| 34 | + |
| 35 | +# 检测操作系统 |
| 36 | +detect_os() { |
| 37 | + case "$(uname -s)" in |
| 38 | + Darwin) echo "macos" ;; |
| 39 | + Linux) echo "linux" ;; |
| 40 | + *) echo "unknown" ;; |
| 41 | + esac |
| 42 | +} |
| 43 | + |
| 44 | +HOST_ARCH=$(detect_arch) |
| 45 | +HOST_OS=$(detect_os) |
| 46 | + |
| 47 | +info "检测到系统: $HOST_OS / $HOST_ARCH" |
| 48 | + |
| 49 | +# 初始化目录 |
| 50 | +mkdir -p "$RELEASE_DIR" "$LOG_DIR" |
| 51 | + |
| 52 | +# ============================================================================= |
| 53 | +# 构建函数 |
| 54 | +# ============================================================================= |
| 55 | + |
| 56 | +build_with_act() { |
| 57 | + local job_name="$1" |
| 58 | + local container_arch="$2" |
| 59 | + local log_file="$LOG_DIR/${job_name}.log" |
| 60 | + |
| 61 | + info "开始构建: $job_name (container: $container_arch)" |
| 62 | + |
| 63 | + local artifact_name |
| 64 | + case "$job_name" in |
| 65 | + linux-gnu-x64) artifact_name="linux-x86_64-gnu-rocketmq.node" ;; |
| 66 | + linux-gnu-arm64) artifact_name="linux-aarch64-gnu-rocketmq.node" ;; |
| 67 | + linux-musl-x64) artifact_name="linux-x86_64-musl-rocketmq.node" ;; |
| 68 | + linux-musl-arm64) artifact_name="linux-aarch64-musl-rocketmq.node" ;; |
| 69 | + esac |
| 70 | + |
| 71 | + # 运行 act(忽略 artifact 上传失败的退出码) |
| 72 | + act -j "$job_name" --container-architecture "linux/$container_arch" \ |
| 73 | + --action-offline-mode \ |
| 74 | + 2>&1 | tee "$log_file" || true |
| 75 | + |
| 76 | + # 从容器中提取构建产物 |
| 77 | + local container_name |
| 78 | + container_name=$(docker ps -a --filter "name=act-Build-and-Release-${job_name}" --format '{{.Names}}' | head -1) |
| 79 | + |
| 80 | + if [[ -n "$container_name" ]]; then |
| 81 | + if docker cp "$container_name:$SCRIPT_DIR/Release/$artifact_name" \ |
| 82 | + "$RELEASE_DIR/$artifact_name" 2>/dev/null; then |
| 83 | + success "$job_name 构建完成: $artifact_name" |
| 84 | + return 0 |
| 85 | + fi |
| 86 | + fi |
| 87 | + |
| 88 | + # 检查日志确认构建是否成功(即使 artifact 上传失败) |
| 89 | + if grep -q "Success - Main Build addon" "$log_file" && \ |
| 90 | + grep -q "Success - Main Package artifact" "$log_file"; then |
| 91 | + warn "$job_name 构建成功但提取产物失败,请手动检查容器" |
| 92 | + return 0 |
| 93 | + fi |
| 94 | + |
| 95 | + error "$job_name 构建失败,查看日志: $log_file" |
| 96 | + return 1 |
| 97 | +} |
| 98 | + |
| 99 | +build_macos_universal() { |
| 100 | + info "开始构建: macOS Universal" |
| 101 | + local log_file="$LOG_DIR/macos-universal.log" |
| 102 | + |
| 103 | + ( |
| 104 | + # 清理之前的构建 |
| 105 | + rm -rf build/ |
| 106 | + |
| 107 | + # 安装依赖 |
| 108 | + npm install --no-audit --prefer-offline |
| 109 | + |
| 110 | + # 构建 RocketMQ 原生依赖 |
| 111 | + ./deps/rocketmq/build.sh |
| 112 | + |
| 113 | + # 构建 addon |
| 114 | + npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release \ |
| 115 | + --CDCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \ |
| 116 | + --CDCMAKE_OSX_DEPLOYMENT_TARGET=11 \ |
| 117 | + --CDCMAKE_SKIP_DEPENDENCY_TRACKING=ON |
| 118 | + |
| 119 | + # 打包产物 |
| 120 | + strip -S -x build/rocketmq.node |
| 121 | + cp build/rocketmq.node "$RELEASE_DIR/darwin-universal-rocketmq.node" |
| 122 | + ) 2>&1 | tee "$log_file" |
| 123 | + |
| 124 | + if [[ -f "$RELEASE_DIR/darwin-universal-rocketmq.node" ]]; then |
| 125 | + success "macOS Universal 构建完成" |
| 126 | + return 0 |
| 127 | + else |
| 128 | + error "macOS Universal 构建失败,查看日志: $log_file" |
| 129 | + return 1 |
| 130 | + fi |
| 131 | +} |
| 132 | + |
| 133 | +# ============================================================================= |
| 134 | +# 构建任务 |
| 135 | +# ============================================================================= |
| 136 | + |
| 137 | +build_linux_native() { |
| 138 | + # 原生架构构建(快) |
| 139 | + if [[ "$HOST_ARCH" == "amd64" ]]; then |
| 140 | + build_with_act "linux-gnu-x64" "amd64" |
| 141 | + build_with_act "linux-musl-x64" "amd64" |
| 142 | + elif [[ "$HOST_ARCH" == "arm64" ]]; then |
| 143 | + build_with_act "linux-gnu-arm64" "arm64" |
| 144 | + build_with_act "linux-musl-arm64" "arm64" |
| 145 | + fi |
| 146 | +} |
| 147 | + |
| 148 | +build_linux_emulated() { |
| 149 | + # 模拟架构构建(慢,需要 QEMU) |
| 150 | + if [[ "$HOST_ARCH" == "amd64" ]]; then |
| 151 | + warn "ARM64 构建需要 QEMU 模拟,会比较慢..." |
| 152 | + build_with_act "linux-gnu-arm64" "arm64" |
| 153 | + build_with_act "linux-musl-arm64" "arm64" |
| 154 | + elif [[ "$HOST_ARCH" == "arm64" ]]; then |
| 155 | + warn "x86_64 构建需要 QEMU 模拟,会比较慢..." |
| 156 | + build_with_act "linux-gnu-x64" "amd64" |
| 157 | + build_with_act "linux-musl-x64" "amd64" |
| 158 | + fi |
| 159 | +} |
| 160 | + |
| 161 | +build_all_linux() { |
| 162 | + build_linux_native |
| 163 | + build_linux_emulated |
| 164 | +} |
| 165 | + |
| 166 | +# ============================================================================= |
| 167 | +# 主逻辑 |
| 168 | +# ============================================================================= |
| 169 | + |
| 170 | +show_help() { |
| 171 | + cat << EOF |
| 172 | +用法: $0 [选项] |
| 173 | +
|
| 174 | +选项: |
| 175 | + all 构建所有平台 (默认) |
| 176 | + linux 构建所有 Linux 目标 |
| 177 | + linux-native 仅构建当前架构的 Linux 目标 (快) |
| 178 | + linux-cross 仅构建交叉架构的 Linux 目标 (慢) |
| 179 | + macos 构建 macOS Universal |
| 180 | +
|
| 181 | + linux-gnu-x64 单独构建 linux-gnu-x64 |
| 182 | + linux-gnu-arm64 单独构建 linux-gnu-arm64 |
| 183 | + linux-musl-x64 单独构建 linux-musl-x64 |
| 184 | + linux-musl-arm64 单独构建 linux-musl-arm64 |
| 185 | +
|
| 186 | + -h, --help 显示帮助信息 |
| 187 | +
|
| 188 | +示例: |
| 189 | + $0 # 构建所有 |
| 190 | + $0 linux-native # 仅构建当前架构 |
| 191 | + $0 linux-gnu-x64 # 仅构建指定目标 |
| 192 | + $0 macos # 仅构建 macOS |
| 193 | +EOF |
| 194 | +} |
| 195 | + |
| 196 | +main() { |
| 197 | + local target="${1:-all}" |
| 198 | + |
| 199 | + case "$target" in |
| 200 | + -h|--help) |
| 201 | + show_help |
| 202 | + exit 0 |
| 203 | + ;; |
| 204 | + all) |
| 205 | + info "=== 构建所有平台 ===" |
| 206 | + if [[ "$HOST_OS" == "macos" ]]; then |
| 207 | + build_macos_universal |
| 208 | + fi |
| 209 | + build_all_linux |
| 210 | + ;; |
| 211 | + linux) |
| 212 | + info "=== 构建所有 Linux 目标 ===" |
| 213 | + build_all_linux |
| 214 | + ;; |
| 215 | + linux-native) |
| 216 | + info "=== 构建当前架构 Linux 目标 ===" |
| 217 | + build_linux_native |
| 218 | + ;; |
| 219 | + linux-cross) |
| 220 | + info "=== 构建交叉架构 Linux 目标 ===" |
| 221 | + build_linux_emulated |
| 222 | + ;; |
| 223 | + macos) |
| 224 | + if [[ "$HOST_OS" != "macos" ]]; then |
| 225 | + error "macOS 构建只能在 macOS 上运行" |
| 226 | + exit 1 |
| 227 | + fi |
| 228 | + build_macos_universal |
| 229 | + ;; |
| 230 | + linux-gnu-x64) |
| 231 | + build_with_act "linux-gnu-x64" "amd64" |
| 232 | + ;; |
| 233 | + linux-gnu-arm64) |
| 234 | + build_with_act "linux-gnu-arm64" "arm64" |
| 235 | + ;; |
| 236 | + linux-musl-x64) |
| 237 | + build_with_act "linux-musl-x64" "amd64" |
| 238 | + ;; |
| 239 | + linux-musl-arm64) |
| 240 | + build_with_act "linux-musl-arm64" "arm64" |
| 241 | + ;; |
| 242 | + *) |
| 243 | + error "未知目标: $target" |
| 244 | + show_help |
| 245 | + exit 1 |
| 246 | + ;; |
| 247 | + esac |
| 248 | + |
| 249 | + echo "" |
| 250 | + info "=== 构建产物 ===" |
| 251 | + ls -lh "$RELEASE_DIR"/*.node 2>/dev/null || warn "没有找到构建产物" |
| 252 | +} |
| 253 | + |
| 254 | +main "$@" |
0 commit comments