Skip to content

Commit 182e276

Browse files
authored
Parametrize WASM compilation process (#87)
* Makefile: extract compilation flags to variables Previously, the WASM compilation step was hard to adjust for different systems, as it required re-writing the actual rule. This change extracts all the flags into make variables, and separates them into system- and project-specific ones. The system-specific ones are expected to change from machine to machine, and now can be easily overridden: $ make WASM_CC=clang WASM_CFLAGS=--target=wasm32-wasi … The project-specific flags are also extracted to variables, although at this point this is mainly for consistency sake. * Makefile: parametrize all binaries Similarly to previous change, this allows for all binary paths to be easily adjusted. * Makefile: polish Tiny leftover nitpicks: - Mark `optimize` and `clean` targets as phony - Use separate target for `lib/` directory creation - Use make's built-in `$(RM)` command
1 parent 4acffa9 commit 182e276

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

Makefile

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1-
lslib/lexer.wat: lib/lexer.wasm
2-
../wabt/bin/wasm2wat lib/lexer.wasm -o lib/lexer.wat
1+
# These flags depend on the system and may be overridden
2+
WASM_CC := ../wasi-sdk-12.0/bin/clang
3+
WASM_CFLAGS := --sysroot=../wasi-sdk-12.0/share/wasi-sysroot
4+
WASM_LDFLAGS := -nostartfiles
35

4-
lib/lexer.wasm: include-wasm/cjs-module-lexer.h src/lexer.c
5-
@mkdir -p lib
6-
../wasi-sdk-12.0/bin/clang src/lexer.c -I include-wasm --sysroot=../wasi-sdk-12.0/share/wasi-sysroot -o lib/lexer.wasm -nostartfiles \
7-
-Wl,-z,stack-size=13312,--no-entry,--compress-relocations,--strip-all,--export=__heap_base,\
8-
--export=parseCJS,--export=sa,--export=e,--export=re,--export=es,--export=ee,--export=rre,--export=ree,--export=res,--export=ru,--export=us,--export=ue \
9-
-Wno-logical-op-parentheses -Wno-parentheses \
10-
-Oz
6+
WASM2WAT := ../wabt/bin/wasm2wat
7+
WASM_OPT := ../binaryen/bin/wasm-opt
8+
9+
# These are project-specific and are expected to be kept intact
10+
WASM_EXTRA_CFLAGS := -I include-wasm/ -Wno-logical-op-parentheses -Wno-parentheses -Oz
11+
WASM_EXTRA_LDFLAGS := -Wl,-z,stack-size=13312,--no-entry,--compress-relocations,--strip-all
12+
WASM_EXTRA_LDFLAGS += -Wl,--export=__heap_base,--export=parseCJS,--export=sa
13+
WASM_EXTRA_LDFLAGS += -Wl,--export=e,--export=re,--export=es,--export=ee
14+
WASM_EXTRA_LDFLAGS += -Wl,--export=rre,--export=ree,--export=res,--export=ru,--export=us,--export=ue
15+
16+
.PHONY: optimize clean
17+
18+
lib/lexer.wat: lib/lexer.wasm
19+
$(WASM2WAT) lib/lexer.wasm -o lib/lexer.wat
20+
21+
lib/lexer.wasm: include-wasm/cjs-module-lexer.h src/lexer.c | lib/
22+
$(WASM_CC) $(WASM_CFLAGS) $(WASM_EXTRA_CFLAGS) \
23+
src/lexer.c -o lib/lexer.wasm \
24+
$(WASM_LDFLAGS) $(WASM_EXTRA_LDFLAGS)
25+
26+
lib/:
27+
@mkdir -p $@
1128

1229
optimize: lib/lexer.wasm
13-
../binaryen/bin/wasm-opt -Oz lib/lexer.wasm -o lib/lexer.wasm
30+
$(WASM_OPT) -Oz lib/lexer.wasm -o lib/lexer.wasm
1431

1532
clean:
16-
rm lib/*
33+
$(RM) lib/*

0 commit comments

Comments
 (0)