forked from 1312Delta/imgui-java-relocator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·328 lines (276 loc) · 11.8 KB
/
test.sh
File metadata and controls
executable file
·328 lines (276 loc) · 11.8 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
#!/usr/bin/env bash
#
# test.sh — Build natives for all platforms, package JARs, and run host tests.
#
# This script validates the full pipeline output:
# 1. Cross-compiles native libraries for all target platforms via Zig
# 2. Verifies each native artifact was produced
# 3. Packages JARs via Gradle
# 4. Runs Java unit tests (pure Java, no native load)
# 5. Runs a native-load smoke test on the host platform
#
# Prerequisites:
# - scripts/relocate.sh must have been run successfully
# - Zig and a JDK must be on PATH
#
# Usage:
# ./scripts/test.sh # full test suite
# ./scripts/test.sh --skip-cross # skip cross-compilation, only test host
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(dirname "$SCRIPT_DIR")"
source "$ROOT_DIR/config.env"
WORK_DIR="$ROOT_DIR/work/imgui-java"
BIN_DIR="$WORK_DIR/bin"
TARGET_PATH="${TARGET_PKG//./\/}"
SKIP_CROSS=false
FAIL=0
for arg in "$@"; do
case "$arg" in
--skip-cross) SKIP_CROSS=true ;;
esac
done
# ─── Helpers ────────────────────────────────────────────────────────────────
pass() { echo " ✓ $1"; }
fail() { echo " ✗ $1"; FAIL=1; }
check_prereqs() {
if [ ! -d "$WORK_DIR/imgui-binding/build/jni" ]; then
echo "ERROR: JNI sources not found. Run scripts/relocate.sh first."
exit 1
fi
command -v zig >/dev/null 2>&1 || { echo "ERROR: zig not found on PATH"; exit 1; }
command -v java >/dev/null 2>&1 || { echo "ERROR: java not found on PATH"; exit 1; }
}
# Maps a Zig target triple to the expected native library filename.
lib_filename() {
local target="$1"
case "$target" in
*windows*) echo "${LIB_NAME}.dll" ;;
*macos*) echo "lib${LIB_NAME}.dylib" ;;
*) echo "lib${LIB_NAME}.so" ;;
esac
}
# Returns the zig-out subdirectory for the given target.
# Zig puts .dll in bin/ and .so/.dylib in lib/.
zig_out_dir() {
local target="$1"
case "$target" in
*windows*) echo "$ROOT_DIR/zig-out/bin" ;;
*) echo "$ROOT_DIR/zig-out/lib" ;;
esac
}
# ─── Main ───────────────────────────────────────────────────────────────────
echo "╔══════════════════════════════════════════════════════════╗"
echo "║ imgui-relocator test suite ║"
echo "╠══════════════════════════════════════════════════════════╣"
echo "║ Library: $LIB_NAME"
echo "║ Package: $TARGET_PKG"
echo "║ Artifact: $ARTIFACT_PREFIX"
echo "║ Skip cross: $SKIP_CROSS"
echo "╚══════════════════════════════════════════════════════════╝"
echo ""
check_prereqs
# ─── Step 1: Cross-compile natives ──────────────────────────────────────────
TARGETS=(
"x86_64-linux-gnu"
"x86_64-windows-gnu"
"x86_64-macos"
"aarch64-macos"
)
if [ "$SKIP_CROSS" = false ]; then
echo "[1/5] Cross-compiling natives for all platforms..."
mkdir -p "$BIN_DIR"
for t in "${TARGETS[@]}"; do
printf " Building %-25s ... " "$t"
if zig build \
--release=fast \
-Dtarget="$t" \
-Dlib-name="$LIB_NAME" \
-Djni-dir="work/imgui-java/imgui-binding/build/jni" \
2>"$ROOT_DIR/work/zig-build-${t}.log"; then
# Copy artifact to bin/
local_lib=$(lib_filename "$t")
built_lib="$(zig_out_dir "$t")/$local_lib"
if [ -f "$built_lib" ]; then
cp "$built_lib" "$BIN_DIR/$local_lib"
pass "$t → $local_lib"
else
fail "$t — built but artifact not found at $built_lib"
fi
else
fail "$t — compilation failed (see work/zig-build-${t}.log)"
fi
done
else
echo "[1/5] Cross-compilation skipped (--skip-cross)."
# Still build for host
echo " Building for host platform..."
zig build \
--release=fast \
-Dlib-name="$LIB_NAME" \
-Djni-dir="work/imgui-java/imgui-binding/build/jni" \
2>"$ROOT_DIR/work/zig-build-host.log" \
&& pass "Host native build" \
|| fail "Host native build (see work/zig-build-host.log)"
# Copy host artifact to bin/
host_lib=$(lib_filename "$(uname -s)")
built_lib="$(zig_out_dir "$(uname -s)")/$host_lib"
if [ -f "$built_lib" ]; then
mkdir -p "$BIN_DIR"
cp "$built_lib" "$BIN_DIR/$host_lib"
fi
fi
echo ""
# ─── Step 2: Verify native artifacts ───────────────────────────────────────
echo "[2/5] Verifying native artifacts in bin/..."
if [ "$SKIP_CROSS" = false ]; then
for t in "${TARGETS[@]}"; do
expected=$(lib_filename "$t")
if [ -f "$BIN_DIR/$expected" ]; then
size=$(stat --printf="%s" "$BIN_DIR/$expected" 2>/dev/null \
|| stat -f "%z" "$BIN_DIR/$expected" 2>/dev/null)
pass "$expected ($(( size / 1024 )) KB)"
else
fail "$expected missing"
fi
done
else
host_lib=$(lib_filename "$(uname -s)")
if [ -f "$BIN_DIR/$host_lib" ]; then
pass "$host_lib present"
else
fail "$host_lib missing"
fi
fi
# On Linux, verify glibc compat — the .so should not require GLIBC_2.14+
if [ -f "$BIN_DIR/lib${LIB_NAME}.so" ] && command -v objdump >/dev/null 2>&1; then
echo ""
echo " Checking glibc version requirements..."
if objdump -T "$BIN_DIR/lib${LIB_NAME}.so" 2>/dev/null | grep -q 'GLIBC_2\.14'; then
fail "lib${LIB_NAME}.so requires GLIBC_2.14 (compat header not working)"
else
max_glibc=$(objdump -T "$BIN_DIR/lib${LIB_NAME}.so" 2>/dev/null \
| grep -oP 'GLIBC_\d+\.\d+(\.\d+)?' | sort -V | tail -1 || echo "unknown")
pass "No GLIBC_2.14 dependency (max: ${max_glibc:-none})"
fi
fi
echo ""
# ─── Step 3: Package JARs ──────────────────────────────────────────────────
echo "[3/5] Building JARs via Gradle..."
cd "$WORK_DIR"
if ./gradlew build -x test -x :example:compileJava --quiet 2>"$ROOT_DIR/work/gradle-build.log"; then
pass "Gradle build succeeded"
else
fail "Gradle build failed (see work/gradle-build.log)"
fi
# Verify JARs were produced
for module in imgui-binding imgui-lwjgl3 imgui-app; do
jar_dir="$module/build/libs"
if ls "$jar_dir"/*.jar >/dev/null 2>&1; then
jar_name=$(ls "$jar_dir"/*.jar | head -1 | xargs basename)
pass "$module → $jar_name"
else
fail "$module — no JAR produced"
fi
done
echo ""
# ─── Step 4: Run Java unit tests ───────────────────────────────────────────
echo "[4/5] Running Java unit tests..."
if ./gradlew imgui-binding:test --quiet 2>"$ROOT_DIR/work/gradle-test.log"; then
pass "Unit tests passed"
else
fail "Unit tests failed (see work/gradle-test.log)"
fi
echo ""
# ─── Step 5: Native load smoke test ────────────────────────────────────────
echo "[5/5] Native load smoke test on host platform..."
cd "$ROOT_DIR"
# Determine host native lib path
host_lib=$(lib_filename "$(uname -s)")
NATIVE_LIB_PATH="$BIN_DIR/$host_lib"
if [ ! -f "$NATIVE_LIB_PATH" ]; then
fail "Host native lib not found at $NATIVE_LIB_PATH — skipping load test"
else
# Build classpath from the compiled binding classes
BINDING_CLASSES="$WORK_DIR/imgui-binding/build/classes/java/main"
if [ ! -d "$BINDING_CLASSES" ]; then
fail "Binding classes not found at $BINDING_CLASSES"
else
# Smoke test: load the native library and verify a JNI symbol resolves.
#
# We do NOT trigger ImGui.<clinit> because it calls nInitJni() →
# ImFontAtlas.nInit() which dereferences native state that doesn't
# exist in a headless context (SIGSEGV). Instead we:
# 1. System.load() the .so directly
# 2. Use Class.forName with initialize=false to find ImGui
# 3. Verify a known native method exists via reflection
SMOKE_DIR="$ROOT_DIR/work/smoke-test"
mkdir -p "$SMOKE_DIR"
cat > "$SMOKE_DIR/NativeLoadTest.java" << 'JAVA_EOF'
import java.io.File;
import java.lang.reflect.Method;
public class NativeLoadTest {
public static void main(String[] args) {
String libPath = args[0];
// Step 1: Load the native library
System.out.println("Loading native library: " + libPath);
try {
System.load(new File(libPath).getAbsolutePath());
System.out.println(" OK: Native library loaded");
} catch (UnsatisfiedLinkError e) {
System.err.println(" FAIL: " + e.getMessage());
System.exit(1);
}
// Step 2: Find ImGui class without triggering static init
String imguiClass = args[1];
System.out.println("Resolving class: " + imguiClass);
try {
Class<?> imgui = Class.forName(imguiClass, false,
NativeLoadTest.class.getClassLoader());
System.out.println(" OK: Class found: " + imgui.getName());
// Step 3: Verify a known native method exists
boolean foundNative = false;
for (Method m : imgui.getDeclaredMethods()) {
if (m.getName().equals("nInitJni")) {
foundNative = true;
break;
}
}
if (foundNative) {
System.out.println(" OK: Native method nInitJni found");
} else {
System.err.println(" FAIL: nInitJni method not found");
System.exit(1);
}
} catch (ClassNotFoundException e) {
System.err.println(" FAIL: " + e.getMessage());
System.exit(1);
}
System.out.println("All smoke tests passed.");
}
}
JAVA_EOF
# Compile and run
if javac -cp "$BINDING_CLASSES" -d "$SMOKE_DIR" "$SMOKE_DIR/NativeLoadTest.java" 2>"$ROOT_DIR/work/smoke-compile.log"; then
if java -cp "$SMOKE_DIR:$BINDING_CLASSES" \
NativeLoadTest "$NATIVE_LIB_PATH" "${TARGET_PKG}.ImGui" 2>&1; then
pass "Native load + JNI symbol resolution OK"
else
fail "Native load smoke test failed"
fi
else
fail "Smoke test compilation failed (see work/smoke-compile.log)"
fi
fi
fi
echo ""
# ─── Summary ────────────────────────────────────────────────────────────────
echo "════════════════════════════════════════════════════════════"
if [ $FAIL -eq 0 ]; then
echo " All tests passed!"
else
echo " Some tests FAILED. Review the output above."
fi
echo "════════════════════════════════════════════════════════════"
exit $FAIL