vendor: add Box3D support for WebAssembly (#7266)

vendor: add Box3D support for WebAssembly

Generate the WASM object directly from the existing build script so vendor binaries stay out of source control while JS target checks remain reproducible.
This commit is contained in:
nic-vdwalt
2026-08-09 20:19:41 +02:00
committed by GitHub
parent 9b45ef8239
commit f26ed4dccd
8 changed files with 58 additions and 7 deletions

View File

@@ -131,8 +131,8 @@ jobs:
./vendor/cgltf/src/build_cgltf.sh
./vendor/miniaudio/src/build_miniaudio.sh
./vendor/kb_text_shape/src/build_unix.sh
- name: Compile Box3D (Ubuntu ARM)
if: matrix.os == 'ubuntu-24.04-arm'
- name: Compile Box3D (Ubuntu)
if: matrix.os == 'ubuntu-latest' || matrix.os == 'ubuntu-24.04-arm'
run: ./vendor/box3d/src/build.sh
- name: Odin check
run: ./odin check examples/demo -vet

2
.gitignore vendored
View File

@@ -319,3 +319,5 @@ build/
cmake-build*/
CMakeLists.txt
sandbox/
vendor/box3d/lib/box3d_wasm.o

View File

@@ -2,6 +2,7 @@
package all
@(require) import "vendor:box2d"
@(require) import "vendor:box3d"
@(require) import "vendor:cgltf"
@(require) import "vendor:fontstash"
@(require) import "vendor:microui"

View File

@@ -11,16 +11,15 @@ BOX3D_SHARED :: #config(BOX3D_SHARED, false)
@(private)
LIB_PATH :: (
"lib/linux-amd64/libbox3d.a" when ODIN_OS == .Linux && ODIN_ARCH == .amd64 && !BOX3D_SHARED
"lib/box3d_wasm.o" when ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32
else "lib/linux-amd64/libbox3d.a" when ODIN_OS == .Linux && ODIN_ARCH == .amd64 && !BOX3D_SHARED
else "lib/linux-arm64/libbox3d.a" when ODIN_OS == .Linux && ODIN_ARCH == .arm64 && !BOX3D_SHARED
else "lib/darwin/libbox3d.a" when ODIN_OS == .Darwin && (ODIN_ARCH == .amd64 || ODIN_ARCH == .arm64) && !BOX3D_SHARED
else "lib/box3d.lib" when ODIN_OS == .Windows
else ""
)
when ODIN_OS == .Windows {
@(export)
foreign import lib "lib/box3d.lib"
} else when LIB_PATH != "" {
when LIB_PATH != "" {
when !#exists(LIB_PATH) {
#panic("Could not find the compiled Box3D library at \"" + LIB_PATH + "\", it can be compiled by running `\"" + ODIN_ROOT + "vendor/box3d/src/build.sh\"`")
}

4
vendor/box3d/box3d_wasm.odin vendored Normal file
View File

@@ -0,0 +1,4 @@
#+build wasm32, wasm64p32
package vendor_box3d
@(require) import _ "vendor:libc-shim"

View File

@@ -5,6 +5,8 @@ cc=${CC:-cc}
ar=${AR:-ar}
ranlib=${RANLIB:-ranlib}
lipo=${LIPO:-lipo}
wasm_cc=${WASM_CC:-clang}
wasm_ld=${WASM_LD:-wasm-ld}
ODIN_ROOT=${ODIN_ROOT:-$(cd "$(dirname "$0")/../../.." && pwd)}
cd "$ODIN_ROOT/vendor/box3d/src" || exit 1
@@ -87,3 +89,25 @@ Linux)
exit 1
;;
esac
echo "Building Box3D for wasm32"
mkdir -p build/wasm
for src in src/*.c; do
obj="build/wasm/$(basename "${src%.c}.o")"
"$wasm_cc" -c -O3 -std=gnu17 --target=wasm32 \
--sysroot="$ODIN_ROOT/vendor/libc-shim" \
-Iinclude \
-include wasm_compat.h \
-DBOX3D_DISABLE_SIMD \
-DNDEBUG \
"$src" -o "$obj"
done
"$wasm_cc" -c -O3 -std=gnu17 --target=wasm32 \
--sysroot="$ODIN_ROOT/vendor/libc-shim" \
-Iinclude \
-include wasm_compat.h \
-DBOX3D_DISABLE_SIMD \
-DNDEBUG \
wasm_compat.c -o build/wasm/wasm_compat.o
"$wasm_ld" -r -o ../lib/box3d_wasm.o build/wasm/*.o
rm -rf build/wasm

8
vendor/box3d/src/wasm_compat.c vendored Normal file
View File

@@ -0,0 +1,8 @@
#include <stdio.h>
int fscanf(FILE* restrict stream, const char* restrict format, ...)
{
(void)stream;
(void)format;
return -1;
}

13
vendor/box3d/src/wasm_compat.h vendored Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#include <stdio.h>
#ifndef PRIx64
#define PRIx64 "llx"
#endif
#ifndef PRIu64
#define PRIu64 "llu"
#endif
int fscanf(FILE* restrict stream, const char* restrict format, ...);