From 1e024de7befea5f879b0120deb8c0d0f71e039ff Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 16 May 2016 23:17:11 +0100 Subject: [PATCH] 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. --- unix/Makefile | 28 ++++++++++++++++++++++++++++ unix/mpconfigport.h | 2 ++ 2 files changed, 30 insertions(+) diff --git a/unix/Makefile b/unix/Makefile index 655f26bb93..ed6ae45775 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -176,6 +176,34 @@ SRC_QSTR += $(SRC_C) $(LIB_SRC_C) # SRC_QSTR SRC_QSTR_AUTO_DEPS += +ifneq ($(FROZEN_MPY_DIR),) +# To use frozen bytecode, put your .py files in a subdirectory (eg frozen/) and +# then invoke make with FROZEN_MPY_DIR=frozen (be sure to build from scratch). +MPY_CROSS = ../mpy-cross/mpy-cross +MPY_TOOL = ../tools/mpy-tool.py +FROZEN_MPY_PY_FILES := $(wildcard $(FROZEN_MPY_DIR)/*.py) +FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy)) +CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool +CFLAGS += -DMICROPY_MODULE_FROZEN_MPY +CFLAGS += -DMICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE=0 # not supported +CFLAGS += -DMPZ_DIG_SIZE=16 # force 16 bits to work on both 32 and 64 bit archs +OBJ += $(BUILD)/$(BUILD)/frozen_mpy.o + +# to create output directory for .mpy files +$(BUILD)/$(FROZEN_MPY_DIR): + $(MKDIR) -p $@ + +# to build .mpy files from .py files +$(BUILD)/$(FROZEN_MPY_DIR)/%.mpy: $(FROZEN_MPY_DIR)/%.py | $(BUILD)/$(FROZEN_MPY_DIR) + @$(ECHO) "MPY $<" + $(Q)$(MPY_CROSS) -o $@ $^ + +# to build frozen_mpy.c from all .mpy files +$(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h + @$(ECHO) "Creating $@" + $(Q)$(PYTHON) $(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h $(FROZEN_MPY_MPY_FILES) > $@ +endif + include ../py/mkrules.mk diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index d9cfbc9190..463f5fe8cf 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -64,7 +64,9 @@ #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_STREAMS_NON_BLOCK (1) #define MICROPY_OPT_COMPUTED_GOTO (1) +#ifndef MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE #define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (1) +#endif #define MICROPY_CAN_OVERRIDE_BUILTINS (1) #define MICROPY_PY_FUNCTION_ATTRS (1) #define MICROPY_PY_DESCRIPTORS (1)