Skip to content

Commit 446b95b

Browse files
committed
Tweak build rules for WebAssembly
The "emcc --version" command displays information about the version of emcc, which contains the "gcc" or "clang" string. As a result, the build rule should only match one compiler and stop checking anymore. If not, the build could be failed by a mix of different compiler's CFLAGS. For example, -flto from clang and -flto=thin from emcc. After refactoring the riscv.[ch] public APIs in commit 820cd9b, it is now possible to adjust the memory size to accommodate varying runtime requirements. The memory size must align to a 4KB page size for the WASM runtime and I set 1GB as default memory size for WASM runtime. The LTO is not supported to build emscripten-port SDL library, so disable it when both ENABLE_LTO=1 and ENABLE_SDL=1 are set. Related to: #75 Close #375
1 parent 343ece7 commit 446b95b

File tree

3 files changed

+67
-11
lines changed

3 files changed

+67
-11
lines changed

Makefile

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CONFIG_FILE := $(OUT)/.config
1010
CFLAGS = -std=gnu99 -O2 -Wall -Wextra
1111
CFLAGS += -Wno-unused-label
1212
CFLAGS += -include src/common.h
13+
CFLAGS_emcc ?=
1314

1415
# Enable link-time optimization (LTO)
1516
ENABLE_LTO ?= 1
@@ -21,7 +22,14 @@ endif
2122
endif
2223
$(call set-feature, LTO)
2324
ifeq ($(call has, LTO), 1)
24-
ifeq ("$(CC_IS_GCC)$(CC_IS_EMCC)", "1")
25+
ifeq ("$(CC_IS_EMCC)", "1")
26+
ifeq ($(call has, SDL), 1)
27+
$(warning LTO is not supported to build emscripten-port SDL using emcc.)
28+
else
29+
CFLAGS += -flto
30+
endif
31+
endif
32+
ifeq ("$(CC_IS_GCC)", "1")
2533
CFLAGS += -flto
2634
endif
2735
ifeq ("$(CC_IS_CLANG)", "1")
@@ -136,6 +144,13 @@ ifeq ("$(CC_IS_EMCC)", "1")
136144
CFLAGS += -mtail-call
137145
endif
138146

147+
# Build emscripten-port SDL
148+
ifeq ("$(CC_IS_EMCC)", "1")
149+
ifeq ($(call has, SDL), 1)
150+
CFLAGS_emcc += -sUSE_SDL=2 -sSDL2_MIXER_FORMATS=wav,mid -sUSE_SDL_MIXER=2
151+
endif
152+
endif
153+
139154
ENABLE_UBSAN ?= 0
140155
ifeq ("$(ENABLE_UBSAN)", "1")
141156
CFLAGS += -fsanitize=undefined -fno-sanitize=alignment -fno-sanitize-recover=all
@@ -148,6 +163,13 @@ $(OUT)/emulate.o: CFLAGS += -foptimize-sibling-calls -fomit-frame-pointer -fno-s
148163
# to the first target after .DEFAULT_GOAL is not set.
149164
.DEFAULT_GOAL :=
150165

166+
WEB_FILES += $(BIN).js \
167+
$(BIN).wasm \
168+
$(BIN).worker.js
169+
ifeq ("$(CC_IS_EMCC)", "1")
170+
BIN := $(BIN).js
171+
endif
172+
151173
all: config $(BIN)
152174

153175
OBJS := \
@@ -167,13 +189,23 @@ OBJS := \
167189
OBJS := $(addprefix $(OUT)/, $(OBJS))
168190
deps := $(OBJS:%.o=%.o.d)
169191

192+
EXPORTED_FUNCS := _main
193+
ifeq ("$(CC_IS_EMCC)", "1")
194+
CFLAGS_emcc += -sINITIAL_MEMORY=2GB \
195+
-sALLOW_MEMORY_GROWTH \
196+
-s"EXPORTED_FUNCTIONS=$(EXPORTED_FUNCS)" \
197+
--embed-file build \
198+
-DMEM_SIZE=0x40000000 \
199+
-w
200+
endif
201+
170202
$(OUT)/%.o: src/%.c
171203
$(VECHO) " CC\t$@\n"
172-
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MF $@.d $<
204+
$(Q)$(CC) -o $@ $(CFLAGS) $(CFLAGS_emcc) -c -MMD -MF $@.d $<
173205

