Skip to content

Commit 1e024de

Browse files
committed
unix: Add ability to include frozen bytecode in the build.
To use frozen bytecode make a subdirectory under the unix/ directory (eg frozen/), put .py files there, then run: make FROZEN_MPY_DIR=frozen Be sure to build from scratch. The .py files will then be available for importing.
1 parent 99b4719 commit 1e024de

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

unix/Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,34 @@ SRC_QSTR += $(SRC_C) $(LIB_SRC_C)
176176
# SRC_QSTR
177177
SRC_QSTR_AUTO_DEPS +=
178178

179+
ifneq ($(FROZEN_MPY_DIR),)
180+
# To use frozen bytecode, put your .py files in a subdirectory (eg frozen/) and
181+
# then invoke make with FROZEN_MPY_DIR=frozen (be sure to build from scratch).
182+
MPY_CROSS = ../mpy-cross/mpy-cross
183+
MPY_TOOL = ../tools/mpy-tool.py
184+
FROZEN_MPY_PY_FILES := $(wildcard $(FROZEN_MPY_DIR)/*.py)
185+
FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
186+
CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool
187+
CFLAGS += -DMICROPY_MODULE_FROZEN_MPY
188+
CFLAGS += -DMICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE=0 # not supported
189+
CFLAGS += -DMPZ_DIG_SIZE=16 # force 16 bits to work on both 32 and 64 bit archs
190+
OBJ += $(BUILD)/$(BUILD)/frozen_mpy.o
191+
192+
# to create output directory for .mpy files
193+
$(BUILD)/$(FROZEN_MPY_DIR):
194+
$(MKDIR) -p $@
195+
196+
# to build .mpy files from .py files
197+
$(BUILD)/$(FROZEN_MPY_DIR)/%.mpy: $(FROZEN_MPY_DIR)/%.py | $(BUILD)/$(FROZEN_MPY_DIR)
198+
@$(ECHO) "MPY $<"
199+
$(Q)$(MPY_CROSS) -o $@ $^
200+
201+
# to build frozen_mpy.c from all .mpy files
202+
$(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h
203+
@$(ECHO) "Creating $@"
204+
$(Q)$(PYTHON) $(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h $(FROZEN_MPY_MPY_FILES) > $@
205+
endif
206+
179207

180208
include ../py/mkrules.mk
181209

unix/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
6565
#define MICROPY_STREAMS_NON_BLOCK (1)
6666
#define MICROPY_OPT_COMPUTED_GOTO (1)
67+
#ifndef MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
6768
#define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (1)
69+
#endif
6870
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
6971
#define MICROPY_PY_FUNCTION_ATTRS (1)
7072
#define MICROPY_PY_DESCRIPTORS (1)

0 commit comments

Comments
 (0)