Skip to content

Commit 691cd3a

Browse files
committed
webassembly: Clean up Makefile and add variant support.
This commit cleans up and generalises the Makefile, adds support for variants (following the unix port) and adds the "standard" variant as the default variant. Signed-off-by: Damien George <[email protected]>
1 parent 98a8ff7 commit 691cd3a

File tree

4 files changed

+82
-19
lines changed

4 files changed

+82
-19
lines changed

ports/webassembly/Makefile

+65-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,67 @@
1-
include ../../py/mkenv.mk
1+
################################################################################
2+
# Initial setup of Makefile environment.
3+
4+
# Select the variant to build for:
5+
ifdef VARIANT_DIR
6+
# Custom variant path - remove trailing slash and get the final component of
7+
# the path as the variant name.
8+
VARIANT ?= $(notdir $(VARIANT_DIR:/=))
9+
else
10+
# If not given on the command line, then default to standard.
11+
VARIANT ?= standard
12+
VARIANT_DIR ?= variants/$(VARIANT)
13+
endif
14+
15+
ifeq ($(wildcard $(VARIANT_DIR)/.),)
16+
$(error Invalid VARIANT specified: $(VARIANT_DIR))
17+
endif
18+
19+
# If the build directory is not given, make it reflect the variant name.
20+
BUILD ?= build-$(VARIANT)
221

3-
CROSS = 0
22+
include ../../py/mkenv.mk
23+
include $(VARIANT_DIR)/mpconfigvariant.mk
424

25+
# Qstr definitions (must come before including py.mk).
526
QSTR_DEFS = qstrdefsport.h
627

28+
# Include py core make definitions.
729
include $(TOP)/py/py.mk
830
include $(TOP)/extmod/extmod.mk
931

32+
################################################################################
33+
# Project specific settings and compiler/linker flags.
34+
1035
CC = emcc
1136
LD = emcc
37+
TERSER ?= npx terser
1238

1339
INC += -I.
1440
INC += -I$(TOP)
1541
INC += -I$(BUILD)
42+
INC += -I$(VARIANT_DIR)
1643

1744
CFLAGS += -std=c99 -Wall -Werror -Wdouble-promotion -Wfloat-conversion
1845
CFLAGS += -Os -DNDEBUG
1946
CFLAGS += $(INC)
2047

48+
JSFLAGS += -s EXPORTED_FUNCTIONS="\
49+
_mp_js_init,\
50+
_mp_js_init_repl,\
51+
_mp_js_do_str,\
52+
_mp_js_process_char,\
53+
_mp_hal_get_interrupt_char,\
54+
_mp_sched_keyboard_interrupt$(EXPORTED_FUNCTIONS_EXTRA)"
55+
JSFLAGS += -s EXPORTED_RUNTIME_METHODS="\
56+
ccall,\
57+
cwrap,\
58+
FS$(EXPORTED_RUNTIME_METHODS_EXTRA)"
59+
JSFLAGS += --js-library library.js
60+
JSFLAGS += -s SUPPORT_LONGJMP=emscripten
61+
62+
################################################################################
63+
# Source files and libraries.
64+
2165
SRC_SHARED = $(addprefix shared/,\
2266
runtime/interrupt_char.c \
2367
runtime/stdout_helpers.c \
@@ -26,33 +70,41 @@ SRC_SHARED = $(addprefix shared/,\
2670
timeutils/timeutils.c \
2771
)
2872

29-
SRC_C = \
73+
SRC_C += \
3074
main.c \
3175
mphalport.c \
3276

77+
# List of sources for qstr extraction.
3378
SRC_QSTR += $(SRC_C) $(SRC_SHARED)
3479

80+
SRC_JS ?= wrapper.js
81+
3582
OBJ += $(PY_O)
3683
OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED:.c=.o))
3784
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
3885

