ports/embed: Enable freezing Python modules.

Use FROZEN_MANIFEST as documented in docs/reference/manifest.rst.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
pull/11430/head
Christian Walther 2023-05-03 22:02:30 +02:00
rodzic 1be753fb7e
commit d3656e97fe
7 zmienionych plików z 34 dodań i 1 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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"
;

Wyświetl plik

@ -0,0 +1 @@
module("frozenhello.py", base_path="modules")

Wyświetl plik

@ -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

Wyświetl plik

@ -0,0 +1,2 @@
def hello():
return "Hello from the cold!"

Wyświetl plik

@ -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)

Wyświetl plik

@ -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