174206
$(BIN): $(OBJS)
175207
$(VECHO) " LD\t$@\n"
176-
$(Q)$(CC) -o $@ $^ $(LDFLAGS)
208+
$(Q)$(CC) -o $@ $(CFLAGS_emcc) $^ $(LDFLAGS)
177209

178210
config: $(CONFIG_FILE)
179211
$(CONFIG_FILE):
@@ -236,7 +268,7 @@ endif
236268
endif
237269

238270
clean:
239-
$(RM) $(BIN) $(OBJS) $(HIST_BIN) $(HIST_OBJS) $(deps) $(CACHE_OUT) src/rv32_jit.c
271+
$(RM) $(BIN) $(OBJS) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT) src/rv32_jit.c
240272
distclean: clean
241273
-$(RM) $(DOOM_DATA) $(QUAKE_DATA)
242274
$(RM) -r $(OUT)/id1

mk/toolchain.mk

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,33 @@ CC_IS_CLANG :=
33
CC_IS_GCC :=
44
ifneq ($(shell $(CC) --version | head -n 1 | grep emcc),)
55
CC_IS_EMCC := 1
6-
endif
7-
ifneq ($(shell $(CC) --version | head -n 1 | grep clang),)
8-
CC_IS_CLANG := 1
9-
endif
10-
ifneq ($(shell $(CC) --version | grep "Free Software Foundation"),)
11-
CC_IS_GCC := 1
6+
7+
# see commit 165c1a3 of emscripten
8+
MIMALLOC_SUPPORT_SINCE_MAJOR := 3
9+
MIMALLOC_SUPPORT_SINCE_MINOR := 1
10+
MIMALLOC_SUPPORT_SINCE_PATCH := 50
11+
MIMALLOC_UNSUPPORTED_WARNING := mimalloc is supported after version $(MIMALLOC_SUPPORT_SINCE_MAJOR).$(MIMALLOC_SUPPORT_SINCE_MINOR).$(MIMALLOC_SUPPORT_SINCE_PATCH)
12+
EMCC_VERSION := $(shell $(CC) --version | head -n 1 | cut -f10 -d ' ')
13+
EMCC_MAJOR := $(shell echo $(EMCC_VERSION) | cut -f1 -d.)
14+
EMCC_MINOR := $(shell echo $(EMCC_VERSION) | cut -f2 -d.)
15+
EMCC_PATCH := $(shell echo $(EMCC_VERSION) | cut -f3 -d.)
16+
ifeq ($(shell echo $(EMCC_MAJOR)\>=$(MIMALLOC_SUPPORT_SINCE_MAJOR) | bc), 1)
17+
ifeq ($(shell echo $(EMCC_MINOR)\>=$(MIMALLOC_SUPPORT_SINCE_MINOR) | bc), 1)
18+
ifeq ($(shell echo $(EMCC_PATCH)\>=$(MIMALLOC_SUPPORT_SINCE_PATCH) | bc), 1)
19+
CFLAGS_emcc += -sMALLOC=mimalloc
20+
else
21+
$(warning $(MIMALLOC_UNSUPPORTED_WARNING))
22+
endif
23+
else
24+
$(warning $(MIMALLOC_UNSUPPORTED_WARNING))
25+
endif
26+
else
27+
$(warning $(MIMALLOC_UNSUPPORTED_WARNING))
28+
endif
29+
else ifneq ($(shell $(CC) --version | head -n 1 | grep clang),)
30+
CC_IS_CLANG := 1
31+
else ifneq ($(shell $(CC) --version | grep "Free Software Foundation"),)
32+
CC_IS_GCC := 1
1233
endif
1334

1435
CFLAGS_NO_CET :=

src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ static void dump_test_signature(const char *prog_name)
168168
elf_delete(elf);
169169
}
170170

171-
#define MEM_SIZE 0xFFFFFFFFULL /* 2^32 - 1 */
171+
/* MEM_SIZE shall be defined on different runtime */
172+
#ifndef MEM_SIZE
173+
#define MEM_SIZE 0xFFFFFFFFULL /* 2^32 - 1 */
174+
#endif
172175
#define STACK_SIZE 0x1000 /* 4096 */
173176
#define ARGS_OFFSET_SIZE 0x1000 /* 4096 */
174177

0 commit comments

Comments
 (0)