forked from rhasspy/openWakeWord-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·332 lines (285 loc) · 9.12 KB
/
build.sh
File metadata and controls
executable file
·332 lines (285 loc) · 9.12 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
#!/usr/bin/env bash
set -euo pipefail
# Build PC executable from Linux/macOS/Windows.
# - Provisions ONNX Runtime package (headers + libs)
# - Then builds src/main.cpp -> build/openwakeword(.exe)
#
# Usage:
# ./build.sh [--onnxruntime 1.23.2]
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
PARENT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
ORT_VERSION_DEFAULT="1.23.2"
ORT_VERSION="$ORT_VERSION_DEFAULT"
install_host_tool_if_missing() {
local cmd="$1"
local apt_pkg="$2"
local dnf_pkg="$3"
local yum_pkg="$4"
local pacman_pkg="$5"
local brew_pkg="$6"
local winget_id="$7"
local choco_pkg="$8"
if command -v "$cmd" >/dev/null 2>&1; then
return 0
fi
echo "[build] (setup) missing tool: $cmd"
case "$(uname -s)" in
Linux*)
if command -v apt-get >/dev/null 2>&1; then
if command -v sudo >/dev/null 2>&1; then
sudo apt-get update || true
sudo apt-get install -y "$apt_pkg" || true
else
apt-get update || true
apt-get install -y "$apt_pkg" || true
fi
elif command -v dnf >/dev/null 2>&1; then
if command -v sudo >/dev/null 2>&1; then
sudo dnf install -y "$dnf_pkg" || true
else
dnf install -y "$dnf_pkg" || true
fi
elif command -v yum >/dev/null 2>&1; then
if command -v sudo >/dev/null 2>&1; then
sudo yum install -y "$yum_pkg" || true
else
yum install -y "$yum_pkg" || true
fi
elif command -v pacman >/dev/null 2>&1; then
if command -v sudo >/dev/null 2>&1; then
sudo pacman -Sy --noconfirm "$pacman_pkg" || true
else
pacman -Sy --noconfirm "$pacman_pkg" || true
fi
fi
;;
Darwin*)
if command -v brew >/dev/null 2>&1; then
brew install "$brew_pkg" || true
fi
;;
MINGW*|MSYS*|CYGWIN*)
if command -v winget >/dev/null 2>&1 && [[ -n "$winget_id" ]]; then
winget install -e --id "$winget_id" --accept-package-agreements --accept-source-agreements || true
elif command -v choco >/dev/null 2>&1 && [[ -n "$choco_pkg" ]]; then
choco install -y "$choco_pkg" || true
fi
;;
esac
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "[build] [ERROR] Required tool still missing after auto-install attempt: $cmd" >&2
exit 2
fi
}
usage() {
cat <<EOF
Usage: $(basename "$0") [--onnxruntime <version>]
Examples:
$(basename "$0")
$(basename "$0") --onnxruntime 1.23.2
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--onnxruntime)
shift
[[ $# -gt 0 ]] || { echo "[build] Missing value for --onnxruntime" >&2; exit 2; }
ORT_VERSION="$1"
shift
;;
*)
echo "[build] Unknown argument: $1" >&2
usage
exit 2
;;
esac
done
OS_RAW="$(uname -s)"
ARCH_RAW="$(uname -m)"
OS=""
ORT_OS=""
ARCH=""
EXT=""
case "$OS_RAW" in
Linux*)
OS="linux"
ORT_OS="linux"
EXT="tgz"
;;
Darwin*)
OS="macos"
ORT_OS="osx"
EXT="tgz"
;;
MINGW*|MSYS*|CYGWIN*)
OS="windows"
ORT_OS="win"
EXT="zip"
;;
*)
echo "[build] [ERROR] Unsupported OS: $OS_RAW" >&2
exit 2
;;
esac
case "$ARCH_RAW" in
x86_64|amd64)
ARCH="x64"
;;
aarch64|arm64)
ARCH="arm64"
;;
*)
echo "[build] [ERROR] Unsupported CPU architecture: $ARCH_RAW" >&2
exit 2
;;
esac
install_host_tool_if_missing curl curl curl curl curl curl cURL curl
if [[ "$EXT" == "tgz" ]]; then
install_host_tool_if_missing tar tar tar tar tar gnu-tar "" ""
else
install_host_tool_if_missing unzip unzip unzip unzip unzip unzip "" unzip
fi
if [[ "$OS" != "windows" ]]; then
install_host_tool_if_missing g++ g++ gcc-c++ gcc-c++ gcc gcc "" mingw
fi
ORT_PACKAGE="onnxruntime-${ORT_OS}-${ARCH}-${ORT_VERSION}"
ORT_URL="https://github.com/microsoft/onnxruntime/releases/download/v${ORT_VERSION}/${ORT_PACKAGE}.${EXT}"
TARGET_ID="${OS}-${ARCH}"
ORT_ROOT="$PARENT_DIR/onnxruntime-pc"
ORT_VER_DIR="$ORT_ROOT/$ORT_VERSION/$TARGET_ID"
ORT_INCLUDE_DIR="$ORT_VER_DIR/include"
ORT_LIB_DIR="$ORT_VER_DIR/lib"
CACHE_DIR="$PARENT_DIR/.cache/onnxruntime-pc/${ORT_VERSION}"
CACHED_ARCHIVE="$CACHE_DIR/${ORT_PACKAGE}.${EXT}"
mkdir -p "$CACHE_DIR" "$ORT_INCLUDE_DIR" "$ORT_LIB_DIR"
have_headers=0
[[ -f "$ORT_INCLUDE_DIR/onnxruntime_cxx_api.h" ]] && have_headers=1
have_libs=0
case "$OS" in
linux)
[[ -f "$ORT_LIB_DIR/libonnxruntime.so" ]] && have_libs=1
;;
macos)
[[ -f "$ORT_LIB_DIR/libonnxruntime.dylib" ]] && have_libs=1
;;
windows)
[[ -f "$ORT_LIB_DIR/onnxruntime.lib" && -f "$ORT_LIB_DIR/onnxruntime.dll" ]] && have_libs=1
;;
esac
if [[ "$have_headers" -ne 1 || "$have_libs" -ne 1 ]]; then
echo "[build] Provisioning ONNX Runtime v${ORT_VERSION} for ${OS}/${ARCH}"
if [[ ! -s "$CACHED_ARCHIVE" ]]; then
echo "[build] Downloading: $ORT_URL"
curl -fL --retry 3 --retry-delay 1 -o "$CACHED_ARCHIVE" "$ORT_URL"
else
echo "[build] Using cached archive: $CACHED_ARCHIVE"
fi
tmp_dir="$(mktemp -d)"
cleanup_tmp() {
rm -rf "$tmp_dir"
}
trap cleanup_tmp EXIT
case "$EXT" in
tgz)
tar -xzf "$CACHED_ARCHIVE" -C "$tmp_dir"
;;
zip)
unzip -q "$CACHED_ARCHIVE" -d "$tmp_dir"
;;
esac
extracted_dir="$tmp_dir/$ORT_PACKAGE"
if [[ ! -d "$extracted_dir" ]]; then
echo "[build] [ERROR] Extracted package directory not found: $extracted_dir" >&2
exit 2
fi
if [[ ! -d "$extracted_dir/include" || ! -d "$extracted_dir/lib" ]]; then
echo "[build] [ERROR] ONNX Runtime package missing include/ or lib/." >&2
exit 2
fi
rm -rf "$ORT_INCLUDE_DIR" "$ORT_LIB_DIR"
mkdir -p "$ORT_INCLUDE_DIR" "$ORT_LIB_DIR"
cp -a "$extracted_dir/include/." "$ORT_INCLUDE_DIR/"
case "$OS" in
linux)
cp -a "$extracted_dir/lib/libonnxruntime.so"* "$ORT_LIB_DIR/"
;;
macos)
cp -a "$extracted_dir/lib/libonnxruntime"*.dylib "$ORT_LIB_DIR/"
;;
windows)
cp -a "$extracted_dir/lib/onnxruntime.lib" "$ORT_LIB_DIR/"
cp -a "$extracted_dir/lib/onnxruntime.dll" "$ORT_LIB_DIR/"
if [[ -f "$extracted_dir/lib/libonnxruntime.dll.a" ]]; then
cp -a "$extracted_dir/lib/libonnxruntime.dll.a" "$ORT_LIB_DIR/"
fi
;;
esac
trap - EXIT
cleanup_tmp
else
echo "[build] ONNX Runtime already present: $ORT_VER_DIR"
fi
rm -rf "$PARENT_DIR/.cache"
BUILD_DIR="$PARENT_DIR/build-pc/$TARGET_ID"
mkdir -p "$BUILD_DIR"
MARKER_FILE="$BUILD_DIR/onnxruntime-version"
SRC_FILE="$SCRIPT_DIR/src/main.cpp"
OUT_FILE="$BUILD_DIR/openwakeword"
if [[ "$OS" == "windows" ]]; then
OUT_FILE="${OUT_FILE}.exe"
fi
if [[ "$OS" == "windows" ]]; then
to_win_path() {
local p="$1"
if command -v cygpath >/dev/null 2>&1; then
cygpath -w "$p"
else
printf '%s\n' "$p"
fi
}
if command -v cl.exe >/dev/null 2>&1; then
echo "[build] Building with cl.exe -> $OUT_FILE"
SRC_WIN="$(to_win_path "$SRC_FILE")"
INC_WIN="$(to_win_path "$ORT_INCLUDE_DIR")"
LIB_WIN="$(to_win_path "$ORT_LIB_DIR")"
OUT_WIN="$(to_win_path "$OUT_FILE")"
cl.exe /nologo /std:c++20 /EHsc /O2 /W4 /I"$INC_WIN" "$SRC_WIN" /link /LIBPATH:"$LIB_WIN" onnxruntime.lib /OUT:"$OUT_WIN"
elif command -v g++ >/dev/null 2>&1 && [[ -f "$ORT_LIB_DIR/libonnxruntime.dll.a" ]]; then
echo "[build] Building with g++ -> $OUT_FILE"
g++ -o "$OUT_FILE" \
-I"$ORT_INCLUDE_DIR" \
-L"$ORT_LIB_DIR" \
-O2 -std=c++20 -Wall -Wextra \
"$SRC_FILE" \
-lpthread -lonnxruntime
else
echo "[build] [ERROR] On Windows, use Visual Studio Developer shell (cl.exe), or provide MinGW import lib libonnxruntime.dll.a." >&2
exit 2
fi
else
CXX="${CXX:-g++}"
if ! command -v "$CXX" >/dev/null 2>&1; then
echo "[build] [ERROR] Compiler not found: $CXX" >&2
exit 2
fi
RPATH_FLAG="-Wl,-rpath,$ORT_LIB_DIR"
if [[ "$OS" == "macos" ]]; then
RPATH_FLAG="-Wl,-rpath,$ORT_LIB_DIR"
fi
echo "[build] Building with $CXX -> $OUT_FILE"
"$CXX" -o "$OUT_FILE" \
-I"$ORT_INCLUDE_DIR" \
-L"$ORT_LIB_DIR" \
-O2 -std=c++20 -Wall -Wextra \
"$RPATH_FLAG" \
"$SRC_FILE" \
-lpthread -lonnxruntime
fi
printf '%s\n' "$ORT_VERSION" > "$MARKER_FILE"
echo "[build] Done: $OUT_FILE"
echo "[build] Marker: $MARKER_FILE ($ORT_VERSION)"