forked from 0xShug0/audio.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_linux.sh
More file actions
executable file
·462 lines (438 loc) · 14.5 KB
/
Copy pathbuild_linux.sh
File metadata and controls
executable file
·462 lines (438 loc) · 14.5 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
#!/usr/bin/env bash
set -euo pipefail
CONDA_ENV=""
BUILD_DIR=""
BUILD_TYPE="RelWithDebInfo"
CUDA_MODE="auto"
CUDA_ARCH=""
VULKAN_MODE="off"
HIP_MODE="off"
GPU_TARGETS=""
USE_CCACHE="OFF"
WITH_TESTS="OFF"
WITH_EXAMPLES="OFF"
WITH_WARMBENCH="OFF"
AUDIOCPP_DEPLOYMENT_BUILD="OFF"
DEPLOYMENT_BUILD_SET="OFF"
AUDIOCPP_BUILD_NATIVE_MODEL_MANAGER="OFF"
AUDIOCPP_USE_SYSTEM_OPENSSL="OFF"
AUDIOCPP_BORINGSSL_ARCHIVE=""
AUDIOCPP_MODEL_SET="full"
AUDIOCPP_MODELS=""
NATIVE_CPU="ON"
LLAMAFILE="ON"
TARGETS=()
JOBS=""
while [[ $# -gt 0 ]]; do
case "$1" in
--conda-env)
CONDA_ENV="$2"
shift 2
;;
--build-dir)
BUILD_DIR="$2"
shift 2
;;
--build-type)
BUILD_TYPE="$2"
shift 2
;;
--backend)
if [[ "$2" == "cuda" ]]; then
CUDA_MODE="on"
VULKAN_MODE="off"
HIP_MODE="off"
elif [[ "$2" == "cpu" ]]; then
CUDA_MODE="off"
VULKAN_MODE="off"
HIP_MODE="off"
elif [[ "$2" == "vulkan" ]]; then
CUDA_MODE="off"
VULKAN_MODE="on"
HIP_MODE="off"
elif [[ "$2" == "hip" || "$2" == "rocm" ]]; then
CUDA_MODE="off"
VULKAN_MODE="off"
HIP_MODE="on"
else
echo "Unsupported legacy --backend value: $2" >&2
exit 1
fi
shift 2
;;
--cuda)
CUDA_MODE="$2"
shift 2
;;
--vulkan)
VULKAN_MODE="$2"
shift 2
;;
--hip)
HIP_MODE="$2"
shift 2
;;
--gpu-targets)
GPU_TARGETS="$2"
shift 2
;;
--cuda-arch)
CUDA_ARCH="$2"
shift 2
;;
--ccache)
USE_CCACHE="ON"
shift
;;
--with-tests)
WITH_TESTS="ON"
shift
;;
--with-examples)
WITH_EXAMPLES="ON"
shift
;;
--with-warmbench)
WITH_WARMBENCH="ON"
shift
;;
--deployment-build)
AUDIOCPP_DEPLOYMENT_BUILD="ON"
DEPLOYMENT_BUILD_SET="ON"
shift
;;
--no-deployment-build)
AUDIOCPP_DEPLOYMENT_BUILD="OFF"
DEPLOYMENT_BUILD_SET="ON"
shift
;;
--native-model-manager)
AUDIOCPP_BUILD_NATIVE_MODEL_MANAGER="ON"
shift
;;
--system-openssl)
AUDIOCPP_USE_SYSTEM_OPENSSL="ON"
shift
;;
--boringssl-archive)
AUDIOCPP_BORINGSSL_ARCHIVE="$2"
shift 2
;;
--model-set)
case "$2" in
full|core|custom)
AUDIOCPP_MODEL_SET="$2"
;;
*)
echo "--model-set must be full, core, or custom" >&2
exit 1
;;
esac
shift 2
;;
--models)
AUDIOCPP_MODELS="$2"
shift 2
;;
--native-cpu)
case "$2" in
ON|on|On|1|true|TRUE|yes|YES)
NATIVE_CPU="ON"
;;
OFF|off|Off|0|false|FALSE|no|NO)
NATIVE_CPU="OFF"
;;
*)
echo "--native-cpu must be ON or OFF" >&2
exit 1
;;
esac
shift 2
;;
--llamafile)
case "$2" in
ON|on|On|1|true|TRUE|yes|YES)
LLAMAFILE="ON"
;;
OFF|off|Off|0|false|FALSE|no|NO)
LLAMAFILE="OFF"
;;
*)
echo "--llamafile must be ON or OFF" >&2
exit 1
;;
esac
shift 2
;;
--target)
TARGETS+=("$2")
shift 2
;;
-j|--jobs)
JOBS="$2"
shift 2
;;
*)
echo "Unknown argument: $1" >&2
exit 1
;;
esac
done
if [[ "$AUDIOCPP_BUILD_NATIVE_MODEL_MANAGER" != "ON" ]]; then
if [[ "$AUDIOCPP_USE_SYSTEM_OPENSSL" == "ON" ]]; then
echo "--system-openssl requires --native-model-manager" >&2
exit 1
fi
if [[ -n "$AUDIOCPP_BORINGSSL_ARCHIVE" ]]; then
echo "--boringssl-archive requires --native-model-manager" >&2
exit 1
fi
fi
GENERATOR="Unix Makefiles"
if command -v ninja >/dev/null 2>&1; then
GENERATOR="Ninja"
fi
ENGINE_ENABLE_CUDA="OFF"
ENGINE_ENABLE_VULKAN="OFF"
case "$CUDA_MODE" in
on)
ENGINE_ENABLE_CUDA="ON"
;;
off)
ENGINE_ENABLE_CUDA="OFF"
;;
auto)
if command -v nvcc >/dev/null 2>&1; then
ENGINE_ENABLE_CUDA="ON"
fi
;;
*)
echo "Unknown --cuda mode: $CUDA_MODE (expected on, off, or auto)" >&2
exit 1
;;
esac
case "$VULKAN_MODE" in
on)
ENGINE_ENABLE_VULKAN="ON"
;;
off)
ENGINE_ENABLE_VULKAN="OFF"
;;
*)
echo "Unknown --vulkan mode: $VULKAN_MODE (expected on or off)" >&2
exit 1
;;
esac
ENGINE_ENABLE_HIP="OFF"
HIP_CLANG_C=""
HIP_CLANG_CXX=""
case "$HIP_MODE" in
on)
ENGINE_ENABLE_HIP="ON"
# ROCm ships its own clang; hipconfig -l prints the ROCm lib dir's
# parent on some installs, so prefer ROCM_PATH/HIP_PATH when set.
ROCM_HOME="${ROCM_PATH:-${HIP_PATH:-/opt/rocm}}"
HIP_CLANG_C="$ROCM_HOME/bin/clang"
HIP_CLANG_CXX="$ROCM_HOME/bin/clang++"
if [[ ! -x "$HIP_CLANG_CXX" ]] && command -v hipconfig >/dev/null 2>&1; then
HIP_CLANG_C="$(hipconfig -l)/clang"
HIP_CLANG_CXX="$(hipconfig -l)/clang++"
fi
if [[ ! -x "$HIP_CLANG_C" || ! -x "$HIP_CLANG_CXX" ]]; then
echo "ROCm clang not found (looked in $ROCM_HOME/bin and via hipconfig). Install ROCm or set ROCM_PATH." >&2
exit 1
fi
if [[ -z "$GPU_TARGETS" ]]; then
AMDGPU_ARCH=""
if [[ -x "$ROCM_HOME/bin/amdgpu-arch" ]]; then
AMDGPU_ARCH="$ROCM_HOME/bin/amdgpu-arch"
elif command -v amdgpu-arch >/dev/null 2>&1; then
AMDGPU_ARCH="$(command -v amdgpu-arch)"
fi
if [[ -n "$AMDGPU_ARCH" ]]; then
GPU_TARGETS="$("$AMDGPU_ARCH" 2>/dev/null | grep -xE 'gfx[0-9a-f]{3,}' | sort -u | paste -sd ';' -)"
fi
# Fall back to rocminfo (e.g. amdgpu-arch missing from minimal
# ROCm packages, or sandboxed environments where it prints nothing).
# Only accept full agent "Name: gfxXXX" lines — a loose grep would
# also catch fragments like "gfx11" from ISA description text.
if [[ -z "$GPU_TARGETS" ]]; then
ROCMINFO=""
if [[ -x "$ROCM_HOME/bin/rocminfo" ]]; then
ROCMINFO="$ROCM_HOME/bin/rocminfo"
elif command -v rocminfo >/dev/null 2>&1; then
ROCMINFO="$(command -v rocminfo)"
fi
if [[ -n "$ROCMINFO" ]]; then
GPU_TARGETS="$("$ROCMINFO" 2>/dev/null | grep -E '^[[:space:]]+Name:[[:space:]]+gfx[0-9a-f]{3,}[[:space:]]*$' | grep -oE 'gfx[0-9a-f]{3,}' | sort -u | paste -sd ';' -)"
fi
fi
if [[ -z "$GPU_TARGETS" ]]; then
echo "Could not detect GPU targets (tried amdgpu-arch and rocminfo)." >&2
echo "If this machine has no visible AMD GPU (VM/container), pass --gpu-targets gfxXXXX explicitly (semicolon-separated for several)." >&2
exit 1
fi
fi
GPU_TARGETS="${GPU_TARGETS//,/;}"
;;
off)
ENGINE_ENABLE_HIP="OFF"
;;
*)
echo "Unknown --hip mode: $HIP_MODE (expected on or off)" >&2
exit 1
;;
esac
# HIP builds default to deployment builds so the produced binaries embed
# package specs and run standalone outside the repo checkout. Other backends
# keep the historical OFF default; --no-deployment-build opts out.
if [[ "$ENGINE_ENABLE_HIP" == "ON" && "$DEPLOYMENT_BUILD_SET" == "OFF" ]]; then
AUDIOCPP_DEPLOYMENT_BUILD="ON"
fi
if [[ "$ENGINE_ENABLE_CUDA" == "ON" && "$ENGINE_ENABLE_VULKAN" == "ON" ]]; then
echo "CUDA and Vulkan backends cannot both be enabled by this script" >&2
exit 1
fi
ENABLED_GPU_BACKENDS=0
[[ "$ENGINE_ENABLE_CUDA" == "ON" ]] && ENABLED_GPU_BACKENDS=$((ENABLED_GPU_BACKENDS + 1))
[[ "$ENGINE_ENABLE_VULKAN" == "ON" ]] && ENABLED_GPU_BACKENDS=$((ENABLED_GPU_BACKENDS + 1))
[[ "$ENGINE_ENABLE_HIP" == "ON" ]] && ENABLED_GPU_BACKENDS=$((ENABLED_GPU_BACKENDS + 1))
if [[ "$ENABLED_GPU_BACKENDS" -gt 1 ]]; then
echo "CUDA, Vulkan, and HIP backends are mutually exclusive in this script" >&2
exit 1
fi
BACKEND_NAME="cpu"
if [[ "$ENGINE_ENABLE_CUDA" == "ON" ]]; then
BACKEND_NAME="cuda"
elif [[ "$ENGINE_ENABLE_VULKAN" == "ON" ]]; then
BACKEND_NAME="vulkan"
elif [[ "$ENGINE_ENABLE_HIP" == "ON" ]]; then
BACKEND_NAME="hip"
fi
BUILD_TYPE_NAME="$(printf '%s' "$BUILD_TYPE" | tr '[:upper:]' '[:lower:]')"
if [[ "$BUILD_TYPE_NAME" == "relwithdebinfo" ]]; then
BUILD_TYPE_NAME="release"
fi
if [[ -z "$BUILD_DIR" ]]; then
BUILD_DIR="build/linux-${BACKEND_NAME}-${BUILD_TYPE_NAME}"
fi
if [[ -z "$JOBS" ]]; then
JOBS="$(nproc 2>/dev/null || echo 8)"
fi
RUNNER=()
if [[ -n "$CONDA_ENV" ]]; then
if ! command -v conda >/dev/null 2>&1; then
echo "conda is required on PATH when --conda-env is used" >&2
exit 1
fi
if ! conda info --envs | awk '{print $1}' | grep -Fxq "$CONDA_ENV"; then
echo "Conda environment '$CONDA_ENV' was not found" >&2
exit 1
fi
RUNNER=(conda run -n "$CONDA_ENV")
fi
if [[ -n "$CONDA_ENV" ]]; then
echo "Using conda env: $CONDA_ENV"
fi
echo "Using generator: $GENERATOR"
echo "Using build dir: $BUILD_DIR"
echo "Including CUDA backend: $ENGINE_ENABLE_CUDA"
if [[ "$ENGINE_ENABLE_CUDA" == "ON" ]]; then
echo "CUDA architectures: ${CUDA_ARCH:-<portable default list>}"
fi
echo "Including Vulkan backend: $ENGINE_ENABLE_VULKAN"
echo "Including HIP backend: $ENGINE_ENABLE_HIP"
if [[ "$ENGINE_ENABLE_HIP" == "ON" ]]; then
echo "ROCm clang: $HIP_CLANG_CXX"
echo "GPU targets: $GPU_TARGETS"
fi
echo "Native CPU optimization: $NATIVE_CPU"
echo "llamafile SGEMM: $LLAMAFILE"
echo "Building examples: $WITH_EXAMPLES"
echo "Building tests: $WITH_TESTS"
echo "Building warmbench: $WITH_WARMBENCH"
echo "Deployment build: $AUDIOCPP_DEPLOYMENT_BUILD"
echo "Native model manager: $AUDIOCPP_BUILD_NATIVE_MODEL_MANAGER"
if [[ "$AUDIOCPP_BUILD_NATIVE_MODEL_MANAGER" == "ON" ]]; then
echo "System OpenSSL: $AUDIOCPP_USE_SYSTEM_OPENSSL"
echo "BoringSSL archive: ${AUDIOCPP_BORINGSSL_ARCHIVE:-<download at configure time>}"
fi
echo "Model composite: $AUDIOCPP_MODEL_SET"
if [[ -n "$AUDIOCPP_MODELS" ]]; then
echo "Selected models: $AUDIOCPP_MODELS"
fi
CMAKE_ARGS=(
-S .
-B "$BUILD_DIR"
-G "$GENERATOR"
-DCMAKE_BUILD_TYPE="$BUILD_TYPE"
-DENGINE_ENABLE_CUDA="$ENGINE_ENABLE_CUDA"
-DENGINE_ENABLE_VULKAN="$ENGINE_ENABLE_VULKAN"
-DENGINE_ENABLE_HIP="$ENGINE_ENABLE_HIP"
-DENGINE_ENABLE_NATIVE_CPU="$NATIVE_CPU"
-DENGINE_ENABLE_LLAMAFILE="$LLAMAFILE"
-DENGINE_BUILD_EXAMPLES="$WITH_EXAMPLES"
-DENGINE_BUILD_TESTS="$WITH_TESTS"
-DENGINE_BUILD_WARMBENCH="$WITH_WARMBENCH"
-DAUDIOCPP_DEPLOYMENT_BUILD="$AUDIOCPP_DEPLOYMENT_BUILD"
-DAUDIOCPP_BUILD_NATIVE_MODEL_MANAGER="$AUDIOCPP_BUILD_NATIVE_MODEL_MANAGER"
-DAUDIOCPP_USE_SYSTEM_OPENSSL="$AUDIOCPP_USE_SYSTEM_OPENSSL"
-UAUDIOCPP_BORINGSSL_ARCHIVE
-DAUDIOCPP_MODEL_SET="$AUDIOCPP_MODEL_SET"
-DAUDIOCPP_MODELS="$AUDIOCPP_MODELS"
)
if [[ -n "$AUDIOCPP_BORINGSSL_ARCHIVE" ]]; then
CMAKE_ARGS+=(-DAUDIOCPP_BORINGSSL_ARCHIVE="$AUDIOCPP_BORINGSSL_ARCHIVE")
fi
# Opt-in: ggml probes for ccache upstream, but audio.cpp's CMakeLists forces that
# probe off, so nothing caches anything unless a launcher is wired up by hand.
# Measured on this tree: ~2% slower on a cold build, ~14x faster on a rebuild.
# Configure prints a hint when ccache is installed and unused; this is the flag
# that hint points at. A launcher already set in the environment is left alone --
# -D wins over both the environment and the existing cache, and this flag means
# "cache the build", not "use ccache rather than whatever you already chose".
if [[ "$USE_CCACHE" == "ON" ]]; then
if ! command -v ccache >/dev/null 2>&1; then
echo "--ccache was passed but ccache is not installed" >&2
exit 1
fi
for lang in C CXX CUDA; do
launcher_var="CMAKE_${lang}_COMPILER_LAUNCHER"
if [[ -n "${!launcher_var:-}" ]]; then
echo "$launcher_var=${!launcher_var} is set; leaving the $lang launcher alone."
continue
fi
CMAKE_ARGS+=("-D${launcher_var}=ccache")
done
fi
if [[ "$ENGINE_ENABLE_HIP" == "ON" ]]; then
CMAKE_ARGS+=(
-DCMAKE_C_COMPILER="$HIP_CLANG_C"
-DCMAKE_CXX_COMPILER="$HIP_CLANG_CXX"
# GPU_BUILD_TARGETS/AMDGPU_TARGETS are sticky cache entries set by
# hip-config from GPU_TARGETS; without -U a previous configure's arch
# list would win over a new --gpu-targets value.
-UGPU_BUILD_TARGETS
-UAMDGPU_TARGETS
-DGPU_TARGETS="$GPU_TARGETS"
)
fi
if [[ "$ENGINE_ENABLE_CUDA" == "ON" && -n "$CUDA_ARCH" ]]; then
CMAKE_ARGS+=(
# CMAKE_CUDA_ARCHITECTURES is a sticky cache entry; -U it so a previous
# configure's arch list does not win over a new --cuda-arch value
# (mirrors the HIP --gpu-targets handling above).
-UCMAKE_CUDA_ARCHITECTURES
-DCMAKE_CUDA_ARCHITECTURES="$CUDA_ARCH"
)
fi
# RUNNER is empty unless --conda-env is used, and TARGETS is empty unless
# --target is used. bash 3.2 (stock macOS /bin/bash) and bash < 4.4 report an
# empty array as unbound under `set -u`; the ${arr[@]+"${arr[@]}"} form expands
# to nothing instead of failing.
${RUNNER[@]+"${RUNNER[@]}"} cmake "${CMAKE_ARGS[@]}"
BUILD_CMD=(${RUNNER[@]+"${RUNNER[@]}"} cmake --build "$BUILD_DIR" --parallel "$JOBS")
for target in ${TARGETS[@]+"${TARGETS[@]}"}; do
BUILD_CMD+=(--target "$target")
done
"${BUILD_CMD[@]}"