@@ -10,6 +10,21 @@ ifeq ($(filter $(check_goal),config defconfig),)
1010 endif
1111endif
1212
13+ # Detect Emscripten early (before including toolchain.mk)
14+ CC_VERSION := $(shell $(CC ) --version 2>/dev/null)
15+ ifneq ($(findstring emcc,$(CC_VERSION ) ) ,)
16+ CC_IS_EMCC := 1
17+ endif
18+
19+ # Enforce SDL backend for Emscripten builds (skip during config targets)
20+ ifeq ($(filter $(check_goal ) ,config defconfig) ,)
21+ 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)
24+ endif
25+ endif
26+ endif
27+
1328# Target variables initialization
1429
1530target-y :=
@@ -23,6 +38,8 @@ target.a-y += libtwin.a
2338# Core library
2439
2540libtwin.a_cflags-y :=
41+ # Emscripten size optimization
42+ libtwin.a_cflags-$(CC_IS_EMCC) += -Oz
2643
2744libtwin.a_files-y = \
2845 src/box.c \
@@ -85,14 +102,26 @@ endif
85102
86103ifeq ($(CONFIG_LOADER_JPEG ) , y)
87104libtwin.a_files-y += src/image-jpeg.c
105+ ifneq ($(CC_IS_EMCC ) , 1)
88106libtwin.a_cflags-y += $(shell pkg-config --cflags libjpeg)
89107TARGET_LIBS += $(shell pkg-config --libs libjpeg)
108+ else
109+ # Emscripten libjpeg port - flags needed for both compile and link
110+ libtwin.a_cflags-y += -sUSE_LIBJPEG=1
111+ TARGET_LIBS += -sUSE_LIBJPEG=1
112+ endif
90113endif
91114
92115ifeq ($(CONFIG_LOADER_PNG ) , y)
93116libtwin.a_files-y += src/image-png.c
117+ ifneq ($(CC_IS_EMCC ) , 1)
94118libtwin.a_cflags-y += $(shell pkg-config --cflags libpng)
95119TARGET_LIBS += $(shell pkg-config --libs libpng)
120+ else
121+ # Emscripten libpng port (includes zlib) - flags needed for both compile and link
122+ libtwin.a_cflags-y += -sUSE_LIBPNG=1 -sUSE_ZLIB=1
123+ TARGET_LIBS += -sUSE_LIBPNG=1 -sUSE_ZLIB=1
124+ endif
96125endif
97126
98127ifeq ($(CONFIG_LOADER_GIF ) , y)
@@ -116,6 +145,8 @@ libapps.a_files-$(CONFIG_DEMO_ANIMATION) += apps/animation.c
116145libapps.a_files-$(CONFIG_DEMO_IMAGE) += apps/image.c
117146
118147libapps.a_includes-y := include
148+ # Emscripten size optimization
149+ libapps.a_cflags-$(CC_IS_EMCC) += -Oz
119150
120151# Graphical backends
121152
@@ -124,8 +155,15 @@ BACKEND := none
124155ifeq ($(CONFIG_BACKEND_SDL ) , y)
125156BACKEND = sdl
126157libtwin.a_files-y += backend/sdl.c
158+ # Emscripten uses ports system for SDL2
159+ ifneq ($(CC_IS_EMCC ) , 1)
127160libtwin.a_cflags-y += $(shell sdl2-config --cflags)
128161TARGET_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
129167endif
130168
131169ifeq ($(CONFIG_BACKEND_FBDEV ) , y)
@@ -149,7 +187,6 @@ libtwin.a_files-y += backend/headless.c
149187endif
150188
151189# Performance tester
152-
153190ifeq ($(CONFIG_PERF_TEST ) , y)
154191target-$(CONFIG_PERF_TEST) += mado-perf
155192mado-perf_depends-y += $(target.a-y )
@@ -167,13 +204,34 @@ target-y += demo-$(BACKEND)
167204demo-$(BACKEND)_depends-y += $(target.a-y )
168205demo-$(BACKEND)_files-y = apps/main.c
169206demo-$(BACKEND)_includes-y := include
207+ # Emscripten size optimization
208+ demo-$(BACKEND)_cflags-$(CC_IS_EMCC) += -Oz
170209demo-$(BACKEND)_ldflags-y := \
171210 $(target.a-y ) \
172211 $(TARGET_LIBS )
212+
213+ # Emscripten-specific linker flags for WebAssembly builds
214+ ifeq ($(CC_IS_EMCC ) , 1)
215+ demo-$(BACKEND)_ldflags-y += \
216+ -sINITIAL_MEMORY=33554432 \
217+ -sALLOW_MEMORY_GROWTH=1 \
218+ -sSTACK_SIZE=1048576 \
219+ -sUSE_SDL=2 \
220+ -sMINIMAL_RUNTIME=0 \
221+ -sDYNAMIC_EXECUTION=0 \
222+ -sASSERTIONS=0 \
223+ -sEXPORTED_FUNCTIONS=_main,_malloc,_free \
224+ -sEXPORTED_RUNTIME_METHODS=ccall,cwrap \
225+ -sNO_EXIT_RUNTIME=1 \
226+ -Oz \
227+ --preload-file assets \
228+ --exclude-file assets/web
229+ endif
173230endif
174231
175232# Font editor tool
176-
233+ # Tools should not be built for WebAssembly
234+ ifneq ($(CC_IS_EMCC ) , 1)
177235ifeq ($(CONFIG_TOOLS ) , y)
178236target-$(CONFIG_TOOL_FONTEDIT) += font-edit
179237font-edit_files-y = \
@@ -194,6 +252,7 @@ headless-ctl_files-y = tools/headless-ctl.c
194252headless-ctl_includes-y := include
195253headless-ctl_ldflags-y := # -lrt
196254endif
255+ endif
197256
198257# Build system integration
199258
@@ -237,3 +296,26 @@ defconfig: $(KCONFIGLIB)
237296config : $(KCONFIGLIB ) configs/Kconfig
238297 @tools/kconfig/menuconfig.py configs/Kconfig
239298 @tools/kconfig/genconfig.py configs/Kconfig
299+
300+ # WebAssembly post-build: Copy artifacts to assets/web/
301+ .PHONY : wasm-install
302+ wasm-install :
303+ @if [ " $( CC_IS_EMCC) " = " 1" ]; then \
304+ echo " Installing WebAssembly artifacts to assets/web/..." ; \
305+ 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 ; \
307+ echo " ✓ WebAssembly build artifacts copied to assets/web/" ; \
308+ echo " " ; \
309+ echo " \033[1;32m========================================\033[0m" ; \
310+ echo " \033[1;32mWebAssembly build complete!\033[0m" ; \
311+ echo " \033[1;32m========================================\033[0m" ; \
312+ echo " " ; \
313+ echo " To test in browser, run:" ; \
314+ echo " \033[1;34m./scripts/serve-wasm.py --open\033[0m" ; \
315+ echo " " ; \
316+ fi
317+
318+ # Override all target to add post-build hook for Emscripten
319+ ifeq ($(CC_IS_EMCC ) , 1)
320+ all : wasm-install
321+ endif
0 commit comments