Skip to content

Commit 5bb1c96

Browse files
authored
Merge pull request #141 from sysprog21/wasm
Add native WebAssembly backend
2 parents 16193dc + 17b4eae commit 5bb1c96

File tree

11 files changed

+1017
-353
lines changed

11 files changed

+1017
-353
lines changed

Makefile

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ ifneq ($(findstring emcc,$(CC_VERSION)),)
1616
CC_IS_EMCC := 1
1717
endif
1818

19-
# Enforce SDL backend for Emscripten builds (skip during config targets)
19+
# Enforce compatible backend for Emscripten builds (skip during config targets)
2020
ifeq ($(filter $(check_goal),config defconfig),)
2121
ifeq ($(CC_IS_EMCC),1)
22-
ifneq ($(CONFIG_BACKEND_SDL),y)
23-
$(error Emscripten (WebAssembly) builds require SDL backend. Please run: env CC=emcc make defconfig)
22+
ifneq ($(CONFIG_BACKEND_WASM),y)
23+
$(error Emscripten (WebAssembly) builds require WASM backend. SDL backend is native-only.)
2424
endif
2525
endif
2626
endif
@@ -155,15 +155,8 @@ BACKEND := none
155155
ifeq ($(CONFIG_BACKEND_SDL), y)
156156
BACKEND = sdl
157157
libtwin.a_files-y += backend/sdl.c
158-
# Emscripten uses ports system for SDL2
159-
ifneq ($(CC_IS_EMCC), 1)
160158
libtwin.a_cflags-y += $(shell sdl2-config --cflags)
161159
TARGET_LIBS += $(shell sdl2-config --libs)
162-
else
163-
# Emscripten SDL2 port - flags needed for both compile and link
164-
libtwin.a_cflags-y += -sUSE_SDL=2
165-
TARGET_LIBS += -sUSE_SDL=2
166-
endif
167160
endif
168161

169162
ifeq ($(CONFIG_BACKEND_FBDEV), y)
@@ -186,6 +179,12 @@ BACKEND = headless
186179
libtwin.a_files-y += backend/headless.c
187180
endif
188181

182+
ifeq ($(CONFIG_BACKEND_WASM), y)
183+
BACKEND = wasm
184+
libtwin.a_files-y += backend/wasm.c
185+
# WASM backend uses Emscripten directly, no external libraries needed
186+
endif
187+
189188
# Performance tester
190189
ifeq ($(CONFIG_PERF_TEST), y)
191190
target-$(CONFIG_PERF_TEST) += mado-perf
@@ -212,22 +211,35 @@ demo-$(BACKEND)_ldflags-y := \
212211

213212
# Emscripten-specific linker flags for WebAssembly builds
214213
ifeq ($(CC_IS_EMCC), 1)
214+
# Base Emscripten flags for all backends
215215
demo-$(BACKEND)_ldflags-y += \
216216
-sINITIAL_MEMORY=33554432 \
217217
-sALLOW_MEMORY_GROWTH=1 \
218218
-sSTACK_SIZE=1048576 \
219-
-sUSE_SDL=2 \
220-
-sMINIMAL_RUNTIME=0 \
221219
-sDYNAMIC_EXECUTION=0 \
222220
-sASSERTIONS=0 \
223221
-sEXPORTED_FUNCTIONS=_main,_malloc,_free \
224-
-sEXPORTED_RUNTIME_METHODS=ccall,cwrap \
222+
-sEXPORTED_RUNTIME_METHODS=ccall,cwrap,HEAPU32,HEAP32 \
225223
-sNO_EXIT_RUNTIME=1 \
226-
-Oz \
227-
--preload-file assets \
228-
--exclude-file assets/web
224+
-Oz
225+
226+
# WebAssembly-specific optimizations
227+
ifeq ($(CONFIG_BACKEND_WASM), y)
228+
demo-$(BACKEND)_cflags-y += -flto
229+
demo-$(BACKEND)_ldflags-y += \
230+
-sMALLOC=emmalloc \
231+
-sFILESYSTEM=1 \
232+
--embed-file assets@/assets \
233+
--exclude-file assets/web \
234+
-sDISABLE_EXCEPTION_CATCHING=1 \
235+
-sEXPORT_ES6=0 \
236+
-sMODULARIZE=0 \
237+
-sENVIRONMENT=web \
238+
-sSUPPORT_ERRNO=0 \
239+
-flto
229240
endif
230241
endif
242+
endif # CONFIG_DEMO_APPLICATIONS
231243

232244
# Font editor tool
233245
# Tools should not be built for WebAssembly
@@ -303,7 +315,8 @@ wasm-install:
303315
@if [ "$(CC_IS_EMCC)" = "1" ]; then \
304316
echo "Installing WebAssembly artifacts to assets/web/..."; \
305317
mkdir -p assets/web; \
306-
cp -f .demo-$(BACKEND)/demo-$(BACKEND) .demo-$(BACKEND)/demo-$(BACKEND).wasm .demo-$(BACKEND)/demo-$(BACKEND).data assets/web/ 2>/dev/null || true; \
318+
cp -f .demo-$(BACKEND)/demo-$(BACKEND) assets/web/demo-$(BACKEND).js 2>/dev/null || true; \
319+
cp -f .demo-$(BACKEND)/demo-$(BACKEND).wasm assets/web/ 2>/dev/null || true; \
307320
echo "✓ WebAssembly build artifacts copied to assets/web/"; \
308321
echo ""; \
309322
echo "\033[1;32m========================================\033[0m"; \

0 commit comments

Comments
 (0)