-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·485 lines (416 loc) · 14.1 KB
/
build.sh
File metadata and controls
executable file
·485 lines (416 loc) · 14.1 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/bin/bash
# ZimaOS-Blue Development Script
# Usage: ./build.sh [command]
# Commands: start (default), server, web, build, clean, prd
set -e
PROJECT_ROOT="$(cd "$(dirname "$0")" && pwd)"
COMMAND="${1:-prd}"
# Enable CGO by default for production-capable builds.
# Allow explicit override from environment when needed.
export CGO_ENABLED="${CGO_ENABLED:-1}"
GO_BUILD_FLAGS="-trimpath -buildvcs=false"
HOST_UNAME_S="$(uname -s)"
if [ "$HOST_UNAME_S" = "Darwin" ]; then
GO_SERVER_TAGS="fts5"
else
GO_SERVER_TAGS="fts5 espeak kokoro"
fi
# Add Python user bin to PATH for edge-tts
export PATH="$PATH:$HOME/Library/Python/3.9/bin:$HOME/.local/bin"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
info() { echo -e "${CYAN}[INFO]${NC} $*"; }
success() { echo -e "${GREEN}[OK]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*"; }
# Run a binary. On macOS, TCC binds speech recognition authorization to the
# parent app's bundle ID. If not running inside a real terminal (e.g. launched
# from an IDE), re-launch in Terminal.app so TCC uses com.apple.Terminal.
run_binary() {
local bin="$1"
shift
# Resolve to absolute path — Terminal.app opens in ~/ by default
case "$bin" in
/*) ;; # already absolute
*) bin="$(cd "$(dirname "$bin")" && pwd)/$(basename "$bin")" ;;
esac
if [ "$(uname -s)" = "Darwin" ] && ! is_real_terminal; then
info "Not running in Terminal.app, re-launching via Terminal..."
local cmd
cmd="'$(echo "$bin" | sed "s/'/'\\\\''/g")'"
for arg in "$@"; do
cmd="$cmd '$(echo "$arg" | sed "s/'/'\\\\''/g")'"
done
osascript -e "tell application \"Terminal\"
activate
-- Reuse the frontmost window if one exists, otherwise do script creates a new one
if (count of windows) > 0 then
do script \"$cmd\" in front window
else
do script \"$cmd\"
end if
end tell"
return
fi
exec "$bin" "$@"
}
# Check if we're running inside a real terminal (not an IDE integrated terminal).
is_real_terminal() {
case "${TERM_PROGRAM:-}" in
Apple_Terminal|iTerm.app|WarpTerminal|Alacritty|tmux) return 0 ;;
esac
case "${__CFBundleIdentifier:-}" in
com.apple.Terminal|com.googlecode.iterm2|dev.warp.Warp-Stable) return 0 ;;
esac
return 1
}
# Check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check prerequisites
check_prereqs() {
info "Checking prerequisites..."
local missing=()
if ! command_exists go; then
missing+=("go (https://golang.org/dl/)")
fi
if ! command_exists node; then
missing+=("node (https://nodejs.org/)")
fi
if ! command_exists npm; then
missing+=("npm (comes with node)")
fi
if [ ${#missing[@]} -gt 0 ]; then
error "Missing prerequisites:"
for m in "${missing[@]}"; do
echo " - $m"
done
exit 1
fi
success "All prerequisites found"
}
# Trim web dist: remove pre-compressed files, stats, and samples not needed for local serving
trim_dist() {
local dist_dir="$1"
info "Trimming dist (removing .gz, .br, stats.html)..."
find "$dist_dir" \( -name "*.gz" -o -name "*.br" -o -name "stats.html" \) -delete 2>/dev/null
info "Dist trimmed to $(du -sh "$dist_dir" | cut -f1)"
}
# Install dependencies
install_deps() {
info "Installing dependencies..."
# Go dependencies
info "Installing Go dependencies..."
cd "$PROJECT_ROOT/server"
go mod download
success "Go dependencies installed"
# Node dependencies
cd "$PROJECT_ROOT/web"
if [ ! -d "node_modules" ]; then
info "Installing Node dependencies..."
npm install
else
info "Node modules already installed, skipping..."
fi
success "Node dependencies installed"
}
# Start the Go server
start_server() {
info "Starting Go server..."
cd "$PROJECT_ROOT/server"
local binary_path="./blue"
if [ "$HOST_UNAME_S" = "Darwin" ]; then
make build-bluecli
binary_path="./bin/bluecli"
else
go build $GO_BUILD_FLAGS -tags "$GO_SERVER_TAGS" -o blue ./cmd/blue
fi
success "Server built successfully"
info "Starting server on http://localhost"
run_binary "$binary_path"
}
# Start the web dev server
start_web() {
info "Starting web dev server..."
cd "$PROJECT_ROOT/web"
if [ ! -d "node_modules" ]; then
info "Installing Node dependencies..."
npm install
fi
info "Starting Vite dev server on http://localhost:3000"
npm run dev
}
# Start both server and web
start_all() {
check_prereqs
install_deps
echo ""
echo -e "${CYAN}========================================${NC}"
echo -e "${CYAN} ZimaOS-Blue Development Environment${NC}"
echo -e "${CYAN}========================================${NC}"
echo ""
echo -e " Backend: ${YELLOW}http://localhost${NC}"
echo -e " Frontend: ${YELLOW}http://localhost:3000${NC} (background)"
echo ""
echo -e " Press ${YELLOW}Ctrl+C${NC} to stop backend server"
echo ""
# Trap to cleanup background processes
trap cleanup EXIT INT TERM
# Start web in background (Go server proxies to it)
info "Starting Vite dev server in background..."
cd "$PROJECT_ROOT/web"
npm run dev &
WEB_PID=$!
# Give Vite time to start
sleep 3
# Start server in foreground
info "Starting Go server (dev mode)..."
cd "$PROJECT_ROOT/server"
local binary_path="./blue"
if [ "$HOST_UNAME_S" = "Darwin" ]; then
make build-bluecli
binary_path="./bin/bluecli"
else
go build $GO_BUILD_FLAGS -tags "$GO_SERVER_TAGS" -o blue ./cmd/blue
fi
run_binary "$binary_path"
}
# Cleanup function
cleanup() {
info "Stopping services..."
# Kill web dev server if running
if [ -n "$WEB_PID" ] && kill -0 "$WEB_PID" 2>/dev/null; then
kill "$WEB_PID" 2>/dev/null || true
fi
# Kill any remaining vite processes
pkill -f "vite" 2>/dev/null || true
success "Services stopped"
}
# Build third_party native libraries (espeak-ng, whisper.cpp, opus)
build_third_party() {
info "Building third_party native libraries..."
# Build espeak-ng
if [ ! -f "$PROJECT_ROOT/third_party/espeak-ng/build/src/libespeak-ng/libespeak-ng.a" ]; then
info "Building espeak-ng..."
cd "$PROJECT_ROOT/third_party/espeak-ng"
cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF
cmake --build build --config Release -j"$(sysctl -n hw.ncpu 2>/dev/null || nproc)"
success "espeak-ng built"
else
info "espeak-ng already built, skipping..."
fi
# Build libsonic.a from espeak-ng's compiled object if missing
if [ ! -f "$PROJECT_ROOT/third_party/espeak-ng/build/libsonic.a" ] && \
[ -f "$PROJECT_ROOT/third_party/espeak-ng/build/CMakeFiles/sonic.dir/_deps/sonic-git-src/sonic.c.o" ]; then
info "Creating libsonic.a..."
ar rcs "$PROJECT_ROOT/third_party/espeak-ng/build/libsonic.a" \
"$PROJECT_ROOT/third_party/espeak-ng/build/CMakeFiles/sonic.dir/_deps/sonic-git-src/sonic.c.o"
success "libsonic.a created"
fi
# Build whisper.cpp
if [ ! -f "$PROJECT_ROOT/third_party/whisper.cpp/build/src/libwhisper.a" ]; then
info "Building whisper.cpp..."
cd "$PROJECT_ROOT/third_party/whisper.cpp"
cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF
cmake --build build --config Release -j"$(sysctl -n hw.ncpu 2>/dev/null || nproc)"
success "whisper.cpp built"
else
info "whisper.cpp already built, skipping..."
fi
# Build opus
if [ ! -f "$PROJECT_ROOT/third_party/opus-src/build/libopus.a" ]; then
info "Building opus..."
cd "$PROJECT_ROOT/third_party/opus-src"
cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF
cmake --build build --config Release -j"$(sysctl -n hw.ncpu 2>/dev/null || nproc)"
success "opus built"
else
info "opus already built, skipping..."
fi
success "Third_party libraries ready"
}
# Code-sign macOS binary or .app bundle (skipped if APPLE_SIGNING_IDENTITY is not set)
codesign_binary() {
local target="$1"
if [ "$(uname -s)" != "Darwin" ]; then
return
fi
if [ -z "$APPLE_SIGNING_IDENTITY" ]; then
warn "APPLE_SIGNING_IDENTITY not set, skipping code signing"
warn "macOS native speech recognition requires a signed binary"
return
fi
info "Code-signing $target ..."
local entitlements="$PROJECT_ROOT/tauri-app/src-tauri/entitlements.plist"
local deep_flag=""
if [[ "$target" == *.app ]]; then
deep_flag="--deep"
fi
codesign --force $deep_flag --options runtime --sign "$APPLE_SIGNING_IDENTITY" \
--entitlements "$entitlements" "$target"
success "Binary signed: $(codesign -dv "$target" 2>&1 | head -1)"
}
# Run the web security audit with optional allowlist support.
run_web_audit() {
if [ -f "$PROJECT_ROOT/web/scripts/audit-ci.mjs" ]; then
if [ -f "$PROJECT_ROOT/web/audit-allowlist.json" ]; then
node scripts/audit-ci.mjs --omit=dev
else
node scripts/audit-ci.mjs --no-allowlist --omit=dev
fi
else
npm audit --omit=dev
fi
}
handle_web_audit_failure() {
local audit_status="$1"
case "$audit_status" in
1)
error "Production dependencies have vulnerabilities. Please fix them before building."
;;
2)
error "Unable to complete npm audit because the npm registry request failed. Check network access and retry."
;;
*)
error "npm audit failed unexpectedly (exit code: $audit_status)."
;;
esac
}
# Build for production
build_all() {
check_prereqs
info "Building for production..."
# Check production dependencies for vulnerabilities
info "Checking production dependencies for vulnerabilities..."
cd "$PROJECT_ROOT/web"
if run_web_audit; then
:
else
audit_status=$?
handle_web_audit_failure "$audit_status"
exit 1
fi
# Build third_party native libraries (FFI mode - shared libs)
if [ ! -d "$PROJECT_ROOT/libs" ] || [ -z "$(ls -A $PROJECT_ROOT/libs 2>/dev/null)" ]; then
info "Building shared libraries for FFI..."
bash "$PROJECT_ROOT/scripts/build-libs.sh"
fi
# Build server
info "Building Go server..."
cd "$PROJECT_ROOT/server"
EXTRA_LDFLAGS=""
if [ "$HOST_UNAME_S" = "Darwin" ]; then
EXTRA_LDFLAGS="-extldflags '-sectcreate __TEXT __info_plist Info.plist'"
fi
go build $GO_BUILD_FLAGS -tags "$GO_SERVER_TAGS" -ldflags="-s -w $EXTRA_LDFLAGS" -o blue ./cmd/blue
# macOS: create .app bundle + codesign (TCC needs proper bundle for speech recognition)
if [ "$(uname -s)" = "Darwin" ]; then
info "Creating .app bundle..."
APP_BUNDLE="$PROJECT_ROOT/server/Blue.app"
rm -rf "$APP_BUNDLE"
mkdir -p "$APP_BUNDLE/Contents/MacOS"
mkdir -p "$APP_BUNDLE/Contents/Resources"
cp "$PROJECT_ROOT/server/blue" "$APP_BUNDLE/Contents/MacOS/blue"
cp "$PROJECT_ROOT/server/Info.plist" "$APP_BUNDLE/Contents/Info.plist"
if [ -d "$PROJECT_ROOT/server/internal/web/dist" ]; then
cp -r "$PROJECT_ROOT/server/internal/web/dist" "$APP_BUNDLE/Contents/Resources/dist"
fi
codesign_binary "$APP_BUNDLE"
fi
success "Server built: server/blue"
# Build web
info "Building web frontend..."
cd "$PROJECT_ROOT/web"
if [ ! -d "node_modules" ]; then
npm install
fi
npm run build
success "Web built: web/dist/"
trim_dist "$PROJECT_ROOT/web/dist"
success "Production build complete!"
}
# Production run: build web, copy to server/internal/web/dist, start server (embedded frontend)
prd_run() {
check_prereqs
info "Production run: build web, copy to server/internal/web, start server..."
# Check production dependencies for vulnerabilities
info "Checking production dependencies for vulnerabilities..."
cd "$PROJECT_ROOT/web"
if run_web_audit; then
:
else
audit_status=$?
handle_web_audit_failure "$audit_status"
exit 1
fi
# Build web
info "Building web frontend..."
cd "$PROJECT_ROOT/web"
if [ ! -d "node_modules" ]; then
npm install
fi
npm run build
success "Web built: web/dist/"
trim_dist "$PROJECT_ROOT/web/dist"
# Copy web/dist to server/internal/web/dist
info "Copying web build to server/internal/web/dist..."
rm -rf "$PROJECT_ROOT/server/internal/web/dist"
cp -r "$PROJECT_ROOT/web/dist" "$PROJECT_ROOT/server/internal/web/dist"
success "Web assets copied to server/internal/web/dist"
# Build server with pack-dist (signs before packing, appends web assets to binary)
info "Building Go server (production mode)..."
cd "$PROJECT_ROOT/server"
make build
success "Server built: server/bin/blue"
# Run the built binary
info "Starting server (production mode, http://localhost)..."
run_binary ./bin/blue
}
# Clean build artifacts
clean_all() {
info "Cleaning build artifacts..."
# Clean server
rm -f "$PROJECT_ROOT/server/echo"
rm -rf "$PROJECT_ROOT/server/data"
rm -rf "$PROJECT_ROOT/server/tmp"
# Clean web
rm -rf "$PROJECT_ROOT/web/dist"
rm -rf "$PROJECT_ROOT/web/node_modules"
rm -rf "$PROJECT_ROOT/server/internal/web/dist"
# Clean embedded skills (build artifact)
rm -rf "$PROJECT_ROOT/server/internal/skill/embedded/skills"
success "Clean complete!"
}
# Main
case "$COMMAND" in
start)
start_all
;;
server)
check_prereqs
start_server
;;
web)
check_prereqs
start_web
;;
build)
build_all
;;
clean)
clean_all
;;
prd)
prd_run
;;
*)
echo "Unknown command: $COMMAND"
echo "Usage: $0 [start|server|web|build|clean|prd]"
exit 1
;;
esac