39-
JSFLAGS += -s ASYNCIFY
40-
JSFLAGS += -s EXPORTED_FUNCTIONS="['_mp_js_init', '_mp_js_init_repl', '_mp_js_do_str', '_mp_js_process_char', '_mp_hal_get_interrupt_char', '_mp_sched_keyboard_interrupt']"
41-
JSFLAGS += -s EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap', 'FS']"
42-
JSFLAGS += --js-library library.js
86+
################################################################################
87+
# Main targets.
88+
89+
.PHONY: all min test
4390

4491
all: $(BUILD)/micropython.js
4592

46-
$(BUILD)/micropython.js: $(OBJ) library.js wrapper.js
47-
$(ECHO) "LINK $(BUILD)/firmware.js"
48-
$(Q)emcc $(LDFLAGS) -o $(BUILD)/firmware.js $(OBJ) $(JSFLAGS)
49-
cat wrapper.js $(BUILD)/firmware.js > $@
93+
$(BUILD)/micropython.js: $(OBJ) library.js $(SRC_JS)
94+
$(ECHO) "LINK $@"
95+
$(Q)emcc $(LDFLAGS) -o $@ $(OBJ) $(JSFLAGS)
96+
$(Q)cat $(SRC_JS) >> $@
5097

51-
min: $(BUILD)/micropython.js
52-
uglifyjs $< -c -o $(BUILD)/micropython.min.js
98+
$(BUILD)/micropython.min.js: $(BUILD)/micropython.js
99+
$(TERSER) $< --compress --module -o $@
100+
101+
min: $(BUILD)/micropython.min.js
53102

54103
test: $(BUILD)/micropython.js $(TOP)/tests/run-tests.py
55104
$(eval DIRNAME=ports/$(notdir $(CURDIR)))
56105
cd $(TOP)/tests && MICROPY_MICROPYTHON=../ports/webassembly/node_run.sh ./run-tests.py -j1
57106

107+
################################################################################
108+
# Remaining make rules.
109+
58110
include $(TOP)/py/mkrules.mk

ports/webassembly/mpconfigport.h

+15-6
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
// Options to control how MicroPython is built for this port, overriding
29+
// defaults in py/mpconfig.h.
30+
2831
#include <stdint.h>
2932
#include <stdlib.h> // for malloc, for MICROPY_GC_SPLIT_HEAP_AUTO
3033

31-
// options to control how MicroPython is built
34+
// Variant-specific definitions.
35+
#include "mpconfigvariant.h"
3236

37+
#ifndef MICROPY_CONFIG_ROM_LEVEL
3338
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)
34-
35-
// You can disable the built-in MicroPython compiler by setting the following
36-
// config option to 0. If you do this then you won't get a REPL prompt, but you
37-
// will still be able to execute pre-compiled scripts, compiled with mpy-cross.
38-
#define MICROPY_ENABLE_COMPILER (1)
39+
#endif
3940

4041
#define MICROPY_ALLOC_PATH_MAX (256)
4142
#define MICROPY_READER_VFS (MICROPY_VFS)
@@ -69,6 +70,13 @@
6970
mp_handle_pending(true); \
7071
} while (0);
7172

73+
// Whether the VM will periodically call mp_js_hook(), which checks for
74+
// interrupt characters on stdin (or equivalent input).
75+
#ifndef MICROPY_VARIANT_ENABLE_JS_HOOK
76+
#define MICROPY_VARIANT_ENABLE_JS_HOOK (0)
77+
#endif
78+
79+
#if MICROPY_VARIANT_ENABLE_JS_HOOK
7280
#define MICROPY_VM_HOOK_COUNT (10)
7381
#define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT;
7482
#define MICROPY_VM_HOOK_POLL if (--vm_hook_divisor == 0) { \
@@ -78,6 +86,7 @@
7886
}
7987
#define MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_POLL
8088
#define MICROPY_VM_HOOK_RETURN MICROPY_VM_HOOK_POLL
89+
#endif
8190

8291
// type definitions for the specific machine
8392

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define MICROPY_VARIANT_ENABLE_JS_HOOK (1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JSFLAGS += -s ASYNCIFY

0 commit comments

Comments
 (0)