diff --git a/examples/embedding-full/README.md b/examples/embedding-full/README.md index a185e16b2b..1670526cc4 100644 --- a/examples/embedding-full/README.md +++ b/examples/embedding-full/README.md @@ -15,6 +15,8 @@ Building the example First build the embed port using: + $ make -C mpy-cross + $ cd examples/embedding-full $ make -f micropython_embed.mk submodules $ make -f micropython_embed.mk diff --git a/examples/embedding-full/main.c b/examples/embedding-full/main.c index 2c936b53fa..a24760afdb 100644 --- a/examples/embedding-full/main.c +++ b/examples/embedding-full/main.c @@ -34,6 +34,9 @@ static const char *example_2 = "help(random)\n" "import time\n" "help(time)\n" + "import frozenhello\n" + "help(frozenhello)\n" + "print('frozenhello.hello():', frozenhello.hello())\n" "\n" "print('finish')\n" ; diff --git a/examples/embedding-full/manifest.py b/examples/embedding-full/manifest.py new file mode 100644 index 0000000000..19c20cc7e7 --- /dev/null +++ b/examples/embedding-full/manifest.py @@ -0,0 +1 @@ +module("frozenhello.py", base_path="modules") diff --git a/examples/embedding-full/micropython_embed.mk b/examples/embedding-full/micropython_embed.mk index 31dd2e8ecd..ee96a5f486 100644 --- a/examples/embedding-full/micropython_embed.mk +++ b/examples/embedding-full/micropython_embed.mk @@ -8,5 +8,8 @@ MICROPYTHON_TOP = ../.. # Include modules from extmod in the output. EMBED_EXTRA = extmod +# Freeze Python modules. +FROZEN_MANIFEST ?= manifest.py + # Include the main makefile fragment to build the MicroPython component. include $(MICROPYTHON_TOP)/ports/embed/embed.mk diff --git a/examples/embedding-full/modules/frozenhello.py b/examples/embedding-full/modules/frozenhello.py new file mode 100644 index 0000000000..cad6804776 --- /dev/null +++ b/examples/embedding-full/modules/frozenhello.py @@ -0,0 +1,2 @@ +def hello(): + return "Hello from the cold!" diff --git a/examples/embedding-full/mpconfigport.h b/examples/embedding-full/mpconfigport.h index 3389c4d036..8175b54db0 100644 --- a/examples/embedding-full/mpconfigport.h +++ b/examples/embedding-full/mpconfigport.h @@ -36,3 +36,13 @@ // We have our own implementation of mp_hal_stdout_tx_strn_cooked(). #undef MP_PLAT_PRINT_STRN + +// Enable freezing of Python modules. These would be set automatically for the +// port build based on the presence of FROZEN_MANIFEST by py/mkrules.mk, but +// must be set explicitly for the application build. +#define MICROPY_MODULE_FROZEN_MPY 1 +#define MICROPY_MODULE_FROZEN_STR 1 +#define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool +// must match MPY_TOOL_FLAGS or defaults for mpy-tool.py arguments +#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) +#define MPZ_DIG_SIZE (16) diff --git a/ports/embed/embed.mk b/ports/embed/embed.mk index b0ca6c82a4..d5e6fb69d7 100644 --- a/ports/embed/embed.mk +++ b/ports/embed/embed.mk @@ -34,6 +34,11 @@ GENHDR_OUTPUT = $(addprefix $(BUILD)/genhdr/, \ root_pointers.h \ ) +# Define the module freezing output. +ifneq ($(FROZEN_MANIFEST),) +FROZEN_OUTPUT = $(BUILD)/frozen_content.c +endif + # Define the top-level target, the generated output files. .PHONY: all all: micropython-embed-package @@ -49,9 +54,12 @@ PACKAGE_DIR_LIST = $(addprefix $(PACKAGE_DIR)/,py extmod shared/runtime genhdr p ifeq ($(filter extmod,$(EMBED_EXTRA)),extmod) PACKAGE_DIR_LIST += $(addprefix $(PACKAGE_DIR)/,lib/uzlib lib/crypto-algorithms lib/re1.5) endif +ifneq ($(FROZEN_MANIFEST),) +PACKAGE_DIR_LIST += $(addprefix $(PACKAGE_DIR)/,frozen) +endif .PHONY: micropython-embed-package -micropython-embed-package: $(GENHDR_OUTPUT) +micropython-embed-package: $(GENHDR_OUTPUT) $(FROZEN_OUTPUT) $(ECHO) "Generate micropython_embed output:" $(Q)$(RM) -rf $(PACKAGE_DIR_LIST) $(Q)$(MKDIR) -p $(PACKAGE_DIR_LIST) @@ -72,6 +80,10 @@ endif $(Q)$(CP) $(TOP)/shared/runtime/gchelper_generic.c $(PACKAGE_DIR)/shared/runtime $(ECHO) "- genhdr" $(Q)$(CP) $(GENHDR_OUTPUT) $(PACKAGE_DIR)/genhdr +ifneq ($(FROZEN_MANIFEST),) + $(ECHO) "- frozen" + $(Q)$(CP) $(FROZEN_OUTPUT) $(PACKAGE_DIR)/frozen +endif $(ECHO) "- port" $(Q)$(CP) $(MICROPYTHON_EMBED_PORT)/port/*.[ch] $(PACKAGE_DIR)/port