From f26ed4dccd5522170a5c9710bcf1963c0141a7fd Mon Sep 17 00:00:00 2001 From: nic-vdwalt <36562088+nic-vdwalt@users.noreply.github.com> Date: Sun, 9 Aug 2026 20:19:41 +0200 Subject: [PATCH] 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. --- .github/workflows/ci.yml | 4 ++-- .gitignore | 2 ++ examples/all/all_vendor_js.odin | 1 + vendor/box3d/box3d.odin | 9 ++++----- vendor/box3d/box3d_wasm.odin | 4 ++++ vendor/box3d/src/build.sh | 24 ++++++++++++++++++++++++ vendor/box3d/src/wasm_compat.c | 8 ++++++++ vendor/box3d/src/wasm_compat.h | 13 +++++++++++++ 8 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 vendor/box3d/box3d_wasm.odin create mode 100644 vendor/box3d/src/wasm_compat.c create mode 100644 vendor/box3d/src/wasm_compat.h diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b3ae67161..3874a65ab 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.gitignore b/.gitignore index bf0efe2a0..56a75c788 100644 --- a/.gitignore +++ b/.gitignore @@ -319,3 +319,5 @@ build/ cmake-build*/ CMakeLists.txt sandbox/ + +vendor/box3d/lib/box3d_wasm.o diff --git a/examples/all/all_vendor_js.odin b/examples/all/all_vendor_js.odin index d7975541d..128747c05 100644 --- a/examples/all/all_vendor_js.odin +++ b/examples/all/all_vendor_js.odin @@ -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" diff --git a/vendor/box3d/box3d.odin b/vendor/box3d/box3d.odin index 25be6ebad..a7de1325e 100644 --- a/vendor/box3d/box3d.odin +++ b/vendor/box3d/box3d.odin @@ -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\"`") } diff --git a/vendor/box3d/box3d_wasm.odin b/vendor/box3d/box3d_wasm.odin new file mode 100644 index 000000000..bb61e0a3d --- /dev/null +++ b/vendor/box3d/box3d_wasm.odin @@ -0,0 +1,4 @@ +#+build wasm32, wasm64p32 +package vendor_box3d + +@(require) import _ "vendor:libc-shim" diff --git a/vendor/box3d/src/build.sh b/vendor/box3d/src/build.sh index 0513d8ea5..b116d1672 100755 --- a/vendor/box3d/src/build.sh +++ b/vendor/box3d/src/build.sh @@ -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 diff --git a/vendor/box3d/src/wasm_compat.c b/vendor/box3d/src/wasm_compat.c new file mode 100644 index 000000000..c9c169c1d --- /dev/null +++ b/vendor/box3d/src/wasm_compat.c @@ -0,0 +1,8 @@ +#include + +int fscanf(FILE* restrict stream, const char* restrict format, ...) +{ + (void)stream; + (void)format; + return -1; +} diff --git a/vendor/box3d/src/wasm_compat.h b/vendor/box3d/src/wasm_compat.h new file mode 100644 index 000000000..60073a58b --- /dev/null +++ b/vendor/box3d/src/wasm_compat.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +#ifndef PRIx64 +#define PRIx64 "llx" +#endif + +#ifndef PRIu64 +#define PRIu64 "llu" +#endif + +int fscanf(FILE* restrict stream, const char* restrict format, ...);