-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·506 lines (445 loc) · 15.9 KB
/
build.sh
File metadata and controls
executable file
·506 lines (445 loc) · 15.9 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/bin/bash
#
# Build script for vice (macOS and Linux)
#
# Usage: ./build.sh [options]
#
# Options:
# --check Run gofmt and staticcheck
# --test Run tests
# --all Run all steps (--check --test, then build)
# --vulkan Build with vulkan support
# --release Build release binary (with downloadresources tag)
# --universal Build universal binary on macOS (arm64 + amd64)
# --help Show this help message
#
# whisper-cpp and sherpa-onnx are built automatically if needed.
set -e
# Expected whisper.cpp submodule SHA (update this when bumping the submodule)
WHISPER_EXPECTED_SHA="050f4ef8286ca6d49b1b0e131462b9d71959f5ff"
# Expected sherpa-onnx submodule SHA (update this when bumping the submodule)
SHERPA_EXPECTED_SHA="8dcca037bb73880c4fcb231a9c41740189e95e7c"
# Check that whisper.cpp submodule is at the expected commit
check_whisper_submodule() {
if [ ! -d "whisper.cpp/.git" ] && [ ! -f "whisper.cpp/.git" ]; then
echo "Error: whisper.cpp submodule is not initialized."
echo ""
echo "Please run:"
echo " git submodule update --init --recursive"
exit 1
fi
WHISPER_ACTUAL_SHA=$(git -C whisper.cpp rev-parse HEAD 2>/dev/null || echo "")
if [ -z "$WHISPER_ACTUAL_SHA" ]; then
echo "Error: Could not determine whisper.cpp submodule version."
echo ""
echo "Please run:"
echo " git submodule update --init --recursive"
exit 1
fi
if [ "$WHISPER_ACTUAL_SHA" != "$WHISPER_EXPECTED_SHA" ]; then
echo "Error: whisper.cpp submodule is at wrong commit."
echo ""
echo " Expected: $WHISPER_EXPECTED_SHA"
echo " Actual: $WHISPER_ACTUAL_SHA"
echo ""
echo "Please run:"
echo " git submodule update --init --recursive"
exit 1
fi
}
# Check that sherpa-onnx submodule is at the expected commit
check_sherpa_submodule() {
if [ ! -d "sherpa-onnx/.git" ] && [ ! -f "sherpa-onnx/.git" ]; then
echo "Error: sherpa-onnx submodule is not initialized."
echo ""
echo "Please run:"
echo " git submodule update --init --recursive"
exit 1
fi
SHERPA_ACTUAL_SHA=$(git -C sherpa-onnx rev-parse HEAD 2>/dev/null || echo "")
if [ -z "$SHERPA_ACTUAL_SHA" ]; then
echo "Error: Could not determine sherpa-onnx submodule version."
echo ""
echo "Please run:"
echo " git submodule update --init --recursive"
exit 1
fi
if [ "$SHERPA_ACTUAL_SHA" != "$SHERPA_EXPECTED_SHA" ]; then
echo "Error: sherpa-onnx submodule is at wrong commit."
echo ""
echo " Expected: $SHERPA_EXPECTED_SHA"
echo " Actual: $SHERPA_ACTUAL_SHA"
echo ""
echo "Please run:"
echo " git submodule update --init --recursive"
exit 1
fi
}
check_whisper_submodule
check_sherpa_submodule
# Sync models from R2 if needed
sync_models() {
local manifest="resources/models/manifest.json"
local models_dir="resources/models"
local stamp="$models_dir/.synced"
if [ ! -f "$manifest" ]; then
return 0 # No manifest = no models to sync
fi
# Determine the sha256sum command (shasum -a 256 on macOS, sha256sum on Linux)
if command -v sha256sum &> /dev/null; then
SHA256CMD="sha256sum"
elif command -v shasum &> /dev/null; then
SHA256CMD="shasum -a 256"
else
echo "Error: No SHA256 command found (sha256sum or shasum)"
exit 1
fi
# Parse JSON - try jq first, fall back to python
if command -v jq &> /dev/null; then
JSON_PARSER="jq"
elif command -v python3 &> /dev/null; then
JSON_PARSER="python3"
elif command -v python &> /dev/null; then
JSON_PARSER="python"
else
echo "Error: No JSON parser found (jq, python3, or python)"
exit 1
fi
# Get list of models from manifest (one per line to handle spaces in names)
if [ "$JSON_PARSER" = "jq" ]; then
model_list=$(jq -r 'keys[]' "$manifest")
else
model_list=$($JSON_PARSER -c "import json; [print(k) for k in json.load(open('$manifest')).keys()]" 2>/dev/null)
fi
# Separate kokoro zip/file entries from individual download entries
local kokoro_zip_hash=""
local kokoro_files=""
local individual_files=""
while IFS= read -r model; do
if [ "$model" = "kokoro-multi-lang-v1_0.zip" ]; then
if [ "$JSON_PARSER" = "jq" ]; then
kokoro_zip_hash=$(jq -r '."kokoro-multi-lang-v1_0.zip".hash' "$manifest")
else
kokoro_zip_hash=$($JSON_PARSER -c "import json; print(json.load(open('$manifest'))['kokoro-multi-lang-v1_0.zip']['hash'])")
fi
elif [[ "$model" == kokoro-multi-lang-v1_0/* ]]; then
if [ -z "$kokoro_files" ]; then
kokoro_files="$model"
else
kokoro_files="$kokoro_files"$'\n'"$model"
fi
else
if [ -z "$individual_files" ]; then
individual_files="$model"
else
individual_files="$individual_files"$'\n'"$model"
fi
fi
done <<< "$model_list"
# Compute manifest hash for stamp file comparison
local manifest_hash=$($SHA256CMD "$manifest" | cut -d' ' -f1)
# Fast path: check if already synced
if [ -f "$stamp" ] && [ "$(cat "$stamp")" = "$manifest_hash" ]; then
local all_exist=true
while IFS= read -r model; do
[ -z "$model" ] && continue
if [ ! -f "$models_dir/$model" ]; then
all_exist=false
break
fi
done <<< "$individual_files"
if [ "$all_exist" = true ] && [ -n "$kokoro_files" ]; then
while IFS= read -r model; do
[ -z "$model" ] && continue
if [ ! -f "$models_dir/$model" ]; then
all_exist=false
break
fi
done <<< "$kokoro_files"
fi
if [ "$all_exist" = true ]; then
return 0 # Already synced
fi
fi
# Check if any kokoro files are missing; download and extract zip if so
if [ -n "$kokoro_files" ]; then
local kokoro_missing=false
while IFS= read -r model; do
[ -z "$model" ] && continue
if [ ! -f "$models_dir/$model" ]; then
kokoro_missing=true
break
fi
done <<< "$kokoro_files"
if [ "$kokoro_missing" = true ]; then
local zip_path="$models_dir/kokoro-multi-lang-v1_0.zip"
echo "Downloading kokoro-multi-lang-v1_0.zip..."
curl -L --progress-bar -o "$zip_path" \
"https://vice-resources.pharr.org/$kokoro_zip_hash"
local actual_hash=$($SHA256CMD "$zip_path" | cut -d' ' -f1)
if [ "$actual_hash" != "$kokoro_zip_hash" ]; then
echo "Error: Downloaded file hash mismatch for kokoro-multi-lang-v1_0.zip"
echo " Expected: $kokoro_zip_hash"
echo " Actual: $actual_hash"
rm -f "$zip_path"
exit 1
fi
echo "Extracting kokoro-multi-lang-v1_0.zip..."
unzip -o -q "$zip_path" -d "$models_dir/"
rm -f "$zip_path"
fi
fi
# Download individual files (whisper models)
if [ -n "$individual_files" ]; then
while IFS= read -r model; do
[ -z "$model" ] && continue
if [ "$JSON_PARSER" = "jq" ]; then
expected_hash=$(jq -r ".\"$model\".hash" "$manifest")
else
expected_hash=$($JSON_PARSER -c "import json; print(json.load(open('$manifest'))['$model']['hash'])")
fi
local model_path="$models_dir/$model"
# Check if file exists with correct hash
if [ -f "$model_path" ]; then
actual_hash=$($SHA256CMD "$model_path" | cut -d' ' -f1)
if [ "$actual_hash" = "$expected_hash" ]; then
continue # File is up to date
fi
echo "Model $model has wrong hash, re-downloading..."
fi
echo "Downloading $model..."
mkdir -p "$(dirname "$model_path")"
curl -L --progress-bar -o "$model_path" \
"https://vice-resources.pharr.org/$expected_hash"
# Verify the download
actual_hash=$($SHA256CMD "$model_path" | cut -d' ' -f1)
if [ "$actual_hash" != "$expected_hash" ]; then
echo "Error: Downloaded file hash mismatch for $model"
echo " Expected: $expected_hash"
echo " Actual: $actual_hash"
rm -f "$model_path"
exit 1
fi
done <<< "$individual_files"
fi
# Write stamp file to skip verification on next build
echo "$manifest_hash" > "$stamp"
}
sync_models
# Detect OS
OS="$(uname -s)"
case "$OS" in
Darwin) OS_TYPE="macos" ;;
Linux) OS_TYPE="linux" ;;
*) echo "Unsupported OS: $OS"; exit 1 ;;
esac
# Default options
DO_CHECK=false
DO_TEST=false
DO_RELEASE=false
DO_UNIVERSAL=false
DO_VULKAN=false
# Parse arguments
for arg in "$@"; do
case "$arg" in
--check) DO_CHECK=true ;;
--test) DO_TEST=true ;;
--release) DO_RELEASE=true ;;
--universal) DO_UNIVERSAL=true ;;
--vulkan) DO_VULKAN=true ;;
--all)
DO_CHECK=true
DO_TEST=true
;;
--help)
head -16 "$0" | tail -14
exit 0
;;
*)
echo "Unknown option: $arg"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Build whisper-cpp
build_whisper() {
echo "=== Building whisper-cpp ==="
if [ "$OS_TYPE" = "macos" ]; then
# Disable GGML_NATIVE since we're building a universal binary.
# x86 flags (AVX, etc.) only affect x86_64 compilation; ARM uses NEON.
cmake -S whisper.cpp -B whisper.cpp/build_go \
-DBUILD_SHARED_LIBS=OFF \
-DGGML_CPU=ON \
-DGGML_METAL=ON \
-DGGML_BLAS=ON \
-DGGML_METAL_EMBED_LIBRARY=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" \
-DCMAKE_OSX_DEPLOYMENT_TARGET=13.4
elif [ "$OS_TYPE" = "linux" ]; then
# Disable GGML_NATIVE to avoid -march=native. Enable instruction sets
# safe for computers from ~2013+ (Haswell era, see build.bat for details).
if [ "$DO_VULKAN" = true ]; then
cmake -S whisper.cpp -B whisper.cpp/build_go \
-DBUILD_SHARED_LIBS=OFF \
-DGGML_CPU=ON \
-DGGML_OPENMP=ON \
-DGGML_NATIVE=OFF \
-DGGML_SSE42=ON \
-DGGML_AVX=ON \
-DGGML_AVX2=ON \
-DGGML_FMA=ON \
-DGGML_F16C=ON \
-DGGML_BMI2=ON \
-DGGML_VULKAN=ON \
-DCMAKE_BUILD_TYPE=Release
else
cmake -S whisper.cpp -B whisper.cpp/build_go \
-DBUILD_SHARED_LIBS=OFF \
-DGGML_CPU=ON \
-DGGML_OPENMP=ON \
-DGGML_NATIVE=OFF \
-DGGML_SSE42=ON \
-DGGML_AVX=ON \
-DGGML_AVX2=ON \
-DGGML_FMA=ON \
-DGGML_F16C=ON \
-DGGML_BMI2=ON \
-DCMAKE_BUILD_TYPE=Release
fi
fi
cmake --build whisper.cpp/build_go --parallel "$(nproc 2>/dev/null || sysctl -n hw.ncpu)"
echo "whisper-cpp built successfully."
}
# Build sherpa-onnx
build_sherpa() {
echo "=== Building sherpa-onnx ==="
if [ "$OS_TYPE" = "macos" ]; then
cmake -S sherpa-onnx -B sherpa-onnx/build_go \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_ONNX_ENABLE_TTS=ON \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_BINARY=OFF \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=OFF \
-DSHERPA_ONNX_BUILD_C_API_EXAMPLES=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" \
-DCMAKE_OSX_DEPLOYMENT_TARGET=13.4
elif [ "$OS_TYPE" = "linux" ]; then
cmake -S sherpa-onnx -B sherpa-onnx/build_go \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_ONNX_ENABLE_TTS=ON \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_BINARY=OFF \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION=OFF \
-DSHERPA_ONNX_BUILD_C_API_EXAMPLES=OFF \
-DCMAKE_BUILD_TYPE=Release
fi
cmake --build sherpa-onnx/build_go --parallel "$(nproc 2>/dev/null || sysctl -n hw.ncpu)"
echo "sherpa-onnx built successfully."
}
# Run checks (gofmt, staticcheck)
run_checks() {
echo "=== Running checks ==="
echo "Checking gofmt..."
GOFMT_OUTPUT=$(gofmt -l . 2>&1 || true)
if [ -n "$GOFMT_OUTPUT" ]; then
echo "The following files require reformatting with gofmt:"
echo "$GOFMT_OUTPUT"
echo "Run 'gofmt -w .' to fix."
exit 1
fi
echo "gofmt: OK"
echo "Running staticcheck..."
if ! command -v staticcheck &> /dev/null; then
echo "Installing staticcheck..."
go install honnef.co/go/tools/cmd/staticcheck@latest
fi
staticcheck ./...
echo "staticcheck: OK"
}
# Build vice
build_vice() {
echo "=== Building vice ==="
# Set version
git describe --tags --abbrev=8 --dirty --always --long > resources/version.txt
echo "Version: $(cat resources/version.txt)"
# Determine build tags
BUILD_TAGS=""
if [ "$OS_TYPE" = "macos" ]; then
BUILD_TAGS="static"
elif [ "$OS_TYPE" = "linux" ]; then
BUILD_TAGS="imguifreetype"
fi
if [ "$DO_RELEASE" = true ]; then
BUILD_TAGS="$BUILD_TAGS,downloadresources"
fi
# Build
if [ "$OS_TYPE" = "macos" ]; then
export MACOSX_DEPLOYMENT_TARGET='13.4'
export CGO_CFLAGS='-mmacosx-version-min=13.4'
export CGO_LDFLAGS='-mmacosx-version-min=13.4'
if [ "$DO_UNIVERSAL" = true ]; then
echo "Building universal binary..."
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -tags "$BUILD_TAGS" -o vice_amd64 ./cmd/vice
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -tags "$BUILD_TAGS" -o vice_arm64 ./cmd/vice
lipo -create -output vice vice_amd64 vice_arm64
rm vice_amd64 vice_arm64
else
CGO_ENABLED=1 GOOS=darwin go build -ldflags="-s -w" -tags "$BUILD_TAGS" -o vice ./cmd/vice
fi
elif [ "$OS_TYPE" = "linux" ]; then
if [ "$DO_VULKAN" = true ]; then
BUILD_TAGS="$BUILD_TAGS,vulkan"
fi
go build -tags "$BUILD_TAGS" -o vice ./cmd/vice
fi
echo "Build complete: ./vice"
}
# Run tests
run_tests() {
echo "=== Running tests ==="
go test -v ./...
echo "Tests passed."
}
# Check if whisper-cpp needs to be built
needs_whisper_build() {
if [ ! -f "whisper.cpp/build_go/src/libwhisper.a" ]; then
return 0
fi
if [ "$OS_TYPE" = "macos" ]; then
if [ ! -f "whisper.cpp/build_go/ggml/src/ggml-metal/libggml-metal.a" ]; then
return 0
fi
fi
return 1
}
# Check if sherpa-onnx needs to be built
needs_sherpa_build() {
if [ ! -f "sherpa-onnx/build_go/lib/libsherpa-onnx-c-api.a" ]; then
return 0
fi
return 1
}
# Main execution
if needs_whisper_build; then
build_whisper
fi
if needs_sherpa_build; then
build_sherpa
fi
if [ "$DO_CHECK" = true ]; then
run_checks
fi
build_vice
if [ "$DO_TEST" = true ]; then
run_tests
fi
echo "=== Done ==="