@@ -16,11 +16,11 @@ ifneq ($(findstring emcc,$(CC_VERSION)),)
1616 CC_IS_EMCC := 1
1717endif
1818
19- # Enforce SDL backend for Emscripten builds (skip during config targets)
19+ # Enforce compatible backend for Emscripten builds (skip during config targets)
2020ifeq ($(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
2626endif
@@ -155,15 +155,8 @@ BACKEND := none
155155ifeq ($(CONFIG_BACKEND_SDL ) , y)
156156BACKEND = sdl
157157libtwin.a_files-y += backend/sdl.c
158- # Emscripten uses ports system for SDL2
159- ifneq ($(CC_IS_EMCC ) , 1)
160158libtwin.a_cflags-y += $(shell sdl2-config --cflags)
161159TARGET_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
167160endif
168161
169162ifeq ($(CONFIG_BACKEND_FBDEV ) , y)
@@ -186,6 +179,12 @@ BACKEND = headless
186179libtwin.a_files-y += backend/headless.c
187180endif
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
190189ifeq ($(CONFIG_PERF_TEST ) , y)
191190target-$(CONFIG_PERF_TEST) += mado-perf
@@ -212,22 +211,35 @@ demo-$(BACKEND)_ldflags-y := \
212211
213212# Emscripten-specific linker flags for WebAssembly builds
214213ifeq ($(CC_IS_EMCC ) , 1)
214+ # Base Emscripten flags for all backends
215215demo-$(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
229240endif
230241endif
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