Skip to content

Commit 8a45bbf

Browse files
committed
feat: add build script for RocketMQ Node.js addon and update dependencies
1 parent dcaf56f commit 8a45bbf

4 files changed

Lines changed: 261 additions & 7 deletions

File tree

build.sh

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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 "$@"

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rocketmq-nodejs",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "Apache RocketMQ Client for Node.js",
55
"main": "index.js",
66
"types": "index.d.ts",
@@ -35,6 +35,6 @@
3535
},
3636
"devDependencies": {
3737
"node-addon-api": "^8.5.0",
38-
"cmake-js": "^7.3.1"
38+
"cmake-js": "^7.4.0"
3939
}
4040
}

rocketmq-v4.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3.8'
22

33
services:
44
namesrv:
5-
image: apache/rocketmq:4.9.6
5+
image: apache/rocketmq:4.9.7
66
platform: linux/amd64
77
container_name: rmqnamesrv
88
ports:
@@ -12,7 +12,7 @@ services:
1212
command: sh mqnamesrv
1313

1414
broker:
15-
image: apache/rocketmq:4.9.6
15+
image: apache/rocketmq:4.9.7
1616
platform: linux/amd64
1717
container_name: rmqbroker
1818
ports:

rocketmq-v5.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3.8'
22

33
services:
44
namesrv:
5-
image: apache/rocketmq:5.3.2
5+
image: apache/rocketmq:5.4.0
66
platform: linux/amd64
77
container_name: rmqnamesrv
88
ports:
@@ -12,7 +12,7 @@ services:
1212
command: sh mqnamesrv
1313

1414
broker:
15-
image: apache/rocketmq:5.3.2
15+
image: apache/rocketmq:5.4.0
1616
platform: linux/amd64
1717
container_name: rmqbroker
1818
ports:
@@ -28,7 +28,7 @@ services:
2828
command: sh mqbroker
2929

3030
proxy:
31-
image: apache/rocketmq:5.3.2
31+
image: apache/rocketmq:5.4.0
3232
platform: linux/amd64
3333
container_name: rmqproxy
3434
networks:

0 commit comments

Comments
 (0)