forked from rhasspy/openWakeWord-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-androidTermux-from-pc.sh
More file actions
executable file
·343 lines (293 loc) · 10.9 KB
/
build-androidTermux-from-pc.sh
File metadata and controls
executable file
·343 lines (293 loc) · 10.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
#!/usr/bin/env bash
set -euo pipefail
# Build Android executable from macOS/PC.
# - Provisions ONNX Runtime Android AAR (headers + libonnxruntime.so for each ABI)
# - Then runs: make -f Makefile-toAndroidTermuxFromPC android-exe
#
# Usage:
# ./build-androidTermux-from-pc.sh [--onnxruntime 1.23.2] [--ANDROID_SDK /path/to/Android/Sdk]
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
ORT_VERSION_DEFAULT="1.23.2"
ORT_VERSION="$ORT_VERSION_DEFAULT"
ANDROID_SDK_OVERRIDE=""
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 "[openWakeWord] (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 "[openWakeWord] [ERROR] Required tool still missing after auto-install attempt: $cmd" >&2
exit 2
fi
}
usage() {
cat <<EOF
Usage: $(basename "$0") [--onnxruntime <version>] [--ANDROID_SDK <path>]
Examples:
$(basename "$0")
$(basename "$0") --onnxruntime 1.23.2
$(basename "$0") --ANDROID_SDK "$HOME/Library/Android/sdk"
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--onnxruntime)
shift
[[ $# -gt 0 ]] || { echo "[openWakeWord] Missing value for --onnxruntime" >&2; exit 2; }
ORT_VERSION="$1"
shift
;;
--ANDROID_SDK)
shift
[[ $# -gt 0 ]] || { echo "[openWakeWord] Missing value for --ANDROID_SDK" >&2; exit 2; }
ANDROID_SDK_OVERRIDE="$1"
shift
;;
*)
echo "[openWakeWord] Unknown argument: $1" >&2
usage
exit 2
;;
esac
done
install_host_tool_if_missing curl curl curl curl curl curl cURL curl
install_host_tool_if_missing unzip unzip unzip unzip unzip unzip "" unzip
install_host_tool_if_missing make make make make make make "" make
SDK=""
if [[ -n "$ANDROID_SDK_OVERRIDE" ]]; then
SDK="$ANDROID_SDK_OVERRIDE"
elif [[ -n "${ANDROID_SDK_ROOT:-}" ]]; then
SDK="$ANDROID_SDK_ROOT"
elif [[ -n "${ANDROID_HOME:-}" ]]; then
SDK="$ANDROID_HOME"
fi
if [[ -z "$SDK" ]]; then
echo "[openWakeWord] [ERROR] Android SDK path not set." >&2
echo "[openWakeWord] Use --ANDROID_SDK <path> or set ANDROID_SDK_ROOT/ANDROID_HOME env." >&2
exit 2
fi
if [[ ! -d "$SDK" ]]; then
echo "[openWakeWord] [ERROR] Android SDK not found at: $SDK" >&2
exit 2
fi
ROOT_DIR="$SCRIPT_DIR"
PARENT_DIR="$(cd "$ROOT_DIR/.." && pwd)"
ORT_ROOT="$PARENT_DIR/onnxruntime-android"
ORT_VER_DIR="$ORT_ROOT/$ORT_VERSION"
ORT_HEADERS_DIR="$ORT_VER_DIR/headers"
ORT_JNI_DIR="$ORT_VER_DIR/jni"
AAR_URL="https://repo1.maven.org/maven2/com/microsoft/onnxruntime/onnxruntime-android/${ORT_VERSION}/onnxruntime-android-${ORT_VERSION}.aar"
# Temporary download cache (we delete the .aar after successful provisioning)
CACHE_DIR="$PARENT_DIR/.cache/onnxruntime-android/${ORT_VERSION}"
CACHED_AAR="$CACHE_DIR/onnxruntime-android-${ORT_VERSION}.aar"
# Keep ABIs aligned with Makefile-toAndroidTermuxFromPC defaults.
ABIS=(arm64-v8a armeabi-v7a x86_64)
if [[ -f "$ORT_HEADERS_DIR/onnxruntime_cxx_api.h" ]]; then
have_headers=1
else
have_headers=0
fi
have_all_libs=1
for abi in "${ABIS[@]}"; do
if [[ ! -f "$ORT_JNI_DIR/$abi/libonnxruntime.so" ]]; then
have_all_libs=0
break
fi
done
if [[ "$have_headers" -ne 1 || "$have_all_libs" -ne 1 ]]; then
echo "[openWakeWord] Provisioning ONNX Runtime Android AAR v${ORT_VERSION}"
mkdir -p "$CACHE_DIR" "$ORT_HEADERS_DIR" "$ORT_JNI_DIR"
if [[ ! -s "$CACHED_AAR" ]]; then
echo "[openWakeWord] Downloading: $AAR_URL"
curl -fL --retry 3 --retry-delay 1 -o "$CACHED_AAR" "$AAR_URL"
else
echo "[openWakeWord] Using cached AAR: $CACHED_AAR"
fi
tmp_dir="$(mktemp -d)"
cleanup_tmp() {
rm -rf "$tmp_dir"
}
trap cleanup_tmp EXIT
unzip -q "$CACHED_AAR" -d "$tmp_dir/aar"
if [[ ! -d "$tmp_dir/aar/headers" ]]; then
echo "[openWakeWord] [ERROR] AAR does not contain expected 'headers/' directory." >&2
exit 2
fi
# Replace headers for this ORT version with a clean copy from the AAR.
rm -rf "$ORT_HEADERS_DIR"
mkdir -p "$ORT_HEADERS_DIR"
cp -a "$tmp_dir/aar/headers/." "$ORT_HEADERS_DIR/"
for abi in "${ABIS[@]}"; do
so_src="$tmp_dir/aar/jni/$abi/libonnxruntime.so"
if [[ ! -f "$so_src" ]]; then
echo "[openWakeWord] [ERROR] Could not find libonnxruntime.so for ABI '$abi' in AAR." >&2
echo "[openWakeWord] Looked for: $so_src" >&2
exit 2
fi
mkdir -p "$ORT_JNI_DIR/$abi"
cp -a "$so_src" "$ORT_JNI_DIR/$abi/"
done
# Shim header: allow #include <onnxruntime_cxx_api.h>
if [[ ! -f "$ORT_HEADERS_DIR/onnxruntime_cxx_api.h" ]]; then
nested_cxx="$ORT_HEADERS_DIR/onnxruntime/core/session/onnxruntime_cxx_api.h"
if [[ -f "$nested_cxx" ]]; then
cat >"$ORT_HEADERS_DIR/onnxruntime_cxx_api.h" <<'EOF'
#pragma once
// Shim header for projects that include <onnxruntime_cxx_api.h> from include root.
// The Android AAR bundles headers under onnxruntime/core/session/.
#include "onnxruntime/core/session/onnxruntime_cxx_api.h"
EOF
fi
fi
if [[ ! -f "$ORT_HEADERS_DIR/onnxruntime_cxx_api.h" ]]; then
echo "[openWakeWord] [ERROR] Headers were not provisioned correctly into: $ORT_HEADERS_DIR" >&2
exit 2
fi
# We don't need the original AAR anymore once required files are copied.
# Keep provisioned outputs (onnxruntime-android/<ver>/...), but delete downloads.
rm -f "$CACHED_AAR" || true
# If the version cache dir is empty after deleting the AAR, remove it too.
rmdir "$CACHE_DIR" 2>/dev/null || true
rmdir "$(dirname "$CACHE_DIR")" 2>/dev/null || true
cleanup_tmp
trap - EXIT
else
echo "[openWakeWord] ONNX Runtime already present: $ORT_VER_DIR"
fi
echo "[openWakeWord] Building via Makefile-toAndroidTermuxFromPC"
make -f Makefile-toAndroidTermuxFromPC android-exe ANDROID_SDK_ROOT="$SDK" ORT_VERSION="$ORT_VERSION"
echo "[openWakeWord] Provisioning libc++_shared.so into build outputs (for Termux runtime)"
# We want Termux to be able to run the executable without having access to the NDK.
# The executable is linked against the NDK C++ shared runtime (libc++_shared.so).
# Copy it next to each built executable, so startInTermux.sh only needs LD_LIBRARY_PATH
# pointing to ORT libs (and the dynamic loader will find libc++_shared.so next to exe).
NDK_DIR=""
if [[ -d "$SDK/ndk" ]]; then
# pick newest installed NDK (same approach as Makefile)
NDK_DIR="$(ls -1d "$SDK/ndk/"* 2>/dev/null | sort -V | tail -n 1 || true)"
fi
if [[ -z "$NDK_DIR" || ! -d "$NDK_DIR" ]]; then
echo "[openWakeWord] [ERROR] Could not detect NDK under: $SDK/ndk" >&2
echo "[openWakeWord] Install NDK (Side by side) in Android Studio SDK Manager." >&2
exit 2
fi
HOST_PREBUILT=""
PREBUILT_DIR="$NDK_DIR/toolchains/llvm/prebuilt"
if [[ ! -d "$PREBUILT_DIR" ]]; then
echo "[openWakeWord] [ERROR] NDK prebuilt toolchain not found under: $NDK_DIR/toolchains/llvm/prebuilt" >&2
exit 2
fi
HOST_OS_RAW="$(uname -s)"
case "$HOST_OS_RAW" in
Darwin*)
prebuilt_candidates=(darwin-arm64 darwin-x86_64)
;;
Linux*)
prebuilt_candidates=(linux-x86_64 linux-arm64)
;;
MINGW*|MSYS*|CYGWIN*)
prebuilt_candidates=(windows-x86_64 windows)
;;
*)
prebuilt_candidates=()
;;
esac
for candidate in "${prebuilt_candidates[@]}"; do
if [[ -d "$PREBUILT_DIR/$candidate" ]]; then
HOST_PREBUILT="$PREBUILT_DIR/$candidate"
break
fi
done
if [[ -z "$HOST_PREBUILT" ]]; then
echo "[openWakeWord] [ERROR] No compatible NDK prebuilt found for host '$HOST_OS_RAW'." >&2
echo "[openWakeWord] Available prebuilt dirs:" >&2
ls -1 "$PREBUILT_DIR" 2>/dev/null >&2 || true
exit 2
fi
SYSROOT="$HOST_PREBUILT/sysroot"
copy_libcxx() {
local abi="$1"
local arch_dir=""
case "$abi" in
arm64-v8a) arch_dir="aarch64-linux-android" ;;
armeabi-v7a) arch_dir="arm-linux-androideabi" ;;
x86_64) arch_dir="x86_64-linux-android" ;;
x86) arch_dir="i686-linux-android" ;;
*)
echo "[openWakeWord] [WARN] Unknown ABI for libc++_shared.so copy: $abi" >&2
return 0
;;
esac
local src="$SYSROOT/usr/lib/$arch_dir/libc++_shared.so"
local dst_dir="$PARENT_DIR/build-android/$abi"
local dst="$dst_dir/libc++_shared.so"
if [[ ! -f "$src" ]]; then
echo "[openWakeWord] [ERROR] libc++_shared.so not found for ABI '$abi'" >&2
echo "[openWakeWord] Looked for: $src" >&2
echo "[openWakeWord] NDK: $NDK_DIR" >&2
return 2
fi
mkdir -p "$dst_dir"
cp -a "$src" "$dst"
printf '%s\n' "$ORT_VERSION" > "$dst_dir/onnxruntime-version"
echo "[openWakeWord] Copied libc++_shared.so for $abi -> $dst"
}
for abi in "${ABIS[@]}"; do
copy_libcxx "$abi"
done