Skip to content

Commit 291c085

Browse files
committed
Support hosting WebAssembly demo page locally
To enhance the user experience of using WebAssembly version of rv32emu, we shall provide a Makefile target (serve-wasm) to serve the WebAssembly demo page in an easy way. Since rv32emu leverages TCO (tail call optimization) when being compiled, so we have to tell if the user's browser supports TCO. Currently, only Chrome with MAJOR 112 and Firefox with MAJOR 121 supports TCO execution runtime, thus we focus on this two browsers. Also, use color text to remind the users if they need to update their Chrome or Firefox. See also: #75
1 parent 093926c commit 291c085

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

Makefile

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include mk/common.mk
2+
include mk/color.mk
23
include mk/toolchain.mk
34

45
OUT ?= build
@@ -167,7 +168,7 @@ $(OUT)/emulate.o: CFLAGS += -foptimize-sibling-calls -fomit-frame-pointer -fno-s
167168
# to the first target after .DEFAULT_GOAL is not set.
168169
.DEFAULT_GOAL :=
169170

170-
WEB_FILES += $(BIN).js \
171+
WEB_FILES := $(BIN).js \
171172
$(BIN).wasm \
172173
$(BIN).worker.js
173174
ifeq ("$(CC_IS_EMCC)", "1")
@@ -215,6 +216,69 @@ CFLAGS_emcc += -sINITIAL_MEMORY=2GB \
215216

216217
# used to download all dependencies of elf executable and bundle into single wasm
217218
deps_emcc += $(DOOM_DATA) $(QUAKE_DATA) $(TIMIDITY_DATA)
219+
220+
# check browser MAJOR version if supports TCO
221+
CHROME_MAJOR :=
222+
CHROME_MAJOR_VERSION_CHECK_CMD :=
223+
CHROME_SUPPORT_TCO_AT_MAJOR := 112
224+
CHROME_SUPPORT_TCO_INFO := Chrome supports TCO, you can use Chrome to request the wasm
225+
CHROME_NO_SUPPORT_TCO_WARNING := Chrome not found or Chrome must have at least version $(CHROME_SUPPORT_TCO_AT_MAJOR) in MAJOR to serve wasm
226+
227+
FIREFOX_MAJOR :=
228+
FIREFOX_MAJOR_VERSION_CHECK_CMD :=
229+
FIREFOX_SUPPORT_TCO_AT_MAJOR := 121
230+
FIREFOX_SUPPORT_TCO_INFO := Firefox supports TCO, you can use Firefox to request the wasm
231+
FIREFOX_NO_SUPPORT_TCO_WARNING := Firefox not found or Firefox must have at least version $(FIREFOX_SUPPORT_TCO_AT_MAJOR) in MAJOR to serve wasm
232+
233+
# FIXME: for Windows
234+
ifeq ($(UNAME_S),Darwin)
235+
CHROME_MAJOR_VERSION_CHECK_CMD := "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --version | awk '{print $$3}' | cut -f1 -d.
236+
FIREFOX_MAJOR_VERSION_CHECK_CMD := /Applications/Firefox.app/Contents/MacOS/firefox --version | awk '{print $$3}' | cut -f1 -d.
237+
else ifeq ($(UNAME_S),Linux)
238+
CHROME_MAJOR_VERSION_CHECK_CMD := google-chrome --version | awk '{print $$3}' | cut -f1 -d.
239+
FIREFOX_MAJOR_VERSION_CHECK_CMD := firefox -v | awk '{print $$3}' | cut -f1 -d.
240+
endif
241+
CHROME_MAJOR := $(shell $(CHROME_MAJOR_VERSION_CHECK_CMD))
242+
FIREFOX_MAJOR := $(shell $(FIREFOX_MAJOR_VERSION_CHECK_CMD))
243+
244+
# Chrome
245+
ifeq ($(shell echo $(CHROME_MAJOR)\>=$(CHROME_SUPPORT_TCO_AT_MAJOR) | bc), 1)
246+
$(info $(shell echo "$(GREEN)$(CHROME_SUPPORT_TCO_INFO)$(NC)"))
247+
else
248+
$(warning $(shell echo "$(YELLOW)$(CHROME_NO_SUPPORT_TCO_WARNING)$(NC)"))
249+
endif
250+
251+
# Firefox
252+
ifeq ($(shell echo $(FIREFOX_MAJOR)\>=$(FIREFOX_SUPPORT_TCO_AT_MAJOR) | bc), 1)
253+
$(info $(shell echo "$(GREEN)$(FIREFOX_SUPPORT_TCO_INFO)$(NC)"))
254+
else
255+
$(warning $(shell echo "$(YELLOW)$(FIREFOX_NO_SUPPORT_TCO_WARNING)$(NC)"))
256+
endif
257+
258+
# used to serve wasm locally
259+
DEMO_DIR := demo
260+
DEMO_IP := 127.0.0.1
261+
DEMO_PORT := 8000
262+
263+
# check if demo root directory exists and create it if not
264+
check-demo-dir-exist:
265+
$(Q)if [ ! -d "$(DEMO_DIR)" ]; then \
266+
mkdir -p "$(DEMO_DIR)"; \
267+
fi
268+
269+
# FIXME: without $(info) generates errors
270+
define cp-web-file
271+
$(Q)cp $(1) $(DEMO_DIR)
272+
$(info)
273+
endef
274+
275+
# WEB_FILES could be cleaned and recompiled, thus do not mix these two files into WEB_FILES
276+
STATIC_WEB_FILES := assets/html/index.html assets/js/coi-serviceworker.min.js
277+
278+
serve-wasm: $(BIN) check-demo-dir-exist
279+
$(foreach T, $(WEB_FILES), $(call cp-web-file, $(T)))
280+
$(foreach T, $(STATIC_WEB_FILES), $(call cp-web-file, $(T)))
281+
$(Q)python3 -m http.server --bind $(DEMO_IP) $(DEMO_PORT) --directory $(DEMO_DIR)
218282
endif
219283

220284
$(OUT)/%.o: src/%.c $(deps_emcc)
@@ -304,6 +368,7 @@ distclean: clean
304368
-$(RM) $(DOOM_DATA) $(QUAKE_DATA)
305369
$(RM) -r $(TIMIDITY_DATA)
306370
$(RM) -r $(OUT)/id1
371+
$(RM) -r $(DEMO_DIR)
307372
$(RM) *.zip
308373
$(RM) -r $(OUT)/mini-gdbstub
309374
-$(RM) $(OUT)/.config

mk/color.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
YELLOW=\033[0;33m
2+
GREEN=\033[0;32m
3+
NC=\033[0m

0 commit comments

Comments
 (0)